@procedures/mcp-server 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/README.md +99 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts +20 -1
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +49 -3
- package/dist/face.js.map +1 -1
- package/dist/host.d.ts +64 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +73 -11
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/tasks.d.ts +131 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +276 -0
- package/dist/tasks.js.map +1 -0
- package/dist/transport/guard.d.ts +61 -0
- package/dist/transport/guard.d.ts.map +1 -0
- package/dist/transport/guard.js +58 -0
- package/dist/transport/guard.js.map +1 -0
- package/dist/transport/http.d.ts +17 -2
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +157 -37
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts +19 -0
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +183 -76
- package/dist/transport/mcp-transport.js.map +1 -1
- package/dist/types.d.ts +43 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/face.ts +82 -4
- package/src/host.ts +133 -21
- package/src/index.ts +23 -1
- package/src/tasks.ts +318 -0
- package/src/transport/guard.ts +90 -0
- package/src/transport/http.ts +164 -36
- package/src/transport/mcp-transport.ts +200 -81
- package/src/types.ts +44 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
# @procedures/mcp-server
|
|
2
2
|
|
|
3
|
+
## 0.14.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 85999c1: MCP tasks and the guards of the HTTP face (CLI-35).
|
|
8
|
+
|
|
9
|
+
- A procedure with `metadata.taskSupport` (`"optional"` or `"required"`) can run as an MCP task. `@procedures/mcp` gives it in the tool definition as `execution.taskSupport`, and exports `taskSupportOf`.
|
|
10
|
+
- `createMcpHost` and `McpServerTransport` take the option `tasks`: a store (`McpTaskStore`), or the options of a new store (`maxRunning`, `ttl`). The host shares one store with each face, and `host.tasks` gives it. A task holds its place in the limit until its procedure returns, also after a cancel. A task above the limit starts and then fails at once with the reason, and the store keeps it for 1 minute. The time to keep a task starts when the task ends, and each session keeps at most 100 tasks. A cancel aborts `ctx.signal` of the procedure. The close of a session and the stop of the host cancel their tasks. Without the option `tasks`, the tools do not change.
|
|
11
|
+
- The checks of a request to the Streamable HTTP face are in one function, `guardMcpRequest`, with the browser checks and `tokenMatches` of the core. A 401 now has a `WWW-Authenticate` header. The package also exports `assertLoopbackBind`, for the owner of a listener.
|
|
12
|
+
- The HTTP face takes `allowedHosts`, `maxSessions` and `maxBodyBytes`. `createMcpHost` and `mcpFace` give the face `browser.allowedHosts` of the policy, so a host of that list can reach the endpoint. Before, the face refused it. Thus a policy with `allowedHosts: "*"` now also turns off the DNS-rebinding check of the face, as it does for the other faces.
|
|
13
|
+
- `maxSessions` has no default: the sessions have no limit, as before. With it, a new session above the limit closes the session that was idle longest (no task in progress and no open response), and when each session is busy, the new session gets 503 with `Retry-After`.
|
|
14
|
+
- `maxBodyBytes` (4 MiB by default, the limit of the SDK) applies to each request of the face.
|
|
15
|
+
- The state of an HTTP face (`getState()`, and `host.state().faces`) gives `sessions`, the count of its open sessions.
|
|
16
|
+
- `@procedures/client/node` exports `readJsonBody` and `BodyError`: the procedure handler and the MCP face read a JSON body with this one function.
|
|
17
|
+
|
|
18
|
+
One default of the Streamable HTTP face changes: only an `initialize` request starts a session. Before, a POST without a session id made a new MCP server, and then the SDK refused the request with 400 ("Server not initialized"). Now the face refuses it with 400 before it makes a server, so a stray request uses no memory. This agrees with the MCP specification, and a caller sees the same status. When the SDK refuses an `initialize` (for example 406), the face now closes the server of that request.
|
|
19
|
+
- 5efe2d2: New: the option `narrowing` of `mcpFace()` (CLI-43). The option gives each call of the face a narrower capability, as a `CapabilityRequest` or a list of them. The face adds it as one more source of the narrowing key (`procedure-narrowing`, CLI-30), so the server applies the meet of all the sources. A narrowing can only narrow: a part that asks for more than the policy of the host gives has no effect. The face lists only the tools that the narrowed capability permits. Thus `mcpFace({ narrowing: { effects: { writes: [] } } })` is a read-only MCP face on the port of the host, as `mark mcp --read-only` is over stdio. A narrowing that is not valid stops the factory with an error. Without the option, the face does not change.
|
|
20
|
+
- a96c870: The MCP hosts run under `run()` of `@mark1russell7/service` (CLI-11, step 1).
|
|
21
|
+
|
|
22
|
+
- `McpHost.declare(svc)` gives an MCP host to a service. The server of the host is a transport part, and each storage is a dependency. A host with a stdio face declares the trigger `stdin-close`. Then the runner starts the host and stops it in stages.
|
|
23
|
+
- `McpHost.handleHttp(req, res)` answers for the Streamable HTTP face with `mount: true`. Mount it on a listener, for example an `httpTransport` of `@mark1russell7/service/http`.
|
|
24
|
+
- `runDevToolsServer()` of `@procedures/mcp-dev-tools` runs the dev-tools server under `run()`. Over stdio, the profile is `ephemeral`, and the end of the input stops the server. With HTTP, the profile is `localDev`, and a listener of the service answers at `/mcp`. `dist/server.js` and `mark mcp` use it.
|
|
25
|
+
|
|
26
|
+
PATCH: each change adds a function or an option, and the old ones still operate.
|
|
27
|
+
|
|
28
|
+
Deprecated, and the next MINOR removes them (decision D13):
|
|
29
|
+
|
|
30
|
+
- The listener of the Streamable HTTP face: the options `port`, `host` and `server` of `HttpTransportOptions`, and `McpHttpTransport.listen()`. Give `mount: true`, and mount `McpHost.handleHttp` on a listener of the service.
|
|
31
|
+
- `startDevToolsServer()` of `@procedures/mcp-dev-tools`. Use `runDevToolsServer()`.
|
|
32
|
+
|
|
33
|
+
Migration for the next MINOR: the site uses none of them. MiniMongo uses none of them. Graph mounts `mcpFace()` on its host, and its stdio host keeps `createMcpHost()` with `stdio`: no change. jarvis uses `createMcpHost()` with stdio and SDK faces: no change. A host with `faces.http: { port }` moves to `faces.http: { mount: true }` and a listener of the service.
|
|
34
|
+
- Updated dependencies [85999c1]
|
|
35
|
+
- Updated dependencies [fc968f6]
|
|
36
|
+
- Updated dependencies [dc7a462]
|
|
37
|
+
- Updated dependencies [e091108]
|
|
38
|
+
- Updated dependencies [073e4c3]
|
|
39
|
+
- Updated dependencies [6969637]
|
|
40
|
+
- @procedures/mcp@0.14.1
|
|
41
|
+
- @procedures/client@0.14.1
|
|
42
|
+
|
|
3
43
|
## 0.14.0
|
|
4
44
|
|
|
5
45
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -15,11 +15,14 @@
|
|
|
15
15
|
- [Install](#install)
|
|
16
16
|
- [Start a host](#start-a-host)
|
|
17
17
|
- [The faces](#the-faces)
|
|
18
|
+
- [A host under the runner](#a-host-under-the-runner)
|
|
18
19
|
- [Questions to the person](#questions-to-the-person)
|
|
19
20
|
- [Progress and cancel](#progress-and-cancel)
|
|
21
|
+
- [Tasks](#tasks)
|
|
20
22
|
- [Text from outside the host](#text-from-outside-the-host)
|
|
21
23
|
- [The result of a tool](#the-result-of-a-tool)
|
|
22
24
|
- [The transport](#the-transport)
|
|
25
|
+
- [The face of a procedures host](#the-face-of-a-procedures-host)
|
|
23
26
|
- [The procedures of this package](#the-procedures-of-this-package)
|
|
24
27
|
- [Set up Claude](#set-up-claude)
|
|
25
28
|
|
|
@@ -101,13 +104,50 @@ Each face is one `McpServerTransport` on the server of the host. A host can have
|
|
|
101
104
|
|---|---|
|
|
102
105
|
| `stdio: true` | The standard input and output. Over stdio, `console.log` writes to stderr from `start()` on, because stdout carries the protocol. |
|
|
103
106
|
| `stdio: { stdin, stdout, stopOnEnd, redirectConsole }` | Other streams, for a test or an embedding. With `stopOnEnd: true` (the default), the host stops when the input ends. |
|
|
104
|
-
| `http: {
|
|
107
|
+
| `http: { mount: true, path, token, allowedOrigins, allowedHosts, maxSessions, maxBodyBytes }` | Streamable HTTP on a listener of the app. Mount `host.handleHttp` on the listener. Without a token, the face takes the token of the policy. |
|
|
108
|
+
| `http: { port, host, path, token, allowedOrigins }` | Deprecated. The face has a listener of its own, on `127.0.0.1` by default. The next MINOR removes it (CLI-11). |
|
|
105
109
|
| `sdk` | One or more MCP SDK transports, for example the `InMemoryTransport` of a test. |
|
|
106
110
|
|
|
107
|
-
When the client closes the input of stdio, the host stops, and `closed` resolves.
|
|
111
|
+
When the client closes the input of stdio, the host stops, and `closed` resolves.
|
|
108
112
|
|
|
109
113
|
Each tool call over Streamable HTTP carries the credentials of its HTTP request: the bearer token, the cookies, the headers and the query. Thus the request hook of the policy (`principalOf`) and the token check of the server apply to each call. When the server reads cookies, the face trusts a request with a cookie only from a named origin (CSRF). A call over stdio comes from this process and has no credentials.
|
|
110
114
|
|
|
115
|
+
The Streamable HTTP face checks each request with `guardMcpRequest` before the request reaches the MCP SDK. The owner of another listener calls the same function.
|
|
116
|
+
|
|
117
|
+
- A request on a loopback address must name a loopback host, or a host of `allowedHosts` (the default is `browser.allowedHosts` of the policy), in its `Host` header. Thus DNS rebinding cannot reach the tools. An `Origin` header must name the server or a permitted origin.
|
|
118
|
+
- With a token, each request sends `Authorization: Bearer <token>`. The check is `tokenMatches` of the core: its time does not tell the token. A request without the token gets 401 with a `WWW-Authenticate` header.
|
|
119
|
+
- Only an `initialize` request starts a session. Thus a stray POST makes no MCP server. The body of each request has a limit of 4 MiB (`maxBodyBytes`).
|
|
120
|
+
- The sessions have no limit by default. With `maxSessions`, a new session above the limit closes the session that was idle longest: no task in progress and no open response (a call or a GET stream). When each session is busy, the new session gets 503 with a `Retry-After` header. A client that sends to a closed session gets 404, and a client of the MCP specification then sends a new `initialize`.
|
|
121
|
+
- `assertLoopbackBind(host, allowRemote)` refuses a listener on an address that is not loopback, unless `allowRemote` is true.
|
|
122
|
+
|
|
123
|
+
## A host under the runner
|
|
124
|
+
|
|
125
|
+
`host.declare(svc)` gives the host to a service of `@mark1russell7/service` (CLI-11). Then `run()` of the service starts the host, owns the signals, stops the host in stages and ends the process. The host does not start or stop by itself.
|
|
126
|
+
|
|
127
|
+
- The server of the host is the transport part `mcp`. Each storage of the server is a dependency.
|
|
128
|
+
- A host with a stdio face declares the trigger `stdin-close`. The end of the input starts the stop in stages, and the face does not stop the host by itself.
|
|
129
|
+
- The HTTP face (`mount: true`) has no listener. Mount `host.handleHttp` on an `httpTransport` of `@mark1russell7/service/http`, and declare that transport on the service.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { createServer } from "node:http";
|
|
133
|
+
import { ephemeral, localDev, service } from "@mark1russell7/service";
|
|
134
|
+
import { httpTransport } from "@mark1russell7/service/http";
|
|
135
|
+
|
|
136
|
+
// Over stdio: the profile ephemeral and the trigger stdin-close
|
|
137
|
+
const stdioHost = createMcpHost({ registry, faces: { stdio: true } });
|
|
138
|
+
await stdioHost.declare(service("my-mcp").profile(ephemeral)).run();
|
|
139
|
+
|
|
140
|
+
// Over Streamable HTTP: a listener of the service, and the face at /mcp on it
|
|
141
|
+
const httpHost = createMcpHost({ registry, policy: { token }, faces: { http: { mount: true } } });
|
|
142
|
+
const listener = httpTransport(createServer(), knobs, { listen: { port: 3003, host: "127.0.0.1" } });
|
|
143
|
+
listener.mount((req, res) => httpHost.handleHttp(req, res));
|
|
144
|
+
const svc = httpHost.declare(service("my-mcp").profile(localDev));
|
|
145
|
+
svc.server("my-mcp.listen", listener.knobs, listener);
|
|
146
|
+
await svc.run();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The `localDev` profile has no policies of readiness, liveness and startup. Declare them on the service before `run()`. The dev-tools server of `@procedures/mcp-dev-tools` (`runDevToolsServer()`) is an example of each mode.
|
|
150
|
+
|
|
111
151
|
## Questions to the person
|
|
112
152
|
|
|
113
153
|
A procedure can ask the person at the caller for input or a confirmation during a call. MCP elicitation is one face that answers. The shape is not specific to MCP: another face (a terminal) can answer the same questions.
|
|
@@ -185,6 +225,34 @@ handler: async (input, ctx) => {
|
|
|
185
225
|
- A stream does not need it: the face gives each item of a stream as progress. When the procedure of a stream uses `ctx.progress`, the face gives only those updates.
|
|
186
226
|
- When the client cancels a call (`notifications/cancelled`), `ctx.signal` of the procedure aborts.
|
|
187
227
|
|
|
228
|
+
## Tasks
|
|
229
|
+
|
|
230
|
+
A long call can run as an MCP task (MCP 2025-11-25). The client gets a task at once, polls its status, gets the result later, and can cancel the task. Two parts turn it on:
|
|
231
|
+
|
|
232
|
+
- The procedure declares it in its metadata: `taskSupport: "optional"` (a task or a normal call) or `taskSupport: "required"` (only a task).
|
|
233
|
+
- The host has a task store: the option `tasks`.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
const crawlSite = createProcedure()
|
|
237
|
+
.path(["crawl", "site"])
|
|
238
|
+
.meta({ description: "Crawl a site", effects: { reads: ["net"], writes: ["store"] }, taskSupport: "optional" })
|
|
239
|
+
// input, output and handler as for each procedure
|
|
240
|
+
.build();
|
|
241
|
+
|
|
242
|
+
const host = createMcpHost({ modules: [crawlModule], tasks: { maxRunning: 4 } });
|
|
243
|
+
await host.start();
|
|
244
|
+
host.tasks?.running(); // the tasks that did not end, for a status page
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
- The tool list gives `execution.taskSupport` of each such tool. Without the option `tasks`, the list does not give it, and each call is a normal request.
|
|
248
|
+
- A progress of the procedure becomes the status message of its task, for example `2 of 5 done`. The message holds only the counts, so no text of the work leaves its envelopes. A client that sent a progress token also gets the progress notifications.
|
|
249
|
+
- A cancel of the task (`tasks/cancel`) aborts `ctx.signal` of the procedure. The close of a session cancels its tasks, and the stop of the host cancels each task.
|
|
250
|
+
- The store runs at most `maxRunning` tasks at one time (8 by default). A task holds its place until its procedure returns, also after a cancel. A task above the limit starts, and then fails at once. The result of the task gives the reason. (The SDK 1.32.1 cannot give an error for a task request.)
|
|
251
|
+
- The time to keep a task (`ttl`) starts when the task ends. The store does not remove a task whose procedure still runs.
|
|
252
|
+
- The call of a task cannot ask the person: the procedure gets no `ctx.elicit`.
|
|
253
|
+
- A normal call of an optional tool runs in the request, with the progress and the cancel of the request. A normal call of a tool with `taskSupport: "required"`, or a task for a tool without task support, gets the error `-32601`.
|
|
254
|
+
- To share one store with other parts, give `tasks: { store: new McpTaskStore({ maxRunning: 4 }) }`. The host closes only a store that it made.
|
|
255
|
+
|
|
188
256
|
## Text from outside the host
|
|
189
257
|
|
|
190
258
|
A procedure that gives text from outside the host (a web page, a mail) declares the fields of its output that hold it. Then a model gets that text apart from the data, as data and not as instructions.
|
|
@@ -258,6 +326,35 @@ await server.start();
|
|
|
258
326
|
- `transport: "http"` with `httpOptions` gives Streamable HTTP. `sdkTransport` connects an MCP SDK transport.
|
|
259
327
|
- `transport: "sse"` is not implemented.
|
|
260
328
|
|
|
329
|
+
## The face of a procedures host
|
|
330
|
+
|
|
331
|
+
`mcpFace()` gives the MCP face of a procedures host (`createProcedureHost()` of `@procedures/peer`). The face serves Streamable HTTP at a path of a listener of the host, next to the other faces.
|
|
332
|
+
|
|
333
|
+
```typescript
|
|
334
|
+
import { mcpFace } from "@procedures/mcp-server";
|
|
335
|
+
|
|
336
|
+
const faces = {
|
|
337
|
+
http: true,
|
|
338
|
+
mcp: mcpFace({ serverInfo: { name: "graph", version: "0.1.0" } }),
|
|
339
|
+
// A read-only MCP face of the same server
|
|
340
|
+
mcpRead: mcpFace({ path: "/mcp-read", serverInfo: { name: "graph", version: "0.1.0" }, narrowing: { effects: { writes: [] } } }),
|
|
341
|
+
};
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
| Option | Default | Function |
|
|
345
|
+
|---|---|---|
|
|
346
|
+
| `path` | `/mcp` | The path of the endpoint. |
|
|
347
|
+
| `token` | the token of the policy | The token that each request sends as `Authorization: Bearer <token>`. |
|
|
348
|
+
| `allowedOrigins` | the origins of the policy | The browser origins that can send requests to the endpoint. |
|
|
349
|
+
| `narrowing` | none | A narrower capability for each call of the face, as a `CapabilityRequest` or a list of them. |
|
|
350
|
+
|
|
351
|
+
The face adds `narrowing` to each call as one more source of the narrowing key (`procedure-narrowing`). Thus the server applies the meet of all the sources: the capability of the policy, the request hook and the key of the caller.
|
|
352
|
+
|
|
353
|
+
- The read-only mode is `{ effects: { writes: [] } }`. The face then lists and starts only the tools that change nothing, as `mark mcp --read-only` does.
|
|
354
|
+
- A narrowing can only narrow. A part that asks for more than the policy gives has no effect.
|
|
355
|
+
- The face lists only the tools that the narrowed capability permits. The time of a narrowing limits each call, not the tools.
|
|
356
|
+
- A narrowing that is not valid stops the factory with an error. Examples are an unknown field and a clearance outside the order of the policy.
|
|
357
|
+
|
|
261
358
|
## The procedures of this package
|
|
262
359
|
|
|
263
360
|
| Procedure | Function |
|