8000 feat: implement MCP-Protocol-Version header requirement for HTTP transport by ochafik · Pull Request #614 · modelcontextprotocol/typescript-sdk · GitHub
[go: up one dir, main page]

Skip to content

feat: implement MCP-Protocol-Version header requirement for HTTP transport #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 13, 2025

Conversation

ochafik
Copy link
Contributor
@ochafik ochafik commented Jun 11, 2025

Implement MCP-Protocol-Version header requirement for HTTP transport (closes #604)

Motivation and Context

This PR implements the MCP-Protocol-Version header requirement as specified in:

(see similar update in Python SDK: modelcontextprotocol/python-sdk#898)

Clients extract and store the negotiated protocol version from the initialization response, include the MCP-Protocol-Version header in all subsequent HTTP requests after initialization.

Servers validate the presence and correctness of this header for non-initialization requests, returning 400 Bad Request for missing or invalid protocol version headers.

How Has This Been Tested?

Updated all existing tests to include the MCP-Protocol-Version header. Added new tests for header validation.

Breaking Changes

This is a breaking change that requires both clients and servers to be updated. Clients that don't send the MCP-Protocol-Version header will be stuck at the protocol version 2025-03-26.

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

Implementation Details

The implementation adds protocol version tracking to all HTTP-bas 8000 ed transports:

  1. Client-side changes:

    • Added protocolVersion?: string property to the Transport interface
    • Client stores the negotiated protocol version from the initialization response
    • HTTP transports (SSE and Streamable HTTP) include the Mcp-Protocol-Version header in all requests after initialization
    • The header value matches the negotiated protocol version from the init response
  2. Server-side changes:

    • Server sets transport.protocolVersion after successful initialization
    • Streamable HTTP server validates the Mcp-Protocol-Version header on all non-initialization requests
    • Returns 400 Bad Request for unsupported protocol versions
    • Logs a warning if the header version differs from the negotiated version (but still accepts the request if the version is supported)
  3. Key behaviors:

    • The header is optional during initialization but required for all subsequent requests
    • If a client sends a different but supported protocol version, the server logs a warning but processes the request
    • This ensures clients properly track and use the negotiated protocol version throughout the session

… it doesn't match negotiated version (it SHOULD match)
@ochafik ochafik requested a review from felixweinberger June 11, 2025 16:57
@ochafik ochafik marked this pull request as ready for review June 11, 2025 17:53
Copy link
Contributor
@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM barring one loose equality check! Otherwise only nits :)

Comment on lines 223 to 224
// Note: this._requestInit?.headers already set in _commonHeaders
// const headers = new Headers({ ...commonHeaders, ...this._requestInit?.headers });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: personally don't think this comment adds much value and just adds potential stuff that becomes outdated over time, so would propose just removing the comments

that this._requestInit?.headers is already set in _commonHeaders is imo sufficiently clear from its implementation

private validateProtocolVersion(req: IncomingMessage, res: ServerResponse): boolean {
let protocolVersion = req.headers["mcp-protocol-version"];
if (Array.isArray(protocolVersion)) {
protocolVersion = protocolVersion[protocolVersion.length - 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make sure protocolVersion.length > 0? not sure if req.headers would ever return an empty array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was implicitly relying on the fact that [][-1] === undefined tbh, but yes in practice if a header isn't set we get undefined, if it's set once we get a string, and if set mode than once we get an array of the values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah TIL that returns undefined and not an error!

Copy link
Contributor
@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this!

In general looks good, left some comments to make it consistent with the spec and some small fixes suggestions

Comment on lines +542 to +544
if (Array.isArray(protocolVersion)) {
protocolVersion = protocolVersion[protocolVersion.length - 1];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this ever be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type-wise it's a possiblity (happens if the client sets the header twice, which is legal; I'd rather not explode when that's the case)

@@ -165,6 +165,8 @@ export class Client<

this._serverCapabilities = result.capabilities;
this._serverVersion = result.serverInfo;
// HTTP transports must set the protocol version in each header after initialization.
transport.protocolVersion = result.protocolVersion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not to set the property directly, instead, expose setProtocolVersion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added setProtocolVersion (optional to avoid having to implement it in all transports - incl. test transports)

@ochafik ochafik requested a review from ihrpr June 13, 2025 17:27
Copy link
Contributor
@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🚀

@ihrpr ihrpr merged commit 6b3d326 into main Jun 13, 2025
5 checks passed
@ihrpr ihrpr deleted the ochafik/protocol-version branch June 13, 2025 17:31
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.

require negotiated version when using HTTP
3 participants
0