@procedures/mcp-server 0.13.2 → 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 +78 -0
- package/README.md +100 -3
- 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 +194 -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 +213 -82
- package/src/types.ts +44 -4
|
@@ -8,9 +8,14 @@
|
|
|
8
8
|
import { Server as McpServer } from "@modelcontextprotocol/sdk/server/index.js";
|
|
9
9
|
import {
|
|
10
10
|
CallToolRequestSchema,
|
|
11
|
+
ErrorCode,
|
|
11
12
|
ListToolsRequestSchema,
|
|
13
|
+
McpError,
|
|
14
|
+
type CallToolResult,
|
|
12
15
|
type ServerNotification,
|
|
16
|
+
type Task,
|
|
13
17
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
18
|
+
import type { RequestTaskStore } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
14
19
|
import type { Transport as SdkTransport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
15
20
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
16
21
|
import type {
|
|
@@ -37,6 +42,7 @@ import { createSseTransport } from "./sse.js";
|
|
|
37
42
|
import { createHttpTransport, type McpHttpTransport } from "./http.js";
|
|
38
43
|
import { optionalMs } from "@procedures/client";
|
|
39
44
|
import { createUntrustedEnvelopes } from "../untrusted.js";
|
|
45
|
+
import { taskStoreOf, type McpTaskStore } from "../tasks.js";
|
|
40
46
|
|
|
41
47
|
/**
|
|
42
48
|
* The tools with the names of a `toolName` function. Without the function, the tools keep the
|
|
@@ -77,6 +83,18 @@ interface CallExtra {
|
|
|
77
83
|
sendNotification: (notification: ServerNotification) => Promise<void>;
|
|
78
84
|
/** The HTTP request of the message: present only for a network transport (Streamable HTTP) */
|
|
79
85
|
requestInfo?: { headers: Record<string, string | string[] | undefined>; url?: URL | undefined } | undefined;
|
|
86
|
+
/** The task store of the request, when the server has tasks (CLI-35) */
|
|
87
|
+
taskStore?: RequestTaskStore | undefined;
|
|
88
|
+
/** The time to keep the task that the client asked for */
|
|
89
|
+
taskRequestedTtl?: number | null | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The capability of a server with tasks (MCP 2025-11-25): list, cancel, and tools/call as a task. */
|
|
93
|
+
const TASKS_CAPABILITY = { list: {}, cancel: {}, requests: { tools: { call: {} } } };
|
|
94
|
+
|
|
95
|
+
/** The status message of a task for a progress: only counts, so no text of the work leaves its envelopes. */
|
|
96
|
+
function progressMessage(progress: number, total: number | undefined): string {
|
|
97
|
+
return `${String(progress)}${total === undefined ? "" : ` of ${String(total)}`} done`;
|
|
80
98
|
}
|
|
81
99
|
|
|
82
100
|
/**
|
|
@@ -108,6 +126,15 @@ function callNarrowing(extra: CallExtra): unknown {
|
|
|
108
126
|
/** The most text blocks of a stream result: the last items of a long stream. */
|
|
109
127
|
const MAX_STREAM_BLOCKS = 100;
|
|
110
128
|
|
|
129
|
+
/**
|
|
130
|
+
* The `_meta` of the error of a stream: its code and its retryable flag, as the error line of
|
|
131
|
+
* NDJSON and the error frame of WebSocket give them (CLI-48). A code that is not a string gives `HANDLER_ERROR`.
|
|
132
|
+
*/
|
|
133
|
+
function streamErrorMeta(error: unknown): { code: string; retryable: boolean } {
|
|
134
|
+
const { code, retryable } = (error ?? {}) as { code?: unknown; retryable?: unknown };
|
|
135
|
+
return { code: typeof code === "string" ? code : "HANDLER_ERROR", retryable: typeof retryable === "boolean" ? retryable : false };
|
|
136
|
+
}
|
|
137
|
+
|
|
111
138
|
/** The text of a value: a string as it is, another value as JSON (pretty, or compact on one line). */
|
|
112
139
|
function toText(value: unknown, json: "pretty" | "compact" = "pretty"): string {
|
|
113
140
|
if (typeof value === "string") return value;
|
|
@@ -176,6 +203,9 @@ export class McpServerTransport implements ServerTransport {
|
|
|
176
203
|
private readonly streamLimits: { items: number; timeMs: number };
|
|
177
204
|
/** This function removes the listener of the end of the stdio input */
|
|
178
205
|
private stdinEnd: (() => void) | undefined;
|
|
206
|
+
/** The store of the tasks (CLI-35), and true when this transport made it (then its stop closes it) */
|
|
207
|
+
private readonly taskStore: McpTaskStore | undefined;
|
|
208
|
+
private readonly ownsTaskStore: boolean;
|
|
179
209
|
|
|
180
210
|
constructor(server: RequestHandler, options: McpServerTransportOptions = {}) {
|
|
181
211
|
this.server = server;
|
|
@@ -191,6 +221,9 @@ export class McpServerTransport implements ServerTransport {
|
|
|
191
221
|
items: Math.max(1, options.streamLimits?.items ?? 100),
|
|
192
222
|
timeMs: optionalMs(options.streamLimits?.time, "streamLimits.time") ?? 30_000,
|
|
193
223
|
};
|
|
224
|
+
const tasks = taskStoreOf(options.tasks);
|
|
225
|
+
this.taskStore = tasks?.store;
|
|
226
|
+
this.ownsTaskStore = tasks?.owned ?? false;
|
|
194
227
|
// The registry of the server, when it has one (a ProcedureServer with an explicit registry):
|
|
195
228
|
// before, the tools came from the global registry, also when the server served another one
|
|
196
229
|
this.registry =
|
|
@@ -214,9 +247,13 @@ export class McpServerTransport implements ServerTransport {
|
|
|
214
247
|
const server = new McpServer(this.options.serverInfo, {
|
|
215
248
|
capabilities: {
|
|
216
249
|
tools: { listChanged: true },
|
|
250
|
+
// MCP tasks (CLI-35): a long tool call can run as a task
|
|
251
|
+
...(this.taskStore ? { tasks: TASKS_CAPABILITY } : {}),
|
|
217
252
|
},
|
|
218
253
|
// The instructions of the server: the client gives them to the model (CLI-10)
|
|
219
254
|
...(this.options.instructions !== undefined ? { instructions: this.options.instructions } : {}),
|
|
255
|
+
// The SDK answers tasks/get, tasks/result, tasks/list and tasks/cancel from the store
|
|
256
|
+
...(this.taskStore ? { taskStore: this.taskStore } : {}),
|
|
220
257
|
});
|
|
221
258
|
this.setupHandlers(server);
|
|
222
259
|
return server;
|
|
@@ -230,7 +267,12 @@ export class McpServerTransport implements ServerTransport {
|
|
|
230
267
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
231
268
|
this.log("ListToolsRequest received");
|
|
232
269
|
|
|
233
|
-
|
|
270
|
+
// Without a task store, no tool runs as a task: the list does not say that it can
|
|
271
|
+
const tools = Array.from(this.tools.values(), (tool) => {
|
|
272
|
+
const definition = toToolDefinition(tool);
|
|
273
|
+
if (!this.taskStore) delete definition.execution;
|
|
274
|
+
return definition;
|
|
275
|
+
});
|
|
234
276
|
|
|
235
277
|
this.log(`Returning ${tools.length} tools`);
|
|
236
278
|
|
|
@@ -239,7 +281,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
239
281
|
|
|
240
282
|
// CallToolRequestSchema -> Execute procedure
|
|
241
283
|
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
242
|
-
const { name, arguments: args } = request.params;
|
|
284
|
+
const { name, arguments: args, task } = request.params;
|
|
243
285
|
|
|
244
286
|
this.log(`CallToolRequest: ${name}`);
|
|
245
287
|
|
|
@@ -253,86 +295,155 @@ export class McpServerTransport implements ServerTransport {
|
|
|
253
295
|
};
|
|
254
296
|
}
|
|
255
297
|
|
|
256
|
-
//
|
|
257
|
-
//
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
298
|
+
// MCP tasks (CLI-35): a call as a task needs a tool that can run as one, and a call of a
|
|
299
|
+
// tool that runs only as a task needs a task. Without a task store each call is a request.
|
|
300
|
+
const support = this.taskStore ? tool.execution?.taskSupport : undefined;
|
|
301
|
+
const callExtra = extra as CallExtra;
|
|
302
|
+
if (task !== undefined) {
|
|
303
|
+
if (!this.taskStore || (support !== "optional" && support !== "required")) {
|
|
304
|
+
throw new McpError(ErrorCode.MethodNotFound, `The tool ${name} does not run as a task`);
|
|
305
|
+
}
|
|
306
|
+
return this.startTask(server, this.taskStore, tool, args, callExtra);
|
|
307
|
+
}
|
|
308
|
+
if (support === "required") {
|
|
309
|
+
throw new McpError(ErrorCode.MethodNotFound, `The tool ${name} runs only as a task: call it with a task`);
|
|
310
|
+
}
|
|
311
|
+
return this.callTool(server, tool, args, callExtra);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* This method starts a tool call as a task (CLI-35). The client gets the task at once, and the
|
|
317
|
+
* call goes on. A progress of the procedure becomes the status message of the task, and a
|
|
318
|
+
* progress notification when the client sent a progress token. A cancel of the task aborts the
|
|
319
|
+
* call. A task above the limit of the store fails at once with the reason. The call of a task
|
|
320
|
+
* cannot ask the person: its request already has its answer.
|
|
321
|
+
*/
|
|
322
|
+
private async startTask(server: McpServer, store: McpTaskStore, tool: McpTool, args: Record<string, unknown> | undefined, extra: CallExtra): Promise<{ task: Task }> {
|
|
323
|
+
const taskStore = extra.taskStore;
|
|
324
|
+
if (!taskStore) throw new McpError(ErrorCode.InternalError, "The server has no task store for this request");
|
|
325
|
+
const ttl = extra.taskRequestedTtl;
|
|
326
|
+
const task = await taskStore.createTask(typeof ttl === "number" ? { ttl } : {});
|
|
327
|
+
const id = task.taskId;
|
|
328
|
+
const finish = async (result: CallToolResult): Promise<void> => {
|
|
329
|
+
// A cancelled task keeps its status: the store refuses a result after a cancel
|
|
330
|
+
await taskStore.storeTaskResult(id, result.isError === true ? "failed" : "completed", result).catch(() => undefined);
|
|
331
|
+
};
|
|
332
|
+
const refused = store.refusal(id);
|
|
333
|
+
if (refused !== undefined) {
|
|
334
|
+
await finish({ content: [{ type: "text", text: refused }], isError: true });
|
|
335
|
+
return { task: await taskStore.getTask(id) };
|
|
336
|
+
}
|
|
337
|
+
const token = extra._meta?.progressToken;
|
|
338
|
+
const background: CallExtra = {
|
|
339
|
+
...extra,
|
|
340
|
+
// The procedure reports its progress to the status of the task, also without a token of the client
|
|
341
|
+
_meta: { ...extra._meta, progressToken: token ?? `task-${id}` },
|
|
342
|
+
signal: store.signal(id),
|
|
343
|
+
sendNotification: async (notification) => {
|
|
344
|
+
if (notification.method === "notifications/progress") {
|
|
345
|
+
const { progress, total } = notification.params;
|
|
346
|
+
await taskStore.updateTaskStatus(id, "working", progressMessage(progress, total)).catch(() => undefined);
|
|
347
|
+
if (token === undefined) return;
|
|
348
|
+
}
|
|
349
|
+
// The response of the request went: later messages go on the stream of the session
|
|
350
|
+
await server.notification(notification).catch(() => undefined);
|
|
351
|
+
},
|
|
352
|
+
};
|
|
353
|
+
// The slot of the task stays taken until the call settles, also after a cancel
|
|
354
|
+
void this.callTool(server, tool, args, background, false)
|
|
355
|
+
.then(finish)
|
|
356
|
+
.catch(() => undefined)
|
|
357
|
+
.finally(() => store.settle(id));
|
|
358
|
+
return { task };
|
|
359
|
+
}
|
|
289
360
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
361
|
+
/**
|
|
362
|
+
* This method calls the procedure of a tool and gives the MCP result. `offerElicit` is false for
|
|
363
|
+
* the call of a task: then the procedure gets no function to ask the person.
|
|
364
|
+
*/
|
|
365
|
+
private async callTool(server: McpServer, tool: McpTool, args: Record<string, unknown> | undefined, extra: CallExtra, offerElicit = true): Promise<CallToolResult> {
|
|
366
|
+
const name = tool.name;
|
|
367
|
+
// The path of the procedure of the tool. The method is the core mapping of every host
|
|
368
|
+
// (deep dive architecture review 3.3). A tool name of the toolName option does not decode,
|
|
369
|
+
// and a sanitized segment does not decode to the segment of the procedure.
|
|
370
|
+
const path = tool.procedurePath;
|
|
371
|
+
|
|
372
|
+
// Build ServerRequest
|
|
373
|
+
const credentials = callCredentials(extra);
|
|
374
|
+
const narrowing = callNarrowing(extra);
|
|
375
|
+
const serverRequest = {
|
|
376
|
+
id: generateRequestId(),
|
|
377
|
+
method: pathToMethod(path),
|
|
378
|
+
payload: args ?? {},
|
|
379
|
+
metadata: {
|
|
380
|
+
transport: "mcp" as const,
|
|
381
|
+
mcpRequestId: generateRequestId(),
|
|
382
|
+
// The narrowing key of the call: the server applies it (CLI-30)
|
|
383
|
+
...(narrowing !== undefined ? { [NARROWING_METADATA_KEY]: narrowing } : {}),
|
|
384
|
+
},
|
|
385
|
+
// A cancelled tool call (notifications/cancelled) aborts the procedure
|
|
386
|
+
signal: extra.signal,
|
|
387
|
+
// The credentials of a call over the network: the request hook (principalOf) and the
|
|
388
|
+
// token check of the server apply to it (CLI-10)
|
|
389
|
+
...(credentials ? { credentials } : {}),
|
|
390
|
+
};
|
|
391
|
+
// What this face offers the procedure (CLI-12): the questions to the person, and the
|
|
392
|
+
// progress for a caller that sent a progress token
|
|
393
|
+
const elicit = offerElicit ? elicitWithPolicy(this.faceElicit(server, extra), this.options.elicitation) : undefined;
|
|
394
|
+
const progress = this.callProgress(extra);
|
|
395
|
+
const callRequest = {
|
|
396
|
+
...serverRequest,
|
|
397
|
+
...(elicit ? { elicit } : {}),
|
|
398
|
+
...(progress ? { progress: progress.report } : {}),
|
|
399
|
+
};
|
|
293
400
|
|
|
294
|
-
|
|
401
|
+
try {
|
|
402
|
+
// Process through universal server
|
|
403
|
+
const response = await this.server.handle(callRequest);
|
|
295
404
|
|
|
296
|
-
|
|
297
|
-
if (response.status.type === "success" && response.stream) {
|
|
298
|
-
const unbounded = response.metadata?.["stream"] === "unbounded";
|
|
299
|
-
// The items are the progress of a stream, unless the procedure reports its own
|
|
300
|
-
return await this.streamResult(response.stream, extra, unbounded, tool, () => progress?.used() === true);
|
|
301
|
-
}
|
|
302
|
-
if (response.status.type === "success") {
|
|
303
|
-
const rendered = this.render(response.payload, tool);
|
|
304
|
-
const hooked = this.options.content?.({ ...rendered, tool, value: response.payload });
|
|
305
|
-
const result = hooked ?? rendered;
|
|
306
|
-
return {
|
|
307
|
-
content: result.content,
|
|
308
|
-
// MCP structured output: the result as an object, for a tool with an output schema.
|
|
309
|
-
// The text block stays, for clients that do not read structured content.
|
|
310
|
-
...(result.structuredContent ? { structuredContent: result.structuredContent } : {}),
|
|
311
|
-
};
|
|
312
|
-
} else {
|
|
313
|
-
// The code and the reason of the error travel on each face (roadmap R25)
|
|
314
|
-
const reason = typeof response.metadata?.["reason"] === "string" ? response.metadata["reason"] : undefined;
|
|
315
|
-
return {
|
|
316
|
-
content: [
|
|
317
|
-
{
|
|
318
|
-
type: "text" as const,
|
|
319
|
-
text: `${response.status.message ?? "Unknown error"}${reason ? ` (reason: ${reason})` : ""}`,
|
|
320
|
-
},
|
|
321
|
-
],
|
|
322
|
-
isError: true,
|
|
323
|
-
_meta: { code: response.status.code, ...(reason ? { reason } : {}) },
|
|
324
|
-
};
|
|
325
|
-
}
|
|
326
|
-
} catch (error) {
|
|
327
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
328
|
-
this.log(`Tool ${name} error: ${message}`);
|
|
405
|
+
this.log(`Tool ${name} completed: ${response.status.type}`);
|
|
329
406
|
|
|
407
|
+
// Convert to MCP result
|
|
408
|
+
if (response.status.type === "success" && response.stream) {
|
|
409
|
+
const unbounded = response.metadata?.["stream"] === "unbounded";
|
|
410
|
+
// The items are the progress of a stream, unless the procedure reports its own
|
|
411
|
+
return await this.streamResult(response.stream, extra, unbounded, tool, () => progress?.used() === true);
|
|
412
|
+
}
|
|
413
|
+
if (response.status.type === "success") {
|
|
414
|
+
const rendered = this.render(response.payload, tool);
|
|
415
|
+
const hooked = this.options.content?.({ ...rendered, tool, value: response.payload });
|
|
416
|
+
const result = hooked ?? rendered;
|
|
330
417
|
return {
|
|
331
|
-
content:
|
|
418
|
+
content: result.content,
|
|
419
|
+
// MCP structured output: the result as an object, for a tool with an output schema.
|
|
420
|
+
// The text block stays, for clients that do not read structured content.
|
|
421
|
+
...(result.structuredContent ? { structuredContent: result.structuredContent } : {}),
|
|
422
|
+
};
|
|
423
|
+
} else {
|
|
424
|
+
// The code, the retryable flag and the reason of the error travel on each face (roadmap
|
|
425
|
+
// R25, CLI-48)
|
|
426
|
+
const reason = typeof response.metadata?.["reason"] === "string" ? response.metadata["reason"] : undefined;
|
|
427
|
+
return {
|
|
428
|
+
content: [
|
|
429
|
+
{
|
|
430
|
+
type: "text" as const,
|
|
431
|
+
text: `${response.status.message ?? "Unknown error"}${reason ? ` (reason: ${reason})` : ""}`,
|
|
432
|
+
},
|
|
433
|
+
],
|
|
332
434
|
isError: true,
|
|
435
|
+
_meta: { code: response.status.code, retryable: response.status.retryable === true, ...(reason ? { reason } : {}) },
|
|
333
436
|
};
|
|
334
437
|
}
|
|
335
|
-
})
|
|
438
|
+
} catch (error) {
|
|
439
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
440
|
+
this.log(`Tool ${name} error: ${message}`);
|
|
441
|
+
|
|
442
|
+
return {
|
|
443
|
+
content: [{ type: "text" as const, text: message }],
|
|
444
|
+
isError: true,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
336
447
|
}
|
|
337
448
|
|
|
338
449
|
/**
|
|
@@ -353,7 +464,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
353
464
|
unbounded = false,
|
|
354
465
|
tool?: McpTool,
|
|
355
466
|
ownProgress: () => boolean = () => false
|
|
356
|
-
): Promise<{ content: Array<{ type: "text"; text: string }>; isError?: boolean }> {
|
|
467
|
+
): Promise<{ content: Array<{ type: "text"; text: string }>; isError?: boolean; _meta?: { code: string; retryable: boolean } }> {
|
|
357
468
|
const progressToken = extra._meta?.progressToken;
|
|
358
469
|
const blocks: Array<{ type: "text"; text: string }> = [];
|
|
359
470
|
let count = 0;
|
|
@@ -387,7 +498,9 @@ export class McpServerTransport implements ServerTransport {
|
|
|
387
498
|
}
|
|
388
499
|
} catch (error) {
|
|
389
500
|
blocks.push({ type: "text", text: error instanceof Error ? error.message : String(error) });
|
|
390
|
-
|
|
501
|
+
// The code and the retryable flag of the end travel on each face: a stream that the
|
|
502
|
+
// shutdown cuts ends with UNAVAILABLE (CLI-48)
|
|
503
|
+
return { content: blocks, isError: true, _meta: streamErrorMeta(error) };
|
|
391
504
|
}
|
|
392
505
|
if (count > MAX_STREAM_BLOCKS) {
|
|
393
506
|
blocks.unshift({ type: "text", text: `(${count} items: the first ${count - MAX_STREAM_BLOCKS} are not shown)` });
|
|
@@ -612,12 +725,22 @@ export class McpServerTransport implements ServerTransport {
|
|
|
612
725
|
// The face trusts cookies only as the server reads them (CSRF)
|
|
613
726
|
const ambientCredentials = (this.server as Partial<RequestHandler>).ambientCredentials;
|
|
614
727
|
const httpOptions = { ...this.options.httpOptions, ...(ambientCredentials ? { ambientCredentials } : {}) };
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
728
|
+
const taskStore = this.taskStore;
|
|
729
|
+
this.http = createHttpTransport(
|
|
730
|
+
httpOptions,
|
|
731
|
+
async (sessionTransport) => {
|
|
732
|
+
const sessionServer = this.createSdkServer();
|
|
733
|
+
this.sessionServers.add(sessionServer);
|
|
734
|
+
sessionServer.onclose = () => {
|
|
735
|
+
this.sessionServers.delete(sessionServer);
|
|
736
|
+
// The close of a session cancels its tasks: their work stops (CLI-35)
|
|
737
|
+
if (taskStore && sessionTransport.sessionId) void taskStore.cancelSession(sessionTransport.sessionId);
|
|
738
|
+
};
|
|
739
|
+
await sessionServer.connect(sessionTransport as unknown as SdkTransport);
|
|
740
|
+
},
|
|
741
|
+
// The session limit keeps each session with a task in progress open
|
|
742
|
+
taskStore ? { busySessions: () => taskStore.busySessions() } : {},
|
|
743
|
+
);
|
|
621
744
|
transport = undefined;
|
|
622
745
|
} else if (this.options.transport === "sse") {
|
|
623
746
|
transport = createSseTransport(this.options.sseOptions);
|
|
@@ -666,6 +789,8 @@ export class McpServerTransport implements ServerTransport {
|
|
|
666
789
|
await this.http?.close();
|
|
667
790
|
for (const sessionServer of this.sessionServers) await sessionServer.close().catch(() => undefined);
|
|
668
791
|
this.sessionServers.clear();
|
|
792
|
+
// A store of this transport stops with it: its tasks are cancelled (CLI-35)
|
|
793
|
+
if (this.ownsTaskStore) await this.taskStore?.close();
|
|
669
794
|
this.http = undefined;
|
|
670
795
|
this.httpUrl = undefined;
|
|
671
796
|
this.running = false;
|
|
@@ -696,9 +821,15 @@ export class McpServerTransport implements ServerTransport {
|
|
|
696
821
|
toolCount: this.tools.size,
|
|
697
822
|
transport: this.options.transport,
|
|
698
823
|
...(this.httpUrl ? { url: this.httpUrl } : {}),
|
|
824
|
+
...(this.http ? { sessions: this.http.sessions } : {}),
|
|
699
825
|
};
|
|
700
826
|
}
|
|
701
827
|
|
|
828
|
+
/** The store of the tasks (CLI-35), or undefined when the transport has no tasks. */
|
|
829
|
+
getTaskStore(): McpTaskStore | undefined {
|
|
830
|
+
return this.taskStore;
|
|
831
|
+
}
|
|
832
|
+
|
|
702
833
|
/**
|
|
703
834
|
* Get the list of available tools.
|
|
704
835
|
*/
|
package/src/types.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { ElicitationPolicy, ProcedureRegistry } from "@procedures/client";
|
|
|
10
10
|
import type { McpServerInfo, McpTool, McpToolFilter } from "@procedures/mcp";
|
|
11
11
|
import type { Duration } from "@procedures/client";
|
|
12
12
|
import type { UntrustedContentOptions } from "./untrusted.js";
|
|
13
|
+
import type { McpTasksOptions } from "./tasks.js";
|
|
13
14
|
|
|
14
15
|
/** The streams of the stdio transport, and what happens when its input ends. */
|
|
15
16
|
export interface StdioTransportOptions {
|
|
@@ -51,11 +52,21 @@ export type McpToolContentHook = (result: McpToolContentInput) => McpToolContent
|
|
|
51
52
|
/**
|
|
52
53
|
* SSE transport configuration options.
|
|
53
54
|
*/
|
|
54
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* The options of the Streamable HTTP transport (roadmap R26). Give `mount: true` and mount the
|
|
57
|
+
* handler of the endpoint on a listener. An endpoint with a listener of its own (`port`, `host`,
|
|
58
|
+
* `server`) is deprecated (CLI-11).
|
|
59
|
+
*/
|
|
55
60
|
export interface HttpTransportOptions {
|
|
56
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* Port to listen on (default: 3003)
|
|
63
|
+
* @deprecated The next MINOR removes the listener of the endpoint (CLI-11). Give `mount: true`, and mount `McpHost.handleHttp` on a listener of the service.
|
|
64
|
+
*/
|
|
57
65
|
port?: number | undefined;
|
|
58
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* Host to bind to (default: "127.0.0.1": only this computer can reach the tools)
|
|
68
|
+
* @deprecated The next MINOR removes the listener of the endpoint (CLI-11). The listener of the service sets its address.
|
|
69
|
+
*/
|
|
59
70
|
host?: string | undefined;
|
|
60
71
|
/** The path of the MCP endpoint (default: "/mcp") */
|
|
61
72
|
path?: string | undefined;
|
|
@@ -63,7 +74,15 @@ export interface HttpTransportOptions {
|
|
|
63
74
|
token?: string | undefined;
|
|
64
75
|
/** Browser origins that can call the server, besides loopback origins */
|
|
65
76
|
allowedOrigins?: string[] | undefined;
|
|
66
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* The host names that a request on a loopback address can name in its `Host` header, besides
|
|
79
|
+
* the loopback names (`browser.allowedHosts` of the host policy). `"*"` turns the check off.
|
|
80
|
+
*/
|
|
81
|
+
allowedHosts?: readonly string[] | "*" | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* An HTTP server to answer on, in place of a server of the transport (it does not listen then)
|
|
84
|
+
* @deprecated The next MINOR removes it (CLI-11). Give `mount: true`, and mount the handler on the server.
|
|
85
|
+
*/
|
|
67
86
|
server?: import("node:http").Server | undefined;
|
|
68
87
|
/**
|
|
69
88
|
* True: the endpoint has no server and attaches to none. The owner of a listener mounts its
|
|
@@ -75,6 +94,18 @@ export interface HttpTransportOptions {
|
|
|
75
94
|
* transport sets it from its server. With a value, a request with a cookie needs a named origin.
|
|
76
95
|
*/
|
|
77
96
|
ambientCredentials?: import("@procedures/client").AmbientCredentials | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* The most sessions at one time (CLI-35). A new session above the limit closes the session that
|
|
99
|
+
* was idle longest: no task in progress and no open response. When each session is busy, the
|
|
100
|
+
* new session gets 503 with `Retry-After`.
|
|
101
|
+
* @default Infinity (no limit)
|
|
102
|
+
*/
|
|
103
|
+
maxSessions?: number | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* The largest body of a request, in bytes (CLI-35).
|
|
106
|
+
* @default 4194304 (4 MiB, the limit of the SDK)
|
|
107
|
+
*/
|
|
108
|
+
maxBodyBytes?: number | undefined;
|
|
78
109
|
}
|
|
79
110
|
|
|
80
111
|
export interface SseTransportOptions {
|
|
@@ -164,6 +195,13 @@ export interface McpServerTransportOptions {
|
|
|
164
195
|
* @default { items: 100, time: seconds(30) }
|
|
165
196
|
*/
|
|
166
197
|
streamLimits?: { items?: number; time?: Duration } | undefined;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* MCP tasks (CLI-35): a store, or the options of a new store (`maxRunning`, `ttl`). With it, a
|
|
201
|
+
* tool whose procedure has `metadata.taskSupport` can run as a task. Without it, each tool call
|
|
202
|
+
* is a normal request. The stop of the transport closes a store that it made.
|
|
203
|
+
*/
|
|
204
|
+
tasks?: McpTasksOptions | undefined;
|
|
167
205
|
}
|
|
168
206
|
|
|
169
207
|
/**
|
|
@@ -178,4 +216,6 @@ export interface McpServerTransportState {
|
|
|
178
216
|
transport: "stdio" | "sse" | "http";
|
|
179
217
|
/** The URL of the Streamable HTTP endpoint, when the transport is "http" */
|
|
180
218
|
url?: string;
|
|
219
|
+
/** The open sessions of the Streamable HTTP endpoint, when the transport is "http" (CLI-35) */
|
|
220
|
+
sessions?: number;
|
|
181
221
|
}
|