mcp

    Download zip
    Author
    Version
    0.6.0
    License
    Zlib
    Last updated
    4 hours ago
    Downloads
    13

    #Omochice/mcp

    A dual-era MCP tool server over stdio, usable by any MoonBit program that offers tools.

    #What is supported

    A client can rely on the following, whichever era it speaks.

    • Legacy clients that open with initialize, at protocol versions 2025-11-25, 2025-06-18, 2025-03-26, and 2024-11-05.
    • Modern clients that name 2026-07-28 in params._meta of every request, answered with resultType and server/discover.
    • tools/list and tools/call, with the arguments of each tool validated against an Omochice/schema definition before the handler runs.
    • ping, notifications/initialized, and notifications/cancelled, which stops the task of the cancelled request.
    • One task per request on moonbitlang/async, serialized writes to stdout, and a graceful end when stdin closes.

    #What is not supported

    The module implements only what a tool server needs, so the gaps below are left open on purpose rather than waiting to be filled.

    • Resources, prompts, progress, logging, subscriptions, and multi round-trip requests.
    • structuredContent; a tool result carries text content only.
    • The HTTP transport; serve takes any @io.Reader and @io.Writer, but only stdio is wired up.

    #Usage

    The program below is src/example/main.mbt and registers one tool that echoes its argument.

    async fn main {
    let text = @schema.required("text", @schema.string())
    let echo = @mcp.tool(
    name="echo",
    description="Returns the given text",
    input=@schema.record([text.as_member()], v => v.get(text)),
    handler=fn(text) { @mcp.ToolResult::text(text) },
    )
    @mcp.Server::new(name="mcp-example", version="0.1.0", tools=[echo]).serve_stdio()
    }

    Server::new also takes instructions? for the text a client shows its model, and on_invalid? to shape the tool result of an argument that fails validation. A handler returns ToolResult::text on success and ToolResult::error on failure, and both reach the client as a tool result rather than a protocol error.

    Content

    pub(all) enum Content {
    Text(String)
    }

    One piece of tool output. Only text is offered, because a tool server that answers with documents serialized as text needs no other content type.

    Server

    pub struct Server {
    // private fields
    }

    A dual-era MCP server offering tools. Build one with Server::new.

    Server::new

    fn Server::new(name~ : String, version~ : String, tools~ : Array[Tool], instructions? : String, on_invalid? : (
    SchemaError
    ) -> ToolResult) -> Server

    Creates a server that identifies itself as name and version and offers tools in the given order. instructions is guidance for the model, sent to the client as is. on_invalid turns a failed argument validation into the result the model sees; it defaults to the joined validation messages.

    Server::serve

    async fn Server::serve(self : Server, input : &
    Reader
    , output : &
    Writer
    ) -> Unit

    Serves requests read from input as lines, writing responses to output as lines, until input reaches end of file and every request in flight has been answered.

    Raises when output fails, since no further response could be delivered.

    Server::serve_stdio

    async fn Server::serve_stdio(self : Server) -> Unit

    Serves the process's standard input and output until standard input closes.

    Raises when standard output fails.

    Tool

    pub struct Tool {
    // private fields
    }

    A tool with its argument type erased, so tools of different argument types share one array. Build one with tool.

    ToolResult

    pub(all) struct ToolResult {
    content : Array[Content]
    is_error : Bool
    }

    What a tool call returns to the model; is_error marks feedback the model can use to correct its next call rather than a protocol failure.

    ToolResult::error

    fn ToolResult::error(text : String) -> ToolResult

    A failed result holding text.

    ToolResult::text

    fn ToolResult::text(text : String) -> ToolResult

    A successful result holding text.

    tool

    fn[T] tool(name~ : String, description~ : String, input~ :
    Schema
    [T], handler~ : async (T) -> ToolResult) -> Tool

    Declares a tool whose arguments are parsed by input before handler runs. Arguments that fail input never reach handler; the server answers them with its invalid-arguments result instead. An error raised by handler becomes an error result so one failing tool cannot take the server down.