feat: add WebSocket server transport #1280
Open
+180
−10
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.
Add a WebSocket server transport (
WebSocketServerTransport) to complement the existingWebSocketClientTransport, allowing MCP servers to communicate over WebSockets in addition to stdio and Streamable HTTP.Motivation and Context
The SDK currently supports:
as well as a WebSocket client transport.
However, there is no corresponding server-side WebSocket transport. This makes it harder to:
This change introduces
WebSocketServerTransport, which implements the sharedTransportinterface and allows MCP servers to accept WebSocket connections using themcpsubprotocol.How Has This Been Tested?
Ran existing test cases
Example you can write once you’ve tested:
Ran
npm run buildandnpm test.Manually tested a simple MCP server using
WebSocketServerTransport:ws://localhost:8787/mcp.WebSocketClientTransport.Breaking Changes
No breaking changes are expected:
Types of changes
Checklist
Additional context
src/server/websocket.tsWebSocketServerTransportusing thewslibrary.mcpsubprotocol and validates incoming messages withJSONRPCMessageSchema.WebSocketServerTransportfromsrc/server/index.tsso it can be imported via@modelcontextprotocol/sdk/server.StdioServerTransport/WebSocketClientTransportpatterns for consistency.