Skip to content

colmugx/mcp.mbt

Repository files navigation

MoonBit MCP SDK

Type-safe Model Context Protocol SDK for MoonBit.

Version: 0.14.0 · Protocol: 2025-11-25 · License: Apache-2.0

Installation

moon add colmugx/mcp

The SDK uses moonbitlang/async@0.19.4 and targets native I/O for server, client, and host runtimes.

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) => {
      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

Document Description
Quickstart Server, client, and host setup
Protocol Types JSON-RPC, errors, and MCP types
Server Guide High-level server API and runtime behavior
Transport Reference Advanced transport internals
Client Guide MCPClient::connect_* and bidirectional mode
Architecture Protocol, runtime, transport, application layers
Host Guide MCPHost named connections and routing
Migration 0.14 Breaking changes from 0.13.x

v0.14 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.

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages