@mcp-ts/sdk 1.3.1 → 1.3.3
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/README.md +371 -290
- package/dist/adapters/agui-adapter.d.mts +3 -3
- package/dist/adapters/agui-adapter.d.ts +3 -3
- package/dist/adapters/agui-middleware.d.mts +3 -3
- package/dist/adapters/agui-middleware.d.ts +3 -3
- package/dist/adapters/ai-adapter.d.mts +3 -3
- package/dist/adapters/ai-adapter.d.ts +3 -3
- package/dist/adapters/langchain-adapter.d.mts +3 -3
- package/dist/adapters/langchain-adapter.d.ts +3 -3
- package/dist/adapters/mastra-adapter.d.mts +3 -3
- package/dist/adapters/mastra-adapter.d.ts +3 -3
- package/dist/client/index.d.mts +10 -66
- package/dist/client/index.d.ts +10 -66
- package/dist/client/index.js +91 -173
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +91 -173
- package/dist/client/index.mjs.map +1 -1
- package/dist/client/react.d.mts +15 -5
- package/dist/client/react.d.ts +15 -5
- package/dist/client/react.js +130 -182
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs +130 -182
- package/dist/client/react.mjs.map +1 -1
- package/dist/client/vue.d.mts +27 -7
- package/dist/client/vue.d.ts +27 -7
- package/dist/client/vue.js +131 -182
- package/dist/client/vue.js.map +1 -1
- package/dist/client/vue.mjs +131 -182
- package/dist/client/vue.mjs.map +1 -1
- package/dist/{events-BgeztGYZ.d.mts → events-CK3N--3g.d.mts} +2 -0
- package/dist/{events-BgeztGYZ.d.ts → events-CK3N--3g.d.ts} +2 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +224 -258
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +224 -258
- package/dist/index.mjs.map +1 -1
- package/dist/{multi-session-client-CxogNckF.d.mts → multi-session-client-DzjmT7FX.d.mts} +4 -10
- package/dist/{multi-session-client-cox_WXUj.d.ts → multi-session-client-FAFpUzZ4.d.ts} +4 -10
- package/dist/server/index.d.mts +18 -23
- package/dist/server/index.d.ts +18 -23
- package/dist/server/index.js +133 -85
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +133 -85
- package/dist/server/index.mjs.map +1 -1
- package/dist/shared/index.d.mts +3 -3
- package/dist/shared/index.d.ts +3 -3
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs.map +1 -1
- package/dist/{types-CLccx9wW.d.mts → types-CW6lghof.d.mts} +6 -0
- package/dist/{types-CLccx9wW.d.ts → types-CW6lghof.d.ts} +6 -0
- package/package.json +1 -1
- package/src/client/core/sse-client.ts +354 -493
- package/src/client/react/index.ts +16 -16
- package/src/client/react/use-mcp-apps.tsx +214 -214
- package/src/client/react/use-mcp.ts +84 -19
- package/src/client/vue/use-mcp.ts +119 -44
- package/src/server/handlers/nextjs-handler.ts +207 -217
- package/src/server/handlers/sse-handler.ts +14 -0
- package/src/server/mcp/oauth-client.ts +48 -46
- package/src/server/storage/types.ts +12 -5
- package/src/shared/events.ts +2 -0
- package/src/shared/types.ts +6 -0
|
@@ -1,493 +1,354 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
this.pendingRequests.delete(response.id);
|
|
356
|
-
|
|
357
|
-
if (response.error) {
|
|
358
|
-
pending.reject(new Error(response.error.message));
|
|
359
|
-
} else {
|
|
360
|
-
pending.resolve(response.result);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// ============================================
|
|
365
|
-
// Private: Event Handling
|
|
366
|
-
// ============================================
|
|
367
|
-
|
|
368
|
-
private setupEventListeners(): void {
|
|
369
|
-
if (!this.eventSource) return;
|
|
370
|
-
|
|
371
|
-
this.eventSource.addEventListener('open', () => {
|
|
372
|
-
this.log('Connected');
|
|
373
|
-
this.reconnectAttempts = 0;
|
|
374
|
-
this.options.onStatusChange?.('connected');
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
this.eventSource.addEventListener('connected', () => {
|
|
378
|
-
this.log('Server ready');
|
|
379
|
-
this.connectionResolver?.();
|
|
380
|
-
this.connectionResolver = null;
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
this.eventSource.addEventListener('connection', (e: MessageEvent) => {
|
|
384
|
-
const event = JSON.parse(e.data) as McpConnectionEvent;
|
|
385
|
-
this.options.onConnectionEvent?.(event);
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
this.eventSource.addEventListener('observability', (e: MessageEvent) => {
|
|
389
|
-
const event = JSON.parse(e.data) as McpObservabilityEvent;
|
|
390
|
-
this.options.onObservabilityEvent?.(event);
|
|
391
|
-
});
|
|
392
|
-
|
|
393
|
-
this.eventSource.addEventListener('rpc-response', (e: MessageEvent) => {
|
|
394
|
-
const response = JSON.parse(e.data) as McpRpcResponse;
|
|
395
|
-
this.handleRpcResponse(response);
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
this.eventSource.addEventListener('error', () => {
|
|
399
|
-
this.log('Connection error', 'error');
|
|
400
|
-
this.options.onStatusChange?.('error');
|
|
401
|
-
this.attemptReconnect();
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
private attemptReconnect(): void {
|
|
406
|
-
if (this.isManuallyDisconnected || this.reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
this.reconnectAttempts++;
|
|
411
|
-
const delay = BASE_RECONNECT_DELAY * this.reconnectAttempts;
|
|
412
|
-
this.log(`Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})`);
|
|
413
|
-
|
|
414
|
-
setTimeout(() => {
|
|
415
|
-
this.disconnect();
|
|
416
|
-
this.connect();
|
|
417
|
-
}, delay);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// ============================================
|
|
421
|
-
// Private: Utilities
|
|
422
|
-
// ============================================
|
|
423
|
-
|
|
424
|
-
private buildUrl(): string {
|
|
425
|
-
const url = new URL(this.options.url, globalThis.location?.origin);
|
|
426
|
-
url.searchParams.set('identity', this.options.identity);
|
|
427
|
-
if (this.options.authToken) {
|
|
428
|
-
url.searchParams.set('token', this.options.authToken);
|
|
429
|
-
}
|
|
430
|
-
return url.toString();
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
private buildHeaders(): HeadersInit {
|
|
434
|
-
const headers: HeadersInit = {
|
|
435
|
-
'Content-Type': 'application/json',
|
|
436
|
-
};
|
|
437
|
-
if (this.options.authToken) {
|
|
438
|
-
headers['Authorization'] = `Bearer ${this.options.authToken}`;
|
|
439
|
-
}
|
|
440
|
-
return headers;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
private rejectAllPendingRequests(error: Error): void {
|
|
444
|
-
for (const [, pending] of this.pendingRequests) {
|
|
445
|
-
clearTimeout(pending.timeoutId);
|
|
446
|
-
pending.reject(error);
|
|
447
|
-
}
|
|
448
|
-
this.pendingRequests.clear();
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
private extractUiResourceUri(tool: { name: string; _meta?: unknown }): string | undefined {
|
|
452
|
-
const meta = (tool._meta as { ui?: ToolUiMetadata })?.ui;
|
|
453
|
-
if (!meta || typeof meta !== 'object') return undefined;
|
|
454
|
-
|
|
455
|
-
// Check visibility constraint
|
|
456
|
-
if (meta.visibility && !meta.visibility.includes('app')) return undefined;
|
|
457
|
-
|
|
458
|
-
// Support both 'resourceUri' and 'uri' field names
|
|
459
|
-
return meta.resourceUri ?? meta.uri;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
private emitUiEventIfPresent(result: unknown, sessionId: string, toolName: string): void {
|
|
463
|
-
const meta = (result as { _meta?: { ui?: ToolUiMetadata } })?._meta;
|
|
464
|
-
const resourceUri = meta?.ui?.resourceUri ?? (meta as any)?.['ui/resourceUri'];
|
|
465
|
-
|
|
466
|
-
if (resourceUri) {
|
|
467
|
-
this.options.onEvent?.({
|
|
468
|
-
type: 'mcp-apps-ui',
|
|
469
|
-
sessionId,
|
|
470
|
-
resourceUri,
|
|
471
|
-
toolName,
|
|
472
|
-
result,
|
|
473
|
-
timestamp: Date.now(),
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
private log(message: string, level: 'info' | 'warn' | 'error' = 'info'): void {
|
|
479
|
-
if (!this.options.debug && level === 'info') return;
|
|
480
|
-
|
|
481
|
-
const prefix = '[SSEClient]';
|
|
482
|
-
switch (level) {
|
|
483
|
-
case 'warn':
|
|
484
|
-
console.warn(prefix, message);
|
|
485
|
-
break;
|
|
486
|
-
case 'error':
|
|
487
|
-
console.error(prefix, message);
|
|
488
|
-
break;
|
|
489
|
-
default:
|
|
490
|
-
console.log(prefix, message);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Stateless RPC-over-stream client for MCP connections.
|
|
3
|
+
*
|
|
4
|
+
* Uses single POST requests with `Accept: text/event-stream` for every RPC call.
|
|
5
|
+
* Progress events and the final rpc-response are delivered in the same response.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { nanoid } from 'nanoid';
|
|
9
|
+
import type {
|
|
10
|
+
McpConnectionEvent,
|
|
11
|
+
McpObservabilityEvent,
|
|
12
|
+
McpAppsUIEvent
|
|
13
|
+
} from '../../shared/events.js';
|
|
14
|
+
import type {
|
|
15
|
+
McpRpcRequest,
|
|
16
|
+
McpRpcResponse,
|
|
17
|
+
McpRpcMethod,
|
|
18
|
+
McpRpcParams,
|
|
19
|
+
ConnectParams,
|
|
20
|
+
SessionListResult,
|
|
21
|
+
ConnectResult,
|
|
22
|
+
DisconnectResult,
|
|
23
|
+
RestoreSessionResult,
|
|
24
|
+
FinishAuthResult,
|
|
25
|
+
ListToolsRpcResult,
|
|
26
|
+
ListPromptsResult,
|
|
27
|
+
ListResourcesResult,
|
|
28
|
+
} from '../../shared/types.js';
|
|
29
|
+
|
|
30
|
+
export interface SSEClientOptions {
|
|
31
|
+
/** MCP endpoint URL */
|
|
32
|
+
url: string;
|
|
33
|
+
|
|
34
|
+
/** User/Client identifier */
|
|
35
|
+
identity: string;
|
|
36
|
+
|
|
37
|
+
/** Optional auth token for authenticated requests */
|
|
38
|
+
authToken?: string;
|
|
39
|
+
|
|
40
|
+
/** Callback for MCP connection state changes */
|
|
41
|
+
onConnectionEvent?: (event: McpConnectionEvent) => void;
|
|
42
|
+
|
|
43
|
+
/** Callback for observability/logging events */
|
|
44
|
+
onObservabilityEvent?: (event: McpObservabilityEvent) => void;
|
|
45
|
+
|
|
46
|
+
/** Callback for connection status changes */
|
|
47
|
+
onStatusChange?: (status: ConnectionStatus) => void;
|
|
48
|
+
|
|
49
|
+
/** Callback for MCP App UI events */
|
|
50
|
+
onEvent?: (event: McpAppsUIEvent) => void;
|
|
51
|
+
|
|
52
|
+
/** Enable debug logging @default false */
|
|
53
|
+
debug?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type ConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'error';
|
|
57
|
+
|
|
58
|
+
interface ToolUiMetadata {
|
|
59
|
+
resourceUri?: string;
|
|
60
|
+
uri?: string;
|
|
61
|
+
visibility?: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class SSEClient {
|
|
65
|
+
private resourceCache = new Map<string, Promise<unknown>>();
|
|
66
|
+
private connected = false;
|
|
67
|
+
|
|
68
|
+
constructor(private readonly options: SSEClientOptions) {}
|
|
69
|
+
|
|
70
|
+
connect(): void {
|
|
71
|
+
if (this.connected) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.connected = true;
|
|
75
|
+
this.options.onStatusChange?.('connected');
|
|
76
|
+
this.log('RPC mode: post_stream');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
disconnect(): void {
|
|
80
|
+
this.connected = false;
|
|
81
|
+
this.options.onStatusChange?.('disconnected');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
isConnected(): boolean {
|
|
85
|
+
return this.connected;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async getSessions(): Promise<SessionListResult> {
|
|
89
|
+
return this.sendRequest<SessionListResult>('getSessions');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async connectToServer(params: ConnectParams): Promise<ConnectResult> {
|
|
93
|
+
return this.sendRequest<ConnectResult>('connect', params);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async disconnectFromServer(sessionId: string): Promise<DisconnectResult> {
|
|
97
|
+
return this.sendRequest<DisconnectResult>('disconnect', { sessionId });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async listTools(sessionId: string): Promise<ListToolsRpcResult> {
|
|
101
|
+
return this.sendRequest<ListToolsRpcResult>('listTools', { sessionId });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async callTool(
|
|
105
|
+
sessionId: string,
|
|
106
|
+
toolName: string,
|
|
107
|
+
toolArgs: Record<string, unknown>
|
|
108
|
+
): Promise<unknown> {
|
|
109
|
+
const result = await this.sendRequest('callTool', { sessionId, toolName, toolArgs });
|
|
110
|
+
this.emitUiEventIfPresent(result, sessionId, toolName);
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async restoreSession(sessionId: string): Promise<RestoreSessionResult> {
|
|
115
|
+
return this.sendRequest<RestoreSessionResult>('restoreSession', { sessionId });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async finishAuth(sessionId: string, code: string): Promise<FinishAuthResult> {
|
|
119
|
+
return this.sendRequest<FinishAuthResult>('finishAuth', { sessionId, code });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async listPrompts(sessionId: string): Promise<ListPromptsResult> {
|
|
123
|
+
return this.sendRequest<ListPromptsResult>('listPrompts', { sessionId });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async getPrompt(sessionId: string, name: string, args?: Record<string, string>): Promise<unknown> {
|
|
127
|
+
return this.sendRequest('getPrompt', { sessionId, name, args });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async listResources(sessionId: string): Promise<ListResourcesResult> {
|
|
131
|
+
return this.sendRequest<ListResourcesResult>('listResources', { sessionId });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async readResource(sessionId: string, uri: string): Promise<unknown> {
|
|
135
|
+
return this.sendRequest('readResource', { sessionId, uri });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
preloadToolUiResources(sessionId: string, tools: Array<{ name: string; _meta?: unknown }>): void {
|
|
139
|
+
for (const tool of tools) {
|
|
140
|
+
const uri = this.extractUiResourceUri(tool);
|
|
141
|
+
if (!uri || this.resourceCache.has(uri)) continue;
|
|
142
|
+
const promise = this.sendRequest('readResource', { sessionId, uri }).catch((err) => {
|
|
143
|
+
this.log(`Failed to preload resource ${uri}: ${err.message}`, 'warn');
|
|
144
|
+
this.resourceCache.delete(uri);
|
|
145
|
+
return null;
|
|
146
|
+
});
|
|
147
|
+
this.resourceCache.set(uri, promise);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
getOrFetchResource(sessionId: string, uri: string): Promise<unknown> {
|
|
152
|
+
const cached = this.resourceCache.get(uri);
|
|
153
|
+
if (cached) return cached;
|
|
154
|
+
const promise = this.sendRequest('readResource', { sessionId, uri });
|
|
155
|
+
this.resourceCache.set(uri, promise);
|
|
156
|
+
return promise;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
hasPreloadedResource(uri: string): boolean {
|
|
160
|
+
return this.resourceCache.has(uri);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
clearResourceCache(): void {
|
|
164
|
+
this.resourceCache.clear();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private async sendRequest<T = unknown>(method: McpRpcMethod, params?: McpRpcParams): Promise<T> {
|
|
168
|
+
if (!this.connected) {
|
|
169
|
+
this.connect();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.log(`RPC request via post_stream: ${method}`);
|
|
173
|
+
|
|
174
|
+
const request: McpRpcRequest = {
|
|
175
|
+
id: `rpc_${nanoid(10)}`,
|
|
176
|
+
method,
|
|
177
|
+
params,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const response = await fetch(this.buildUrl(), {
|
|
181
|
+
method: 'POST',
|
|
182
|
+
headers: this.buildHeaders(),
|
|
183
|
+
body: JSON.stringify(request),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (!response.ok) {
|
|
187
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const contentType = (response.headers.get('content-type') || '').toLowerCase();
|
|
191
|
+
if (!contentType.includes('text/event-stream')) {
|
|
192
|
+
const data = await response.json() as McpRpcResponse;
|
|
193
|
+
return this.parseRpcResponse<T>(data);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const data = await this.readRpcResponseFromStream(response);
|
|
197
|
+
return this.parseRpcResponse<T>(data);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private async readRpcResponseFromStream(response: Response): Promise<McpRpcResponse> {
|
|
201
|
+
if (!response.body) {
|
|
202
|
+
throw new Error('Streaming response body is missing');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const reader = response.body.getReader();
|
|
206
|
+
const decoder = new TextDecoder();
|
|
207
|
+
let buffer = '';
|
|
208
|
+
let rpcResponse: McpRpcResponse | null = null;
|
|
209
|
+
|
|
210
|
+
const dispatchBlock = (block: string) => {
|
|
211
|
+
const lines = block.split('\n');
|
|
212
|
+
let eventName = 'message';
|
|
213
|
+
const dataLines: string[] = [];
|
|
214
|
+
|
|
215
|
+
for (const rawLine of lines) {
|
|
216
|
+
const line = rawLine.replace(/\r$/, '');
|
|
217
|
+
if (!line || line.startsWith(':')) continue;
|
|
218
|
+
if (line.startsWith('event:')) {
|
|
219
|
+
eventName = line.slice('event:'.length).trim();
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (line.startsWith('data:')) {
|
|
223
|
+
dataLines.push(line.slice('data:'.length).trimStart());
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (!dataLines.length) return;
|
|
228
|
+
const payloadText = dataLines.join('\n');
|
|
229
|
+
let payload: unknown = payloadText;
|
|
230
|
+
try {
|
|
231
|
+
payload = JSON.parse(payloadText);
|
|
232
|
+
} catch {
|
|
233
|
+
// Keep raw text
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
switch (eventName) {
|
|
237
|
+
case 'connected':
|
|
238
|
+
this.options.onStatusChange?.('connected');
|
|
239
|
+
break;
|
|
240
|
+
case 'connection':
|
|
241
|
+
this.options.onConnectionEvent?.(payload as McpConnectionEvent);
|
|
242
|
+
break;
|
|
243
|
+
case 'observability':
|
|
244
|
+
this.options.onObservabilityEvent?.(payload as McpObservabilityEvent);
|
|
245
|
+
break;
|
|
246
|
+
case 'rpc-response':
|
|
247
|
+
rpcResponse = payload as McpRpcResponse;
|
|
248
|
+
break;
|
|
249
|
+
default:
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
while (true) {
|
|
255
|
+
const { value, done } = await reader.read();
|
|
256
|
+
if (done) break;
|
|
257
|
+
buffer += decoder.decode(value, { stream: true });
|
|
258
|
+
|
|
259
|
+
let separatorMatch = buffer.match(/\r?\n\r?\n/);
|
|
260
|
+
while (separatorMatch && separatorMatch.index !== undefined) {
|
|
261
|
+
const separatorIndex = separatorMatch.index;
|
|
262
|
+
const separatorLength = separatorMatch[0].length;
|
|
263
|
+
const block = buffer.slice(0, separatorIndex);
|
|
264
|
+
buffer = buffer.slice(separatorIndex + separatorLength);
|
|
265
|
+
dispatchBlock(block);
|
|
266
|
+
separatorMatch = buffer.match(/\r?\n\r?\n/);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (buffer.trim()) {
|
|
271
|
+
dispatchBlock(buffer);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (!rpcResponse) {
|
|
275
|
+
throw new Error('Missing rpc-response event in streamed RPC result');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return rpcResponse;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private parseRpcResponse<T>(data: McpRpcResponse): T {
|
|
282
|
+
if ('result' in data) {
|
|
283
|
+
return data.result as T;
|
|
284
|
+
}
|
|
285
|
+
if ('error' in data && data.error) {
|
|
286
|
+
throw new Error(data.error.message || 'Unknown RPC error');
|
|
287
|
+
}
|
|
288
|
+
// JSON omits `result` when it is `undefined` (response becomes `{ id: ... }`).
|
|
289
|
+
// Treat that shape as a successful void result.
|
|
290
|
+
if (data && typeof data === 'object' && 'id' in data) {
|
|
291
|
+
return undefined as T;
|
|
292
|
+
}
|
|
293
|
+
throw new Error('Invalid RPC response format');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private buildUrl(): string {
|
|
297
|
+
const url = new URL(this.options.url, globalThis.location?.origin);
|
|
298
|
+
url.searchParams.set('identity', this.options.identity);
|
|
299
|
+
if (this.options.authToken) {
|
|
300
|
+
url.searchParams.set('token', this.options.authToken);
|
|
301
|
+
}
|
|
302
|
+
return url.toString();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private buildHeaders(): HeadersInit {
|
|
306
|
+
const headers: HeadersInit = {
|
|
307
|
+
'Content-Type': 'application/json',
|
|
308
|
+
'Accept': 'text/event-stream',
|
|
309
|
+
};
|
|
310
|
+
if (this.options.authToken) {
|
|
311
|
+
headers['Authorization'] = `Bearer ${this.options.authToken}`;
|
|
312
|
+
}
|
|
313
|
+
return headers;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private extractUiResourceUri(tool: { name: string; _meta?: unknown }): string | undefined {
|
|
317
|
+
const meta = (tool._meta as { ui?: ToolUiMetadata })?.ui;
|
|
318
|
+
if (!meta || typeof meta !== 'object') return undefined;
|
|
319
|
+
if (meta.visibility && !meta.visibility.includes('app')) return undefined;
|
|
320
|
+
return meta.resourceUri ?? meta.uri;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private emitUiEventIfPresent(result: unknown, sessionId: string, toolName: string): void {
|
|
324
|
+
const meta = (result as { _meta?: { ui?: ToolUiMetadata } })?._meta;
|
|
325
|
+
const resourceUri = meta?.ui?.resourceUri ?? (meta as any)?.['ui/resourceUri'];
|
|
326
|
+
|
|
327
|
+
if (resourceUri) {
|
|
328
|
+
this.options.onEvent?.({
|
|
329
|
+
type: 'mcp-apps-ui',
|
|
330
|
+
sessionId,
|
|
331
|
+
resourceUri,
|
|
332
|
+
toolName,
|
|
333
|
+
result,
|
|
334
|
+
timestamp: Date.now(),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private log(message: string, level: 'info' | 'warn' | 'error' = 'info'): void {
|
|
340
|
+
if (!this.options.debug && level === 'info') return;
|
|
341
|
+
|
|
342
|
+
const prefix = '[SSEClient]';
|
|
343
|
+
switch (level) {
|
|
344
|
+
case 'warn':
|
|
345
|
+
console.warn(prefix, message);
|
|
346
|
+
break;
|
|
347
|
+
case 'error':
|
|
348
|
+
console.error(prefix, message);
|
|
349
|
+
break;
|
|
350
|
+
default:
|
|
351
|
+
console.log(prefix, message);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|