File tree 2 files changed +54
-0
lines changed
2 files changed +54
-0
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" ,
You can’t perform that action at this time.
0 commit comments