@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
package/dist/tasks.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tasks (CLI-35): the long-running task primitive of the MCP specification 2025-11-25.
|
|
3
|
+
*
|
|
4
|
+
* A client starts a long tool call as a task: it gets a task id at once, polls the status, gets
|
|
5
|
+
* the result later, and can cancel the task. A procedure declares that its tool can run as a task
|
|
6
|
+
* with `metadata.taskSupport` (`"optional"` or `"required"`). The MCP host gives the store of the
|
|
7
|
+
* tasks (`tasks` of `createMcpHost`). Without that option the host has no tasks, and each tool
|
|
8
|
+
* call is a normal request.
|
|
9
|
+
*
|
|
10
|
+
* - The store keeps the tasks in memory and binds each task to its session. One store serves each
|
|
11
|
+
* session and each face of a host.
|
|
12
|
+
* - A slot of the limit (`maxRunning`) stays taken until the work of the task ends, not until its
|
|
13
|
+
* status ends: a cancelled task holds its slot until its procedure returns. A task above the
|
|
14
|
+
* limit is created, and then fails at once with the reason. The SDK 1.32.1 cannot give an error
|
|
15
|
+
* for a task request, so the client reads the reason in the result of the task.
|
|
16
|
+
* - The time to keep a task (`ttl`) starts when the task ends. The store never removes a task
|
|
17
|
+
* whose work runs, so a short `ttl` does not let work escape the limit or the cancel. A refused
|
|
18
|
+
* task stays 1 minute, and each session keeps at most 100 tasks: a new task removes the oldest
|
|
19
|
+
* task of its session that ended.
|
|
20
|
+
* - A cancel of a task (`tasks/cancel`) aborts the signal of its procedure call. The close of a
|
|
21
|
+
* session cancels its tasks, and the stop of the host cancels each task.
|
|
22
|
+
*
|
|
23
|
+
* The SDK marks its task API as experimental, so this part follows the SDK when it changes.
|
|
24
|
+
*/
|
|
25
|
+
import type { CreateTaskOptions, TaskStore } from "@modelcontextprotocol/sdk/experimental/tasks/interfaces.js";
|
|
26
|
+
import { type Request, type RequestId, type Result, type Task } from "@modelcontextprotocol/sdk/types.js";
|
|
27
|
+
import { type Duration } from "@procedures/client";
|
|
28
|
+
/** The limits of the tasks of one MCP host. */
|
|
29
|
+
export declare const MCP_TASK_LIMITS: {
|
|
30
|
+
/** A task and its result stay for this time after the task ends: 1 hour by default. */
|
|
31
|
+
readonly ttlMs: 3600000;
|
|
32
|
+
/** The longest time that a client can ask for: 1 day. */
|
|
33
|
+
readonly maxTtlMs: 86400000;
|
|
34
|
+
/** The shortest time that a client can ask for: 1 second. */
|
|
35
|
+
readonly minTtlMs: 1000;
|
|
36
|
+
/** The time that the server proposes between two polls of the status: 1 second. */
|
|
37
|
+
readonly pollMs: 1000;
|
|
38
|
+
/** The most tasks whose work runs at one time. A new task above the limit fails at once. */
|
|
39
|
+
readonly maxRunning: 8;
|
|
40
|
+
/** The tasks on one page of `tasks/list`. */
|
|
41
|
+
readonly pageSize: 10;
|
|
42
|
+
/** A refused task and its reason stay for this time: 1 minute. */
|
|
43
|
+
readonly refusedTtlMs: 60000;
|
|
44
|
+
/** The most tasks that the store keeps for one session. A new task removes the oldest task of its session that ended. */
|
|
45
|
+
readonly maxStoredPerSession: 100;
|
|
46
|
+
};
|
|
47
|
+
/** A task whose work did not end, as a status page of the host shows it. */
|
|
48
|
+
export interface McpTaskInfo {
|
|
49
|
+
/** The task id that the client got. */
|
|
50
|
+
readonly id: string;
|
|
51
|
+
/** The name of the tool, for example `crawl.site`. */
|
|
52
|
+
readonly tool: string;
|
|
53
|
+
/** `working`, `input_required`, or `cancelled` while the procedure stops. */
|
|
54
|
+
readonly status: string;
|
|
55
|
+
/** The text of the progress, for example `3 of 10 done`, or null. */
|
|
56
|
+
readonly message: string | null;
|
|
57
|
+
readonly createdAt: string;
|
|
58
|
+
readonly updatedAt: string;
|
|
59
|
+
}
|
|
60
|
+
/** The options of a task store. */
|
|
61
|
+
export interface McpTaskStoreOptions {
|
|
62
|
+
/** The most tasks whose work runs at one time. The default is 8. */
|
|
63
|
+
readonly maxRunning?: number | undefined;
|
|
64
|
+
/** The time that a task and its result stay after the task ends, when the client asks for no time. The default is 1 hour. */
|
|
65
|
+
readonly ttl?: Duration | undefined;
|
|
66
|
+
/** This function runs when a task starts, changes its status or ends. An error of it does not stop a task. */
|
|
67
|
+
readonly onChange?: (() => void) | undefined;
|
|
68
|
+
}
|
|
69
|
+
/** The tasks of an MCP host (`tasks` of `createMcpHost`): a store, or the options of a new store. */
|
|
70
|
+
export interface McpTasksOptions extends McpTaskStoreOptions {
|
|
71
|
+
/** The store of the tasks. Without it, the host makes a store with the other options. */
|
|
72
|
+
readonly store?: McpTaskStore | undefined;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The store of the tasks of an MCP host. It keeps an abort signal for each task, so a cancel of
|
|
76
|
+
* the task aborts the work of its procedure.
|
|
77
|
+
*/
|
|
78
|
+
export declare class McpTaskStore implements TaskStore {
|
|
79
|
+
private readonly options;
|
|
80
|
+
private readonly entries;
|
|
81
|
+
private readonly maxRunning;
|
|
82
|
+
private readonly ttlMs;
|
|
83
|
+
private closed;
|
|
84
|
+
constructor(options?: McpTaskStoreOptions);
|
|
85
|
+
private changed;
|
|
86
|
+
/** The entry of a task, when the session may see it: a task of another session is not found. */
|
|
87
|
+
private lookup;
|
|
88
|
+
private found;
|
|
89
|
+
/** The time to keep a task starts when it ends, and the store removes it only when its work also ended. */
|
|
90
|
+
private expire;
|
|
91
|
+
/**
|
|
92
|
+
* This method removes the oldest tasks of a session that ended, until the session has room for
|
|
93
|
+
* one more task. A task whose work runs stays: the limit of the running tasks bounds them.
|
|
94
|
+
*/
|
|
95
|
+
private makeRoom;
|
|
96
|
+
/** The count of the tasks whose work runs. */
|
|
97
|
+
private runningCount;
|
|
98
|
+
/** The store counts and inserts a task in one step, so a batch of requests cannot pass the limit. */
|
|
99
|
+
createTask(params: CreateTaskOptions, _requestId: RequestId, request: Request, sessionId?: string): Promise<Task>;
|
|
100
|
+
getTask(taskId: string, sessionId?: string): Promise<Task | null>;
|
|
101
|
+
storeTaskResult(taskId: string, status: "completed" | "failed", result: Result, sessionId?: string): Promise<void>;
|
|
102
|
+
getTaskResult(taskId: string, sessionId?: string): Promise<Result>;
|
|
103
|
+
updateTaskStatus(taskId: string, status: Task["status"], statusMessage?: string, sessionId?: string): Promise<void>;
|
|
104
|
+
listTasks(cursor?: string, sessionId?: string): Promise<{
|
|
105
|
+
tasks: Task[];
|
|
106
|
+
nextCursor?: string;
|
|
107
|
+
}>;
|
|
108
|
+
/**
|
|
109
|
+
* This method tells the store that the work of a task ended: the procedure returned or threw.
|
|
110
|
+
* Its slot is free, and the time to keep the task starts when the task also has an end status.
|
|
111
|
+
*/
|
|
112
|
+
settle(taskId: string): void;
|
|
113
|
+
/** The abort signal of a task. A cancel of the task aborts it. An unknown task gives an aborted signal. */
|
|
114
|
+
signal(taskId: string): AbortSignal;
|
|
115
|
+
/** The reason that a task did not start, or undefined for a task that started. */
|
|
116
|
+
refusal(taskId: string): string | undefined;
|
|
117
|
+
/** The tasks whose work did not end, the oldest first. */
|
|
118
|
+
running(): McpTaskInfo[];
|
|
119
|
+
/** The sessions that have a task whose work runs. The session limit of the HTTP face keeps them open. */
|
|
120
|
+
busySessions(): Set<string>;
|
|
121
|
+
/** This method cancels the tasks of a session that closed. Their work stops. */
|
|
122
|
+
cancelSession(sessionId: string, message?: string): Promise<void>;
|
|
123
|
+
/** This method cancels each task that runs and removes all the tasks and their timers. A second close does nothing. */
|
|
124
|
+
close(): Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
/** The task store of an option: the given store, or a new store. `owned` is true for a new store. */
|
|
127
|
+
export declare function taskStoreOf(options: McpTasksOptions | undefined): {
|
|
128
|
+
store: McpTaskStore;
|
|
129
|
+
owned: boolean;
|
|
130
|
+
} | undefined;
|
|
131
|
+
//# sourceMappingURL=tasks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.d.ts","sourceRoot":"","sources":["../src/tasks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4DAA4D,CAAC;AAC/G,OAAO,EAAuB,KAAK,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,KAAK,IAAI,EAAE,MAAM,oCAAoC,CAAC;AAC/H,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,oBAAoB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AE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,IAAI,EAAE,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAWpG;IAED;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAM3B;IAED,2GAA2G;IAC3G,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAElC;IAED,kFAAkF;IAClF,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE1C;IAED,0DAA0D;IAC1D,OAAO,IAAI,WAAW,EAAE,CAQvB;IAED,yGAAyG;IACzG,YAAY,IAAI,GAAG,CAAC,MAAM,CAAC,CAI1B;IAED,gFAAgF;IAC1E,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,SAAmC,GAAG,OAAO,CAAC,IAAI,CAAC,CAKhG;IAED,uHAAuH;IACjH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAU3B;CACF;AAED,qGAAqG;AACrG,wBAAgB,WAAW,CAAC,OAAO,EAAE,eAAe,GAAG,SAAS,GAAG;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAIrH"}
|
package/dist/tasks.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tasks (CLI-35): the long-running task primitive of the MCP specification 2025-11-25.
|
|
3
|
+
*
|
|
4
|
+
* A client starts a long tool call as a task: it gets a task id at once, polls the status, gets
|
|
5
|
+
* the result later, and can cancel the task. A procedure declares that its tool can run as a task
|
|
6
|
+
* with `metadata.taskSupport` (`"optional"` or `"required"`). The MCP host gives the store of the
|
|
7
|
+
* tasks (`tasks` of `createMcpHost`). Without that option the host has no tasks, and each tool
|
|
8
|
+
* call is a normal request.
|
|
9
|
+
*
|
|
10
|
+
* - The store keeps the tasks in memory and binds each task to its session. One store serves each
|
|
11
|
+
* session and each face of a host.
|
|
12
|
+
* - A slot of the limit (`maxRunning`) stays taken until the work of the task ends, not until its
|
|
13
|
+
* status ends: a cancelled task holds its slot until its procedure returns. A task above the
|
|
14
|
+
* limit is created, and then fails at once with the reason. The SDK 1.32.1 cannot give an error
|
|
15
|
+
* for a task request, so the client reads the reason in the result of the task.
|
|
16
|
+
* - The time to keep a task (`ttl`) starts when the task ends. The store never removes a task
|
|
17
|
+
* whose work runs, so a short `ttl` does not let work escape the limit or the cancel. A refused
|
|
18
|
+
* task stays 1 minute, and each session keeps at most 100 tasks: a new task removes the oldest
|
|
19
|
+
* task of its session that ended.
|
|
20
|
+
* - A cancel of a task (`tasks/cancel`) aborts the signal of its procedure call. The close of a
|
|
21
|
+
* session cancels its tasks, and the stop of the host cancels each task.
|
|
22
|
+
*
|
|
23
|
+
* The SDK marks its task API as experimental, so this part follows the SDK when it changes.
|
|
24
|
+
*/
|
|
25
|
+
import { randomUUID } from "node:crypto";
|
|
26
|
+
import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
27
|
+
import { optionalMs } from "@procedures/client";
|
|
28
|
+
/** The limits of the tasks of one MCP host. */
|
|
29
|
+
export const MCP_TASK_LIMITS = {
|
|
30
|
+
/** A task and its result stay for this time after the task ends: 1 hour by default. */
|
|
31
|
+
ttlMs: 3_600_000,
|
|
32
|
+
/** The longest time that a client can ask for: 1 day. */
|
|
33
|
+
maxTtlMs: 86_400_000,
|
|
34
|
+
/** The shortest time that a client can ask for: 1 second. */
|
|
35
|
+
minTtlMs: 1_000,
|
|
36
|
+
/** The time that the server proposes between two polls of the status: 1 second. */
|
|
37
|
+
pollMs: 1_000,
|
|
38
|
+
/** The most tasks whose work runs at one time. A new task above the limit fails at once. */
|
|
39
|
+
maxRunning: 8,
|
|
40
|
+
/** The tasks on one page of `tasks/list`. */
|
|
41
|
+
pageSize: 10,
|
|
42
|
+
/** A refused task and its reason stay for this time: 1 minute. */
|
|
43
|
+
refusedTtlMs: 60_000,
|
|
44
|
+
/** The most tasks that the store keeps for one session. A new task removes the oldest task of its session that ended. */
|
|
45
|
+
maxStoredPerSession: 100,
|
|
46
|
+
};
|
|
47
|
+
const TERMINAL = new Set(["completed", "failed", "cancelled"]);
|
|
48
|
+
/**
|
|
49
|
+
* The store of the tasks of an MCP host. It keeps an abort signal for each task, so a cancel of
|
|
50
|
+
* the task aborts the work of its procedure.
|
|
51
|
+
*/
|
|
52
|
+
export class McpTaskStore {
|
|
53
|
+
options;
|
|
54
|
+
entries = new Map();
|
|
55
|
+
maxRunning;
|
|
56
|
+
ttlMs;
|
|
57
|
+
closed = false;
|
|
58
|
+
constructor(options = {}) {
|
|
59
|
+
this.options = options;
|
|
60
|
+
const maxRunning = options.maxRunning ?? MCP_TASK_LIMITS.maxRunning;
|
|
61
|
+
if (!Number.isInteger(maxRunning) || maxRunning < 1)
|
|
62
|
+
throw new RangeError(`maxRunning must be an integer of 1 or more, not ${String(maxRunning)}`);
|
|
63
|
+
this.maxRunning = maxRunning;
|
|
64
|
+
this.ttlMs = optionalMs(options.ttl, "ttl") ?? MCP_TASK_LIMITS.ttlMs;
|
|
65
|
+
}
|
|
66
|
+
changed() {
|
|
67
|
+
try {
|
|
68
|
+
this.options.onChange?.();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// A listener writes a status file, for example: its error does not stop a task
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The entry of a task, when the session may see it: a task of another session is not found. */
|
|
75
|
+
lookup(taskId, sessionId) {
|
|
76
|
+
const entry = this.entries.get(taskId);
|
|
77
|
+
if (entry && sessionId !== undefined && entry.sessionId !== undefined && entry.sessionId !== sessionId)
|
|
78
|
+
return undefined;
|
|
79
|
+
return entry;
|
|
80
|
+
}
|
|
81
|
+
found(taskId, sessionId) {
|
|
82
|
+
const entry = this.lookup(taskId, sessionId);
|
|
83
|
+
if (!entry)
|
|
84
|
+
throw new Error(`Task with ID ${taskId} not found`);
|
|
85
|
+
return entry;
|
|
86
|
+
}
|
|
87
|
+
/** The time to keep a task starts when it ends, and the store removes it only when its work also ended. */
|
|
88
|
+
expire(entry) {
|
|
89
|
+
if (entry.timer)
|
|
90
|
+
clearTimeout(entry.timer);
|
|
91
|
+
if (!TERMINAL.has(entry.task.status) || !entry.settled || this.closed)
|
|
92
|
+
return;
|
|
93
|
+
const ttl = entry.task.ttl ?? this.ttlMs;
|
|
94
|
+
entry.timer = setTimeout(() => {
|
|
95
|
+
this.entries.delete(entry.task.taskId);
|
|
96
|
+
}, ttl);
|
|
97
|
+
entry.timer.unref?.();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* This method removes the oldest tasks of a session that ended, until the session has room for
|
|
101
|
+
* one more task. A task whose work runs stays: the limit of the running tasks bounds them.
|
|
102
|
+
*/
|
|
103
|
+
makeRoom(sessionId) {
|
|
104
|
+
const own = [...this.entries.values()].filter((entry) => entry.sessionId === sessionId);
|
|
105
|
+
let excess = own.length + 1 - MCP_TASK_LIMITS.maxStoredPerSession;
|
|
106
|
+
for (const entry of own) {
|
|
107
|
+
if (excess <= 0)
|
|
108
|
+
break;
|
|
109
|
+
if (!entry.settled || !TERMINAL.has(entry.task.status))
|
|
110
|
+
continue;
|
|
111
|
+
if (entry.timer)
|
|
112
|
+
clearTimeout(entry.timer);
|
|
113
|
+
this.entries.delete(entry.task.taskId);
|
|
114
|
+
excess--;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** The count of the tasks whose work runs. */
|
|
118
|
+
runningCount() {
|
|
119
|
+
let count = 0;
|
|
120
|
+
for (const entry of this.entries.values())
|
|
121
|
+
if (!entry.settled)
|
|
122
|
+
count++;
|
|
123
|
+
return count;
|
|
124
|
+
}
|
|
125
|
+
/** The store counts and inserts a task in one step, so a batch of requests cannot pass the limit. */
|
|
126
|
+
async createTask(params, _requestId, request, sessionId) {
|
|
127
|
+
if (this.closed)
|
|
128
|
+
throw new McpError(ErrorCode.InvalidRequest, "The MCP host is stopping: it starts no task");
|
|
129
|
+
this.makeRoom(sessionId);
|
|
130
|
+
const running = this.runningCount();
|
|
131
|
+
const refused = running >= this.maxRunning ? `The host runs ${String(running)} tasks, its limit. Wait for a task to end, or cancel one.` : undefined;
|
|
132
|
+
const asked = params.ttl ?? this.ttlMs;
|
|
133
|
+
const createdAt = new Date().toISOString();
|
|
134
|
+
const task = {
|
|
135
|
+
taskId: randomUUID(),
|
|
136
|
+
status: "working",
|
|
137
|
+
ttl: refused !== undefined ? MCP_TASK_LIMITS.refusedTtlMs : Math.min(Math.max(MCP_TASK_LIMITS.minTtlMs, asked), MCP_TASK_LIMITS.maxTtlMs),
|
|
138
|
+
createdAt,
|
|
139
|
+
lastUpdatedAt: createdAt,
|
|
140
|
+
pollInterval: params.pollInterval ?? MCP_TASK_LIMITS.pollMs,
|
|
141
|
+
};
|
|
142
|
+
const name = request.params?.name;
|
|
143
|
+
this.entries.set(task.taskId, {
|
|
144
|
+
task,
|
|
145
|
+
sessionId,
|
|
146
|
+
tool: typeof name === "string" ? name : request.method,
|
|
147
|
+
controller: new AbortController(),
|
|
148
|
+
refused,
|
|
149
|
+
// A refused task never starts its work, so it takes no slot
|
|
150
|
+
settled: refused !== undefined,
|
|
151
|
+
});
|
|
152
|
+
this.changed();
|
|
153
|
+
return { ...task };
|
|
154
|
+
}
|
|
155
|
+
async getTask(taskId, sessionId) {
|
|
156
|
+
const entry = this.lookup(taskId, sessionId);
|
|
157
|
+
return entry ? { ...entry.task } : null;
|
|
158
|
+
}
|
|
159
|
+
async storeTaskResult(taskId, status, result, sessionId) {
|
|
160
|
+
const entry = this.found(taskId, sessionId);
|
|
161
|
+
if (TERMINAL.has(entry.task.status)) {
|
|
162
|
+
throw new Error(`Cannot store result for task ${taskId} in terminal status '${entry.task.status}'. Task results can only be stored once.`);
|
|
163
|
+
}
|
|
164
|
+
entry.result = result;
|
|
165
|
+
entry.task.status = status;
|
|
166
|
+
entry.task.lastUpdatedAt = new Date().toISOString();
|
|
167
|
+
this.expire(entry);
|
|
168
|
+
this.changed();
|
|
169
|
+
}
|
|
170
|
+
async getTaskResult(taskId, sessionId) {
|
|
171
|
+
const entry = this.found(taskId, sessionId);
|
|
172
|
+
if (!entry.result)
|
|
173
|
+
throw new Error(`Task ${taskId} has no result stored`);
|
|
174
|
+
return entry.result;
|
|
175
|
+
}
|
|
176
|
+
async updateTaskStatus(taskId, status, statusMessage, sessionId) {
|
|
177
|
+
const entry = this.found(taskId, sessionId);
|
|
178
|
+
if (TERMINAL.has(entry.task.status)) {
|
|
179
|
+
throw new Error(`Cannot update task ${taskId} from terminal status '${entry.task.status}' to '${status}'. Terminal states (completed, failed, cancelled) cannot transition to other states.`);
|
|
180
|
+
}
|
|
181
|
+
entry.task.status = status;
|
|
182
|
+
if (statusMessage)
|
|
183
|
+
entry.task.statusMessage = statusMessage;
|
|
184
|
+
entry.task.lastUpdatedAt = new Date().toISOString();
|
|
185
|
+
// A cancel aborts the work of the procedure: its requests in progress stop
|
|
186
|
+
if (status === "cancelled")
|
|
187
|
+
entry.controller.abort(new Error("The client cancelled the task"));
|
|
188
|
+
this.expire(entry);
|
|
189
|
+
this.changed();
|
|
190
|
+
}
|
|
191
|
+
async listTasks(cursor, sessionId) {
|
|
192
|
+
const ids = [...this.entries.keys()].filter((id) => this.lookup(id, sessionId) !== undefined);
|
|
193
|
+
let start = 0;
|
|
194
|
+
if (cursor) {
|
|
195
|
+
const index = ids.indexOf(cursor);
|
|
196
|
+
if (index < 0)
|
|
197
|
+
throw new Error(`Invalid cursor: ${cursor}`);
|
|
198
|
+
start = index + 1;
|
|
199
|
+
}
|
|
200
|
+
const page = ids.slice(start, start + MCP_TASK_LIMITS.pageSize);
|
|
201
|
+
const tasks = page.map((id) => ({ ...this.entries.get(id).task }));
|
|
202
|
+
return start + MCP_TASK_LIMITS.pageSize < ids.length ? { tasks, nextCursor: page[page.length - 1] } : { tasks };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* This method tells the store that the work of a task ended: the procedure returned or threw.
|
|
206
|
+
* Its slot is free, and the time to keep the task starts when the task also has an end status.
|
|
207
|
+
*/
|
|
208
|
+
settle(taskId) {
|
|
209
|
+
const entry = this.entries.get(taskId);
|
|
210
|
+
if (!entry || entry.settled)
|
|
211
|
+
return;
|
|
212
|
+
entry.settled = true;
|
|
213
|
+
this.expire(entry);
|
|
214
|
+
this.changed();
|
|
215
|
+
}
|
|
216
|
+
/** The abort signal of a task. A cancel of the task aborts it. An unknown task gives an aborted signal. */
|
|
217
|
+
signal(taskId) {
|
|
218
|
+
return this.entries.get(taskId)?.controller.signal ?? AbortSignal.abort(new Error("The task is not known"));
|
|
219
|
+
}
|
|
220
|
+
/** The reason that a task did not start, or undefined for a task that started. */
|
|
221
|
+
refusal(taskId) {
|
|
222
|
+
return this.entries.get(taskId)?.refused;
|
|
223
|
+
}
|
|
224
|
+
/** The tasks whose work did not end, the oldest first. */
|
|
225
|
+
running() {
|
|
226
|
+
const infos = [];
|
|
227
|
+
for (const [id, entry] of this.entries) {
|
|
228
|
+
if (entry.settled)
|
|
229
|
+
continue;
|
|
230
|
+
const { task } = entry;
|
|
231
|
+
infos.push({ id, tool: entry.tool, status: task.status, message: task.statusMessage ?? null, createdAt: task.createdAt, updatedAt: task.lastUpdatedAt });
|
|
232
|
+
}
|
|
233
|
+
return infos;
|
|
234
|
+
}
|
|
235
|
+
/** The sessions that have a task whose work runs. The session limit of the HTTP face keeps them open. */
|
|
236
|
+
busySessions() {
|
|
237
|
+
const busy = new Set();
|
|
238
|
+
for (const entry of this.entries.values())
|
|
239
|
+
if (!entry.settled && entry.sessionId !== undefined)
|
|
240
|
+
busy.add(entry.sessionId);
|
|
241
|
+
return busy;
|
|
242
|
+
}
|
|
243
|
+
/** This method cancels the tasks of a session that closed. Their work stops. */
|
|
244
|
+
async cancelSession(sessionId, message = "The session of the task closed") {
|
|
245
|
+
for (const [id, entry] of this.entries) {
|
|
246
|
+
if (entry.sessionId !== sessionId || TERMINAL.has(entry.task.status))
|
|
247
|
+
continue;
|
|
248
|
+
await this.updateTaskStatus(id, "cancelled", message, sessionId).catch(() => undefined);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** This method cancels each task that runs and removes all the tasks and their timers. A second close does nothing. */
|
|
252
|
+
async close() {
|
|
253
|
+
if (this.closed)
|
|
254
|
+
return;
|
|
255
|
+
for (const [id, entry] of this.entries) {
|
|
256
|
+
if (TERMINAL.has(entry.task.status))
|
|
257
|
+
continue;
|
|
258
|
+
await this.updateTaskStatus(id, "cancelled", "The MCP host stopped", entry.sessionId).catch(() => undefined);
|
|
259
|
+
}
|
|
260
|
+
this.closed = true;
|
|
261
|
+
for (const entry of this.entries.values())
|
|
262
|
+
if (entry.timer)
|
|
263
|
+
clearTimeout(entry.timer);
|
|
264
|
+
this.entries.clear();
|
|
265
|
+
this.changed();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** The task store of an option: the given store, or a new store. `owned` is true for a new store. */
|
|
269
|
+
export function taskStoreOf(options) {
|
|
270
|
+
if (options === undefined)
|
|
271
|
+
return undefined;
|
|
272
|
+
if (options.store)
|
|
273
|
+
return { store: options.store, owned: false };
|
|
274
|
+
return { store: new McpTaskStore(options), owned: true };
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=tasks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.js","sourceRoot":"","sources":["../src/tasks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAwD,MAAM,oCAAoC,CAAC;AAC/H,OAAO,EAAE,UAAU,EAAiB,MAAM,oBAAoB,CAAC;AAE/D,+CAA+C;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,uFAAuF;IACvF,KAAK,EAAE,SAAS;IAChB,yDAA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CAAC,gBAAgB,CAAC,EAAE,EAAE,WAAW,EAAE,sBAAsB,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC/G,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YAAE,IAAI,KAAK,CAAC,KAAK;gBAAE,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACtF,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;CACF;AAED,qGAAqG;AACrG,MAAM,UAAU,WAAW,CAAC,OAAoC;IAC9D,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC5C,IAAI,OAAO,CAAC,KAAK;QAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IACjE,OAAO,EAAE,KAAK,EAAE,IAAI,YAAY,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks of a request to the Streamable HTTP face (CLI-35), in one place.
|
|
3
|
+
*
|
|
4
|
+
* The `handle` of the face calls `guardMcpRequest` before a request reaches the MCP SDK. The face
|
|
5
|
+
* mounts on more than one kind of listener, so the listener adds no second check for its path.
|
|
6
|
+
*
|
|
7
|
+
* - The checks of a web browser (`checkBrowserRequest` of the core): a request on a loopback
|
|
8
|
+
* address names a loopback host or an allowed host (DNS rebinding), and an `Origin` header names
|
|
9
|
+
* the server or a permitted origin. A request with cookies needs a named origin (CSRF).
|
|
10
|
+
* - The bearer token, with `tokenMatches` of the core: its time does not tell the token.
|
|
11
|
+
*
|
|
12
|
+
* The other guards of the face:
|
|
13
|
+
*
|
|
14
|
+
* - The face reads the body of a request that starts a session to a limit (4 MiB, the limit of
|
|
15
|
+
* the SDK), with `readJsonBody` of the core. Only an `initialize` request starts a session, so
|
|
16
|
+
* a stray POST makes no MCP server.
|
|
17
|
+
* - With `maxSessions`, the face keeps at most that many sessions. A new session above the limit
|
|
18
|
+
* closes the session that was idle longest: no task in progress and no open response.
|
|
19
|
+
* - `assertLoopbackBind` refuses a listener on an address that is not loopback, unless the
|
|
20
|
+
* operator allows it. The owner of the listener calls it before it listens.
|
|
21
|
+
*/
|
|
22
|
+
import type { IncomingMessage } from "node:http";
|
|
23
|
+
import { type BrowserGuardOptions } from "@procedures/client";
|
|
24
|
+
/** The limits of the Streamable HTTP face. `maxBodyBytes` and `maxSessions` of the HTTP options change them. */
|
|
25
|
+
export declare const MCP_HTTP_LIMITS: {
|
|
26
|
+
/** The largest body of a request: 4 MiB, the limit of the SDK. */
|
|
27
|
+
readonly maxBodyBytes: 4_194_304;
|
|
28
|
+
/** The most sessions at one time: no limit, unless the HTTP options give one. */
|
|
29
|
+
readonly maxSessions: number;
|
|
30
|
+
/** The seconds in the `Retry-After` header of a 503, when each session is busy. */
|
|
31
|
+
readonly retryAfterSeconds: 5;
|
|
32
|
+
};
|
|
33
|
+
/** The options of `guardMcpRequest`. */
|
|
34
|
+
export interface McpRequestGuardOptions {
|
|
35
|
+
/** The token that each request sends as `Authorization: Bearer <token>`. Without it, the face checks no token. */
|
|
36
|
+
readonly token?: string | undefined;
|
|
37
|
+
/** The browser origins that can call the face, besides its own origin. */
|
|
38
|
+
readonly allowedOrigins?: BrowserGuardOptions["allowedOrigins"];
|
|
39
|
+
/** The host names that a request on a loopback address can name, besides the loopback names. */
|
|
40
|
+
readonly allowedHosts?: BrowserGuardOptions["allowedHosts"];
|
|
41
|
+
/** True when the server trusts cookies for the request: then only a named origin can call (CSRF). */
|
|
42
|
+
readonly ambient?: boolean | undefined;
|
|
43
|
+
}
|
|
44
|
+
/** The result of the checks: the request passes, or the HTTP status, the error and the headers to send. */
|
|
45
|
+
export type McpGuardResult = {
|
|
46
|
+
readonly ok: true;
|
|
47
|
+
} | {
|
|
48
|
+
readonly ok: false;
|
|
49
|
+
readonly status: 401 | 403 | 405 | 415;
|
|
50
|
+
readonly code: string;
|
|
51
|
+
readonly message: string;
|
|
52
|
+
readonly headers?: Readonly<Record<string, string>> | undefined;
|
|
53
|
+
};
|
|
54
|
+
/** The checks of a request to the MCP endpoint: the browser checks, then the token. */
|
|
55
|
+
export declare function guardMcpRequest(req: Pick<IncomingMessage, "method" | "headers" | "socket">, options?: McpRequestGuardOptions): McpGuardResult;
|
|
56
|
+
/**
|
|
57
|
+
* This function refuses a listener on an address that is not loopback, unless `allowRemote` is
|
|
58
|
+
* true. Then other computers can reach the tools and try the token.
|
|
59
|
+
*/
|
|
60
|
+
export declare function assertLoopbackBind(host: string, allowRemote?: boolean): void;
|
|
61
|
+
//# sourceMappingURL=guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../../src/transport/guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAA+D,KAAK,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE3H,gHAAgH;AAChH,eAAO,MAAM,eAAe,EAAE;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,YAAY,EAAE,SAAS,CAAC;IACjC,iFAAiF;IACjF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,mFAAmF;IACnF,QAAQ,CAAC,iBAAiB,EAAE,CAAC,CAAC;CAK/B,CAAC;AAEF,wCAAwC;AACxC,MAAM,WAAW,sBAAsB;IACrC,kHAAkH;IAClH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,0EAA0E;IAC1E,QAAQ,CAAC,cAAc,CAAC,EAAE,mBAAmB,CAAC,gBAAgB,CAAC,CAAC;IAChE,gGAAgG;IAChG,QAAQ,CAAC,YAAY,CAAC,EAAE,mBAAmB,CAAC,cAAc,CAAC,CAAC;IAC5D,qGAAqG;IACrG,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACxC;AAED,2GAA2G;AAC3G,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;CAAE,GACrB;IACE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;CACjE,CAAC;AAEN,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC,EAAE,OAAO,GAAE,sBAA2B,GAAG,cAAc,CAiBjJ;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,UAAQ,GAAG,IAAI,CAG1E"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks of a request to the Streamable HTTP face (CLI-35), in one place.
|
|
3
|
+
*
|
|
4
|
+
* The `handle` of the face calls `guardMcpRequest` before a request reaches the MCP SDK. The face
|
|
5
|
+
* mounts on more than one kind of listener, so the listener adds no second check for its path.
|
|
6
|
+
*
|
|
7
|
+
* - The checks of a web browser (`checkBrowserRequest` of the core): a request on a loopback
|
|
8
|
+
* address names a loopback host or an allowed host (DNS rebinding), and an `Origin` header names
|
|
9
|
+
* the server or a permitted origin. A request with cookies needs a named origin (CSRF).
|
|
10
|
+
* - The bearer token, with `tokenMatches` of the core: its time does not tell the token.
|
|
11
|
+
*
|
|
12
|
+
* The other guards of the face:
|
|
13
|
+
*
|
|
14
|
+
* - The face reads the body of a request that starts a session to a limit (4 MiB, the limit of
|
|
15
|
+
* the SDK), with `readJsonBody` of the core. Only an `initialize` request starts a session, so
|
|
16
|
+
* a stray POST makes no MCP server.
|
|
17
|
+
* - With `maxSessions`, the face keeps at most that many sessions. A new session above the limit
|
|
18
|
+
* closes the session that was idle longest: no task in progress and no open response.
|
|
19
|
+
* - `assertLoopbackBind` refuses a listener on an address that is not loopback, unless the
|
|
20
|
+
* operator allows it. The owner of the listener calls it before it listens.
|
|
21
|
+
*/
|
|
22
|
+
import { bearerOf, checkBrowserRequest, isLoopbackHost, tokenMatches } from "@procedures/client";
|
|
23
|
+
/** The limits of the Streamable HTTP face. `maxBodyBytes` and `maxSessions` of the HTTP options change them. */
|
|
24
|
+
export const MCP_HTTP_LIMITS = {
|
|
25
|
+
maxBodyBytes: 4_194_304,
|
|
26
|
+
maxSessions: Number.POSITIVE_INFINITY,
|
|
27
|
+
retryAfterSeconds: 5,
|
|
28
|
+
};
|
|
29
|
+
/** The checks of a request to the MCP endpoint: the browser checks, then the token. */
|
|
30
|
+
export function guardMcpRequest(req, options = {}) {
|
|
31
|
+
const browser = {
|
|
32
|
+
...(options.allowedOrigins !== undefined ? { allowedOrigins: options.allowedOrigins } : {}),
|
|
33
|
+
...(options.allowedHosts !== undefined ? { allowedHosts: options.allowedHosts } : {}),
|
|
34
|
+
};
|
|
35
|
+
const checked = checkBrowserRequest(req, browser, false, options.ambient === true);
|
|
36
|
+
if (!checked.ok)
|
|
37
|
+
return checked;
|
|
38
|
+
if (options.token !== undefined && !tokenMatches(options.token, bearerOf(req.headers.authorization))) {
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
status: 401,
|
|
42
|
+
code: "UNAUTHORIZED",
|
|
43
|
+
message: "Unauthorized: this server requires its token",
|
|
44
|
+
headers: { "www-authenticate": 'Bearer realm="mcp"' },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return { ok: true };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* This function refuses a listener on an address that is not loopback, unless `allowRemote` is
|
|
51
|
+
* true. Then other computers can reach the tools and try the token.
|
|
52
|
+
*/
|
|
53
|
+
export function assertLoopbackBind(host, allowRemote = false) {
|
|
54
|
+
if (isLoopbackHost(host) || allowRemote)
|
|
55
|
+
return;
|
|
56
|
+
throw new Error(`The MCP endpoint would listen on ${host}, which is not a loopback address. To serve other computers, allow it (allowRemote).`);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../../src/transport/guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,cAAc,EAAE,YAAY,EAA4B,MAAM,oBAAoB,CAAC;AAE3H,gHAAgH;AAChH,MAAM,CAAC,MAAM,eAAe,GAOxB;IACF,YAAY,EAAE,SAAS;IACvB,WAAW,EAAE,MAAM,CAAC,iBAAiB;IACrC,iBAAiB,EAAE,CAAC;CACrB,CAAC;AAyBF,uFAAuF;AACvF,MAAM,UAAU,eAAe,CAAC,GAA2D,EAAE,OAAO,GAA2B,EAAE;IAC/H,MAAM,OAAO,GAAwB;QACnC,GAAG,CAAC,OAAO,CAAC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3F,GAAG,CAAC,OAAO,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACtF,CAAC;IACF,MAAM,OAAO,GAAG,mBAAmB,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC;IACnF,IAAI,CAAC,OAAO,CAAC,EAAE;QAAE,OAAO,OAAO,CAAC;IAChC,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC;QACrG,OAAO;YACL,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,GAAG;YACX,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,8CAA8C;YACvD,OAAO,EAAE,EAAE,kBAAkB,EAAE,oBAAoB,EAAE;SACtD,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AACtB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY,EAAE,WAAW,GAAG,KAAK;IAClE,IAAI,cAAc,CAAC,IAAI,CAAC,IAAI,WAAW;QAAE,OAAO;IAChD,MAAM,IAAI,KAAK,CAAC,oCAAoC,IAAI,sFAAsF,CAAC,CAAC;AAClJ,CAAC"}
|
package/dist/transport/http.d.ts
CHANGED
|
@@ -12,13 +12,28 @@
|
|
|
12
12
|
* Each tool call carries the credentials of its HTTP request (CLI-10), so the request hook of the
|
|
13
13
|
* server (`principalOf`) and its token check apply. A server that reads cookies trusts a request
|
|
14
14
|
* with a cookie only from a named origin (CSRF).
|
|
15
|
+
*
|
|
16
|
+
* The checks of each request are in `guard.ts` (CLI-35). Only an `initialize` request starts a
|
|
17
|
+
* session, each body has a limit, and with `maxSessions` the endpoint keeps at most that many sessions.
|
|
15
18
|
*/
|
|
16
19
|
import { type IncomingMessage, type ServerResponse } from "node:http";
|
|
17
20
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
18
21
|
import type { HttpTransportOptions } from "../types.js";
|
|
22
|
+
/** The limits of the sessions of the endpoint (CLI-35). The HTTP options win over them. */
|
|
23
|
+
export interface McpHttpSessionOptions {
|
|
24
|
+
/** The most sessions at one time. The default is no limit. */
|
|
25
|
+
readonly maxSessions?: number | undefined;
|
|
26
|
+
/** The largest body of a request. The default is 4 MiB, the limit of the SDK. */
|
|
27
|
+
readonly maxBodyBytes?: number | undefined;
|
|
28
|
+
/** The sessions that have a task in progress: the session limit does not close them. */
|
|
29
|
+
readonly busySessions?: (() => ReadonlySet<string>) | undefined;
|
|
30
|
+
}
|
|
19
31
|
/** The HTTP endpoint of an MCP server, with a session for each client. */
|
|
20
32
|
export interface McpHttpTransport {
|
|
21
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* This function starts to listen, when the endpoint has a server of its own. It gives the URL, or the path of a mounted endpoint.
|
|
35
|
+
* @deprecated The next MINOR removes the listener of the endpoint (CLI-11): then the endpoint is always mounted.
|
|
36
|
+
*/
|
|
22
37
|
listen(): Promise<string>;
|
|
23
38
|
/**
|
|
24
39
|
* This function answers a request of the path of the endpoint and gives true. It gives false for
|
|
@@ -34,5 +49,5 @@ export interface McpHttpTransport {
|
|
|
34
49
|
* This function makes the Streamable HTTP endpoint of an MCP server. `connect` connects the
|
|
35
50
|
* transport of a new session to an MCP server of its own.
|
|
36
51
|
*/
|
|
37
|
-
export declare function createHttpTransport(options: HttpTransportOptions, connect: (transport: StreamableHTTPServerTransport) => Promise<void
|
|
52
|
+
export declare function createHttpTransport(options: HttpTransportOptions, connect: (transport: StreamableHTTPServerTransport) => Promise<void>, limits?: McpHttpSessionOptions): McpHttpTransport;
|
|
38
53
|
//# sourceMappingURL=http.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/transport/http.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/transport/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAgB,KAAK,eAAe,EAA6B,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAE/G,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAInG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGxD,2FAA2F;AAC3F,MAAM,WAAW,qBAAqB;IACpC,8DAA8D;IAC9D,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C,iFAAiF;IACjF,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,wFAAwF;IACxF,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;CACjE;AAED,0EAA0E;AAC1E,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1B;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC;IAC3D,mCAAmC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,oFAAoF;IACpF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAOD;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,CAAC,SAAS,EAAE,6BAA6B,KAAK,OAAO,CAAC,IAAI,CAAC,EACpE,MAAM,GAAE,qBAA0B,GACjC,gBAAgB,CA6MlB"}
|