Skip to content

Add sessionID to RequestHandlerExtra type for tool invocations so multi-user services can distinguish users #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/inMemory.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ export class InMemoryTransport implements Transport {
onclose?: () => void;
onerror?: (error: Error) => void;
onmessage?: (message: JSONRPCMessage) => void;
sessionId?: string;

/**
* 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.
53 changes: 53 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
@@ -323,6 +323,59 @@ describe("tool()", () => {
mcpServer.tool("tool2", () => ({ content: [] }));
});

test("should pass sessionId to tool callback via RequestHandlerExtra", async () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

const client = new Client(
{
name: "test client",
version: "1.0",
},
{
capabilities: {
tools: {},
},
},
);

let receivedSessionId: string | undefined;
mcpServer.tool("test-tool", async (extra) => {
receivedSessionId = extra.sessionId;
return {
content: [
{
type: "text",
text: "Test response",
},
],
};
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
// Set a test sessionId on the server transport
serverTransport.sessionId = "test-session-123";

await Promise.all([
client.connect(clientTransport),
mcpServer.server.connect(serverTransport),
]);

await client.request(
{
method: "tools/call",
params: {
name: "test-tool",
},
},
CallToolResultSchema,
);

expect(receivedSessionId).toBe("test-session-123");
});

test("should allow client to call server tools", async () => {
const mcpServer = new McpServer({
name: "test server",
13 changes: 12 additions & 1 deletion src/shared/protocol.ts
Original file line number Diff line number Diff line change
@@ -88,6 +88,11 @@ export type RequestHandlerExtra = {
* An abort signal used to communicate if the request was cancelled from the sender's side.
*/
signal: AbortSignal;

/**
* The session ID from the transport, if available.
*/
sessionId?: string;
};

/**
@@ -307,9 +312,15 @@ export abstract class Protocol<
const abortController = new AbortController();
this._requestHandlerAbortControllers.set(request.id, abortController);

// Create extra object with both abort signal and sessionId from transport
const extra: RequestHandlerExtra = {
signal: abortController.signal,
sessionId: this._transport?.sessionId,
};

// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
Promise.resolve()
.then(() => handler(request, { signal: abortController.signal }))
.then(() => handler(request, extra))
.then(
(result) => {
if (abortController.signal.aborted) {
5 changes: 5 additions & 0 deletions src/shared/transport.ts
Original file line number Diff line number Diff line change
@@ -41,4 +41,9 @@ export interface Transport {
* Callback for when a message (request or response) is received over the connection.
*/
onmessage?: (message: JSONRPCMessage) => void;

/**
* The session ID generated for this connection.
*/
sessionId?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a docstring here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You bet, I'll take a look this afternoon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey sir - added a test and a doc string, let me know if you need anything else!

}