fix: prevent responses being sent to wrong client when multiple transports connect #820
+196
−4
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.
This PR enhances the Protocol class to correctly handle multiple client connections by ensuring responses are always sent back through their originating transport. Currently, when multiple clients connect to a Protocol instance, responses can be misdirected due to the transport reference being overwritten on each new connection.
Motivation and Context
The Current Behavior
The Protocol class maintains a single
_transport
reference that gets updated each timeconnect()
is called. This creates a scenario where responses can be sent to a different transport than the one that originated the request:This is not a theoretical edge case - it happens whenever clients connect while requests are being processed.
Community Reports
This behavior has been observed and reported by multiple users:
These reports highlight the challenges developers face when trying to scale MCP servers in production environments.
Why This Must Be Fixed
1. Security Risk
Responses containing sensitive data can be sent to the wrong client. In a scenario where Client A queries private data and Client B connects during processing, Client B receives Client A's confidential response. This is not just a privacy concern but a potential compliance violation (GDPR, HIPAA, etc.).
2. Fundamental Correctness
This violates the basic contract of request-response protocols - that responses return to their originator. No amount of application-level workarounds can reliably prevent this at scale.
3. Architectural Limitation
Current workaround requires one server instance per client, which:
4. Specification Alignment
The MCP architecture documentation states "each client having a 1:1 relationship with a particular server" from the host's perspective - describing the logical relationship where each client connects to its designated server. This architectural principle doesn't require servers to be limited to single transport connections at the implementation level.
Indeed, allowing servers to handle multiple transport connections is common in protocol implementations and is supported in other MCP SDKs like Python.
5. Production Impact
Teams deploying MCP in production environments have encountered this limitation, as evidenced by issues #204 and #243. The current workaround of creating separate server instances per client adds complexity and resource overhead that could be avoided with proper multi-transport support.
How Has This Been Tested?
Test Implementation
Added comprehensive tests in
protocol-transport-handling.test.ts
that demonstrate:Test Scenarios Covered
Reproducing the Bug
Breaking Changes
No breaking changes. This fix is backward compatible and requires no changes to existing code.
Types of changes
Checklist
Additional context
Implementation Approach
The fix is minimal and elegant - we capture the transport reference at request time using a closure:
This ensures each request's lifecycle is bound to its originating transport, regardless of subsequent connections.
Current Workaround vs Proper Fix
Current Workaround (inefficient):
With This Fix (proper multi-client support):
Why This Wasn't Caught Earlier
The bug only manifests when:
Many examples and tests use single clients or synchronous handlers, masking the issue.
Alignment with MCP Architecture
The MCP specification states that hosts manage multiple clients, each with a 1:1 relationship with a server. This fix ensures that relationship is properly maintained at the transport level, allowing a single server process to correctly handle multiple client connections as intended by the architecture.