Skip to content

Multiple clients to the same SSE connection? #204

Open
@chazcb

Description

@chazcb

👋 Because server.connect(transport) replaces any existing transport for a Server, I've been struggling to understand the intended pattern for simultaneous SSE client connections. Is it intended that an SSE MCP server can only have one Client?

Example:

const sessions = new Map<string, SSEServerTransport>();
const server = new McpServer({ name: "test-server", version: "1.0.0" });

app.get("/sse", (req, res) => {
  const transport = new SSEServerTransport("/messages", res);
  sessions.set(transport.sessionId, transport);
  server.connect(transport);  // ❌ Each call to /sse overwrites previous transport for server!
});

// Setup message endpoint
app.post("/messages", async (req, res) => {
  const sessionId = req.query.sessionId as string;
  const transport = sessions.get(sessionId);
  if (!transport) {
    return res.status(400).json({ error: "Invalid session ID" });
  }
  await transport.handlePostMessage(req, res);
});

// When multiple clients connect:
const client1 = await createClient("client-1");  // ✅ First client works
const client2 = await createClient("client-2");  // ❌ Second client connection breaks client1

// Now when client1 calls a tool:
await client1.callTool({ name: "echo", arguments: { message: "hello" } });
// ❌ Response goes to client2 instead of client1, even though we're tracking sessions

One workaround for this is to create a clone of the McpServer within the /sse handler, but that doesn't seem to match with the concept of a "sessionId".

Would appreciate any guidance here, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions