main.bend relies on unsafe/foreign
raw source on the hub · import ezmcp@0.5.0.0/main.bend as Main
ezmcp: Model Context Protocol servers and clients for Bend 2, revisions 2025-03-26, 2025-06-18 and 2025-11-25
A server is a name, a version, instructions and a tool table; a runner
(String -> Json -> IO(Rpc.Out)) runs a tool by name on its arguments and
answers ok of a tool result, or refuse with a JSON-RPC error. ezmcp serves it over streamable HTTP (on ezhttp)
or stdio, and answers initialize, ping, tools/list and tools/call itself.
The client side builds requests and reads replies over HTTP. This file is
the whole public interface; everything under src/ is internal, except the
types src/server.bend, src/rpc.bend and src/web.bend name.
9 imports
import Base import ezjson@1.5.0.0/src/value.bend as Value import ezhttp@0.26.0.0/src/http.bend as Http import ./src/rpc.bend as Rpc import ./src/server.bend as Server import ./src/web.bend as Web import ./src/client.bend as Client import ./src/io.bend as Io import ./src/openapi.bend as Openapi
Definitions
def tool source · line 25 · raw
@name:String -> @description:String -> @input:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.ToolSpec
a tool: its name, a description for the model, and the JSON Schema of its arguments (an object schema)
def server source · line 30 · raw
@name:String -> @version:String -> @instructions:String -> @tools:List<&2, 0x244a8dc4388b93088fc9417f57a9eccb/src/server.ToolSpec> -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server
a server: name and version (serverInfo), instructions for the client ("" for none), and its tools in the order tools/list shows them
def latest source · line 39 · raw
String
the latest protocol revision ezmcp speaks
def revisions source · line 43 · raw
List<&2, String>
the protocol revisions ezmcp speaks, oldest first
def step source · line 51 · raw
@srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @rev:String -> @text:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Act
what a server does with one message's text, read by revision rev: Quiet
(send nothing), Send or Fault (send this reply; a Fault is a message it
could not accept), Run (run a tool, then send ran of its result), or,
under 2025-03-26, Batch (do each of its acts, then send batch.reply of
their replies). For a transport of your own
def turn source · line 57 · raw
@srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @rev:String -> @text:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Turn
step's act, and the revision a stream's next message is read by: the one
an initialize was answered with, else rev. For a stream transport of your
own, which starts at latest()
def ran source · line 62 · raw
@rev:String -> @id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @out:0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
the reply to a tool call with this id under revision rev, once the
runner answered (2025-03-26 leaves structuredContent out)
def batch.reply source · line 67 · raw
@replies:List<&2, Maybe<&2, 0x597f8af3f77d8983aacfa076df42d235/src/value.Json>> -> Maybe<&2, 0x597f8af3f77d8983aacfa076df42d235/src/value.Json>
what a batch sends back, from its acts' replies in order (None for an act with none): nothing when none has one, else their array
def ok source · line 88 · raw
@result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out
a runner's answer for a tool that ran: its tool result, which may itself
report a failure with isError (result.error)
def refuse source · line 94 · raw
@code:0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Code -> @message:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out
a runner's answer for a call it refuses as a protocol error: a JSON-RPC error with this code and message instead of a tool result (server/tools#error-handling)
def refuse.params source · line 99 · raw
@message:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out
refuse with -32602 Invalid params, as for arguments that do not match the tool's input schema
def refuse.internal source · line 103 · raw
@message:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out
refuse with -32603 Internal error, as for a server that cannot run the tool
def text source · line 109 · raw
@text2:String -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a text content block
def result source · line 114 · raw
@blocks:List<&2, 0x597f8af3f77d8983aacfa076df42d235/src/value.Json> -> @is_error:Bool -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a tool result from content blocks; is_error flags a tool that failed, so the model sees the failure (server/tools#error-handling)
def result.text source · line 118 · raw
@text:String -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a tool result of one text block
def result.error source · line 122 · raw
@text:String -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a failed tool's result: one text block, isError true
def result.json source · line 127 · raw
@val:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a tool result with structured content, and its JSON text as a text block; a 2025-03-26 client gets the text block alone
def http.decide source · line 134 · raw
@srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @path:String -> @origins:List<&2, String> -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> 0x244a8dc4388b93088fc9417f57a9eccb/src/web.Web
what the HTTP transport decides for one request to an endpoint at path
that allows the browser origins listed: a reply now, or a tool to run
def openapi.ops source · line 311 · raw
@doc:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> List<&2, 0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op>
every operation of an OpenAPI document (a JSON value), in the order of
its paths, each path's in the order get, put, post, delete, options,
head, patch, trace; its local references (#/...) followed
def openapi.tool source · line 315 · raw
@op:0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.ToolSpec
an operation's tool: its name, description and input schema
def openapi.name source · line 319 · raw
@op:0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op -> String
an operation's tool name: its operationId as a tool name
def openapi.renamed source · line 323 · raw
@op:0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op -> @name:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op
an operation under another tool name, for a client that limits names
def openapi.check source · line 328 · raw
@op:0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op -> @args:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> Maybe<&2, String>
what is wrong with a call's arguments for an operation's input schema, when anything is: Some of the message, or None when they fit
def openapi.call source · line 335 · raw
@op:0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Op -> @args:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x244a8dc4388b93088fc9417f57a9eccb/src/openapi.Call
the request a call of an operation with these arguments stands for: Send{method, target, headers, body} (the target is the path and query, for the caller to append to the API's base URL), or Unfilled{path} when a path parameter has no argument
def client.initialize source · line 341 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @name:String -> @version:String -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
the initialize request, naming the client
def client.initialized source · line 349 · raw
0x597f8af3f77d8983aacfa076df42d235/src/value.Json
the notification to send once initialize succeeded
def client.tools source · line 353 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a tools/list request
def client.call source · line 357 · raw
@id:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> @name:String -> @args:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> 0x597f8af3f77d8983aacfa076df42d235/src/value.Json
a tools/call request
def client.read source · line 362 · raw
@text:String -> 0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Reply
what a reply's JSON text says: Got{id, result}, Refused{id, code, message} or Unread{why}
def client.version source · line 366 · raw
@result:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> String
the revision an initialize result names, "" when it names none
def client.ask source · line 372 · raw
@url:String -> @version:String -> @timeout_ms:U32 -> @msg:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Reply)
POST a request to an MCP endpoint and read its reply, JSON or an event
stream. version is the negotiated revision ("" before initialize); the
timeout bounds connecting, sending and receiving
def client.tell source · line 381 · raw
@url:String -> @version:String -> @timeout_ms:U32 -> @msg:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(Bool)
POST a notification; whether the server accepted it (any 2xx)
Templates
template http.handle source · line 144 · raw
@-run:(@_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)
one request to the MCP endpoint at path, as an ezhttp IO handler, for a
server that serves other paths too
template http.serve source · line 156 · raw
@-run:(@_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)
serve the endpoint on host:port for limit connections, each on its own
fork with no limit on how many at once, then stop once every connection is
closed
template http.handle_with source · line 187 · raw
@-C:Data -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)
one request to the MCP endpoint at path, its runner given ctx
template http.serve_with source · line 202 · raw
@-C:Data -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @limit:U32 -> @max:U32 -> IO(Unit)
serve the endpoint on addr, (host, port), for limit connections, its
runner given ctx, each connection on its own fork and at most max at
once (0: no limit; 1: one at a time), then stop once every connection is
closed
template http.handle_from source · line 238 · raw
@-C:Data -> @-srv:(@_:C -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @req:0x0b59049ea5717d6f2fd30293c7271ede/src/http.Request -> IO(0x0b59049ea5717d6f2fd30293c7271ede/src/http.Reply)
one request to the MCP endpoint at path, its server and runner given
ctx
template http.serve_from source · line 252 · raw
@-C:Data -> @-srv:(@_:C -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @limit:U32 -> @max:U32 -> IO(Unit)
serve the endpoint on addr, (host, port), for limit connections, its
server and runner given ctx, each connection on its own fork and at most
max at once (0: no limit), then stop once every connection is closed
Unsafe
unsafe http.serve_forever source · line 169 · raw
@-run:(@_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @host:String -> @port:U32 -> IO(Unit)
serve the endpoint on host:port until the process is stopped, each connection on its own fork with no limit on how many at once
unsafe http.serve_forever_with source · line 218 · raw
@-C:Data -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @max:U32 -> IO(Unit)
serve the endpoint on addr, (host, port), its runner given ctx, each
connection on its own fork and at most max at once (0: no limit; 1: one
at a time), until the process is stopped
unsafe http.serve_forever_from source · line 268 · raw
@-C:Data -> @-srv:(@_:C -> 0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @max:U32 -> IO(Unit)
serve the endpoint on addr, (host, port), its server and runner given
ctx, each connection on its own fork and at most max at once (0: no
limit), until the process is stopped
unsafe stdio.serve_with source · line 281 · raw
@-C:Data -> @-run:(@_:C -> @_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> @+ctx:C -> IO(Unit)
serve stdin and stdout, its runner given ctx, until stdin ends
unsafe stdio.serve source · line 293 · raw
@-run:(@_:String -> @_:0x597f8af3f77d8983aacfa076df42d235/src/value.Json -> IO(0x244a8dc4388b93088fc9417f57a9eccb/src/rpc.Out)) -> @-srv:0x244a8dc4388b93088fc9417f57a9eccb/src/server.Server -> IO(Unit)
serve newline-delimited JSON-RPC on stdin and stdout until stdin ends; write logs with IO.print_err, never to stdout