# mcp

The Model Context Protocol over Streamable HTTP: the program as an MCP
server (its typed fns as tools, its rows as resources, its prompts), and
as a client of other servers, whose tools the [ai](../ai/README.md)
extension's tool loop can call. The whole of it is
[docs/mcp.md](../../docs/mcp.md).

```text
use "pkg:mcp/server.mote"      // POST/GET/DELETE /mcp, and what the program says it offers
use "pkg:mcp/client.mote"      // mcpServer, mcpListTools, mcpCallTool, resources, prompts
use "pkg:mcp/ai.mote"          // mcpAiTools, mcpAiCall (takes pkg:ai/ai.mote and client.mote)

fn mcpTools(who: string): McpTool[] = [mcpToolFor("weather", schema(weatherIn))]
fn mcpCall(call: McpCall, tell: (done: number, total: number, words: string) => bool): McpResult
```

Settings: `MCP_TOKEN` (a secret: the one bearer token, unless the program
says `fn mcpWho`), `MCP_ORIGINS`, `MCP_RESOURCE_METADATA`.

| File | |
|---|---|
| `mcp.mote` | The shapes (tool, content, result, resource, prompt) and the protocol's JSON. |
| `server.mote` | The routes at `/mcp`, sessions, the door (Origin, bearer), each method. |
| `client.mote` | Sessions with other servers, kept; JSON and event-stream answers. |
| `ai.mote` | Another server's tools in `aiRun`. |
| `tests.mote` | Its tests of the client: `use "pkg:mcp/tests.mote"`, then `mote test` (tests/svc_mcp/unit.mote also tests the server). |

**Covered:** protocol version 2025-06-18 (2025-03-26 and 2025-11-25
taken): initialize and notifications/initialized, sessions
(`Mcp-Session-Id`, 404 when over, DELETE), `MCP-Protocol-Version`, ping,
tools/list and tools/call (input checked against the schema, a tool's
failure a result with `isError`, `structuredContent` checked against
`outputSchema`), progress as `notifications/progress` events, resources/list
and read (text or bytes), resources/templates/list (empty), prompts/list
and get (required arguments checked), logging/setLevel, completion/complete
(empty), notifications and responses taken with 202, batches, JSON-RPC
errors with their codes, the Origin checked, a bearer token.

**Left out, on purpose or for now:** the server's own stream (GET `/mcp`
is 405: it sends nothing unasked, so no sampling, elicitation, roots or
list_changed notifications from it); resumable streams (event ids and
`Last-Event-ID` on MCP's streams); resource subscriptions and templates of
the program's own; pagination of the server's lists (it gives all, no
`nextCursor`; the client follows one); the OAuth 2.1 side (the
authorization server, and the protected resource metadata route: see
docs/mcp.md); the stdio transport.

**Checked only against itself**: the extension's own client, a plain
fetch client in `tests/svc_mcp.mjs` that speaks the protocol as written,
and `given net` answers (`tests/all.sh real_svc_mcp`). No other MCP
implementation has talked to it yet: docs/mcp.md has the steps.
