Skip to content

Commit c396b4a

Browse files
committed
added test for session id using in memory transport
1 parent 57f3b07 commit c396b4a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/inMemory.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class InMemoryTransport implements Transport {
1111
onclose?: () => void;
1212
onerror?: (error: Error) => void;
1313
onmessage?: (message: JSONRPCMessage) => void;
14+
sessionId?: string;
1415

1516
/**
1617
* 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.

src/server/mcp.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,59 @@ describe("tool()", () => {
323323
mcpServer.tool("tool2", () => ({ content: [] }));
324324
});
325325

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+
326379
test("should allow client to call server tools", async () => {
327380
const mcpServer = new McpServer({
328381
name: "test server",

0 commit comments

Comments
 (0)