mcp

    Type-safe MCP SDK in MoonBit with server/client support and dual transport (STDIO/HTTP)

    mcp
    modelcontextprotocol
    Download zip
    Author
    Version
    0.17.4
    License
    Apache-2.0
    Last updated
    6 days ago
    Downloads
    245

    #MoonBit MCP SDK

    Type-safe Model Context Protocol SDK for MoonBit.

    Version: 0.15.0 · Protocol: 2026-07-28 (stateless) · License: Apache-2.0

    #Installation

    moon add colmugx/mcp

    #Quick Start

    #Server

    async fn main {
    @mcp.MCPServer::MCPServer("demo-server", "1.0.0")
    .tool("echo", "Echo text", Json::object({}), fn(args) {
    let text = match args {
    Object(obj) =>
    match obj.get("text") {
    Some(String(value)) => value
    _ => ""
    }
    _ => ""
    }
    Ok(@tool.ToolResult::text(text))
    })
    .run_stdio()
    }

    Use .run_http(port=4240, path="/mcp") for Streamable HTTP.

    #Client

    async fn main {
    match @mcp.MCPClient::connect_http(
    url="http://localhost:4240/mcp",
    name="demo-client",
    version="1.0.0",
    ) {
    Ok(client) => {
    // Optional one-shot discovery of server identity/capabilities.
    ignore(client.discover())
    match client.list_tools() {
    Ok(result) => for tool in result.tools { println(tool.name) }
    Err(e) => println(e.message())
    }
    client.close()
    }
    Err(e) => println("connect failed: \{e.message()}")
    }
    }

    For local subprocess servers, use MCPClient::connect_stdio(cmd~, args?, name~, version~, extra_env?, group~).

    #Host

    async fn main {
    let host = @mcp.MCPHost::MCPHost(name="my-host", version="1.0.0")
    @async.with_task_group(group => {
    ignore(host.connect_stdio(name="local", cmd="moon", args=["run", "server"], group~))
    ignore(host.connect_http(name="remote", url="http://localhost:4240/mcp"))
    match host.list_tools() {
    Ok(result) => for tool in result.tools { println(tool.name) } // local.echo
    Err(e) => println(e.message())
    }
    ignore(host.call_tool("local.echo", arguments="{\"text\":\"hello\"}"))
    host.close_all()
    })
    }

    Host tool names are qualified as connection.tool so multiple servers can expose the same tool name without collision.

    #Documentation

    DocumentDescription
    QuickstartServer, client, and host setup
    Protocol TypesJSON-RPC, errors, and MCP types
    Server GuideHigh-level server API and runtime behavior
    Transport ReferenceAdvanced transport internals
    Client GuideMCPClient::connect_* and bidirectional mode
    ArchitectureProtocol, runtime, transport, application layers
    Host GuideMCPHost named connections and routing
    Migration 0.152025-11-25 → 2026-07-28 migration, node by node
    Migration 0.14Breaking changes from 0.13.x

    #API Direction

    The default public path is intentionally small:

    • MCPServer for serving tools, resources, and prompts.
    • MCPClient for one server connection.
    • MCPHost for multiple named server connections.
    • colmugx/mcp/client/legacy for talking to 2025-11-25-era servers (LegacyClient).

    Transports and request builders are advanced/internal details. Existing low-level imports may still be useful for SDK contributors, but application code should prefer the high-level APIs above.

    #License

    Apache-2.0

    CallToolResult

    Client and Host

    CompletionResult

    Client and Host

    ContentItem

    Protocol types

    JsonRpcRequest

    Protocol types

    JsonSchema

    Protocol core

    ListPromptsResult

    Client and Host

    ListResourcesResult

    Client and Host

    ListToolsResult

    Client and Host

    MCPClient

    Client and Host

    MCPError

    Protocol types

    MCPHost

    Client and Host

    MCPServer

    Server

    ParamDef

    Protocol tool types

    Params

    Protocol core

    ReadResourceResult

    Client and Host

    ServerCapabilities

    Protocol types

    ServerInfo

    Protocol types

    Tool

    Protocol tool types

    ToolCallOutcome

    Protocol tool types

    ToolDefinition

    Protocol types

    ToolError

    Protocol core

    ToolResult

    Protocol tool types

    TransportError

    Protocol types

    get_number

    fn get_number(json : Json, key : String) -> Result[Double,
    ToolResult
    ]

    Extract number from JSON, returning error ToolResult on failure

    get_optional_number

    fn get_optional_number(json : Json, key : String) -> Result[Double?,
    ToolResult
    ]

    Extract optional number from JSON

    get_optional_string

    fn get_optional_string(json : Json, key : String) -> Result[String?,
    ToolResult
    ]

    Extract optional string from JSON

    get_string

    fn get_string(json : Json, key : String) -> Result[String,
    ToolResult
    ]

    Extract string from JSON, returning error ToolResult on failure

    simple_tool

    fn simple_tool(name : String, description : String, handler : (Json) ->
    ToolResult
    ) ->
    ToolWrapper

    tool_fn

    fn[Args :
    Params
    , Ret :
    ToToolResult
    ] tool_fn(handler : (Args) -> Ret, name~ : String, description~ : String) ->
    ToolWrapper

    Source Files