A MoonBit SDK for the Model Context Protocol (MCP). Build MCP servers and clients in MoonBit.
moon add 123123213weqw/moonbit_mcpimport {
"123123213weqw/moonbit_mcp" @mcp,
}
///|
fn main {
let server = @mcp.McpServer::new("echo-server", "1.0.0")
server.tool(
"echo",
@mcp.JsonSchema::object() |> @mcp.JsonSchema::string("message"),
fn(args) {
let msg = @mcp.ContentBlock::as_text(
@mcp.ContentBlock::TextContent("echo: " + args.stringify(), None)
).or_else(fn() { "no args" })
@mcp.CallToolResult::text(msg)
},
)
// In a real server, you'd drive server.process_message(raw) from a transport.
}| Layer | Modules |
|---|---|
| Protocol | JSON-RPC 2.0 parsing/encoding, MCP message types (Request/Response/Notification) |
| Types | ContentBlock (text/image/audio/resource), Tool, Resource, Prompt, LogMessage |
| Server | Low-level Server (handler registry + dispatch) + high-level McpServer builder |
| Schema | JSON Schema builder for tool input/output schemas |
| Transport | trait Transport abstraction + InMemoryTransport (tests) + BufferedTransport |
┌─────────────────────────────────┐
│ McpServer (high-level builder) │
│ .tool() / .resource() / ... │
└──────────────┬──────────────────┘
│ registers handlers
┌──────────────▼──────────────────┐
│ Server (low-level dispatcher) │
│ initialize / tools/list / call │
│ + capability negotiation │
└──────────────┬──────────────────┘
│ uses
┌──────────────▼──────────────────┐
│ JSON-RPC 2.0 parse_message() │
│ + message_to_string() │
└──────────────┬──────────────────┘
│ drives any
┌──────────────▼──────────────────┐
│ trait Transport │
│ InMemoryTransport | Buffered │
└─────────────────────────────────┘moon fmt --check # format check
moon check --deny-warn # type check (warnings as errors)
moon test --deny-warn # run tests
moon test --target all # wasm / wasm-gc / js / native
moon info # regenerate public interface snapshottype MessageHandler = (String) -> Unitpub trait Transport {
fn send(Self, String) -> Unit
}pub(all) struct CallToolResult {
content : Array[ContentBlock]
structured_content : Json?
is_error : Bool
} derive(Eq, Debug)pub(all) enum ContentBlock {
TextContent(String, ContentAnnotations?)
ImageContent(String, String, ContentAnnotations?)
AudioContent(String, String, ContentAnnotations?)
ResourceLink(String, String, String?, String?, String?, ContentAnnotations?)
EmbeddedResource(ResourceContents, ContentAnnotations?)
} derive(Eq, Debug)pub(all) struct GetPromptResult {
description : String?
messages : Array[PromptMessage]
} derive(Eq, Debug)pub(all) struct InMemoryTransport {
handler : (String) -> Unit?
closed : Bool
peer : InMemoryTransport?
}pub(all) struct JsonSchema {
typ : SchemaType
description : String?
properties : Map[String, SchemaProperty]
required : Array[String]
} derive(Eq, Debug)fn McpServer::tool(self : McpServer, name : String, schema : JsonSchema, cb : (Json) -> CallToolResult) -> McpServerfn McpServer::tool_desc(self : McpServer, name : String, desc : String, schema : JsonSchema, cb : (Json) -> CallToolResult) -> McpServerpub(all) struct Prompt {
name : String
title : String?
description : String?
arguments : Array[PromptArgument]
} derive(Eq, Debug)pub(all) enum RpcMessage {
MsgRequest(RpcRequest)
MsgResponse(RpcResponse)
MsgNotification(RpcNotification)
} derive(Eq, Debug)pub(all) struct Server {
server_info : Implementation
capabilities : ServerCapabilities
handlers : Map[String, (Json, ServerContext) -> Result[Json, McpError]]
initialized : Bool
}pub(all) struct ServerContext {
server_info : Implementation
capabilities : ServerCapabilities
initialized : Bool
}pub(all) struct Tool {
name : String
title : String?
description : String?
input_schema : JsonSchema
output_schema : JsonSchema?
annotations : ToolAnnotations?
} derive(Eq, Debug)let implementation_error_start : Intlet internal_error_code : Intlet invalid_params_code : Intlet invalid_request_code : Intlet latest_protocol_version : Stringlet method_not_found_code : Intlet resource_not_found_code : IntA MoonBit SDK for the Model Context Protocol (MCP). Build MCP servers and clients in MoonBit.