@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
|
@@ -38,8 +38,8 @@ class Protocol {
|
|
|
38
38
|
// Automatic pong by default.
|
|
39
39
|
_request => ({}));
|
|
40
40
|
// Install task handlers if TaskStore is provided
|
|
41
|
-
this._taskStore = _options
|
|
42
|
-
this._taskMessageQueue = _options
|
|
41
|
+
this._taskStore = _options?.taskStore;
|
|
42
|
+
this._taskMessageQueue = _options?.taskMessageQueue;
|
|
43
43
|
if (this._taskStore) {
|
|
44
44
|
this.setRequestHandler(types_js_1.GetTaskRequestSchema, async (request, extra) => {
|
|
45
45
|
const task = await this._taskStore.getTask(request.params.taskId, extra.sessionId);
|
|
@@ -55,7 +55,6 @@ class Protocol {
|
|
|
55
55
|
});
|
|
56
56
|
this.setRequestHandler(types_js_1.GetTaskPayloadRequestSchema, async (request, extra) => {
|
|
57
57
|
const handleTaskResult = async () => {
|
|
58
|
-
var _a;
|
|
59
58
|
const taskId = request.params.taskId;
|
|
60
59
|
// Deliver queued messages
|
|
61
60
|
if (this._taskMessageQueue) {
|
|
@@ -91,7 +90,7 @@ class Protocol {
|
|
|
91
90
|
}
|
|
92
91
|
// Send the message on the response stream by passing the relatedRequestId
|
|
93
92
|
// This tells the transport to write the message to the tasks/result response stream
|
|
94
|
-
await
|
|
93
|
+
await this._transport?.send(queuedMessage.message, { relatedRequestId: extra.requestId });
|
|
95
94
|
}
|
|
96
95
|
}
|
|
97
96
|
// Now check task status
|
|
@@ -125,9 +124,8 @@ class Protocol {
|
|
|
125
124
|
return await handleTaskResult();
|
|
126
125
|
});
|
|
127
126
|
this.setRequestHandler(types_js_1.ListTasksRequestSchema, async (request, extra) => {
|
|
128
|
-
var _a;
|
|
129
127
|
try {
|
|
130
|
-
const { tasks, nextCursor } = await this._taskStore.listTasks(
|
|
128
|
+
const { tasks, nextCursor } = await this._taskStore.listTasks(request.params?.cursor, extra.sessionId);
|
|
131
129
|
// @ts-expect-error SendResultT cannot contain ListTasksResult, but we include it in our derived types everywhere else
|
|
132
130
|
return {
|
|
133
131
|
tasks,
|
|
@@ -173,9 +171,12 @@ class Protocol {
|
|
|
173
171
|
}
|
|
174
172
|
}
|
|
175
173
|
async _oncancel(notification) {
|
|
174
|
+
if (!notification.params.requestId) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
176
177
|
// Handle request cancellation
|
|
177
178
|
const controller = this._requestHandlerAbortControllers.get(notification.params.requestId);
|
|
178
|
-
controller
|
|
179
|
+
controller?.abort(notification.params.reason);
|
|
179
180
|
}
|
|
180
181
|
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
|
|
181
182
|
this._timeoutInfo.set(messageId, {
|
|
@@ -216,22 +217,21 @@ class Protocol {
|
|
|
216
217
|
* 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.
|
|
217
218
|
*/
|
|
218
219
|
async connect(transport) {
|
|
219
|
-
var _a, _b, _c;
|
|
220
220
|
this._transport = transport;
|
|
221
|
-
const _onclose =
|
|
221
|
+
const _onclose = this.transport?.onclose;
|
|
222
222
|
this._transport.onclose = () => {
|
|
223
|
-
_onclose
|
|
223
|
+
_onclose?.();
|
|
224
224
|
this._onclose();
|
|
225
225
|
};
|
|
226
|
-
const _onerror =
|
|
226
|
+
const _onerror = this.transport?.onerror;
|
|
227
227
|
this._transport.onerror = (error) => {
|
|
228
|
-
_onerror
|
|
228
|
+
_onerror?.(error);
|
|
229
229
|
this._onerror(error);
|
|
230
230
|
};
|
|
231
|
-
const _onmessage =
|
|
231
|
+
const _onmessage = this._transport?.onmessage;
|
|
232
232
|
this._transport.onmessage = (message, extra) => {
|
|
233
|
-
_onmessage
|
|
234
|
-
if ((0, types_js_1.
|
|
233
|
+
_onmessage?.(message, extra);
|
|
234
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(message) || (0, types_js_1.isJSONRPCErrorResponse)(message)) {
|
|
235
235
|
this._onresponse(message);
|
|
236
236
|
}
|
|
237
237
|
else if ((0, types_js_1.isJSONRPCRequest)(message)) {
|
|
@@ -247,7 +247,6 @@ class Protocol {
|
|
|
247
247
|
await this._transport.start();
|
|
248
248
|
}
|
|
249
249
|
_onclose() {
|
|
250
|
-
var _a;
|
|
251
250
|
const responseHandlers = this._responseHandlers;
|
|
252
251
|
this._responseHandlers = new Map();
|
|
253
252
|
this._progressHandlers.clear();
|
|
@@ -255,18 +254,16 @@ class Protocol {
|
|
|
255
254
|
this._pendingDebouncedNotifications.clear();
|
|
256
255
|
const error = types_js_1.McpError.fromError(types_js_1.ErrorCode.ConnectionClosed, 'Connection closed');
|
|
257
256
|
this._transport = undefined;
|
|
258
|
-
|
|
257
|
+
this.onclose?.();
|
|
259
258
|
for (const handler of responseHandlers.values()) {
|
|
260
259
|
handler(error);
|
|
261
260
|
}
|
|
262
261
|
}
|
|
263
262
|
_onerror(error) {
|
|
264
|
-
|
|
265
|
-
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
263
|
+
this.onerror?.(error);
|
|
266
264
|
}
|
|
267
265
|
_onnotification(notification) {
|
|
268
|
-
|
|
269
|
-
const handler = (_a = this._notificationHandlers.get(notification.method)) !== null && _a !== void 0 ? _a : this.fallbackNotificationHandler;
|
|
266
|
+
const handler = this._notificationHandlers.get(notification.method) ?? this.fallbackNotificationHandler;
|
|
270
267
|
// Ignore notifications not being subscribed to.
|
|
271
268
|
if (handler === undefined) {
|
|
272
269
|
return;
|
|
@@ -277,12 +274,11 @@ class Protocol {
|
|
|
277
274
|
.catch(error => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
|
|
278
275
|
}
|
|
279
276
|
_onrequest(request, extra) {
|
|
280
|
-
|
|
281
|
-
const handler = (_a = this._requestHandlers.get(request.method)) !== null && _a !== void 0 ? _a : this.fallbackRequestHandler;
|
|
277
|
+
const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
|
|
282
278
|
// Capture the current transport at request time to ensure responses go to the correct client
|
|
283
279
|
const capturedTransport = this._transport;
|
|
284
280
|
// Extract taskId from request metadata if present (needed early for method not found case)
|
|
285
|
-
const relatedTaskId =
|
|
281
|
+
const relatedTaskId = request.params?._meta?.[types_js_1.RELATED_TASK_META_KEY]?.taskId;
|
|
286
282
|
if (handler === undefined) {
|
|
287
283
|
const errorResponse = {
|
|
288
284
|
jsonrpc: '2.0',
|
|
@@ -298,21 +294,23 @@ class Protocol {
|
|
|
298
294
|
type: 'error',
|
|
299
295
|
message: errorResponse,
|
|
300
296
|
timestamp: Date.now()
|
|
301
|
-
}, capturedTransport
|
|
297
|
+
}, capturedTransport?.sessionId).catch(error => this._onerror(new Error(`Failed to enqueue error response: ${error}`)));
|
|
302
298
|
}
|
|
303
299
|
else {
|
|
304
|
-
capturedTransport
|
|
300
|
+
capturedTransport
|
|
301
|
+
?.send(errorResponse)
|
|
302
|
+
.catch(error => this._onerror(new Error(`Failed to send an error response: ${error}`)));
|
|
305
303
|
}
|
|
306
304
|
return;
|
|
307
305
|
}
|
|
308
306
|
const abortController = new AbortController();
|
|
309
307
|
this._requestHandlerAbortControllers.set(request.id, abortController);
|
|
310
|
-
const taskCreationParams = (
|
|
311
|
-
const taskStore = this._taskStore ? this.requestTaskStore(request, capturedTransport
|
|
308
|
+
const taskCreationParams = (0, types_js_1.isTaskAugmentedRequestParams)(request.params) ? request.params.task : undefined;
|
|
309
|
+
const taskStore = this._taskStore ? this.requestTaskStore(request, capturedTransport?.sessionId) : undefined;
|
|
312
310
|
const fullExtra = {
|
|
313
311
|
signal: abortController.signal,
|
|
314
|
-
sessionId: capturedTransport
|
|
315
|
-
_meta:
|
|
312
|
+
sessionId: capturedTransport?.sessionId,
|
|
313
|
+
_meta: request.params?._meta,
|
|
316
314
|
sendNotification: async (notification) => {
|
|
317
315
|
// Include related-task metadata if this request is part of a task
|
|
318
316
|
const notificationOptions = { relatedRequestId: request.id };
|
|
@@ -322,7 +320,6 @@ class Protocol {
|
|
|
322
320
|
await this.notification(notification, notificationOptions);
|
|
323
321
|
},
|
|
324
322
|
sendRequest: async (r, resultSchema, options) => {
|
|
325
|
-
var _a, _b;
|
|
326
323
|
// Include related-task metadata if this request is part of a task
|
|
327
324
|
const requestOptions = { ...options, relatedRequestId: request.id };
|
|
328
325
|
if (relatedTaskId && !requestOptions.relatedTask) {
|
|
@@ -330,20 +327,20 @@ class Protocol {
|
|
|
330
327
|
}
|
|
331
328
|
// Set task status to input_required when sending a request within a task context
|
|
332
329
|
// Use the taskId from options (explicit) or fall back to relatedTaskId (inherited)
|
|
333
|
-
const effectiveTaskId =
|
|
330
|
+
const effectiveTaskId = requestOptions.relatedTask?.taskId ?? relatedTaskId;
|
|
334
331
|
if (effectiveTaskId && taskStore) {
|
|
335
332
|
await taskStore.updateTaskStatus(effectiveTaskId, 'input_required');
|
|
336
333
|
}
|
|
337
334
|
return await this.request(r, resultSchema, requestOptions);
|
|
338
335
|
},
|
|
339
|
-
authInfo: extra
|
|
336
|
+
authInfo: extra?.authInfo,
|
|
340
337
|
requestId: request.id,
|
|
341
|
-
requestInfo: extra
|
|
338
|
+
requestInfo: extra?.requestInfo,
|
|
342
339
|
taskId: relatedTaskId,
|
|
343
340
|
taskStore: taskStore,
|
|
344
|
-
taskRequestedTtl: taskCreationParams
|
|
345
|
-
closeSSEStream: extra
|
|
346
|
-
closeStandaloneSSEStream: extra
|
|
341
|
+
taskRequestedTtl: taskCreationParams?.ttl,
|
|
342
|
+
closeSSEStream: extra?.closeSSEStream,
|
|
343
|
+
closeStandaloneSSEStream: extra?.closeStandaloneSSEStream
|
|
347
344
|
};
|
|
348
345
|
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
|
|
349
346
|
Promise.resolve()
|
|
@@ -371,13 +368,12 @@ class Protocol {
|
|
|
371
368
|
type: 'response',
|
|
372
369
|
message: response,
|
|
373
370
|
timestamp: Date.now()
|
|
374
|
-
}, capturedTransport
|
|
371
|
+
}, capturedTransport?.sessionId);
|
|
375
372
|
}
|
|
376
373
|
else {
|
|
377
|
-
await
|
|
374
|
+
await capturedTransport?.send(response);
|
|
378
375
|
}
|
|
379
376
|
}, async (error) => {
|
|
380
|
-
var _a;
|
|
381
377
|
if (abortController.signal.aborted) {
|
|
382
378
|
// Request was cancelled
|
|
383
379
|
return;
|
|
@@ -387,7 +383,7 @@ class Protocol {
|
|
|
387
383
|
id: request.id,
|
|
388
384
|
error: {
|
|
389
385
|
code: Number.isSafeInteger(error['code']) ? error['code'] : types_js_1.ErrorCode.InternalError,
|
|
390
|
-
message:
|
|
386
|
+
message: error.message ?? 'Internal error',
|
|
391
387
|
...(error['data'] !== undefined && { data: error['data'] })
|
|
392
388
|
}
|
|
393
389
|
};
|
|
@@ -397,10 +393,10 @@ class Protocol {
|
|
|
397
393
|
type: 'error',
|
|
398
394
|
message: errorResponse,
|
|
399
395
|
timestamp: Date.now()
|
|
400
|
-
}, capturedTransport
|
|
396
|
+
}, capturedTransport?.sessionId);
|
|
401
397
|
}
|
|
402
398
|
else {
|
|
403
|
-
await
|
|
399
|
+
await capturedTransport?.send(errorResponse);
|
|
404
400
|
}
|
|
405
401
|
})
|
|
406
402
|
.catch(error => this._onerror(new Error(`Failed to send response: ${error}`)))
|
|
@@ -439,7 +435,7 @@ class Protocol {
|
|
|
439
435
|
const resolver = this._requestResolvers.get(messageId);
|
|
440
436
|
if (resolver) {
|
|
441
437
|
this._requestResolvers.delete(messageId);
|
|
442
|
-
if ((0, types_js_1.
|
|
438
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response)) {
|
|
443
439
|
resolver(response);
|
|
444
440
|
}
|
|
445
441
|
else {
|
|
@@ -457,7 +453,7 @@ class Protocol {
|
|
|
457
453
|
this._cleanupTimeout(messageId);
|
|
458
454
|
// Keep progress handler alive for CreateTaskResult responses
|
|
459
455
|
let isTaskResponse = false;
|
|
460
|
-
if ((0, types_js_1.
|
|
456
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response) && response.result && typeof response.result === 'object') {
|
|
461
457
|
const result = response.result;
|
|
462
458
|
if (result.task && typeof result.task === 'object') {
|
|
463
459
|
const task = result.task;
|
|
@@ -470,7 +466,7 @@ class Protocol {
|
|
|
470
466
|
if (!isTaskResponse) {
|
|
471
467
|
this._progressHandlers.delete(messageId);
|
|
472
468
|
}
|
|
473
|
-
if ((0, types_js_1.
|
|
469
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response)) {
|
|
474
470
|
handler(response);
|
|
475
471
|
}
|
|
476
472
|
else {
|
|
@@ -485,8 +481,7 @@ class Protocol {
|
|
|
485
481
|
* Closes the connection.
|
|
486
482
|
*/
|
|
487
483
|
async close() {
|
|
488
|
-
|
|
489
|
-
await ((_a = this._transport) === null || _a === void 0 ? void 0 : _a.close());
|
|
484
|
+
await this._transport?.close();
|
|
490
485
|
}
|
|
491
486
|
/**
|
|
492
487
|
* Sends a request and returns an AsyncGenerator that yields response messages.
|
|
@@ -516,8 +511,7 @@ class Protocol {
|
|
|
516
511
|
* @experimental Use `client.experimental.tasks.requestStream()` to access this method.
|
|
517
512
|
*/
|
|
518
513
|
async *requestStream(request, resultSchema, options) {
|
|
519
|
-
|
|
520
|
-
const { task } = options !== null && options !== void 0 ? options : {};
|
|
514
|
+
const { task } = options ?? {};
|
|
521
515
|
// For non-task requests, just yield the result
|
|
522
516
|
if (!task) {
|
|
523
517
|
try {
|
|
@@ -580,10 +574,10 @@ class Protocol {
|
|
|
580
574
|
return;
|
|
581
575
|
}
|
|
582
576
|
// Wait before polling again
|
|
583
|
-
const pollInterval =
|
|
577
|
+
const pollInterval = task.pollInterval ?? this._options?.defaultTaskPollInterval ?? 1000;
|
|
584
578
|
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
585
579
|
// Check if cancelled
|
|
586
|
-
|
|
580
|
+
options?.signal?.throwIfAborted();
|
|
587
581
|
}
|
|
588
582
|
}
|
|
589
583
|
catch (error) {
|
|
@@ -599,10 +593,9 @@ class Protocol {
|
|
|
599
593
|
* Do not use this method to emit notifications! Use notification() instead.
|
|
600
594
|
*/
|
|
601
595
|
request(request, resultSchema, options) {
|
|
602
|
-
const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options
|
|
596
|
+
const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options ?? {};
|
|
603
597
|
// Send the request
|
|
604
598
|
return new Promise((resolve, reject) => {
|
|
605
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
606
599
|
const earlyReject = (error) => {
|
|
607
600
|
reject(error);
|
|
608
601
|
};
|
|
@@ -610,7 +603,7 @@ class Protocol {
|
|
|
610
603
|
earlyReject(new Error('Not connected'));
|
|
611
604
|
return;
|
|
612
605
|
}
|
|
613
|
-
if (
|
|
606
|
+
if (this._options?.enforceStrictCapabilities === true) {
|
|
614
607
|
try {
|
|
615
608
|
this.assertCapabilityForMethod(request.method);
|
|
616
609
|
// If task creation is requested, also check task capabilities
|
|
@@ -623,19 +616,19 @@ class Protocol {
|
|
|
623
616
|
return;
|
|
624
617
|
}
|
|
625
618
|
}
|
|
626
|
-
|
|
619
|
+
options?.signal?.throwIfAborted();
|
|
627
620
|
const messageId = this._requestMessageId++;
|
|
628
621
|
const jsonrpcRequest = {
|
|
629
622
|
...request,
|
|
630
623
|
jsonrpc: '2.0',
|
|
631
624
|
id: messageId
|
|
632
625
|
};
|
|
633
|
-
if (options
|
|
626
|
+
if (options?.onprogress) {
|
|
634
627
|
this._progressHandlers.set(messageId, options.onprogress);
|
|
635
628
|
jsonrpcRequest.params = {
|
|
636
629
|
...request.params,
|
|
637
630
|
_meta: {
|
|
638
|
-
...(
|
|
631
|
+
...(request.params?._meta || {}),
|
|
639
632
|
progressToken: messageId
|
|
640
633
|
}
|
|
641
634
|
};
|
|
@@ -652,31 +645,31 @@ class Protocol {
|
|
|
652
645
|
jsonrpcRequest.params = {
|
|
653
646
|
...jsonrpcRequest.params,
|
|
654
647
|
_meta: {
|
|
655
|
-
...(
|
|
648
|
+
...(jsonrpcRequest.params?._meta || {}),
|
|
656
649
|
[types_js_1.RELATED_TASK_META_KEY]: relatedTask
|
|
657
650
|
}
|
|
658
651
|
};
|
|
659
652
|
}
|
|
660
653
|
const cancel = (reason) => {
|
|
661
|
-
var _a;
|
|
662
654
|
this._responseHandlers.delete(messageId);
|
|
663
655
|
this._progressHandlers.delete(messageId);
|
|
664
656
|
this._cleanupTimeout(messageId);
|
|
665
|
-
|
|
657
|
+
this._transport
|
|
658
|
+
?.send({
|
|
666
659
|
jsonrpc: '2.0',
|
|
667
660
|
method: 'notifications/cancelled',
|
|
668
661
|
params: {
|
|
669
662
|
requestId: messageId,
|
|
670
663
|
reason: String(reason)
|
|
671
664
|
}
|
|
672
|
-
}, { relatedRequestId, resumptionToken, onresumptiontoken })
|
|
665
|
+
}, { relatedRequestId, resumptionToken, onresumptiontoken })
|
|
666
|
+
.catch(error => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
|
|
673
667
|
// Wrap the reason in an McpError if it isn't already
|
|
674
668
|
const error = reason instanceof types_js_1.McpError ? reason : new types_js_1.McpError(types_js_1.ErrorCode.RequestTimeout, String(reason));
|
|
675
669
|
reject(error);
|
|
676
670
|
};
|
|
677
671
|
this._responseHandlers.set(messageId, response => {
|
|
678
|
-
|
|
679
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
672
|
+
if (options?.signal?.aborted) {
|
|
680
673
|
return;
|
|
681
674
|
}
|
|
682
675
|
if (response instanceof Error) {
|
|
@@ -696,15 +689,14 @@ class Protocol {
|
|
|
696
689
|
reject(error);
|
|
697
690
|
}
|
|
698
691
|
});
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
cancel((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.reason);
|
|
692
|
+
options?.signal?.addEventListener('abort', () => {
|
|
693
|
+
cancel(options?.signal?.reason);
|
|
702
694
|
});
|
|
703
|
-
const timeout =
|
|
695
|
+
const timeout = options?.timeout ?? exports.DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
704
696
|
const timeoutHandler = () => cancel(types_js_1.McpError.fromError(types_js_1.ErrorCode.RequestTimeout, 'Request timed out', { timeout }));
|
|
705
|
-
this._setupTimeout(messageId, timeout, options
|
|
697
|
+
this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
|
|
706
698
|
// Queue request if related to a task
|
|
707
|
-
const relatedTaskId = relatedTask
|
|
699
|
+
const relatedTaskId = relatedTask?.taskId;
|
|
708
700
|
if (relatedTaskId) {
|
|
709
701
|
// Store the response resolver for this request so responses can be routed back
|
|
710
702
|
const responseResolver = (response) => {
|
|
@@ -778,13 +770,12 @@ class Protocol {
|
|
|
778
770
|
* Emits a notification, which is a one-way message that does not expect a response.
|
|
779
771
|
*/
|
|
780
772
|
async notification(notification, options) {
|
|
781
|
-
var _a, _b, _c, _d, _e;
|
|
782
773
|
if (!this._transport) {
|
|
783
774
|
throw new Error('Not connected');
|
|
784
775
|
}
|
|
785
776
|
this.assertNotificationCapability(notification.method);
|
|
786
777
|
// Queue notification if related to a task
|
|
787
|
-
const relatedTaskId =
|
|
778
|
+
const relatedTaskId = options?.relatedTask?.taskId;
|
|
788
779
|
if (relatedTaskId) {
|
|
789
780
|
// Build the JSONRPC notification with metadata
|
|
790
781
|
const jsonrpcNotification = {
|
|
@@ -793,7 +784,7 @@ class Protocol {
|
|
|
793
784
|
params: {
|
|
794
785
|
...notification.params,
|
|
795
786
|
_meta: {
|
|
796
|
-
...(
|
|
787
|
+
...(notification.params?._meta || {}),
|
|
797
788
|
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
798
789
|
}
|
|
799
790
|
}
|
|
@@ -807,10 +798,10 @@ class Protocol {
|
|
|
807
798
|
// This prevents duplicate delivery for bidirectional transports
|
|
808
799
|
return;
|
|
809
800
|
}
|
|
810
|
-
const debouncedMethods =
|
|
801
|
+
const debouncedMethods = this._options?.debouncedNotificationMethods ?? [];
|
|
811
802
|
// A notification can only be debounced if it's in the list AND it's "simple"
|
|
812
803
|
// (i.e., has no parameters and no related request ID or related task that could be lost).
|
|
813
|
-
const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !
|
|
804
|
+
const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !options?.relatedRequestId && !options?.relatedTask;
|
|
814
805
|
if (canDebounce) {
|
|
815
806
|
// If a notification of this type is already scheduled, do nothing.
|
|
816
807
|
if (this._pendingDebouncedNotifications.has(notification.method)) {
|
|
@@ -821,7 +812,6 @@ class Protocol {
|
|
|
821
812
|
// Schedule the actual send to happen in the next microtask.
|
|
822
813
|
// This allows all synchronous calls in the current event loop tick to be coalesced.
|
|
823
814
|
Promise.resolve().then(() => {
|
|
824
|
-
var _a, _b;
|
|
825
815
|
// Un-mark the notification so the next one can be scheduled.
|
|
826
816
|
this._pendingDebouncedNotifications.delete(notification.method);
|
|
827
817
|
// SAFETY CHECK: If the connection was closed while this was pending, abort.
|
|
@@ -833,13 +823,13 @@ class Protocol {
|
|
|
833
823
|
jsonrpc: '2.0'
|
|
834
824
|
};
|
|
835
825
|
// Augment with related task metadata if relatedTask is provided
|
|
836
|
-
if (options
|
|
826
|
+
if (options?.relatedTask) {
|
|
837
827
|
jsonrpcNotification = {
|
|
838
828
|
...jsonrpcNotification,
|
|
839
829
|
params: {
|
|
840
830
|
...jsonrpcNotification.params,
|
|
841
831
|
_meta: {
|
|
842
|
-
...(
|
|
832
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
843
833
|
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
844
834
|
}
|
|
845
835
|
}
|
|
@@ -847,7 +837,7 @@ class Protocol {
|
|
|
847
837
|
}
|
|
848
838
|
// Send the notification, but don't await it here to avoid blocking.
|
|
849
839
|
// Handle potential errors with a .catch().
|
|
850
|
-
|
|
840
|
+
this._transport?.send(jsonrpcNotification, options).catch(error => this._onerror(error));
|
|
851
841
|
});
|
|
852
842
|
// Return immediately.
|
|
853
843
|
return;
|
|
@@ -857,13 +847,13 @@ class Protocol {
|
|
|
857
847
|
jsonrpc: '2.0'
|
|
858
848
|
};
|
|
859
849
|
// Augment with related task metadata if relatedTask is provided
|
|
860
|
-
if (options
|
|
850
|
+
if (options?.relatedTask) {
|
|
861
851
|
jsonrpcNotification = {
|
|
862
852
|
...jsonrpcNotification,
|
|
863
853
|
params: {
|
|
864
854
|
...jsonrpcNotification.params,
|
|
865
855
|
_meta: {
|
|
866
|
-
...(
|
|
856
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
867
857
|
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
868
858
|
}
|
|
869
859
|
}
|
|
@@ -939,12 +929,11 @@ class Protocol {
|
|
|
939
929
|
* simply propagates the error.
|
|
940
930
|
*/
|
|
941
931
|
async _enqueueTaskMessage(taskId, message, sessionId) {
|
|
942
|
-
var _a;
|
|
943
932
|
// Task message queues are only used when taskStore is configured
|
|
944
933
|
if (!this._taskStore || !this._taskMessageQueue) {
|
|
945
934
|
throw new Error('Cannot enqueue task message: taskStore and taskMessageQueue are not configured');
|
|
946
935
|
}
|
|
947
|
-
const maxQueueSize =
|
|
936
|
+
const maxQueueSize = this._options?.maxTaskQueueSize;
|
|
948
937
|
await this._taskMessageQueue.enqueue(taskId, message, sessionId, maxQueueSize);
|
|
949
938
|
}
|
|
950
939
|
/**
|
|
@@ -981,16 +970,15 @@ class Protocol {
|
|
|
981
970
|
* @returns Promise that resolves when an update occurs or rejects if aborted
|
|
982
971
|
*/
|
|
983
972
|
async _waitForTaskUpdate(taskId, signal) {
|
|
984
|
-
var _a, _b, _c;
|
|
985
973
|
// Get the task's poll interval, falling back to default
|
|
986
|
-
let interval =
|
|
974
|
+
let interval = this._options?.defaultTaskPollInterval ?? 1000;
|
|
987
975
|
try {
|
|
988
|
-
const task = await
|
|
989
|
-
if (task
|
|
976
|
+
const task = await this._taskStore?.getTask(taskId);
|
|
977
|
+
if (task?.pollInterval) {
|
|
990
978
|
interval = task.pollInterval;
|
|
991
979
|
}
|
|
992
980
|
}
|
|
993
|
-
catch
|
|
981
|
+
catch {
|
|
994
982
|
// Use default interval if task lookup fails
|
|
995
983
|
}
|
|
996
984
|
return new Promise((resolve, reject) => {
|