Type-safe MCP SDK in MoonBit with server/client support and dual transport (STDIO/HTTP)
Dependencies
moon add colmugx/mcpasync 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()
}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()}")
}
}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()
})
}| 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.15 | 2025-11-25 → 2026-07-28 migration, node by node |
| Migration 0.14 | Breaking changes from 0.13.x |
fn[Args : Params, Ret : ToToolResult] tool_fn(handler : (Args) -> Ret, name~ : String, description~ : String) -> ToolWrapperType-safe MCP SDK in MoonBit with server/client support and dual transport (STDIO/HTTP)
Dependencies