fix(examples): create fresh McpServer per client in three examples#1610
Merged
mattzcarey merged 1 commit intomodelcontextprotocol:mainfrom Mar 2, 2026
Merged
Conversation
Move McpServer creation and tool/resource registration into a getServer() factory function that is called for each new client connection, matching the pattern used by simpleStreamableHttp and jsonResponseStreamableHttp. Sharing a single McpServer instance across multiple clients can cause issues with concurrent connections. The three updated examples are: - elicitationFormExample.ts - standaloneSseWithGetStreamableHttp.ts - ssePollingExample.ts Fixes modelcontextprotocol#1278
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
mattzcarey
reviewed
Mar 2, 2026
Comment on lines
+148
to
+158
| // Close all active transports to properly clean up resources | ||
| for (const sessionId in transports) { | ||
| try { | ||
| console.log(`Closing transport for session ${sessionId}`); | ||
| await transports[sessionId]!.close(); | ||
| delete transports[sessionId]; | ||
| delete servers[sessionId]; | ||
| } catch (error) { | ||
| console.error(`Error closing transport for session ${sessionId}:`, error); | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
this shouldnt be necessary?
mattzcarey
approved these changes
Mar 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1278
Three example files were sharing a single
McpServerinstance across all client connections. This can cause issues when multiple clients connect concurrently, as the server's internal state (tools, resources, sessions) becomes entangled between unrelated clients.This PR updates the following examples to create a fresh
McpServerper client connection using agetServer()factory function, matching the pattern already used bysimpleStreamableHttp.tsandjsonResponseStreamableHttp.ts:elicitationFormExample.ts— WrappedMcpServercreation and all three tool registrations (register_user,create_event,update_shipping_address) intogetServer(), called on each new initialization request.standaloneSseWithGetStreamableHttp.ts— MovedMcpServercreation and initial resource registration intogetServer(). TheaddResourcehelper now takes a server parameter. The periodic resource change interval adds resources to all active server instances. Added proper cleanup of bothtransportsandserversmaps on close/shutdown.ssePollingExample.ts— WrappedMcpServercreation andlong-tasktool registration intogetServer(), called when creating a new transport.Test plan
pnpm build:all)pnpm typecheck:all)pnpm lint:all)