@modelcontextprotocol/sdk 1.24.3 → 1.25.0
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/dist/cjs/client/auth-extensions.js +5 -9
- package/dist/cjs/client/auth-extensions.js.map +1 -1
- package/dist/cjs/client/auth.js +30 -37
- package/dist/cjs/client/auth.js.map +1 -1
- package/dist/cjs/client/index.d.ts +61 -14
- package/dist/cjs/client/index.d.ts.map +1 -1
- package/dist/cjs/client/index.js +117 -36
- package/dist/cjs/client/index.js.map +1 -1
- package/dist/cjs/client/middleware.js +3 -3
- package/dist/cjs/client/middleware.js.map +1 -1
- package/dist/cjs/client/sse.js +19 -27
- package/dist/cjs/client/sse.js.map +1 -1
- package/dist/cjs/client/stdio.js +18 -28
- package/dist/cjs/client/stdio.js.map +1 -1
- package/dist/cjs/client/streamableHttp.d.ts.map +1 -1
- package/dist/cjs/client/streamableHttp.js +39 -49
- package/dist/cjs/client/streamableHttp.js.map +1 -1
- package/dist/cjs/client/websocket.js +6 -11
- package/dist/cjs/client/websocket.js.map +1 -1
- package/dist/cjs/examples/client/elicitationUrlExample.js +4 -5
- package/dist/cjs/examples/client/elicitationUrlExample.js.map +1 -1
- package/dist/cjs/examples/client/simpleOAuthClient.js +3 -3
- package/dist/cjs/examples/client/simpleOAuthClient.js.map +1 -1
- package/dist/cjs/examples/client/simpleStreamableHttp.js +7 -9
- package/dist/cjs/examples/client/simpleStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/client/simpleTaskInteractiveClient.js +1 -2
- package/dist/cjs/examples/client/simpleTaskInteractiveClient.js.map +1 -1
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.d.ts +10 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.d.ts.map +1 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.js +81 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.js.map +1 -0
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js +10 -4
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleSseServer.js +6 -3
- package/dist/cjs/examples/server/simpleSseServer.js.map +1 -1
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js +12 -6
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleStreamableHttp.js +21 -11
- package/dist/cjs/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleTaskInteractive.js +7 -11
- package/dist/cjs/examples/server/simpleTaskInteractive.js.map +1 -1
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js +10 -7
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/cjs/examples/server/standaloneSseWithGetStreamableHttp.js +1 -1
- package/dist/cjs/examples/server/standaloneSseWithGetStreamableHttp.js.map +1 -1
- package/dist/cjs/experimental/tasks/client.js +1 -2
- package/dist/cjs/experimental/tasks/client.js.map +1 -1
- package/dist/cjs/experimental/tasks/helpers.js +3 -5
- package/dist/cjs/experimental/tasks/helpers.js.map +1 -1
- package/dist/cjs/experimental/tasks/interfaces.d.ts +3 -3
- package/dist/cjs/experimental/tasks/interfaces.d.ts.map +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts.map +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.js +3 -5
- package/dist/cjs/experimental/tasks/stores/in-memory.js.map +1 -1
- package/dist/cjs/inMemory.js +5 -7
- package/dist/cjs/inMemory.js.map +1 -1
- package/dist/cjs/server/auth/handlers/token.js +1 -1
- package/dist/cjs/server/auth/handlers/token.js.map +1 -1
- package/dist/cjs/server/auth/providers/proxyProvider.js +11 -17
- package/dist/cjs/server/auth/providers/proxyProvider.js.map +1 -1
- package/dist/cjs/server/auth/router.js +3 -6
- package/dist/cjs/server/auth/router.js.map +1 -1
- package/dist/cjs/server/completable.js +1 -1
- package/dist/cjs/server/completable.js.map +1 -1
- package/dist/cjs/server/index.d.ts +3 -3
- package/dist/cjs/server/index.d.ts.map +1 -1
- package/dist/cjs/server/index.js +20 -30
- package/dist/cjs/server/index.js.map +1 -1
- package/dist/cjs/server/mcp.d.ts.map +1 -1
- package/dist/cjs/server/mcp.js +25 -11
- package/dist/cjs/server/mcp.js.map +1 -1
- package/dist/cjs/server/middleware/hostHeaderValidation.js +1 -1
- package/dist/cjs/server/middleware/hostHeaderValidation.js.map +1 -1
- package/dist/cjs/server/sse.js +14 -17
- package/dist/cjs/server/sse.js.map +1 -1
- package/dist/cjs/server/stdio.js +4 -7
- package/dist/cjs/server/stdio.js.map +1 -1
- package/dist/cjs/server/streamableHttp.d.ts +52 -154
- package/dist/cjs/server/streamableHttp.d.ts.map +1 -1
- package/dist/cjs/server/streamableHttp.js +78 -653
- package/dist/cjs/server/streamableHttp.js.map +1 -1
- package/dist/cjs/server/webStandardStreamableHttp.d.ts +267 -0
- package/dist/cjs/server/webStandardStreamableHttp.d.ts.map +1 -0
- package/dist/cjs/server/webStandardStreamableHttp.js +729 -0
- package/dist/cjs/server/webStandardStreamableHttp.js.map +1 -0
- package/dist/cjs/server/zod-compat.d.ts +3 -1
- package/dist/cjs/server/zod-compat.d.ts.map +1 -1
- package/dist/cjs/server/zod-compat.js +11 -19
- package/dist/cjs/server/zod-compat.js.map +1 -1
- package/dist/cjs/server/zod-json-schema-compat.js +5 -6
- package/dist/cjs/server/zod-json-schema-compat.js.map +1 -1
- package/dist/cjs/shared/metadataUtils.js +1 -2
- package/dist/cjs/shared/metadataUtils.js.map +1 -1
- package/dist/cjs/shared/protocol.d.ts +1 -1
- package/dist/cjs/shared/protocol.d.ts.map +1 -1
- package/dist/cjs/shared/protocol.js +76 -88
- package/dist/cjs/shared/protocol.js.map +1 -1
- package/dist/cjs/shared/transport.js +1 -1
- package/dist/cjs/shared/transport.js.map +1 -1
- package/dist/cjs/spec.types.d.ts +315 -26
- package/dist/cjs/spec.types.d.ts.map +1 -1
- package/dist/cjs/spec.types.js +2 -2
- package/dist/cjs/spec.types.js.map +1 -1
- package/dist/cjs/types.d.ts +1392 -1248
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js +201 -136
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/validation/ajv-provider.d.ts +1 -1
- package/dist/cjs/validation/ajv-provider.d.ts.map +1 -1
- package/dist/cjs/validation/ajv-provider.js +4 -5
- package/dist/cjs/validation/ajv-provider.js.map +1 -1
- package/dist/cjs/validation/cfworker-provider.js +4 -5
- package/dist/cjs/validation/cfworker-provider.js.map +1 -1
- package/dist/cjs/validation/types.d.ts +12 -2
- package/dist/cjs/validation/types.d.ts.map +1 -1
- package/dist/esm/client/auth-extensions.js +5 -9
- package/dist/esm/client/auth-extensions.js.map +1 -1
- package/dist/esm/client/auth.js +30 -37
- package/dist/esm/client/auth.js.map +1 -1
- package/dist/esm/client/index.d.ts +61 -14
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +118 -37
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/middleware.js +3 -3
- package/dist/esm/client/middleware.js.map +1 -1
- package/dist/esm/client/sse.js +19 -27
- package/dist/esm/client/sse.js.map +1 -1
- package/dist/esm/client/stdio.js +18 -28
- package/dist/esm/client/stdio.js.map +1 -1
- package/dist/esm/client/streamableHttp.d.ts.map +1 -1
- package/dist/esm/client/streamableHttp.js +40 -50
- package/dist/esm/client/streamableHttp.js.map +1 -1
- package/dist/esm/client/websocket.js +6 -11
- package/dist/esm/client/websocket.js.map +1 -1
- package/dist/esm/examples/client/elicitationUrlExample.js +4 -5
- package/dist/esm/examples/client/elicitationUrlExample.js.map +1 -1
- package/dist/esm/examples/client/simpleOAuthClient.js +3 -3
- package/dist/esm/examples/client/simpleOAuthClient.js.map +1 -1
- package/dist/esm/examples/client/simpleStreamableHttp.js +7 -9
- package/dist/esm/examples/client/simpleStreamableHttp.js.map +1 -1
- package/dist/esm/examples/client/simpleTaskInteractiveClient.js +1 -2
- package/dist/esm/examples/client/simpleTaskInteractiveClient.js.map +1 -1
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts +10 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts.map +1 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.js +56 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.js.map +1 -0
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js +10 -4
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleSseServer.js +6 -3
- package/dist/esm/examples/server/simpleSseServer.js.map +1 -1
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js +12 -6
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleStreamableHttp.js +21 -11
- package/dist/esm/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleTaskInteractive.js +7 -11
- package/dist/esm/examples/server/simpleTaskInteractive.js.map +1 -1
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js +10 -7
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js +1 -1
- package/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js.map +1 -1
- package/dist/esm/experimental/tasks/client.js +1 -2
- package/dist/esm/experimental/tasks/client.js.map +1 -1
- package/dist/esm/experimental/tasks/helpers.js +3 -5
- package/dist/esm/experimental/tasks/helpers.js.map +1 -1
- package/dist/esm/experimental/tasks/interfaces.d.ts +3 -3
- package/dist/esm/experimental/tasks/interfaces.d.ts.map +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts.map +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.js +3 -5
- package/dist/esm/experimental/tasks/stores/in-memory.js.map +1 -1
- package/dist/esm/inMemory.js +5 -7
- package/dist/esm/inMemory.js.map +1 -1
- package/dist/esm/server/auth/handlers/token.js +1 -1
- package/dist/esm/server/auth/handlers/token.js.map +1 -1
- package/dist/esm/server/auth/providers/proxyProvider.js +11 -17
- package/dist/esm/server/auth/providers/proxyProvider.js.map +1 -1
- package/dist/esm/server/auth/router.js +3 -6
- package/dist/esm/server/auth/router.js.map +1 -1
- package/dist/esm/server/completable.js +1 -1
- package/dist/esm/server/completable.js.map +1 -1
- package/dist/esm/server/index.d.ts +3 -3
- package/dist/esm/server/index.d.ts.map +1 -1
- package/dist/esm/server/index.js +20 -30
- package/dist/esm/server/index.js.map +1 -1
- package/dist/esm/server/mcp.d.ts.map +1 -1
- package/dist/esm/server/mcp.js +25 -11
- package/dist/esm/server/mcp.js.map +1 -1
- package/dist/esm/server/middleware/hostHeaderValidation.js +1 -1
- package/dist/esm/server/middleware/hostHeaderValidation.js.map +1 -1
- package/dist/esm/server/sse.js +14 -17
- package/dist/esm/server/sse.js.map +1 -1
- package/dist/esm/server/stdio.js +4 -7
- package/dist/esm/server/stdio.js.map +1 -1
- package/dist/esm/server/streamableHttp.d.ts +52 -154
- package/dist/esm/server/streamableHttp.d.ts.map +1 -1
- package/dist/esm/server/streamableHttp.js +78 -650
- package/dist/esm/server/streamableHttp.js.map +1 -1
- package/dist/esm/server/webStandardStreamableHttp.d.ts +267 -0
- package/dist/esm/server/webStandardStreamableHttp.d.ts.map +1 -0
- package/dist/esm/server/webStandardStreamableHttp.js +725 -0
- package/dist/esm/server/webStandardStreamableHttp.js.map +1 -0
- package/dist/esm/server/zod-compat.d.ts +3 -1
- package/dist/esm/server/zod-compat.d.ts.map +1 -1
- package/dist/esm/server/zod-compat.js +11 -19
- package/dist/esm/server/zod-compat.js.map +1 -1
- package/dist/esm/server/zod-json-schema-compat.js +5 -6
- package/dist/esm/server/zod-json-schema-compat.js.map +1 -1
- package/dist/esm/shared/metadataUtils.js +1 -2
- package/dist/esm/shared/metadataUtils.js.map +1 -1
- package/dist/esm/shared/protocol.d.ts +1 -1
- package/dist/esm/shared/protocol.d.ts.map +1 -1
- package/dist/esm/shared/protocol.js +77 -89
- package/dist/esm/shared/protocol.js.map +1 -1
- package/dist/esm/shared/transport.js +1 -1
- package/dist/esm/shared/transport.js.map +1 -1
- package/dist/esm/spec.types.d.ts +315 -26
- package/dist/esm/spec.types.d.ts.map +1 -1
- package/dist/esm/spec.types.js +2 -2
- package/dist/esm/spec.types.js.map +1 -1
- package/dist/esm/types.d.ts +1392 -1248
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +194 -130
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/validation/ajv-provider.d.ts +1 -1
- package/dist/esm/validation/ajv-provider.d.ts.map +1 -1
- package/dist/esm/validation/ajv-provider.js +3 -4
- package/dist/esm/validation/ajv-provider.js.map +1 -1
- package/dist/esm/validation/cfworker-provider.js +4 -5
- package/dist/esm/validation/cfworker-provider.js.map +1 -1
- package/dist/esm/validation/types.d.ts +12 -2
- package/dist/esm/validation/types.d.ts.map +1 -1
- package/package.json +3 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { safeParse } from '../server/zod-compat.js';
|
|
2
|
-
import { CancelledNotificationSchema, CreateTaskResultSchema, ErrorCode, GetTaskRequestSchema, GetTaskResultSchema, GetTaskPayloadRequestSchema, ListTasksRequestSchema, ListTasksResultSchema, CancelTaskRequestSchema, CancelTaskResultSchema,
|
|
2
|
+
import { CancelledNotificationSchema, CreateTaskResultSchema, ErrorCode, GetTaskRequestSchema, GetTaskResultSchema, GetTaskPayloadRequestSchema, ListTasksRequestSchema, ListTasksResultSchema, CancelTaskRequestSchema, CancelTaskResultSchema, isJSONRPCErrorResponse, isJSONRPCRequest, isJSONRPCResultResponse, isJSONRPCNotification, McpError, PingRequestSchema, ProgressNotificationSchema, RELATED_TASK_META_KEY, TaskStatusNotificationSchema, isTaskAugmentedRequestParams } from '../types.js';
|
|
3
3
|
import { isTerminal } from '../experimental/tasks/interfaces.js';
|
|
4
4
|
import { getMethodLiteral, parseWithCompat } from '../server/zod-json-schema-compat.js';
|
|
5
5
|
/**
|
|
@@ -34,8 +34,8 @@ export class Protocol {
|
|
|
34
34
|
// Automatic pong by default.
|
|
35
35
|
_request => ({}));
|
|
36
36
|
// Install task handlers if TaskStore is provided
|
|
37
|
-
this._taskStore = _options
|
|
38
|
-
this._taskMessageQueue = _options
|
|
37
|
+
this._taskStore = _options?.taskStore;
|
|
38
|
+
this._taskMessageQueue = _options?.taskMessageQueue;
|
|
39
39
|
if (this._taskStore) {
|
|
40
40
|
this.setRequestHandler(GetTaskRequestSchema, async (request, extra) => {
|
|
41
41
|
const task = await this._taskStore.getTask(request.params.taskId, extra.sessionId);
|
|
@@ -51,7 +51,6 @@ export class Protocol {
|
|
|
51
51
|
});
|
|
52
52
|
this.setRequestHandler(GetTaskPayloadRequestSchema, async (request, extra) => {
|
|
53
53
|
const handleTaskResult = async () => {
|
|
54
|
-
var _a;
|
|
55
54
|
const taskId = request.params.taskId;
|
|
56
55
|
// Deliver queued messages
|
|
57
56
|
if (this._taskMessageQueue) {
|
|
@@ -87,7 +86,7 @@ export class Protocol {
|
|
|
87
86
|
}
|
|
88
87
|
// Send the message on the response stream by passing the relatedRequestId
|
|
89
88
|
// This tells the transport to write the message to the tasks/result response stream
|
|
90
|
-
await
|
|
89
|
+
await this._transport?.send(queuedMessage.message, { relatedRequestId: extra.requestId });
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
92
|
// Now check task status
|
|
@@ -121,9 +120,8 @@ export class Protocol {
|
|
|
121
120
|
return await handleTaskResult();
|
|
122
121
|
});
|
|
123
122
|
this.setRequestHandler(ListTasksRequestSchema, async (request, extra) => {
|
|
124
|
-
var _a;
|
|
125
123
|
try {
|
|
126
|
-
const { tasks, nextCursor } = await this._taskStore.listTasks(
|
|
124
|
+
const { tasks, nextCursor } = await this._taskStore.listTasks(request.params?.cursor, extra.sessionId);
|
|
127
125
|
// @ts-expect-error SendResultT cannot contain ListTasksResult, but we include it in our derived types everywhere else
|
|
128
126
|
return {
|
|
129
127
|
tasks,
|
|
@@ -169,9 +167,12 @@ export class Protocol {
|
|
|
169
167
|
}
|
|
170
168
|
}
|
|
171
169
|
async _oncancel(notification) {
|
|
170
|
+
if (!notification.params.requestId) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
172
173
|
// Handle request cancellation
|
|
173
174
|
const controller = this._requestHandlerAbortControllers.get(notification.params.requestId);
|
|
174
|
-
controller
|
|
175
|
+
controller?.abort(notification.params.reason);
|
|
175
176
|
}
|
|
176
177
|
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
|
|
177
178
|
this._timeoutInfo.set(messageId, {
|
|
@@ -212,22 +213,21 @@ export class Protocol {
|
|
|
212
213
|
* The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
|
|
213
214
|
*/
|
|
214
215
|
async connect(transport) {
|
|
215
|
-
var _a, _b, _c;
|
|
216
216
|
this._transport = transport;
|
|
217
|
-
const _onclose =
|
|
217
|
+
const _onclose = this.transport?.onclose;
|
|
218
218
|
this._transport.onclose = () => {
|
|
219
|
-
_onclose
|
|
219
|
+
_onclose?.();
|
|
220
220
|
this._onclose();
|
|
221
221
|
};
|
|
222
|
-
const _onerror =
|
|
222
|
+
const _onerror = this.transport?.onerror;
|
|
223
223
|
this._transport.onerror = (error) => {
|
|
224
|
-
_onerror
|
|
224
|
+
_onerror?.(error);
|
|
225
225
|
this._onerror(error);
|
|
226
226
|
};
|
|
227
|
-
const _onmessage =
|
|
227
|
+
const _onmessage = this._transport?.onmessage;
|
|
228
228
|
this._transport.onmessage = (message, extra) => {
|
|
229
|
-
_onmessage
|
|
230
|
-
if (
|
|
229
|
+
_onmessage?.(message, extra);
|
|
230
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
231
231
|
this._onresponse(message);
|
|
232
232
|
}
|
|
233
233
|
else if (isJSONRPCRequest(message)) {
|
|
@@ -243,7 +243,6 @@ export class Protocol {
|
|
|
243
243
|
await this._transport.start();
|
|
244
244
|
}
|
|
245
245
|
_onclose() {
|
|
246
|
-
var _a;
|
|
247
246
|
const responseHandlers = this._responseHandlers;
|
|
248
247
|
this._responseHandlers = new Map();
|
|
249
248
|
this._progressHandlers.clear();
|
|
@@ -251,18 +250,16 @@ export class Protocol {
|
|
|
251
250
|
this._pendingDebouncedNotifications.clear();
|
|
252
251
|
const error = McpError.fromError(ErrorCode.ConnectionClosed, 'Connection closed');
|
|
253
252
|
this._transport = undefined;
|
|
254
|
-
|
|
253
|
+
this.onclose?.();
|
|
255
254
|
for (const handler of responseHandlers.values()) {
|
|
256
255
|
handler(error);
|
|
257
256
|
}
|
|
258
257
|
}
|
|
259
258
|
_onerror(error) {
|
|
260
|
-
|
|
261
|
-
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
259
|
+
this.onerror?.(error);
|
|
262
260
|
}
|
|
263
261
|
_onnotification(notification) {
|
|
264
|
-
|
|
265
|
-
const handler = (_a = this._notificationHandlers.get(notification.method)) !== null && _a !== void 0 ? _a : this.fallbackNotificationHandler;
|
|
262
|
+
const handler = this._notificationHandlers.get(notification.method) ?? this.fallbackNotificationHandler;
|
|
266
263
|
// Ignore notifications not being subscribed to.
|
|
267
264
|
if (handler === undefined) {
|
|
268
265
|
return;
|
|
@@ -273,12 +270,11 @@ export class Protocol {
|
|
|
273
270
|
.catch(error => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
|
|
274
271
|
}
|
|
275
272
|
_onrequest(request, extra) {
|
|
276
|
-
|
|
277
|
-
const handler = (_a = this._requestHandlers.get(request.method)) !== null && _a !== void 0 ? _a : this.fallbackRequestHandler;
|
|
273
|
+
const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
|
|
278
274
|
// Capture the current transport at request time to ensure responses go to the correct client
|
|
279
275
|
const capturedTransport = this._transport;
|
|
280
276
|
// Extract taskId from request metadata if present (needed early for method not found case)
|
|
281
|
-
const relatedTaskId =
|
|
277
|
+
const relatedTaskId = request.params?._meta?.[RELATED_TASK_META_KEY]?.taskId;
|
|
282
278
|
if (handler === undefined) {
|
|
283
279
|
const errorResponse = {
|
|
284
280
|
jsonrpc: '2.0',
|
|
@@ -294,21 +290,23 @@ export class Protocol {
|
|
|
294
290
|
type: 'error',
|
|
295
291
|
message: errorResponse,
|
|
296
292
|
timestamp: Date.now()
|
|
297
|
-
}, capturedTransport
|
|
293
|
+
}, capturedTransport?.sessionId).catch(error => this._onerror(new Error(`Failed to enqueue error response: ${error}`)));
|
|
298
294
|
}
|
|
299
295
|
else {
|
|
300
|
-
capturedTransport
|
|
296
|
+
capturedTransport
|
|
297
|
+
?.send(errorResponse)
|
|
298
|
+
.catch(error => this._onerror(new Error(`Failed to send an error response: ${error}`)));
|
|
301
299
|
}
|
|
302
300
|
return;
|
|
303
301
|
}
|
|
304
302
|
const abortController = new AbortController();
|
|
305
303
|
this._requestHandlerAbortControllers.set(request.id, abortController);
|
|
306
|
-
const taskCreationParams = (
|
|
307
|
-
const taskStore = this._taskStore ? this.requestTaskStore(request, capturedTransport
|
|
304
|
+
const taskCreationParams = isTaskAugmentedRequestParams(request.params) ? request.params.task : undefined;
|
|
305
|
+
const taskStore = this._taskStore ? this.requestTaskStore(request, capturedTransport?.sessionId) : undefined;
|
|
308
306
|
const fullExtra = {
|
|
309
307
|
signal: abortController.signal,
|
|
310
|
-
sessionId: capturedTransport
|
|
311
|
-
_meta:
|
|
308
|
+
sessionId: capturedTransport?.sessionId,
|
|
309
|
+
_meta: request.params?._meta,
|
|
312
310
|
sendNotification: async (notification) => {
|
|
313
311
|
// Include related-task metadata if this request is part of a task
|
|
314
312
|
const notificationOptions = { relatedRequestId: request.id };
|
|
@@ -318,7 +316,6 @@ export class Protocol {
|
|
|
318
316
|
await this.notification(notification, notificationOptions);
|
|
319
317
|
},
|
|
320
318
|
sendRequest: async (r, resultSchema, options) => {
|
|
321
|
-
var _a, _b;
|
|
322
319
|
// Include related-task metadata if this request is part of a task
|
|
323
320
|
const requestOptions = { ...options, relatedRequestId: request.id };
|
|
324
321
|
if (relatedTaskId && !requestOptions.relatedTask) {
|
|
@@ -326,20 +323,20 @@ export class Protocol {
|
|
|
326
323
|
}
|
|
327
324
|
// Set task status to input_required when sending a request within a task context
|
|
328
325
|
// Use the taskId from options (explicit) or fall back to relatedTaskId (inherited)
|
|
329
|
-
const effectiveTaskId =
|
|
326
|
+
const effectiveTaskId = requestOptions.relatedTask?.taskId ?? relatedTaskId;
|
|
330
327
|
if (effectiveTaskId && taskStore) {
|
|
331
328
|
await taskStore.updateTaskStatus(effectiveTaskId, 'input_required');
|
|
332
329
|
}
|
|
333
330
|
return await this.request(r, resultSchema, requestOptions);
|
|
334
331
|
},
|
|
335
|
-
authInfo: extra
|
|
332
|
+
authInfo: extra?.authInfo,
|
|
336
333
|
requestId: request.id,
|
|
337
|
-
requestInfo: extra
|
|
334
|
+
requestInfo: extra?.requestInfo,
|
|
338
335
|
taskId: relatedTaskId,
|
|
339
336
|
taskStore: taskStore,
|
|
340
|
-
taskRequestedTtl: taskCreationParams
|
|
341
|
-
closeSSEStream: extra
|
|
342
|
-
closeStandaloneSSEStream: extra
|
|
337
|
+
taskRequestedTtl: taskCreationParams?.ttl,
|
|
338
|
+
closeSSEStream: extra?.closeSSEStream,
|
|
339
|
+
closeStandaloneSSEStream: extra?.closeStandaloneSSEStream
|
|
343
340
|
};
|
|
344
341
|
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
|
|
345
342
|
Promise.resolve()
|
|
@@ -367,13 +364,12 @@ export class Protocol {
|
|
|
367
364
|
type: 'response',
|
|
368
365
|
message: response,
|
|
369
366
|
timestamp: Date.now()
|
|
370
|
-
}, capturedTransport
|
|
367
|
+
}, capturedTransport?.sessionId);
|
|
371
368
|
}
|
|
372
369
|
else {
|
|
373
|
-
await
|
|
370
|
+
await capturedTransport?.send(response);
|
|
374
371
|
}
|
|
375
372
|
}, async (error) => {
|
|
376
|
-
var _a;
|
|
377
373
|
if (abortController.signal.aborted) {
|
|
378
374
|
// Request was cancelled
|
|
379
375
|
return;
|
|
@@ -383,7 +379,7 @@ export class Protocol {
|
|
|
383
379
|
id: request.id,
|
|
384
380
|
error: {
|
|
385
381
|
code: Number.isSafeInteger(error['code']) ? error['code'] : ErrorCode.InternalError,
|
|
386
|
-
message:
|
|
382
|
+
message: error.message ?? 'Internal error',
|
|
387
383
|
...(error['data'] !== undefined && { data: error['data'] })
|
|
388
384
|
}
|
|
389
385
|
};
|
|
@@ -393,10 +389,10 @@ export class Protocol {
|
|
|
393
389
|
type: 'error',
|
|
394
390
|
message: errorResponse,
|
|
395
391
|
timestamp: Date.now()
|
|
396
|
-
}, capturedTransport
|
|
392
|
+
}, capturedTransport?.sessionId);
|
|
397
393
|
}
|
|
398
394
|
else {
|
|
399
|
-
await
|
|
395
|
+
await capturedTransport?.send(errorResponse);
|
|
400
396
|
}
|
|
401
397
|
})
|
|
402
398
|
.catch(error => this._onerror(new Error(`Failed to send response: ${error}`)))
|
|
@@ -435,7 +431,7 @@ export class Protocol {
|
|
|
435
431
|
const resolver = this._requestResolvers.get(messageId);
|
|
436
432
|
if (resolver) {
|
|
437
433
|
this._requestResolvers.delete(messageId);
|
|
438
|
-
if (
|
|
434
|
+
if (isJSONRPCResultResponse(response)) {
|
|
439
435
|
resolver(response);
|
|
440
436
|
}
|
|
441
437
|
else {
|
|
@@ -453,7 +449,7 @@ export class Protocol {
|
|
|
453
449
|
this._cleanupTimeout(messageId);
|
|
454
450
|
// Keep progress handler alive for CreateTaskResult responses
|
|
455
451
|
let isTaskResponse = false;
|
|
456
|
-
if (
|
|
452
|
+
if (isJSONRPCResultResponse(response) && response.result && typeof response.result === 'object') {
|
|
457
453
|
const result = response.result;
|
|
458
454
|
if (result.task && typeof result.task === 'object') {
|
|
459
455
|
const task = result.task;
|
|
@@ -466,7 +462,7 @@ export class Protocol {
|
|
|
466
462
|
if (!isTaskResponse) {
|
|
467
463
|
this._progressHandlers.delete(messageId);
|
|
468
464
|
}
|
|
469
|
-
if (
|
|
465
|
+
if (isJSONRPCResultResponse(response)) {
|
|
470
466
|
handler(response);
|
|
471
467
|
}
|
|
472
468
|
else {
|
|
@@ -481,8 +477,7 @@ export class Protocol {
|
|
|
481
477
|
* Closes the connection.
|
|
482
478
|
*/
|
|
483
479
|
async close() {
|
|
484
|
-
|
|
485
|
-
await ((_a = this._transport) === null || _a === void 0 ? void 0 : _a.close());
|
|
480
|
+
await this._transport?.close();
|
|
486
481
|
}
|
|
487
482
|
/**
|
|
488
483
|
* Sends a request and returns an AsyncGenerator that yields response messages.
|
|
@@ -512,8 +507,7 @@ export class Protocol {
|
|
|
512
507
|
* @experimental Use `client.experimental.tasks.requestStream()` to access this method.
|
|
513
508
|
*/
|
|
514
509
|
async *requestStream(request, resultSchema, options) {
|
|
515
|
-
|
|
516
|
-
const { task } = options !== null && options !== void 0 ? options : {};
|
|
510
|
+
const { task } = options ?? {};
|
|
517
511
|
// For non-task requests, just yield the result
|
|
518
512
|
if (!task) {
|
|
519
513
|
try {
|
|
@@ -576,10 +570,10 @@ export class Protocol {
|
|
|
576
570
|
return;
|
|
577
571
|
}
|
|
578
572
|
// Wait before polling again
|
|
579
|
-
const pollInterval =
|
|
573
|
+
const pollInterval = task.pollInterval ?? this._options?.defaultTaskPollInterval ?? 1000;
|
|
580
574
|
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
581
575
|
// Check if cancelled
|
|
582
|
-
|
|
576
|
+
options?.signal?.throwIfAborted();
|
|
583
577
|
}
|
|
584
578
|
}
|
|
585
579
|
catch (error) {
|
|
@@ -595,10 +589,9 @@ export class Protocol {
|
|
|
595
589
|
* Do not use this method to emit notifications! Use notification() instead.
|
|
596
590
|
*/
|
|
597
591
|
request(request, resultSchema, options) {
|
|
598
|
-
const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options
|
|
592
|
+
const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options ?? {};
|
|
599
593
|
// Send the request
|
|
600
594
|
return new Promise((resolve, reject) => {
|
|
601
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
602
595
|
const earlyReject = (error) => {
|
|
603
596
|
reject(error);
|
|
604
597
|
};
|
|
@@ -606,7 +599,7 @@ export class Protocol {
|
|
|
606
599
|
earlyReject(new Error('Not connected'));
|
|
607
600
|
return;
|
|
608
601
|
}
|
|
609
|
-
if (
|
|
602
|
+
if (this._options?.enforceStrictCapabilities === true) {
|
|
610
603
|
try {
|
|
611
604
|
this.assertCapabilityForMethod(request.method);
|
|
612
605
|
// If task creation is requested, also check task capabilities
|
|
@@ -619,19 +612,19 @@ export class Protocol {
|
|
|
619
612
|
return;
|
|
620
613
|
}
|
|
621
614
|
}
|
|
622
|
-
|
|
615
|
+
options?.signal?.throwIfAborted();
|
|
623
616
|
const messageId = this._requestMessageId++;
|
|
624
617
|
const jsonrpcRequest = {
|
|
625
618
|
...request,
|
|
626
619
|
jsonrpc: '2.0',
|
|
627
620
|
id: messageId
|
|
628
621
|
};
|
|
629
|
-
if (options
|
|
622
|
+
if (options?.onprogress) {
|
|
630
623
|
this._progressHandlers.set(messageId, options.onprogress);
|
|
631
624
|
jsonrpcRequest.params = {
|
|
632
625
|
...request.params,
|
|
633
626
|
_meta: {
|
|
634
|
-
...(
|
|
627
|
+
...(request.params?._meta || {}),
|
|
635
628
|
progressToken: messageId
|
|
636
629
|
}
|
|
637
630
|
};
|
|
@@ -648,31 +641,31 @@ export class Protocol {
|
|
|
648
641
|
jsonrpcRequest.params = {
|
|
649
642
|
...jsonrpcRequest.params,
|
|
650
643
|
_meta: {
|
|
651
|
-
...(
|
|
644
|
+
...(jsonrpcRequest.params?._meta || {}),
|
|
652
645
|
[RELATED_TASK_META_KEY]: relatedTask
|
|
653
646
|
}
|
|
654
647
|
};
|
|
655
648
|
}
|
|
656
649
|
const cancel = (reason) => {
|
|
657
|
-
var _a;
|
|
658
650
|
this._responseHandlers.delete(messageId);
|
|
659
651
|
this._progressHandlers.delete(messageId);
|
|
660
652
|
this._cleanupTimeout(messageId);
|
|
661
|
-
|
|
653
|
+
this._transport
|
|
654
|
+
?.send({
|
|
662
655
|
jsonrpc: '2.0',
|
|
663
656
|
method: 'notifications/cancelled',
|
|
664
657
|
params: {
|
|
665
658
|
requestId: messageId,
|
|
666
659
|
reason: String(reason)
|
|
667
660
|
}
|
|
668
|
-
}, { relatedRequestId, resumptionToken, onresumptiontoken })
|
|
661
|
+
}, { relatedRequestId, resumptionToken, onresumptiontoken })
|
|
662
|
+
.catch(error => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
|
|
669
663
|
// Wrap the reason in an McpError if it isn't already
|
|
670
664
|
const error = reason instanceof McpError ? reason : new McpError(ErrorCode.RequestTimeout, String(reason));
|
|
671
665
|
reject(error);
|
|
672
666
|
};
|
|
673
667
|
this._responseHandlers.set(messageId, response => {
|
|
674
|
-
|
|
675
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
668
|
+
if (options?.signal?.aborted) {
|
|
676
669
|
return;
|
|
677
670
|
}
|
|
678
671
|
if (response instanceof Error) {
|
|
@@ -692,15 +685,14 @@ export class Protocol {
|
|
|
692
685
|
reject(error);
|
|
693
686
|
}
|
|
694
687
|
});
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
cancel((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.reason);
|
|
688
|
+
options?.signal?.addEventListener('abort', () => {
|
|
689
|
+
cancel(options?.signal?.reason);
|
|
698
690
|
});
|
|
699
|
-
const timeout =
|
|
691
|
+
const timeout = options?.timeout ?? DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
700
692
|
const timeoutHandler = () => cancel(McpError.fromError(ErrorCode.RequestTimeout, 'Request timed out', { timeout }));
|
|
701
|
-
this._setupTimeout(messageId, timeout, options
|
|
693
|
+
this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
|
|
702
694
|
// Queue request if related to a task
|
|
703
|
-
const relatedTaskId = relatedTask
|
|
695
|
+
const relatedTaskId = relatedTask?.taskId;
|
|
704
696
|
if (relatedTaskId) {
|
|
705
697
|
// Store the response resolver for this request so responses can be routed back
|
|
706
698
|
const responseResolver = (response) => {
|
|
@@ -774,13 +766,12 @@ export class Protocol {
|
|
|
774
766
|
* Emits a notification, which is a one-way message that does not expect a response.
|
|
775
767
|
*/
|
|
776
768
|
async notification(notification, options) {
|
|
777
|
-
var _a, _b, _c, _d, _e;
|
|
778
769
|
if (!this._transport) {
|
|
779
770
|
throw new Error('Not connected');
|
|
780
771
|
}
|
|
781
772
|
this.assertNotificationCapability(notification.method);
|
|
782
773
|
// Queue notification if related to a task
|
|
783
|
-
const relatedTaskId =
|
|
774
|
+
const relatedTaskId = options?.relatedTask?.taskId;
|
|
784
775
|
if (relatedTaskId) {
|
|
785
776
|
// Build the JSONRPC notification with metadata
|
|
786
777
|
const jsonrpcNotification = {
|
|
@@ -789,7 +780,7 @@ export class Protocol {
|
|
|
789
780
|
params: {
|
|
790
781
|
...notification.params,
|
|
791
782
|
_meta: {
|
|
792
|
-
...(
|
|
783
|
+
...(notification.params?._meta || {}),
|
|
793
784
|
[RELATED_TASK_META_KEY]: options.relatedTask
|
|
794
785
|
}
|
|
795
786
|
}
|
|
@@ -803,10 +794,10 @@ export class Protocol {
|
|
|
803
794
|
// This prevents duplicate delivery for bidirectional transports
|
|
804
795
|
return;
|
|
805
796
|
}
|
|
806
|
-
const debouncedMethods =
|
|
797
|
+
const debouncedMethods = this._options?.debouncedNotificationMethods ?? [];
|
|
807
798
|
// A notification can only be debounced if it's in the list AND it's "simple"
|
|
808
799
|
// (i.e., has no parameters and no related request ID or related task that could be lost).
|
|
809
|
-
const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !
|
|
800
|
+
const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !options?.relatedRequestId && !options?.relatedTask;
|
|
810
801
|
if (canDebounce) {
|
|
811
802
|
// If a notification of this type is already scheduled, do nothing.
|
|
812
803
|
if (this._pendingDebouncedNotifications.has(notification.method)) {
|
|
@@ -817,7 +808,6 @@ export class Protocol {
|
|
|
817
808
|
// Schedule the actual send to happen in the next microtask.
|
|
818
809
|
// This allows all synchronous calls in the current event loop tick to be coalesced.
|
|
819
810
|
Promise.resolve().then(() => {
|
|
820
|
-
var _a, _b;
|
|
821
811
|
// Un-mark the notification so the next one can be scheduled.
|
|
822
812
|
this._pendingDebouncedNotifications.delete(notification.method);
|
|
823
813
|
// SAFETY CHECK: If the connection was closed while this was pending, abort.
|
|
@@ -829,13 +819,13 @@ export class Protocol {
|
|
|
829
819
|
jsonrpc: '2.0'
|
|
830
820
|
};
|
|
831
821
|
// Augment with related task metadata if relatedTask is provided
|
|
832
|
-
if (options
|
|
822
|
+
if (options?.relatedTask) {
|
|
833
823
|
jsonrpcNotification = {
|
|
834
824
|
...jsonrpcNotification,
|
|
835
825
|
params: {
|
|
836
826
|
...jsonrpcNotification.params,
|
|
837
827
|
_meta: {
|
|
838
|
-
...(
|
|
828
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
839
829
|
[RELATED_TASK_META_KEY]: options.relatedTask
|
|
840
830
|
}
|
|
841
831
|
}
|
|
@@ -843,7 +833,7 @@ export class Protocol {
|
|
|
843
833
|
}
|
|
844
834
|
// Send the notification, but don't await it here to avoid blocking.
|
|
845
835
|
// Handle potential errors with a .catch().
|
|
846
|
-
|
|
836
|
+
this._transport?.send(jsonrpcNotification, options).catch(error => this._onerror(error));
|
|
847
837
|
});
|
|
848
838
|
// Return immediately.
|
|
849
839
|
return;
|
|
@@ -853,13 +843,13 @@ export class Protocol {
|
|
|
853
843
|
jsonrpc: '2.0'
|
|
854
844
|
};
|
|
855
845
|
// Augment with related task metadata if relatedTask is provided
|
|
856
|
-
if (options
|
|
846
|
+
if (options?.relatedTask) {
|
|
857
847
|
jsonrpcNotification = {
|
|
858
848
|
...jsonrpcNotification,
|
|
859
849
|
params: {
|
|
860
850
|
...jsonrpcNotification.params,
|
|
861
851
|
_meta: {
|
|
862
|
-
...(
|
|
852
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
863
853
|
[RELATED_TASK_META_KEY]: options.relatedTask
|
|
864
854
|
}
|
|
865
855
|
}
|
|
@@ -935,12 +925,11 @@ export class Protocol {
|
|
|
935
925
|
* simply propagates the error.
|
|
936
926
|
*/
|
|
937
927
|
async _enqueueTaskMessage(taskId, message, sessionId) {
|
|
938
|
-
var _a;
|
|
939
928
|
// Task message queues are only used when taskStore is configured
|
|
940
929
|
if (!this._taskStore || !this._taskMessageQueue) {
|
|
941
930
|
throw new Error('Cannot enqueue task message: taskStore and taskMessageQueue are not configured');
|
|
942
931
|
}
|
|
943
|
-
const maxQueueSize =
|
|
932
|
+
const maxQueueSize = this._options?.maxTaskQueueSize;
|
|
944
933
|
await this._taskMessageQueue.enqueue(taskId, message, sessionId, maxQueueSize);
|
|
945
934
|
}
|
|
946
935
|
/**
|
|
@@ -977,16 +966,15 @@ export class Protocol {
|
|
|
977
966
|
* @returns Promise that resolves when an update occurs or rejects if aborted
|
|
978
967
|
*/
|
|
979
968
|
async _waitForTaskUpdate(taskId, signal) {
|
|
980
|
-
var _a, _b, _c;
|
|
981
969
|
// Get the task's poll interval, falling back to default
|
|
982
|
-
let interval =
|
|
970
|
+
let interval = this._options?.defaultTaskPollInterval ?? 1000;
|
|
983
971
|
try {
|
|
984
|
-
const task = await
|
|
985
|
-
if (task
|
|
972
|
+
const task = await this._taskStore?.getTask(taskId);
|
|
973
|
+
if (task?.pollInterval) {
|
|
986
974
|
interval = task.pollInterval;
|
|
987
975
|
}
|
|
988
976
|
}
|
|
989
|
-
catch
|
|
977
|
+
catch {
|
|
990
978
|
// Use default interval if task lookup fails
|
|
991
979
|
}
|
|
992
980
|
return new Promise((resolve, reject) => {
|