mcp API reference
Model Context Protocol (https://modelcontextprotocol.io) - the stateless JSON-RPC 2.0 surface an LLM host and an MCP peer speak. This module is both a server (expose tools / resources / prompts to a host) and a client (call a remote MCP server) over either HTTP or a launched stdio subprocess. It is JSON-RPC 2.0 on the wire, so the HTTP client posts over http and reads replies with jsonrpc.parseReply, and the server is a purpose-built router (handle) that dispatches only to registered items - unlike jsonrpc.handle's open name dispatch, MCP is an allow-list.
A Server is built value-semantically: server(name, version) then addTool / addResource / addPrompt, each returning a fresh Server. Each registered item is handled by a top-level func NAME(arg as json.Value) in the entry program, passed as a func value and called through its home interpreter (the same mechanism the web module uses), so it resolves its own imports and builds its json.Value result in the entry program's context. handle(server, requestBody) -> replyBody is the transport- agnostic dispatcher; serveStdio(server) runs the primary stdio transport (newline-delimited JSON-RPC on the program's own stdin/stdout).
Scope: the stateless protocol only. No sessions, and no server-sent events beyond a POST's own response stream. The stdio server (serveStdio), the HTTP client (connect), and the stdio subprocess client (connectStdio, over os.spawn and /bin/sh) are all implemented; the stdio client's exchange is one-shot (launch, handshake + op, read replies) since there is no persistent child pipe. Because it uses http / os.spawn, this module needs the default jennifer binary.
Security. A tool call is dispatched only when the tool name is in the registry; an unknown tool is a tool error, not an arbitrary-name dispatch. A handler that throws yields a generic error to the peer - the thrown message is never put on the wire, matching jsonrpc's posture.
Client errors. Over HTTP a failure is the jsonrpc module's jsonrpc.RpcError (kind "jsonrpc"); over stdio it is an mcp.McpError (kind "mcp"). Both carry the server's JSON-RPC code and data for an error reply, and a reason for the other failures. A socket failure over HTTP is a net.NetError (kind "net"), a reached cap kind "limit", a stdio server that does not answer within the client's stdioTimeoutMs kind "timeout". A client carrying an Authorization header refuses an http:// endpoint on a non-loopback host before connecting, unless its allowInsecureHttp field is set.
Import with import "mcp.j" as mcp;. See the mcp guide for prose and examples.
Contents
| Name | What it is |
|---|---|
addPrompt | Register a prompt, returning a fresh server (value-semantic). |
addResource | Register a resource, returning a fresh server (value-semantic). |
addTool | Register a tool, returning a fresh server (value-semantic). |
callTool | Call a remote tool by name. |
connect | Connect to a remote MCP server over HTTP. |
connectStdio | Connect to an MCP server launched as a subprocess over stdio - the primary MCP transport (a host runs t... |
connectWith | Connect to a remote MCP server with extra request headers (auth, ...). |
getPrompt | Get a remote prompt by name. |
handle | Dispatch a single JSON-RPC request body against the server and return the reply body (transport-agnostic: w... |
initialize | Perform the initialize handshake and return the server's result (its protocol version, capabilities, and ... |
listPrompts | List the remote server's prompts. |
listResources | List the remote server's resources. |
listTools | List the remote server's tools. |
promptArg | Build one prompt argument declaration for addPrompt. |
property | Add a property to a schema, returning a fresh schema. |
readResource | Read a remote resource by URI. |
schema | Build an empty JSON Schema object skeleton (type: object) for a tool's inputSchema. |
serveStdio | Run the stdio MCP server: read newline-delimited JSON-RPC requests from the program's own stdin, dispatch e... |
server | Build an empty server with the given identity. |
Client | struct: An MCP client. |
McpError | struct: A failed exchange with a stdio MCP server. |
Prompt | struct: A prompt template the server exposes. |
PromptArg | struct: One argument a prompt declares, so a host can collect it before prompts/get. |
Resource | struct: A resource the server exposes. |
Server | struct: An MCP server: an identity plus registries of tools, resources, and prompts. |
Tool | struct: A tool the server exposes. |
Functions
addPrompt
mcp.addPrompt(
server as Server,
name as string,
description as string,
arguments as list of PromptArg,
handler as func)Register a prompt, returning a fresh server (value-semantic). arguments declares the arguments a host should collect (build each with promptArg; pass [] for a prompt that takes none).
server- the server to extend
name- the prompt's unique name
description- a description of the prompt
arguments- the declared arguments (may be empty)
handler- the handler func value that builds the messages
- Returns
Server - a server with the prompt added
addResource
mcp.addResource(
server as Server,
uri as string,
name as string,
description as string,
mimeType as string,
handler as func)Register a resource, returning a fresh server (value-semantic).
server- the server to extend
uri- the resource's unique URI
name- a short display name
description- a description of the resource
mimeType- the content type of the resource's text
handler- the handler func value that produces the content
- Returns
Server - a server with the resource added
addTool
mcp.addTool(
server as Server,
name as string,
description as string,
inputSchema as json.Value,
handler as func)Register a tool, returning a fresh server (value-semantic).
server- the server to extend
name- the tool's unique name
description- a description of the tool
inputSchema- the JSON Schema for the tool's arguments
handler- the handler func value the call dispatches to
- Returns
Server - a server with the tool added
callTool
mcp.callTool(client as Client, name as string, arguments as json.Value)Call a remote tool by name. Returns the whole tool result (a content array plus an isError flag), so the caller can check isError.
client- the MCP client
name- the tool name
arguments- the tool arguments object
- Returns
json.Value - the
tools/callresult
connect
mcp.connect(endpoint as string)Connect to a remote MCP server over HTTP.
endpoint- the MCP endpoint URL (
http://orhttps://) - Returns
Client - a configured client
connectStdio
mcp.connectStdio(argv as list of string)Connect to an MCP server launched as a subprocess over stdio - the primary MCP transport (a host runs the server and speaks newline-delimited JSON-RPC on its stdin/stdout). argv is the server command line (program first). Because the exchange is one-shot (no persistent pipe), each call launches the server, sends the initialize handshake plus the operation as newline-delimited JSON on stdin, closes stdin, and reads the replies from stdout - so a stateless server sees a complete short session per call. Once the reply is in, a server that does not exit by itself is stopped (SIGTERM, then SIGKILL, 2 s apart); one that has not replied within stdioTimeoutMs (default 60 s) is stopped the same way and the call throws kind "timeout". Needs the default binary (os.spawn) and a POSIX /bin/sh, which feeds the server its stdin.
argv- the server command line (
["python", "srv.py"], ...) - Returns
Client - a configured stdio client
connectWith
mcp.connectWith(endpoint as string, headers as map of string to string)Connect to a remote MCP server with extra request headers (auth, ...).
endpoint- the MCP endpoint URL
headers- headers sent with every request
- Returns
Client - a configured client
getPrompt
mcp.getPrompt(client as Client, name as string, arguments as json.Value)Get a remote prompt by name. Returns the whole prompts/get result (a description plus the messages array).
client- the MCP client
name- the prompt name
arguments- the prompt arguments object
- Returns
json.Value - the
prompts/getresult
handle
mcp.handle(server as Server, requestBody as string)Dispatch a single JSON-RPC request body against the server and return the reply body (transport-agnostic: wire it to httpd / net / stdio). A notification (no id, or any notifications/ method) yields "". A parse failure is a -32700 reply; a request that is not a JSON-RPC 2.0 request (not an object, a jsonrpc other than "2.0", a non-string method, an id that is not a string or an integer) is -32600, with a null id when the id is the problem; params that are not an object are -32602; an unknown protocol method is -32601. A tools/call to an unknown tool, or a tool handler that throws, is a successful reply carrying a tool result with isError: true (the thrown message is never put on the wire).
server- the server whose registries route the request
requestBody- the raw request JSON
- Returns
string - the reply JSON, or
""when no reply is owed
initialize
mcp.initialize(client as Client)Perform the initialize handshake and return the server's result (its protocol version, capabilities, and serverInfo). Per the MCP lifecycle, this also sends the required notifications/initialized once the handshake succeeds, so the session is ready for further requests. Throws a jsonrpc.RpcError (HTTP) or an McpError (stdio) on any transport or protocol failure.
client- the MCP client
- Returns
json.Value - the server's
initializeresult
listPrompts
mcp.listPrompts(client as Client)List the remote server's prompts. Returns the prompts array from the prompts/list result.
client- the MCP client
- Returns
json.Value - the array of prompt descriptors
listResources
mcp.listResources(client as Client)List the remote server's resources. Returns the resources array from the resources/list result.
client- the MCP client
- Returns
json.Value - the array of resource descriptors
listTools
mcp.listTools(client as Client)List the remote server's tools. Returns the tools array from the tools/list result.
client- the MCP client
- Returns
json.Value - the array of tool descriptors
promptArg
mcp.promptArg(name as string, description as string, required as bool)Build one prompt argument declaration for addPrompt.
name- the argument's name
description- a description of the argument
required- whether the host must supply it
- Returns
PromptArg - the argument declaration
property
mcp.property(
schema as json.Value,
name as string,
jsonType as string,
description as string,
required as bool)Add a property to a schema, returning a fresh schema. Sets properties/<name> = {type, description} and appends name to required when required is true.
schema- the schema to extend
name- the property name
jsonType- the JSON Schema type (
"string","integer", ...) description- a description of the property
required- whether the property is required
- Returns
json.Value - the extended schema
readResource
mcp.readResource(client as Client, uri as string)Read a remote resource by URI. Returns the whole resources/read result (a contents array).
client- the MCP client
uri- the resource URI
- Returns
json.Value - the
resources/readresult
schema
mcp.schema()Build an empty JSON Schema object skeleton (type: object) for a tool's inputSchema. Add fields with property.
- Returns
json.Value - a
{"type":"object","properties":{},"required":[]}skeleton
serveStdio
mcp.serveStdio(server as Server)Run the stdio MCP server: read newline-delimited JSON-RPC requests from the program's own stdin, dispatch each through handle, and write each non-empty reply to stdout. Blank lines are skipped; the loop ends at EOF. This is the primary MCP transport (an LLM host launches the program and speaks to it over stdin/stdout). A line that is not UTF-8, or one over io.readLine's cap, is answered with a -32700 parse error, like one that is not JSON, and the loop goes on; the rest of an over-cap line is skipped, not read as a request.
server- the server whose registries route each request
server
mcp.server(name as string, version as string)Build an empty server with the given identity.
name- the server name
version- the server version
- Returns
Server - a server with no registered items
Structs
Client
An MCP client. Over HTTP (connect / connectWith) it wraps a jsonrpc.Client; over stdio (connectStdio) it holds the server's argv and drives it as a subprocess. The same call surface (initialize / listTools / callTool / ...) works for both transports.
def struct Client {
rpc as jsonrpc.Client,
argv as list of string,
isStdio as bool,
allowInsecureHttp as bool,
stdioTimeoutMs as int
};rpc- the underlying JSON-RPC HTTP client (HTTP transport)
argv- the stdio server command line (stdio transport)
isStdio- true for the stdio transport, false for HTTP
allowInsecureHttp- allow an
Authorizationheader over plainhttp://to a non-loopback host (HTTP transport; default false) stdioTimeoutMs- how long one stdio exchange may take, launch to reply, before the server is killed (0 = 60 s)
McpError
A failed exchange with a stdio MCP server. Every error of kind "mcp" is one, so its fields are safe to read once the kind matches. (Over HTTP the client throws jsonrpc.RpcError, kind "jsonrpc".)
def struct McpError {
kind as string,
message as string,
reason as string,
code as int,
data as json.Value,
exitCode as int
};kind- always
"mcp" messagemcp error <code>: <message>for an error reply, else what went wrongreason"rpc"(the server answered with an error object),"launch"(the server could not be run),"exit"(it exited non-zero),"protocol"(its stdout carried something other than JSON-RPC 2.0 messages, or a malformed reply) or"noreply"(no reply for the request)code- the error object's
code(such as -32602, invalid params); 0 unlessreasonis"rpc"or the object had none data- the error object's
datamember; JSONnullwhen it has none orreasonis not"rpc" exitCode- the server's exit code when
reasonis"exit"; 0 otherwise
Prompt
A prompt template the server exposes. Its handler is a func value func NAME(arguments as json.Value) returning the prompt messages as a json.Value; arguments declares what a host should collect (surfaced by prompts/list).
def struct Prompt {
name as string,
description as string,
arguments as list of PromptArg,
handler as func
};name- the prompt's unique name (the
prompts/getkey) description- a human / model readable description
arguments- the declared arguments (may be empty)
handler- the handler func value that builds the messages
PromptArg
One argument a prompt declares, so a host can collect it before prompts/get. Build with promptArg.
def struct PromptArg {
name as string,
description as string,
required as bool
};name- the argument's name
description- a human / model readable description
required- whether the host must supply the argument
Resource
A resource the server exposes. Its handler is a func value func NAME(uri as json.Value) returning the resource's text.
def struct Resource {
uri as string,
name as string,
description as string,
mimeType as string,
handler as func
};uri- the resource's unique URI (the
resources/readkey) name- a short display name
description- a human / model readable description
mimeType- the content type of the resource's text
handler- the handler func value that produces the content
Server
An MCP server: an identity plus registries of tools, resources, and prompts. Value-semantic; build with server and the add* functions.
def struct Server {
name as string,
version as string,
tools as list of Tool,
resources as list of Resource,
prompts as list of Prompt
};name- the server name reported in
initialize version- the server version reported in
initialize tools- the registered tools
resources- the registered resources
prompts- the registered prompts
Tool
A tool the server exposes. Its handler is a func value func NAME(args as json.Value) in the entry program that runs when the tool is called.
def struct Tool {
name as string,
description as string,
handler as func,
inputSchema as json.Value
};name- the tool's unique name (the
tools/callkey) description- a human / model readable description
handler- the handler func value to dispatch the call to
inputSchema- the JSON Schema for the tool's arguments