Skip to content

Conversation

@KhizarA77
Copy link

@KhizarA77 KhizarA77 commented Dec 10, 2025

Add a WebSocket server transport (WebSocketServerTransport) to complement the existing WebSocketClientTransport, allowing MCP servers to communicate over WebSockets in addition to stdio and Streamable HTTP.

Motivation and Context

The SDK currently supports:

  • stdio-based transports, and
  • Streamable HTTP (SSE/JSON) transports,
    as well as a WebSocket client transport.

However, there is no corresponding server-side WebSocket transport. This makes it harder to:

  • host MCP servers behind a WebSocket endpoint, or
  • integrate with environments where WebSockets are the preferred or only option.

This change introduces WebSocketServerTransport, which implements the shared Transport interface and allows MCP servers to accept WebSocket connections using the mcp subprotocol.

How Has This Been Tested?

  • Ran existing test cases
    Example you can write once you’ve tested:

  • Ran npm run build and npm test.

  • Manually tested a simple MCP server using WebSocketServerTransport:

    • Started a server on ws://localhost:8787/mcp.
    • Connected using WebSocketClientTransport.
    • Verified JSON-RPC requests and responses flowed correctly in both directions.

Breaking Changes

No breaking changes are expected:

  • This adds a new transport implementation and an export.
  • Existing transports (stdio and Streamable HTTP) and APIs are unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  • New file: src/server/websocket.ts
    • Implements WebSocketServerTransport using the ws library.
    • Accepts a single WebSocket client per transport instance.
    • Enforces the mcp subprotocol and validates incoming messages with JSONRPCMessageSchema.
  • Exported WebSocketServerTransport from src/server/index.ts so it can be imported via @modelcontextprotocol/sdk/server.
  • Designed to mirror the existing StdioServerTransport/WebSocketClientTransport patterns for consistency.

@KhizarA77 KhizarA77 requested a review from a team as a code owner December 10, 2025 10:42
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 10, 2025

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/sdk@1280

commit: 42a8d6d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant