File tree 4 files changed +71
-1
lines changed
4 files changed +71
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ export class InMemoryTransport implements Transport {
11
11
onclose ?: ( ) => void ;
12
12
onerror ?: ( error : Error ) => void ;
13
13
onmessage ?: ( message : JSONRPCMessage ) => void ;
14
+ sessionId ?: string ;
14
15
15
16
/**
16
17
* Creates a pair of linked in-memory transports that can communicate with each other. One should be passed to a Client and one to a Server.
Original file line number Diff line number Diff line change @@ -323,6 +323,59 @@ describe("tool()", () => {
323
323
mcpServer . tool ( "tool2" , ( ) => ( { content : [ ] } ) ) ;
324
324
} ) ;
325
325
326
+ test ( "should pass sessionId to tool callback via RequestHandlerExtra" , async ( ) => {
327
+ const mcpServer = new McpServer ( {
328
+ name : "test server" ,
329
+ version : "1.0" ,
330
+ } ) ;
331
+
332
+ const client = new Client (
333
+ {
334
+ name : "test client" ,
335
+ version : "1.0" ,
336
+ } ,
337
+ {
338
+ capabilities : {
339
+ tools : { } ,
340
+ } ,
341
+ } ,
342
+ ) ;
343
+
344
+ let receivedSessionId : string | undefined ;
345
+ mcpServer . tool ( "test-tool" , async ( extra ) => {
346
+ receivedSessionId = extra . sessionId ;
347
+ return {
348
+ content : [
349
+ {
350
+ type : "text" ,
351
+ text : "Test response" ,
352
+ } ,
353
+ ] ,
354
+ } ;
355
+ } ) ;
356
+
357
+ const [ clientTransport , serverTransport ] = InMemoryTransport . createLinkedPair ( ) ;
358
+ // Set a test sessionId on the server transport
359
+ serverTransport . sessionId = "test-session-123" ;
360
+
361
+ await Promise . all ( [
362
+ client . connect ( clientTransport ) ,
363
+ mcpServer . server . connect ( serverTransport ) ,
364
+ ] ) ;
365
+
366
+ await client . request (
367
+ {
368
+ method : "tools/call" ,
369
+ params : {
370
+ name : "test-tool" ,
371
+ } ,
372
+ } ,
373
+ CallToolResultSchema ,
374
+ ) ;
375
+
376
+ expect ( receivedSessionId ) . toBe ( "test-session-123" ) ;
377
+ } ) ;
378
+
326
379
test ( "should allow client to call server tools" , async ( ) => {
327
380
const mcpServer = new McpServer ( {
328
381
name : "test server" ,
Original file line number Diff line number Diff line change @@ -88,6 +88,11 @@ export type RequestHandlerExtra = {
88
88
* An abort signal used to communicate if the request was cancelled from the sender's side.
89
89
*/
90
90
signal : AbortSignal ;
91
+
92
+ /**
93
+ * The session ID from the transport, if available.
94
+ */
95
+ sessionId ?: string ;
91
96
} ;
92
97
93
98
/**
@@ -307,9 +312,15 @@ export abstract class Protocol<
307
312
const abortController = new AbortController ( ) ;
308
313
this . _requestHandlerAbortControllers . set ( request . id , abortController ) ;
309
314
315
+ // Create extra object with both abort signal and sessionId from transport
316
+ const extra : RequestHandlerExtra = {
317
+ signal : abortController . signal ,
318
+ sessionId : this . _transport ?. sessionId ,
319
+ } ;
320
+
310
321
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
311
322
Promise . resolve ( )
312
- . then ( ( ) => handler ( request , { signal : abortController . signal } ) )
323
+ . then ( ( ) => handler ( request , extra ) )
313
324
. then (
314
325
( result ) => {
315
326
if ( abortController . signal . aborted ) {
Original file line number Diff line number Diff line change @@ -41,4 +41,9 @@ export interface Transport {
41
41
* Callback for when a message (request or response) is received over the connection.
42
42
*/
43
43
onmessage ?: ( message : JSONRPCMessage ) => void ;
44
+
45
+ /**
46
+ * The session ID generated for this connection.
47
+ */
48
+ sessionId ?: string ;
44
49
}
You can’t perform that action at this time.
0 commit comments