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