@contextvm/mcp-sdk 1.27.1-contextvm.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/LICENSE +21 -0
- package/README.md +141 -0
- package/dist/cjs/client/index.d.ts +588 -0
- package/dist/cjs/client/index.d.ts.map +1 -0
- package/dist/cjs/client/index.js +629 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/client/stdio.d.ts +77 -0
- package/dist/cjs/client/stdio.d.ts.map +1 -0
- package/dist/cjs/client/stdio.js +199 -0
- package/dist/cjs/client/stdio.js.map +1 -0
- package/dist/cjs/experimental/index.d.ts +13 -0
- package/dist/cjs/experimental/index.d.ts.map +1 -0
- package/dist/cjs/experimental/index.js +29 -0
- package/dist/cjs/experimental/index.js.map +1 -0
- package/dist/cjs/experimental/tasks/client.d.ts +121 -0
- package/dist/cjs/experimental/tasks/client.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/client.js +188 -0
- package/dist/cjs/experimental/tasks/client.js.map +1 -0
- package/dist/cjs/experimental/tasks/helpers.d.ts +47 -0
- package/dist/cjs/experimental/tasks/helpers.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/helpers.js +68 -0
- package/dist/cjs/experimental/tasks/helpers.js.map +1 -0
- package/dist/cjs/experimental/tasks/index.d.ts +16 -0
- package/dist/cjs/experimental/tasks/index.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/index.js +39 -0
- package/dist/cjs/experimental/tasks/index.js.map +1 -0
- package/dist/cjs/experimental/tasks/interfaces.d.ts +232 -0
- package/dist/cjs/experimental/tasks/interfaces.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/interfaces.js +19 -0
- package/dist/cjs/experimental/tasks/interfaces.js.map +1 -0
- package/dist/cjs/experimental/tasks/mcp-server.d.ts +77 -0
- package/dist/cjs/experimental/tasks/mcp-server.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/mcp-server.js +36 -0
- package/dist/cjs/experimental/tasks/mcp-server.js.map +1 -0
- package/dist/cjs/experimental/tasks/server.d.ts +170 -0
- package/dist/cjs/experimental/tasks/server.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/server.js +250 -0
- package/dist/cjs/experimental/tasks/server.js.map +1 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts +94 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.js +251 -0
- package/dist/cjs/experimental/tasks/stores/in-memory.js.map +1 -0
- package/dist/cjs/experimental/tasks/types.d.ts +10 -0
- package/dist/cjs/experimental/tasks/types.d.ts.map +1 -0
- package/dist/cjs/experimental/tasks/types.js +28 -0
- package/dist/cjs/experimental/tasks/types.js.map +1 -0
- package/dist/cjs/inMemory.d.ts +31 -0
- package/dist/cjs/inMemory.d.ts.map +1 -0
- package/dist/cjs/inMemory.js +51 -0
- package/dist/cjs/inMemory.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/server/completable.d.ts +38 -0
- package/dist/cjs/server/completable.d.ts.map +1 -0
- package/dist/cjs/server/completable.js +48 -0
- package/dist/cjs/server/completable.js.map +1 -0
- package/dist/cjs/server/index.d.ts +196 -0
- package/dist/cjs/server/index.d.ts.map +1 -0
- package/dist/cjs/server/index.js +444 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/server/mcp.d.ts +364 -0
- package/dist/cjs/server/mcp.d.ts.map +1 -0
- package/dist/cjs/server/mcp.js +918 -0
- package/dist/cjs/server/mcp.js.map +1 -0
- package/dist/cjs/server/stdio.d.ts +28 -0
- package/dist/cjs/server/stdio.d.ts.map +1 -0
- package/dist/cjs/server/stdio.js +82 -0
- package/dist/cjs/server/stdio.js.map +1 -0
- package/dist/cjs/server/zod-compat.d.ts +84 -0
- package/dist/cjs/server/zod-compat.d.ts.map +1 -0
- package/dist/cjs/server/zod-compat.js +244 -0
- package/dist/cjs/server/zod-compat.js.map +1 -0
- package/dist/cjs/server/zod-json-schema-compat.d.ts +12 -0
- package/dist/cjs/server/zod-json-schema-compat.d.ts.map +1 -0
- package/dist/cjs/server/zod-json-schema-compat.js +79 -0
- package/dist/cjs/server/zod-json-schema-compat.js.map +1 -0
- package/dist/cjs/shared/auth-info.d.ts +32 -0
- package/dist/cjs/shared/auth-info.d.ts.map +1 -0
- package/dist/cjs/shared/auth-info.js +3 -0
- package/dist/cjs/shared/auth-info.js.map +1 -0
- package/dist/cjs/shared/metadataUtils.d.ts +16 -0
- package/dist/cjs/shared/metadataUtils.d.ts.map +1 -0
- package/dist/cjs/shared/metadataUtils.js +25 -0
- package/dist/cjs/shared/metadataUtils.js.map +1 -0
- package/dist/cjs/shared/protocol.d.ts +443 -0
- package/dist/cjs/shared/protocol.d.ts.map +1 -0
- package/dist/cjs/shared/protocol.js +1104 -0
- package/dist/cjs/shared/protocol.js.map +1 -0
- package/dist/cjs/shared/responseMessage.d.ts +45 -0
- package/dist/cjs/shared/responseMessage.d.ts.map +1 -0
- package/dist/cjs/shared/responseMessage.js +23 -0
- package/dist/cjs/shared/responseMessage.js.map +1 -0
- package/dist/cjs/shared/stdio.d.ts +13 -0
- package/dist/cjs/shared/stdio.d.ts.map +1 -0
- package/dist/cjs/shared/stdio.js +37 -0
- package/dist/cjs/shared/stdio.js.map +1 -0
- package/dist/cjs/shared/toolNameValidation.d.ts +31 -0
- package/dist/cjs/shared/toolNameValidation.d.ts.map +1 -0
- package/dist/cjs/shared/toolNameValidation.js +97 -0
- package/dist/cjs/shared/toolNameValidation.js.map +1 -0
- package/dist/cjs/shared/transport.d.ts +89 -0
- package/dist/cjs/shared/transport.d.ts.map +1 -0
- package/dist/cjs/shared/transport.js +43 -0
- package/dist/cjs/shared/transport.js.map +1 -0
- package/dist/cjs/shared/uriTemplate.d.ts +25 -0
- package/dist/cjs/shared/uriTemplate.d.ts.map +1 -0
- package/dist/cjs/shared/uriTemplate.js +243 -0
- package/dist/cjs/shared/uriTemplate.js.map +1 -0
- package/dist/cjs/spec.types.d.ts +2299 -0
- package/dist/cjs/spec.types.d.ts.map +1 -0
- package/dist/cjs/spec.types.js +27 -0
- package/dist/cjs/spec.types.js.map +1 -0
- package/dist/cjs/types.d.ts +8137 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +2092 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validation/ajv-provider.d.ts +53 -0
- package/dist/cjs/validation/ajv-provider.d.ts.map +1 -0
- package/dist/cjs/validation/ajv-provider.js +94 -0
- package/dist/cjs/validation/ajv-provider.js.map +1 -0
- package/dist/cjs/validation/cfworker-provider.d.ts +51 -0
- package/dist/cjs/validation/cfworker-provider.d.ts.map +1 -0
- package/dist/cjs/validation/cfworker-provider.js +69 -0
- package/dist/cjs/validation/cfworker-provider.js.map +1 -0
- package/dist/cjs/validation/index.d.ts +29 -0
- package/dist/cjs/validation/index.d.ts.map +1 -0
- package/dist/cjs/validation/index.js +30 -0
- package/dist/cjs/validation/index.js.map +1 -0
- package/dist/cjs/validation/types.d.ts +65 -0
- package/dist/cjs/validation/types.d.ts.map +1 -0
- package/dist/cjs/validation/types.js +3 -0
- package/dist/cjs/validation/types.js.map +1 -0
- package/dist/esm/client/index.d.ts +588 -0
- package/dist/esm/client/index.d.ts.map +1 -0
- package/dist/esm/client/index.js +624 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/client/stdio.d.ts +77 -0
- package/dist/esm/client/stdio.d.ts.map +1 -0
- package/dist/esm/client/stdio.js +191 -0
- package/dist/esm/client/stdio.js.map +1 -0
- package/dist/esm/experimental/index.d.ts +13 -0
- package/dist/esm/experimental/index.d.ts.map +1 -0
- package/dist/esm/experimental/index.js +13 -0
- package/dist/esm/experimental/index.js.map +1 -0
- package/dist/esm/experimental/tasks/client.d.ts +121 -0
- package/dist/esm/experimental/tasks/client.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/client.js +184 -0
- package/dist/esm/experimental/tasks/client.js.map +1 -0
- package/dist/esm/experimental/tasks/helpers.d.ts +47 -0
- package/dist/esm/experimental/tasks/helpers.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/helpers.js +64 -0
- package/dist/esm/experimental/tasks/helpers.js.map +1 -0
- package/dist/esm/experimental/tasks/index.d.ts +16 -0
- package/dist/esm/experimental/tasks/index.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/index.js +20 -0
- package/dist/esm/experimental/tasks/index.js.map +1 -0
- package/dist/esm/experimental/tasks/interfaces.d.ts +232 -0
- package/dist/esm/experimental/tasks/interfaces.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/interfaces.js +16 -0
- package/dist/esm/experimental/tasks/interfaces.js.map +1 -0
- package/dist/esm/experimental/tasks/mcp-server.d.ts +77 -0
- package/dist/esm/experimental/tasks/mcp-server.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/mcp-server.js +32 -0
- package/dist/esm/experimental/tasks/mcp-server.js.map +1 -0
- package/dist/esm/experimental/tasks/server.d.ts +170 -0
- package/dist/esm/experimental/tasks/server.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/server.js +246 -0
- package/dist/esm/experimental/tasks/server.js.map +1 -0
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts +94 -0
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/stores/in-memory.js +246 -0
- package/dist/esm/experimental/tasks/stores/in-memory.js.map +1 -0
- package/dist/esm/experimental/tasks/types.d.ts +10 -0
- package/dist/esm/experimental/tasks/types.d.ts.map +1 -0
- package/dist/esm/experimental/tasks/types.js +10 -0
- package/dist/esm/experimental/tasks/types.js.map +1 -0
- package/dist/esm/inMemory.d.ts +31 -0
- package/dist/esm/inMemory.d.ts.map +1 -0
- package/dist/esm/inMemory.js +47 -0
- package/dist/esm/inMemory.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/server/completable.d.ts +38 -0
- package/dist/esm/server/completable.d.ts.map +1 -0
- package/dist/esm/server/completable.js +41 -0
- package/dist/esm/server/completable.js.map +1 -0
- package/dist/esm/server/index.d.ts +196 -0
- package/dist/esm/server/index.d.ts.map +1 -0
- package/dist/esm/server/index.js +440 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/server/mcp.d.ts +364 -0
- package/dist/esm/server/mcp.d.ts.map +1 -0
- package/dist/esm/server/mcp.js +913 -0
- package/dist/esm/server/mcp.js.map +1 -0
- package/dist/esm/server/stdio.d.ts +28 -0
- package/dist/esm/server/stdio.d.ts.map +1 -0
- package/dist/esm/server/stdio.js +75 -0
- package/dist/esm/server/stdio.js.map +1 -0
- package/dist/esm/server/zod-compat.d.ts +84 -0
- package/dist/esm/server/zod-compat.d.ts.map +1 -0
- package/dist/esm/server/zod-compat.js +209 -0
- package/dist/esm/server/zod-compat.js.map +1 -0
- package/dist/esm/server/zod-json-schema-compat.d.ts +12 -0
- package/dist/esm/server/zod-json-schema-compat.d.ts.map +1 -0
- package/dist/esm/server/zod-json-schema-compat.js +51 -0
- package/dist/esm/server/zod-json-schema-compat.js.map +1 -0
- package/dist/esm/shared/auth-info.d.ts +32 -0
- package/dist/esm/shared/auth-info.d.ts.map +1 -0
- package/dist/esm/shared/auth-info.js +2 -0
- package/dist/esm/shared/auth-info.js.map +1 -0
- package/dist/esm/shared/metadataUtils.d.ts +16 -0
- package/dist/esm/shared/metadataUtils.d.ts.map +1 -0
- package/dist/esm/shared/metadataUtils.js +22 -0
- package/dist/esm/shared/metadataUtils.js.map +1 -0
- package/dist/esm/shared/protocol.d.ts +443 -0
- package/dist/esm/shared/protocol.d.ts.map +1 -0
- package/dist/esm/shared/protocol.js +1099 -0
- package/dist/esm/shared/protocol.js.map +1 -0
- package/dist/esm/shared/responseMessage.d.ts +45 -0
- package/dist/esm/shared/responseMessage.d.ts.map +1 -0
- package/dist/esm/shared/responseMessage.js +19 -0
- package/dist/esm/shared/responseMessage.js.map +1 -0
- package/dist/esm/shared/stdio.d.ts +13 -0
- package/dist/esm/shared/stdio.d.ts.map +1 -0
- package/dist/esm/shared/stdio.js +31 -0
- package/dist/esm/shared/stdio.js.map +1 -0
- package/dist/esm/shared/toolNameValidation.d.ts +31 -0
- package/dist/esm/shared/toolNameValidation.d.ts.map +1 -0
- package/dist/esm/shared/toolNameValidation.js +92 -0
- package/dist/esm/shared/toolNameValidation.js.map +1 -0
- package/dist/esm/shared/transport.d.ts +89 -0
- package/dist/esm/shared/transport.d.ts.map +1 -0
- package/dist/esm/shared/transport.js +39 -0
- package/dist/esm/shared/transport.js.map +1 -0
- package/dist/esm/shared/uriTemplate.d.ts +25 -0
- package/dist/esm/shared/uriTemplate.d.ts.map +1 -0
- package/dist/esm/shared/uriTemplate.js +239 -0
- package/dist/esm/shared/uriTemplate.js.map +1 -0
- package/dist/esm/spec.types.d.ts +2299 -0
- package/dist/esm/spec.types.d.ts.map +1 -0
- package/dist/esm/spec.types.js +24 -0
- package/dist/esm/spec.types.js.map +1 -0
- package/dist/esm/types.d.ts +8137 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2052 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validation/ajv-provider.d.ts +53 -0
- package/dist/esm/validation/ajv-provider.d.ts.map +1 -0
- package/dist/esm/validation/ajv-provider.js +87 -0
- package/dist/esm/validation/ajv-provider.js.map +1 -0
- package/dist/esm/validation/cfworker-provider.d.ts +51 -0
- package/dist/esm/validation/cfworker-provider.d.ts.map +1 -0
- package/dist/esm/validation/cfworker-provider.js +65 -0
- package/dist/esm/validation/cfworker-provider.js.map +1 -0
- package/dist/esm/validation/index.d.ts +29 -0
- package/dist/esm/validation/index.d.ts.map +1 -0
- package/dist/esm/validation/index.js +29 -0
- package/dist/esm/validation/index.js.map +1 -0
- package/dist/esm/validation/types.d.ts +65 -0
- package/dist/esm/validation/types.d.ts.map +1 -0
- package/dist/esm/validation/types.js +2 -0
- package/dist/esm/validation/types.js.map +1 -0
- package/package.json +124 -0
|
@@ -0,0 +1,1104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Protocol = exports.DEFAULT_REQUEST_TIMEOUT_MSEC = void 0;
|
|
4
|
+
exports.mergeCapabilities = mergeCapabilities;
|
|
5
|
+
const zod_compat_js_1 = require("../server/zod-compat.js");
|
|
6
|
+
const types_js_1 = require("../types.js");
|
|
7
|
+
const interfaces_js_1 = require("../experimental/tasks/interfaces.js");
|
|
8
|
+
const zod_json_schema_compat_js_1 = require("../server/zod-json-schema-compat.js");
|
|
9
|
+
/**
|
|
10
|
+
* The default request timeout, in miliseconds.
|
|
11
|
+
*/
|
|
12
|
+
exports.DEFAULT_REQUEST_TIMEOUT_MSEC = 60000;
|
|
13
|
+
/**
|
|
14
|
+
* Implements MCP protocol framing on top of a pluggable transport, including
|
|
15
|
+
* features like request/response linking, notifications, and progress.
|
|
16
|
+
*/
|
|
17
|
+
class Protocol {
|
|
18
|
+
constructor(_options) {
|
|
19
|
+
this._options = _options;
|
|
20
|
+
this._requestMessageId = 0;
|
|
21
|
+
this._requestHandlers = new Map();
|
|
22
|
+
this._requestHandlerAbortControllers = new Map();
|
|
23
|
+
this._notificationHandlers = new Map();
|
|
24
|
+
this._responseHandlers = new Map();
|
|
25
|
+
this._progressHandlers = new Map();
|
|
26
|
+
this._timeoutInfo = new Map();
|
|
27
|
+
this._pendingDebouncedNotifications = new Set();
|
|
28
|
+
// Maps task IDs to progress tokens to keep handlers alive after CreateTaskResult
|
|
29
|
+
this._taskProgressTokens = new Map();
|
|
30
|
+
this._requestResolvers = new Map();
|
|
31
|
+
this.setNotificationHandler(types_js_1.CancelledNotificationSchema, notification => {
|
|
32
|
+
this._oncancel(notification);
|
|
33
|
+
});
|
|
34
|
+
this.setNotificationHandler(types_js_1.ProgressNotificationSchema, notification => {
|
|
35
|
+
this._onprogress(notification);
|
|
36
|
+
});
|
|
37
|
+
this.setRequestHandler(types_js_1.PingRequestSchema,
|
|
38
|
+
// Automatic pong by default.
|
|
39
|
+
_request => ({}));
|
|
40
|
+
// Install task handlers if TaskStore is provided
|
|
41
|
+
this._taskStore = _options?.taskStore;
|
|
42
|
+
this._taskMessageQueue = _options?.taskMessageQueue;
|
|
43
|
+
if (this._taskStore) {
|
|
44
|
+
this.setRequestHandler(types_js_1.GetTaskRequestSchema, async (request, extra) => {
|
|
45
|
+
const task = await this._taskStore.getTask(request.params.taskId, extra.sessionId);
|
|
46
|
+
if (!task) {
|
|
47
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, 'Failed to retrieve task: Task not found');
|
|
48
|
+
}
|
|
49
|
+
// Per spec: tasks/get responses SHALL NOT include related-task metadata
|
|
50
|
+
// as the taskId parameter is the source of truth
|
|
51
|
+
// @ts-expect-error SendResultT cannot contain GetTaskResult, but we include it in our derived types everywhere else
|
|
52
|
+
return {
|
|
53
|
+
...task
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
this.setRequestHandler(types_js_1.GetTaskPayloadRequestSchema, async (request, extra) => {
|
|
57
|
+
const handleTaskResult = async () => {
|
|
58
|
+
const taskId = request.params.taskId;
|
|
59
|
+
// Deliver queued messages
|
|
60
|
+
if (this._taskMessageQueue) {
|
|
61
|
+
let queuedMessage;
|
|
62
|
+
while ((queuedMessage = await this._taskMessageQueue.dequeue(taskId, extra.sessionId))) {
|
|
63
|
+
// Handle response and error messages by routing them to the appropriate resolver
|
|
64
|
+
if (queuedMessage.type === 'response' || queuedMessage.type === 'error') {
|
|
65
|
+
const message = queuedMessage.message;
|
|
66
|
+
const requestId = message.id;
|
|
67
|
+
// Lookup resolver in _requestResolvers map
|
|
68
|
+
const resolver = this._requestResolvers.get(requestId);
|
|
69
|
+
if (resolver) {
|
|
70
|
+
// Remove resolver from map after invocation
|
|
71
|
+
this._requestResolvers.delete(requestId);
|
|
72
|
+
// Invoke resolver with response or error
|
|
73
|
+
if (queuedMessage.type === 'response') {
|
|
74
|
+
resolver(message);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// Convert JSONRPCError to McpError
|
|
78
|
+
const errorMessage = message;
|
|
79
|
+
const error = new types_js_1.McpError(errorMessage.error.code, errorMessage.error.message, errorMessage.error.data);
|
|
80
|
+
resolver(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Handle missing resolver gracefully with error logging
|
|
85
|
+
const messageType = queuedMessage.type === 'response' ? 'Response' : 'Error';
|
|
86
|
+
this._onerror(new Error(`${messageType} handler missing for request ${requestId}`));
|
|
87
|
+
}
|
|
88
|
+
// Continue to next message
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// Send the message on the response stream by passing the relatedRequestId
|
|
92
|
+
// This tells the transport to write the message to the tasks/result response stream
|
|
93
|
+
await this._transport?.send(queuedMessage.message, { relatedRequestId: extra.requestId });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Now check task status
|
|
97
|
+
const task = await this._taskStore.getTask(taskId, extra.sessionId);
|
|
98
|
+
if (!task) {
|
|
99
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Task not found: ${taskId}`);
|
|
100
|
+
}
|
|
101
|
+
// Block if task is not terminal (we've already delivered all queued messages above)
|
|
102
|
+
if (!(0, interfaces_js_1.isTerminal)(task.status)) {
|
|
103
|
+
// Wait for status change or new messages
|
|
104
|
+
await this._waitForTaskUpdate(taskId, extra.signal);
|
|
105
|
+
// After waking up, recursively call to deliver any new messages or result
|
|
106
|
+
return await handleTaskResult();
|
|
107
|
+
}
|
|
108
|
+
// If task is terminal, return the result
|
|
109
|
+
if ((0, interfaces_js_1.isTerminal)(task.status)) {
|
|
110
|
+
const result = await this._taskStore.getTaskResult(taskId, extra.sessionId);
|
|
111
|
+
this._clearTaskQueue(taskId);
|
|
112
|
+
return {
|
|
113
|
+
...result,
|
|
114
|
+
_meta: {
|
|
115
|
+
...result._meta,
|
|
116
|
+
[types_js_1.RELATED_TASK_META_KEY]: {
|
|
117
|
+
taskId: taskId
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return await handleTaskResult();
|
|
123
|
+
};
|
|
124
|
+
return await handleTaskResult();
|
|
125
|
+
});
|
|
126
|
+
this.setRequestHandler(types_js_1.ListTasksRequestSchema, async (request, extra) => {
|
|
127
|
+
try {
|
|
128
|
+
const { tasks, nextCursor } = await this._taskStore.listTasks(request.params?.cursor, extra.sessionId);
|
|
129
|
+
// @ts-expect-error SendResultT cannot contain ListTasksResult, but we include it in our derived types everywhere else
|
|
130
|
+
return {
|
|
131
|
+
tasks,
|
|
132
|
+
nextCursor,
|
|
133
|
+
_meta: {}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Failed to list tasks: ${error instanceof Error ? error.message : String(error)}`);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
this.setRequestHandler(types_js_1.CancelTaskRequestSchema, async (request, extra) => {
|
|
141
|
+
try {
|
|
142
|
+
// Get the current task to check if it's in a terminal state, in case the implementation is not atomic
|
|
143
|
+
const task = await this._taskStore.getTask(request.params.taskId, extra.sessionId);
|
|
144
|
+
if (!task) {
|
|
145
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Task not found: ${request.params.taskId}`);
|
|
146
|
+
}
|
|
147
|
+
// Reject cancellation of terminal tasks
|
|
148
|
+
if ((0, interfaces_js_1.isTerminal)(task.status)) {
|
|
149
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Cannot cancel task in terminal status: ${task.status}`);
|
|
150
|
+
}
|
|
151
|
+
await this._taskStore.updateTaskStatus(request.params.taskId, 'cancelled', 'Client cancelled task execution.', extra.sessionId);
|
|
152
|
+
this._clearTaskQueue(request.params.taskId);
|
|
153
|
+
const cancelledTask = await this._taskStore.getTask(request.params.taskId, extra.sessionId);
|
|
154
|
+
if (!cancelledTask) {
|
|
155
|
+
// Task was deleted during cancellation (e.g., cleanup happened)
|
|
156
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Task not found after cancellation: ${request.params.taskId}`);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
_meta: {},
|
|
160
|
+
...cancelledTask
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
// Re-throw McpError as-is
|
|
165
|
+
if (error instanceof types_js_1.McpError) {
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, `Failed to cancel task: ${error instanceof Error ? error.message : String(error)}`);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async _oncancel(notification) {
|
|
174
|
+
if (!notification.params.requestId) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// Handle request cancellation
|
|
178
|
+
const controller = this._requestHandlerAbortControllers.get(notification.params.requestId);
|
|
179
|
+
controller?.abort(notification.params.reason);
|
|
180
|
+
}
|
|
181
|
+
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
|
|
182
|
+
this._timeoutInfo.set(messageId, {
|
|
183
|
+
timeoutId: setTimeout(onTimeout, timeout),
|
|
184
|
+
startTime: Date.now(),
|
|
185
|
+
timeout,
|
|
186
|
+
maxTotalTimeout,
|
|
187
|
+
resetTimeoutOnProgress,
|
|
188
|
+
onTimeout
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
_resetTimeout(messageId) {
|
|
192
|
+
const info = this._timeoutInfo.get(messageId);
|
|
193
|
+
if (!info)
|
|
194
|
+
return false;
|
|
195
|
+
const totalElapsed = Date.now() - info.startTime;
|
|
196
|
+
if (info.maxTotalTimeout && totalElapsed >= info.maxTotalTimeout) {
|
|
197
|
+
this._timeoutInfo.delete(messageId);
|
|
198
|
+
throw types_js_1.McpError.fromError(types_js_1.ErrorCode.RequestTimeout, 'Maximum total timeout exceeded', {
|
|
199
|
+
maxTotalTimeout: info.maxTotalTimeout,
|
|
200
|
+
totalElapsed
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
clearTimeout(info.timeoutId);
|
|
204
|
+
info.timeoutId = setTimeout(info.onTimeout, info.timeout);
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
_cleanupTimeout(messageId) {
|
|
208
|
+
const info = this._timeoutInfo.get(messageId);
|
|
209
|
+
if (info) {
|
|
210
|
+
clearTimeout(info.timeoutId);
|
|
211
|
+
this._timeoutInfo.delete(messageId);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Attaches to the given transport, starts it, and starts listening for messages.
|
|
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.
|
|
218
|
+
*/
|
|
219
|
+
async connect(transport) {
|
|
220
|
+
if (this._transport) {
|
|
221
|
+
throw new Error('Already connected to a transport. Call close() before connecting to a new transport, or use a separate Protocol instance per connection.');
|
|
222
|
+
}
|
|
223
|
+
this._transport = transport;
|
|
224
|
+
const _onclose = this.transport?.onclose;
|
|
225
|
+
this._transport.onclose = () => {
|
|
226
|
+
_onclose?.();
|
|
227
|
+
this._onclose();
|
|
228
|
+
};
|
|
229
|
+
const _onerror = this.transport?.onerror;
|
|
230
|
+
this._transport.onerror = (error) => {
|
|
231
|
+
_onerror?.(error);
|
|
232
|
+
this._onerror(error);
|
|
233
|
+
};
|
|
234
|
+
const _onmessage = this._transport?.onmessage;
|
|
235
|
+
this._transport.onmessage = (message, extra) => {
|
|
236
|
+
_onmessage?.(message, extra);
|
|
237
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(message) || (0, types_js_1.isJSONRPCErrorResponse)(message)) {
|
|
238
|
+
this._onresponse(message);
|
|
239
|
+
}
|
|
240
|
+
else if ((0, types_js_1.isJSONRPCRequest)(message)) {
|
|
241
|
+
this._onrequest(message, extra);
|
|
242
|
+
}
|
|
243
|
+
else if ((0, types_js_1.isJSONRPCNotification)(message)) {
|
|
244
|
+
this._onnotification(message);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
this._onerror(new Error(`Unknown message type: ${JSON.stringify(message)}`));
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
await this._transport.start();
|
|
251
|
+
}
|
|
252
|
+
_onclose() {
|
|
253
|
+
const responseHandlers = this._responseHandlers;
|
|
254
|
+
this._responseHandlers = new Map();
|
|
255
|
+
this._progressHandlers.clear();
|
|
256
|
+
this._taskProgressTokens.clear();
|
|
257
|
+
this._pendingDebouncedNotifications.clear();
|
|
258
|
+
// Abort all in-flight request handlers so they stop sending messages
|
|
259
|
+
for (const controller of this._requestHandlerAbortControllers.values()) {
|
|
260
|
+
controller.abort();
|
|
261
|
+
}
|
|
262
|
+
this._requestHandlerAbortControllers.clear();
|
|
263
|
+
const error = types_js_1.McpError.fromError(types_js_1.ErrorCode.ConnectionClosed, 'Connection closed');
|
|
264
|
+
this._transport = undefined;
|
|
265
|
+
this.onclose?.();
|
|
266
|
+
for (const handler of responseHandlers.values()) {
|
|
267
|
+
handler(error);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
_onerror(error) {
|
|
271
|
+
this.onerror?.(error);
|
|
272
|
+
}
|
|
273
|
+
_onnotification(notification) {
|
|
274
|
+
const handler = this._notificationHandlers.get(notification.method) ?? this.fallbackNotificationHandler;
|
|
275
|
+
// Ignore notifications not being subscribed to.
|
|
276
|
+
if (handler === undefined) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
|
|
280
|
+
Promise.resolve()
|
|
281
|
+
.then(() => handler(notification))
|
|
282
|
+
.catch(error => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
|
|
283
|
+
}
|
|
284
|
+
_onrequest(request, extra) {
|
|
285
|
+
const handler = this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
|
|
286
|
+
// Capture the current transport at request time to ensure responses go to the correct client
|
|
287
|
+
const capturedTransport = this._transport;
|
|
288
|
+
// Extract taskId from request metadata if present (needed early for method not found case)
|
|
289
|
+
const relatedTaskId = request.params?._meta?.[types_js_1.RELATED_TASK_META_KEY]?.taskId;
|
|
290
|
+
if (handler === undefined) {
|
|
291
|
+
const errorResponse = {
|
|
292
|
+
jsonrpc: '2.0',
|
|
293
|
+
id: request.id,
|
|
294
|
+
error: {
|
|
295
|
+
code: types_js_1.ErrorCode.MethodNotFound,
|
|
296
|
+
message: 'Method not found'
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
// Queue or send the error response based on whether this is a task-related request
|
|
300
|
+
if (relatedTaskId && this._taskMessageQueue) {
|
|
301
|
+
this._enqueueTaskMessage(relatedTaskId, {
|
|
302
|
+
type: 'error',
|
|
303
|
+
message: errorResponse,
|
|
304
|
+
timestamp: Date.now()
|
|
305
|
+
}, capturedTransport?.sessionId).catch(error => this._onerror(new Error(`Failed to enqueue error response: ${error}`)));
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
capturedTransport
|
|
309
|
+
?.send(errorResponse)
|
|
310
|
+
.catch(error => this._onerror(new Error(`Failed to send an error response: ${error}`)));
|
|
311
|
+
}
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
const abortController = new AbortController();
|
|
315
|
+
this._requestHandlerAbortControllers.set(request.id, abortController);
|
|
316
|
+
const taskCreationParams = (0, types_js_1.isTaskAugmentedRequestParams)(request.params) ? request.params.task : undefined;
|
|
317
|
+
const taskStore = this._taskStore ? this.requestTaskStore(request, capturedTransport?.sessionId) : undefined;
|
|
318
|
+
const fullExtra = {
|
|
319
|
+
signal: abortController.signal,
|
|
320
|
+
sessionId: capturedTransport?.sessionId,
|
|
321
|
+
_meta: request.params?._meta,
|
|
322
|
+
sendNotification: async (notification) => {
|
|
323
|
+
if (abortController.signal.aborted)
|
|
324
|
+
return;
|
|
325
|
+
// Include related-task metadata if this request is part of a task
|
|
326
|
+
const notificationOptions = { relatedRequestId: request.id };
|
|
327
|
+
if (relatedTaskId) {
|
|
328
|
+
notificationOptions.relatedTask = { taskId: relatedTaskId };
|
|
329
|
+
}
|
|
330
|
+
await this.notification(notification, notificationOptions);
|
|
331
|
+
},
|
|
332
|
+
sendRequest: async (r, resultSchema, options) => {
|
|
333
|
+
if (abortController.signal.aborted) {
|
|
334
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.ConnectionClosed, 'Request was cancelled');
|
|
335
|
+
}
|
|
336
|
+
// Include related-task metadata if this request is part of a task
|
|
337
|
+
const requestOptions = { ...options, relatedRequestId: request.id };
|
|
338
|
+
if (relatedTaskId && !requestOptions.relatedTask) {
|
|
339
|
+
requestOptions.relatedTask = { taskId: relatedTaskId };
|
|
340
|
+
}
|
|
341
|
+
// Set task status to input_required when sending a request within a task context
|
|
342
|
+
// Use the taskId from options (explicit) or fall back to relatedTaskId (inherited)
|
|
343
|
+
const effectiveTaskId = requestOptions.relatedTask?.taskId ?? relatedTaskId;
|
|
344
|
+
if (effectiveTaskId && taskStore) {
|
|
345
|
+
await taskStore.updateTaskStatus(effectiveTaskId, 'input_required');
|
|
346
|
+
}
|
|
347
|
+
return await this.request(r, resultSchema, requestOptions);
|
|
348
|
+
},
|
|
349
|
+
authInfo: extra?.authInfo,
|
|
350
|
+
requestId: request.id,
|
|
351
|
+
requestInfo: extra?.requestInfo,
|
|
352
|
+
taskId: relatedTaskId,
|
|
353
|
+
taskStore: taskStore,
|
|
354
|
+
taskRequestedTtl: taskCreationParams?.ttl,
|
|
355
|
+
closeSSEStream: extra?.closeSSEStream,
|
|
356
|
+
closeStandaloneSSEStream: extra?.closeStandaloneSSEStream
|
|
357
|
+
};
|
|
358
|
+
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
|
|
359
|
+
Promise.resolve()
|
|
360
|
+
.then(() => {
|
|
361
|
+
// If this request asked for task creation, check capability first
|
|
362
|
+
if (taskCreationParams) {
|
|
363
|
+
// Check if the request method supports task creation
|
|
364
|
+
this.assertTaskHandlerCapability(request.method);
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
.then(() => handler(request, fullExtra))
|
|
368
|
+
.then(async (result) => {
|
|
369
|
+
if (abortController.signal.aborted) {
|
|
370
|
+
// Request was cancelled
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const response = {
|
|
374
|
+
result,
|
|
375
|
+
jsonrpc: '2.0',
|
|
376
|
+
id: request.id
|
|
377
|
+
};
|
|
378
|
+
// Queue or send the response based on whether this is a task-related request
|
|
379
|
+
if (relatedTaskId && this._taskMessageQueue) {
|
|
380
|
+
await this._enqueueTaskMessage(relatedTaskId, {
|
|
381
|
+
type: 'response',
|
|
382
|
+
message: response,
|
|
383
|
+
timestamp: Date.now()
|
|
384
|
+
}, capturedTransport?.sessionId);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
await capturedTransport?.send(response);
|
|
388
|
+
}
|
|
389
|
+
}, async (error) => {
|
|
390
|
+
if (abortController.signal.aborted) {
|
|
391
|
+
// Request was cancelled
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const errorResponse = {
|
|
395
|
+
jsonrpc: '2.0',
|
|
396
|
+
id: request.id,
|
|
397
|
+
error: {
|
|
398
|
+
code: Number.isSafeInteger(error['code']) ? error['code'] : types_js_1.ErrorCode.InternalError,
|
|
399
|
+
message: error.message ?? 'Internal error',
|
|
400
|
+
...(error['data'] !== undefined && { data: error['data'] })
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
// Queue or send the error response based on whether this is a task-related request
|
|
404
|
+
if (relatedTaskId && this._taskMessageQueue) {
|
|
405
|
+
await this._enqueueTaskMessage(relatedTaskId, {
|
|
406
|
+
type: 'error',
|
|
407
|
+
message: errorResponse,
|
|
408
|
+
timestamp: Date.now()
|
|
409
|
+
}, capturedTransport?.sessionId);
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
await capturedTransport?.send(errorResponse);
|
|
413
|
+
}
|
|
414
|
+
})
|
|
415
|
+
.catch(error => this._onerror(new Error(`Failed to send response: ${error}`)))
|
|
416
|
+
.finally(() => {
|
|
417
|
+
this._requestHandlerAbortControllers.delete(request.id);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
_onprogress(notification) {
|
|
421
|
+
const { progressToken, ...params } = notification.params;
|
|
422
|
+
const messageId = Number(progressToken);
|
|
423
|
+
const handler = this._progressHandlers.get(messageId);
|
|
424
|
+
if (!handler) {
|
|
425
|
+
this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const responseHandler = this._responseHandlers.get(messageId);
|
|
429
|
+
const timeoutInfo = this._timeoutInfo.get(messageId);
|
|
430
|
+
if (timeoutInfo && responseHandler && timeoutInfo.resetTimeoutOnProgress) {
|
|
431
|
+
try {
|
|
432
|
+
this._resetTimeout(messageId);
|
|
433
|
+
}
|
|
434
|
+
catch (error) {
|
|
435
|
+
// Clean up if maxTotalTimeout was exceeded
|
|
436
|
+
this._responseHandlers.delete(messageId);
|
|
437
|
+
this._progressHandlers.delete(messageId);
|
|
438
|
+
this._cleanupTimeout(messageId);
|
|
439
|
+
responseHandler(error);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
handler(params);
|
|
444
|
+
}
|
|
445
|
+
_onresponse(response) {
|
|
446
|
+
const messageId = Number(response.id);
|
|
447
|
+
// Check if this is a response to a queued request
|
|
448
|
+
const resolver = this._requestResolvers.get(messageId);
|
|
449
|
+
if (resolver) {
|
|
450
|
+
this._requestResolvers.delete(messageId);
|
|
451
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response)) {
|
|
452
|
+
resolver(response);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
const error = new types_js_1.McpError(response.error.code, response.error.message, response.error.data);
|
|
456
|
+
resolver(error);
|
|
457
|
+
}
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const handler = this._responseHandlers.get(messageId);
|
|
461
|
+
if (handler === undefined) {
|
|
462
|
+
this._onerror(new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
this._responseHandlers.delete(messageId);
|
|
466
|
+
this._cleanupTimeout(messageId);
|
|
467
|
+
// Keep progress handler alive for CreateTaskResult responses
|
|
468
|
+
let isTaskResponse = false;
|
|
469
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response) && response.result && typeof response.result === 'object') {
|
|
470
|
+
const result = response.result;
|
|
471
|
+
if (result.task && typeof result.task === 'object') {
|
|
472
|
+
const task = result.task;
|
|
473
|
+
if (typeof task.taskId === 'string') {
|
|
474
|
+
isTaskResponse = true;
|
|
475
|
+
this._taskProgressTokens.set(task.taskId, messageId);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (!isTaskResponse) {
|
|
480
|
+
this._progressHandlers.delete(messageId);
|
|
481
|
+
}
|
|
482
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(response)) {
|
|
483
|
+
handler(response);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
const error = types_js_1.McpError.fromError(response.error.code, response.error.message, response.error.data);
|
|
487
|
+
handler(error);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
get transport() {
|
|
491
|
+
return this._transport;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Closes the connection.
|
|
495
|
+
*/
|
|
496
|
+
async close() {
|
|
497
|
+
await this._transport?.close();
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Sends a request and returns an AsyncGenerator that yields response messages.
|
|
501
|
+
* The generator is guaranteed to end with either a 'result' or 'error' message.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```typescript
|
|
505
|
+
* const stream = protocol.requestStream(request, resultSchema, options);
|
|
506
|
+
* for await (const message of stream) {
|
|
507
|
+
* switch (message.type) {
|
|
508
|
+
* case 'taskCreated':
|
|
509
|
+
* console.log('Task created:', message.task.taskId);
|
|
510
|
+
* break;
|
|
511
|
+
* case 'taskStatus':
|
|
512
|
+
* console.log('Task status:', message.task.status);
|
|
513
|
+
* break;
|
|
514
|
+
* case 'result':
|
|
515
|
+
* console.log('Final result:', message.result);
|
|
516
|
+
* break;
|
|
517
|
+
* case 'error':
|
|
518
|
+
* console.error('Error:', message.error);
|
|
519
|
+
* break;
|
|
520
|
+
* }
|
|
521
|
+
* }
|
|
522
|
+
* ```
|
|
523
|
+
*
|
|
524
|
+
* @experimental Use `client.experimental.tasks.requestStream()` to access this method.
|
|
525
|
+
*/
|
|
526
|
+
async *requestStream(request, resultSchema, options) {
|
|
527
|
+
const { task } = options ?? {};
|
|
528
|
+
// For non-task requests, just yield the result
|
|
529
|
+
if (!task) {
|
|
530
|
+
try {
|
|
531
|
+
const result = await this.request(request, resultSchema, options);
|
|
532
|
+
yield { type: 'result', result };
|
|
533
|
+
}
|
|
534
|
+
catch (error) {
|
|
535
|
+
yield {
|
|
536
|
+
type: 'error',
|
|
537
|
+
error: error instanceof types_js_1.McpError ? error : new types_js_1.McpError(types_js_1.ErrorCode.InternalError, String(error))
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
// For task-augmented requests, we need to poll for status
|
|
543
|
+
// First, make the request to create the task
|
|
544
|
+
let taskId;
|
|
545
|
+
try {
|
|
546
|
+
// Send the request and get the CreateTaskResult
|
|
547
|
+
const createResult = await this.request(request, types_js_1.CreateTaskResultSchema, options);
|
|
548
|
+
// Extract taskId from the result
|
|
549
|
+
if (createResult.task) {
|
|
550
|
+
taskId = createResult.task.taskId;
|
|
551
|
+
yield { type: 'taskCreated', task: createResult.task };
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InternalError, 'Task creation did not return a task');
|
|
555
|
+
}
|
|
556
|
+
// Poll for task completion
|
|
557
|
+
while (true) {
|
|
558
|
+
// Get current task status
|
|
559
|
+
const task = await this.getTask({ taskId }, options);
|
|
560
|
+
yield { type: 'taskStatus', task };
|
|
561
|
+
// Check if task is terminal
|
|
562
|
+
if ((0, interfaces_js_1.isTerminal)(task.status)) {
|
|
563
|
+
if (task.status === 'completed') {
|
|
564
|
+
// Get the final result
|
|
565
|
+
const result = await this.getTaskResult({ taskId }, resultSchema, options);
|
|
566
|
+
yield { type: 'result', result };
|
|
567
|
+
}
|
|
568
|
+
else if (task.status === 'failed') {
|
|
569
|
+
yield {
|
|
570
|
+
type: 'error',
|
|
571
|
+
error: new types_js_1.McpError(types_js_1.ErrorCode.InternalError, `Task ${taskId} failed`)
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
else if (task.status === 'cancelled') {
|
|
575
|
+
yield {
|
|
576
|
+
type: 'error',
|
|
577
|
+
error: new types_js_1.McpError(types_js_1.ErrorCode.InternalError, `Task ${taskId} was cancelled`)
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
// When input_required, call tasks/result to deliver queued messages
|
|
583
|
+
// (elicitation, sampling) via SSE and block until terminal
|
|
584
|
+
if (task.status === 'input_required') {
|
|
585
|
+
const result = await this.getTaskResult({ taskId }, resultSchema, options);
|
|
586
|
+
yield { type: 'result', result };
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
// Wait before polling again
|
|
590
|
+
const pollInterval = task.pollInterval ?? this._options?.defaultTaskPollInterval ?? 1000;
|
|
591
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
592
|
+
// Check if cancelled
|
|
593
|
+
options?.signal?.throwIfAborted();
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
catch (error) {
|
|
597
|
+
yield {
|
|
598
|
+
type: 'error',
|
|
599
|
+
error: error instanceof types_js_1.McpError ? error : new types_js_1.McpError(types_js_1.ErrorCode.InternalError, String(error))
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Sends a request and waits for a response.
|
|
605
|
+
*
|
|
606
|
+
* Do not use this method to emit notifications! Use notification() instead.
|
|
607
|
+
*/
|
|
608
|
+
request(request, resultSchema, options) {
|
|
609
|
+
const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options ?? {};
|
|
610
|
+
// Send the request
|
|
611
|
+
return new Promise((resolve, reject) => {
|
|
612
|
+
const earlyReject = (error) => {
|
|
613
|
+
reject(error);
|
|
614
|
+
};
|
|
615
|
+
if (!this._transport) {
|
|
616
|
+
earlyReject(new Error('Not connected'));
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
if (this._options?.enforceStrictCapabilities === true) {
|
|
620
|
+
try {
|
|
621
|
+
this.assertCapabilityForMethod(request.method);
|
|
622
|
+
// If task creation is requested, also check task capabilities
|
|
623
|
+
if (task) {
|
|
624
|
+
this.assertTaskCapability(request.method);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
catch (e) {
|
|
628
|
+
earlyReject(e);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
options?.signal?.throwIfAborted();
|
|
633
|
+
const messageId = this._requestMessageId++;
|
|
634
|
+
const jsonrpcRequest = {
|
|
635
|
+
...request,
|
|
636
|
+
jsonrpc: '2.0',
|
|
637
|
+
id: messageId
|
|
638
|
+
};
|
|
639
|
+
if (options?.onprogress) {
|
|
640
|
+
this._progressHandlers.set(messageId, options.onprogress);
|
|
641
|
+
jsonrpcRequest.params = {
|
|
642
|
+
...request.params,
|
|
643
|
+
_meta: {
|
|
644
|
+
...(request.params?._meta || {}),
|
|
645
|
+
progressToken: messageId
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
// Augment with task creation parameters if provided
|
|
650
|
+
if (task) {
|
|
651
|
+
jsonrpcRequest.params = {
|
|
652
|
+
...jsonrpcRequest.params,
|
|
653
|
+
task: task
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
// Augment with related task metadata if relatedTask is provided
|
|
657
|
+
if (relatedTask) {
|
|
658
|
+
jsonrpcRequest.params = {
|
|
659
|
+
...jsonrpcRequest.params,
|
|
660
|
+
_meta: {
|
|
661
|
+
...(jsonrpcRequest.params?._meta || {}),
|
|
662
|
+
[types_js_1.RELATED_TASK_META_KEY]: relatedTask
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const cancel = (reason) => {
|
|
667
|
+
this._responseHandlers.delete(messageId);
|
|
668
|
+
this._progressHandlers.delete(messageId);
|
|
669
|
+
this._cleanupTimeout(messageId);
|
|
670
|
+
this._transport
|
|
671
|
+
?.send({
|
|
672
|
+
jsonrpc: '2.0',
|
|
673
|
+
method: 'notifications/cancelled',
|
|
674
|
+
params: {
|
|
675
|
+
requestId: messageId,
|
|
676
|
+
reason: String(reason)
|
|
677
|
+
}
|
|
678
|
+
}, { relatedRequestId, resumptionToken, onresumptiontoken })
|
|
679
|
+
.catch(error => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
|
|
680
|
+
// Wrap the reason in an McpError if it isn't already
|
|
681
|
+
const error = reason instanceof types_js_1.McpError ? reason : new types_js_1.McpError(types_js_1.ErrorCode.RequestTimeout, String(reason));
|
|
682
|
+
reject(error);
|
|
683
|
+
};
|
|
684
|
+
this._responseHandlers.set(messageId, response => {
|
|
685
|
+
if (options?.signal?.aborted) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
if (response instanceof Error) {
|
|
689
|
+
return reject(response);
|
|
690
|
+
}
|
|
691
|
+
try {
|
|
692
|
+
const parseResult = (0, zod_compat_js_1.safeParse)(resultSchema, response.result);
|
|
693
|
+
if (!parseResult.success) {
|
|
694
|
+
// Type guard: if success is false, error is guaranteed to exist
|
|
695
|
+
reject(parseResult.error);
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
resolve(parseResult.data);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
catch (error) {
|
|
702
|
+
reject(error);
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
options?.signal?.addEventListener('abort', () => {
|
|
706
|
+
cancel(options?.signal?.reason);
|
|
707
|
+
});
|
|
708
|
+
const timeout = options?.timeout ?? exports.DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
709
|
+
const timeoutHandler = () => cancel(types_js_1.McpError.fromError(types_js_1.ErrorCode.RequestTimeout, 'Request timed out', { timeout }));
|
|
710
|
+
this._setupTimeout(messageId, timeout, options?.maxTotalTimeout, timeoutHandler, options?.resetTimeoutOnProgress ?? false);
|
|
711
|
+
// Queue request if related to a task
|
|
712
|
+
const relatedTaskId = relatedTask?.taskId;
|
|
713
|
+
if (relatedTaskId) {
|
|
714
|
+
// Store the response resolver for this request so responses can be routed back
|
|
715
|
+
const responseResolver = (response) => {
|
|
716
|
+
const handler = this._responseHandlers.get(messageId);
|
|
717
|
+
if (handler) {
|
|
718
|
+
handler(response);
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
// Log error when resolver is missing, but don't fail
|
|
722
|
+
this._onerror(new Error(`Response handler missing for side-channeled request ${messageId}`));
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
this._requestResolvers.set(messageId, responseResolver);
|
|
726
|
+
this._enqueueTaskMessage(relatedTaskId, {
|
|
727
|
+
type: 'request',
|
|
728
|
+
message: jsonrpcRequest,
|
|
729
|
+
timestamp: Date.now()
|
|
730
|
+
}).catch(error => {
|
|
731
|
+
this._cleanupTimeout(messageId);
|
|
732
|
+
reject(error);
|
|
733
|
+
});
|
|
734
|
+
// Don't send through transport - queued messages are delivered via tasks/result only
|
|
735
|
+
// This prevents duplicate delivery for bidirectional transports
|
|
736
|
+
}
|
|
737
|
+
else {
|
|
738
|
+
// No related task - send through transport normally
|
|
739
|
+
this._transport.send(jsonrpcRequest, { relatedRequestId, resumptionToken, onresumptiontoken }).catch(error => {
|
|
740
|
+
this._cleanupTimeout(messageId);
|
|
741
|
+
reject(error);
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Gets the current status of a task.
|
|
748
|
+
*
|
|
749
|
+
* @experimental Use `client.experimental.tasks.getTask()` to access this method.
|
|
750
|
+
*/
|
|
751
|
+
async getTask(params, options) {
|
|
752
|
+
// @ts-expect-error SendRequestT cannot directly contain GetTaskRequest, but we ensure all type instantiations contain it anyways
|
|
753
|
+
return this.request({ method: 'tasks/get', params }, types_js_1.GetTaskResultSchema, options);
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Retrieves the result of a completed task.
|
|
757
|
+
*
|
|
758
|
+
* @experimental Use `client.experimental.tasks.getTaskResult()` to access this method.
|
|
759
|
+
*/
|
|
760
|
+
async getTaskResult(params, resultSchema, options) {
|
|
761
|
+
// @ts-expect-error SendRequestT cannot directly contain GetTaskPayloadRequest, but we ensure all type instantiations contain it anyways
|
|
762
|
+
return this.request({ method: 'tasks/result', params }, resultSchema, options);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Lists tasks, optionally starting from a pagination cursor.
|
|
766
|
+
*
|
|
767
|
+
* @experimental Use `client.experimental.tasks.listTasks()` to access this method.
|
|
768
|
+
*/
|
|
769
|
+
async listTasks(params, options) {
|
|
770
|
+
// @ts-expect-error SendRequestT cannot directly contain ListTasksRequest, but we ensure all type instantiations contain it anyways
|
|
771
|
+
return this.request({ method: 'tasks/list', params }, types_js_1.ListTasksResultSchema, options);
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Cancels a specific task.
|
|
775
|
+
*
|
|
776
|
+
* @experimental Use `client.experimental.tasks.cancelTask()` to access this method.
|
|
777
|
+
*/
|
|
778
|
+
async cancelTask(params, options) {
|
|
779
|
+
// @ts-expect-error SendRequestT cannot directly contain CancelTaskRequest, but we ensure all type instantiations contain it anyways
|
|
780
|
+
return this.request({ method: 'tasks/cancel', params }, types_js_1.CancelTaskResultSchema, options);
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* Emits a notification, which is a one-way message that does not expect a response.
|
|
784
|
+
*/
|
|
785
|
+
async notification(notification, options) {
|
|
786
|
+
if (!this._transport) {
|
|
787
|
+
throw new Error('Not connected');
|
|
788
|
+
}
|
|
789
|
+
this.assertNotificationCapability(notification.method);
|
|
790
|
+
// Queue notification if related to a task
|
|
791
|
+
const relatedTaskId = options?.relatedTask?.taskId;
|
|
792
|
+
if (relatedTaskId) {
|
|
793
|
+
// Build the JSONRPC notification with metadata
|
|
794
|
+
const jsonrpcNotification = {
|
|
795
|
+
...notification,
|
|
796
|
+
jsonrpc: '2.0',
|
|
797
|
+
params: {
|
|
798
|
+
...notification.params,
|
|
799
|
+
_meta: {
|
|
800
|
+
...(notification.params?._meta || {}),
|
|
801
|
+
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
await this._enqueueTaskMessage(relatedTaskId, {
|
|
806
|
+
type: 'notification',
|
|
807
|
+
message: jsonrpcNotification,
|
|
808
|
+
timestamp: Date.now()
|
|
809
|
+
});
|
|
810
|
+
// Don't send through transport - queued messages are delivered via tasks/result only
|
|
811
|
+
// This prevents duplicate delivery for bidirectional transports
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const debouncedMethods = this._options?.debouncedNotificationMethods ?? [];
|
|
815
|
+
// A notification can only be debounced if it's in the list AND it's "simple"
|
|
816
|
+
// (i.e., has no parameters and no related request ID or related task that could be lost).
|
|
817
|
+
const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !options?.relatedRequestId && !options?.relatedTask;
|
|
818
|
+
if (canDebounce) {
|
|
819
|
+
// If a notification of this type is already scheduled, do nothing.
|
|
820
|
+
if (this._pendingDebouncedNotifications.has(notification.method)) {
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
// Mark this notification type as pending.
|
|
824
|
+
this._pendingDebouncedNotifications.add(notification.method);
|
|
825
|
+
// Schedule the actual send to happen in the next microtask.
|
|
826
|
+
// This allows all synchronous calls in the current event loop tick to be coalesced.
|
|
827
|
+
Promise.resolve().then(() => {
|
|
828
|
+
// Un-mark the notification so the next one can be scheduled.
|
|
829
|
+
this._pendingDebouncedNotifications.delete(notification.method);
|
|
830
|
+
// SAFETY CHECK: If the connection was closed while this was pending, abort.
|
|
831
|
+
if (!this._transport) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
let jsonrpcNotification = {
|
|
835
|
+
...notification,
|
|
836
|
+
jsonrpc: '2.0'
|
|
837
|
+
};
|
|
838
|
+
// Augment with related task metadata if relatedTask is provided
|
|
839
|
+
if (options?.relatedTask) {
|
|
840
|
+
jsonrpcNotification = {
|
|
841
|
+
...jsonrpcNotification,
|
|
842
|
+
params: {
|
|
843
|
+
...jsonrpcNotification.params,
|
|
844
|
+
_meta: {
|
|
845
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
846
|
+
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
// Send the notification, but don't await it here to avoid blocking.
|
|
852
|
+
// Handle potential errors with a .catch().
|
|
853
|
+
this._transport?.send(jsonrpcNotification, options).catch(error => this._onerror(error));
|
|
854
|
+
});
|
|
855
|
+
// Return immediately.
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
let jsonrpcNotification = {
|
|
859
|
+
...notification,
|
|
860
|
+
jsonrpc: '2.0'
|
|
861
|
+
};
|
|
862
|
+
// Augment with related task metadata if relatedTask is provided
|
|
863
|
+
if (options?.relatedTask) {
|
|
864
|
+
jsonrpcNotification = {
|
|
865
|
+
...jsonrpcNotification,
|
|
866
|
+
params: {
|
|
867
|
+
...jsonrpcNotification.params,
|
|
868
|
+
_meta: {
|
|
869
|
+
...(jsonrpcNotification.params?._meta || {}),
|
|
870
|
+
[types_js_1.RELATED_TASK_META_KEY]: options.relatedTask
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
await this._transport.send(jsonrpcNotification, options);
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
879
|
+
*
|
|
880
|
+
* Note that this will replace any previous request handler for the same method.
|
|
881
|
+
*/
|
|
882
|
+
setRequestHandler(requestSchema, handler) {
|
|
883
|
+
const method = (0, zod_json_schema_compat_js_1.getMethodLiteral)(requestSchema);
|
|
884
|
+
this.assertRequestHandlerCapability(method);
|
|
885
|
+
this._requestHandlers.set(method, (request, extra) => {
|
|
886
|
+
const parsed = (0, zod_json_schema_compat_js_1.parseWithCompat)(requestSchema, request);
|
|
887
|
+
return Promise.resolve(handler(parsed, extra));
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Removes the request handler for the given method.
|
|
892
|
+
*/
|
|
893
|
+
removeRequestHandler(method) {
|
|
894
|
+
this._requestHandlers.delete(method);
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.
|
|
898
|
+
*/
|
|
899
|
+
assertCanSetRequestHandler(method) {
|
|
900
|
+
if (this._requestHandlers.has(method)) {
|
|
901
|
+
throw new Error(`A request handler for ${method} already exists, which would be overridden`);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
906
|
+
*
|
|
907
|
+
* Note that this will replace any previous notification handler for the same method.
|
|
908
|
+
*/
|
|
909
|
+
setNotificationHandler(notificationSchema, handler) {
|
|
910
|
+
const method = (0, zod_json_schema_compat_js_1.getMethodLiteral)(notificationSchema);
|
|
911
|
+
this._notificationHandlers.set(method, notification => {
|
|
912
|
+
const parsed = (0, zod_json_schema_compat_js_1.parseWithCompat)(notificationSchema, notification);
|
|
913
|
+
return Promise.resolve(handler(parsed));
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Removes the notification handler for the given method.
|
|
918
|
+
*/
|
|
919
|
+
removeNotificationHandler(method) {
|
|
920
|
+
this._notificationHandlers.delete(method);
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Cleans up the progress handler associated with a task.
|
|
924
|
+
* This should be called when a task reaches a terminal status.
|
|
925
|
+
*/
|
|
926
|
+
_cleanupTaskProgressHandler(taskId) {
|
|
927
|
+
const progressToken = this._taskProgressTokens.get(taskId);
|
|
928
|
+
if (progressToken !== undefined) {
|
|
929
|
+
this._progressHandlers.delete(progressToken);
|
|
930
|
+
this._taskProgressTokens.delete(taskId);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Enqueues a task-related message for side-channel delivery via tasks/result.
|
|
935
|
+
* @param taskId The task ID to associate the message with
|
|
936
|
+
* @param message The message to enqueue
|
|
937
|
+
* @param sessionId Optional session ID for binding the operation to a specific session
|
|
938
|
+
* @throws Error if taskStore is not configured or if enqueue fails (e.g., queue overflow)
|
|
939
|
+
*
|
|
940
|
+
* Note: If enqueue fails, it's the TaskMessageQueue implementation's responsibility to handle
|
|
941
|
+
* the error appropriately (e.g., by failing the task, logging, etc.). The Protocol layer
|
|
942
|
+
* simply propagates the error.
|
|
943
|
+
*/
|
|
944
|
+
async _enqueueTaskMessage(taskId, message, sessionId) {
|
|
945
|
+
// Task message queues are only used when taskStore is configured
|
|
946
|
+
if (!this._taskStore || !this._taskMessageQueue) {
|
|
947
|
+
throw new Error('Cannot enqueue task message: taskStore and taskMessageQueue are not configured');
|
|
948
|
+
}
|
|
949
|
+
const maxQueueSize = this._options?.maxTaskQueueSize;
|
|
950
|
+
await this._taskMessageQueue.enqueue(taskId, message, sessionId, maxQueueSize);
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Clears the message queue for a task and rejects any pending request resolvers.
|
|
954
|
+
* @param taskId The task ID whose queue should be cleared
|
|
955
|
+
* @param sessionId Optional session ID for binding the operation to a specific session
|
|
956
|
+
*/
|
|
957
|
+
async _clearTaskQueue(taskId, sessionId) {
|
|
958
|
+
if (this._taskMessageQueue) {
|
|
959
|
+
// Reject any pending request resolvers
|
|
960
|
+
const messages = await this._taskMessageQueue.dequeueAll(taskId, sessionId);
|
|
961
|
+
for (const message of messages) {
|
|
962
|
+
if (message.type === 'request' && (0, types_js_1.isJSONRPCRequest)(message.message)) {
|
|
963
|
+
// Extract request ID from the message
|
|
964
|
+
const requestId = message.message.id;
|
|
965
|
+
const resolver = this._requestResolvers.get(requestId);
|
|
966
|
+
if (resolver) {
|
|
967
|
+
resolver(new types_js_1.McpError(types_js_1.ErrorCode.InternalError, 'Task cancelled or completed'));
|
|
968
|
+
this._requestResolvers.delete(requestId);
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
// Log error when resolver is missing during cleanup for better observability
|
|
972
|
+
this._onerror(new Error(`Resolver missing for request ${requestId} during task ${taskId} cleanup`));
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Waits for a task update (new messages or status change) with abort signal support.
|
|
980
|
+
* Uses polling to check for updates at the task's configured poll interval.
|
|
981
|
+
* @param taskId The task ID to wait for
|
|
982
|
+
* @param signal Abort signal to cancel the wait
|
|
983
|
+
* @returns Promise that resolves when an update occurs or rejects if aborted
|
|
984
|
+
*/
|
|
985
|
+
async _waitForTaskUpdate(taskId, signal) {
|
|
986
|
+
// Get the task's poll interval, falling back to default
|
|
987
|
+
let interval = this._options?.defaultTaskPollInterval ?? 1000;
|
|
988
|
+
try {
|
|
989
|
+
const task = await this._taskStore?.getTask(taskId);
|
|
990
|
+
if (task?.pollInterval) {
|
|
991
|
+
interval = task.pollInterval;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
catch {
|
|
995
|
+
// Use default interval if task lookup fails
|
|
996
|
+
}
|
|
997
|
+
return new Promise((resolve, reject) => {
|
|
998
|
+
if (signal.aborted) {
|
|
999
|
+
reject(new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'Request cancelled'));
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
// Wait for the poll interval, then resolve so caller can check for updates
|
|
1003
|
+
const timeoutId = setTimeout(resolve, interval);
|
|
1004
|
+
// Clean up timeout and reject if aborted
|
|
1005
|
+
signal.addEventListener('abort', () => {
|
|
1006
|
+
clearTimeout(timeoutId);
|
|
1007
|
+
reject(new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'Request cancelled'));
|
|
1008
|
+
}, { once: true });
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
requestTaskStore(request, sessionId) {
|
|
1012
|
+
const taskStore = this._taskStore;
|
|
1013
|
+
if (!taskStore) {
|
|
1014
|
+
throw new Error('No task store configured');
|
|
1015
|
+
}
|
|
1016
|
+
return {
|
|
1017
|
+
createTask: async (taskParams) => {
|
|
1018
|
+
if (!request) {
|
|
1019
|
+
throw new Error('No request provided');
|
|
1020
|
+
}
|
|
1021
|
+
return await taskStore.createTask(taskParams, request.id, {
|
|
1022
|
+
method: request.method,
|
|
1023
|
+
params: request.params
|
|
1024
|
+
}, sessionId);
|
|
1025
|
+
},
|
|
1026
|
+
getTask: async (taskId) => {
|
|
1027
|
+
const task = await taskStore.getTask(taskId, sessionId);
|
|
1028
|
+
if (!task) {
|
|
1029
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, 'Failed to retrieve task: Task not found');
|
|
1030
|
+
}
|
|
1031
|
+
return task;
|
|
1032
|
+
},
|
|
1033
|
+
storeTaskResult: async (taskId, status, result) => {
|
|
1034
|
+
await taskStore.storeTaskResult(taskId, status, result, sessionId);
|
|
1035
|
+
// Get updated task state and send notification
|
|
1036
|
+
const task = await taskStore.getTask(taskId, sessionId);
|
|
1037
|
+
if (task) {
|
|
1038
|
+
const notification = types_js_1.TaskStatusNotificationSchema.parse({
|
|
1039
|
+
method: 'notifications/tasks/status',
|
|
1040
|
+
params: task
|
|
1041
|
+
});
|
|
1042
|
+
await this.notification(notification);
|
|
1043
|
+
if ((0, interfaces_js_1.isTerminal)(task.status)) {
|
|
1044
|
+
this._cleanupTaskProgressHandler(taskId);
|
|
1045
|
+
// Don't clear queue here - it will be cleared after delivery via tasks/result
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
},
|
|
1049
|
+
getTaskResult: taskId => {
|
|
1050
|
+
return taskStore.getTaskResult(taskId, sessionId);
|
|
1051
|
+
},
|
|
1052
|
+
updateTaskStatus: async (taskId, status, statusMessage) => {
|
|
1053
|
+
// Check if task exists
|
|
1054
|
+
const task = await taskStore.getTask(taskId, sessionId);
|
|
1055
|
+
if (!task) {
|
|
1056
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Task "${taskId}" not found - it may have been cleaned up`);
|
|
1057
|
+
}
|
|
1058
|
+
// Don't allow transitions from terminal states
|
|
1059
|
+
if ((0, interfaces_js_1.isTerminal)(task.status)) {
|
|
1060
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Cannot update task "${taskId}" from terminal status "${task.status}" to "${status}". Terminal states (completed, failed, cancelled) cannot transition to other states.`);
|
|
1061
|
+
}
|
|
1062
|
+
await taskStore.updateTaskStatus(taskId, status, statusMessage, sessionId);
|
|
1063
|
+
// Get updated task state and send notification
|
|
1064
|
+
const updatedTask = await taskStore.getTask(taskId, sessionId);
|
|
1065
|
+
if (updatedTask) {
|
|
1066
|
+
const notification = types_js_1.TaskStatusNotificationSchema.parse({
|
|
1067
|
+
method: 'notifications/tasks/status',
|
|
1068
|
+
params: updatedTask
|
|
1069
|
+
});
|
|
1070
|
+
await this.notification(notification);
|
|
1071
|
+
if ((0, interfaces_js_1.isTerminal)(updatedTask.status)) {
|
|
1072
|
+
this._cleanupTaskProgressHandler(taskId);
|
|
1073
|
+
// Don't clear queue here - it will be cleared after delivery via tasks/result
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
},
|
|
1077
|
+
listTasks: cursor => {
|
|
1078
|
+
return taskStore.listTasks(cursor, sessionId);
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
exports.Protocol = Protocol;
|
|
1084
|
+
function isPlainObject(value) {
|
|
1085
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
1086
|
+
}
|
|
1087
|
+
function mergeCapabilities(base, additional) {
|
|
1088
|
+
const result = { ...base };
|
|
1089
|
+
for (const key in additional) {
|
|
1090
|
+
const k = key;
|
|
1091
|
+
const addValue = additional[k];
|
|
1092
|
+
if (addValue === undefined)
|
|
1093
|
+
continue;
|
|
1094
|
+
const baseValue = result[k];
|
|
1095
|
+
if (isPlainObject(baseValue) && isPlainObject(addValue)) {
|
|
1096
|
+
result[k] = { ...baseValue, ...addValue };
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
result[k] = addValue;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return result;
|
|
1103
|
+
}
|
|
1104
|
+
//# sourceMappingURL=protocol.js.map
|