Fix client to accept any supported server protocol version during negotiation#1537
Closed
mikekistler wants to merge 2 commits intomainfrom
Closed
Fix client to accept any supported server protocol version during negotiation#1537mikekistler wants to merge 2 commits intomainfrom
mikekistler wants to merge 2 commits intomainfrom
Conversation
Contributor
Author
|
This needs fixes. Moving to draft for now. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes MCP client protocol-version negotiation so an explicitly requested version doesn’t require an exact match from the server, and also introduces Streamable HTTP header standardization support (standard + custom parameter headers) with accompanying conformance and unit tests.
Changes:
- Relax client initialization validation to accept any server protocol version in
SupportedProtocolVersions(while still allowing an explicitProtocolVersionas a forward-compat fallback). - Add
x-mcp-headersupport for tools (schema extension + client header emission + server-side validation + header encoding helpers). - Expand conformance coverage (client + server) and add new unit tests for header behavior and protocol downgrade negotiation.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs | Adds unit tests ensuring x-mcp-header schema extensions are produced and validated. |
| tests/ModelContextProtocol.Tests/Server/McpHeaderAttributeTests.cs | Adds constructor validation tests for McpHeaderAttribute. |
| tests/ModelContextProtocol.Tests/Client/McpRequestHeadersTests.cs | Adds tests for new HTTP header constants and HeaderMismatch error code value. |
| tests/ModelContextProtocol.Tests/Client/McpHeaderEncoderTests.cs | Adds tests for header value encoding/decoding rules. |
| tests/ModelContextProtocol.Tests/Client/McpClientTests.cs | Adds downgrade negotiation test (AcceptsLowerServerProtocolVersion). |
| tests/ModelContextProtocol.ConformanceServer/Tools/ConformanceTools.cs | Adds a conformance tool annotated with McpHeader for header scenarios. |
| tests/ModelContextProtocol.ConformanceClient/Program.cs | Adds conformance scenarios and allows protocol version override via env var. |
| tests/ModelContextProtocol.AspNetCore.Tests/ServerConformanceTests.cs | Adds server conformance scenarios for HTTP header validation. |
| tests/ModelContextProtocol.AspNetCore.Tests/ClientConformanceTests.cs | Adds client conformance scenarios for HTTP header standardization. |
| tests/Common/Utils/NodeHelpers.cs | Passes MCP_CONFORMANCE_PROTOCOL_VERSION through to the Node conformance runner. |
| src/ModelContextProtocol.Core/Server/McpHeaderAttribute.cs | Introduces McpHeaderAttribute and header-name validation logic. |
| src/ModelContextProtocol.Core/Server/AIFunctionMcpServerTool.cs | Post-processes tool schemas to add x-mcp-header extensions from attributes. |
| src/ModelContextProtocol.Core/Protocol/McpHttpHeaders.cs | Adds constants for MCP-specific HTTP headers and a feature-gating version constant. |
| src/ModelContextProtocol.Core/McpSessionHandler.cs | Adds DRAFT-2026-v1 to supported protocol versions. |
| src/ModelContextProtocol.Core/McpErrorCode.cs | Adds HeaderMismatch = -32001 for Streamable HTTP header validation failures. |
| src/ModelContextProtocol.Core/Client/StreamableHttpClientSessionTransport.cs | Adds standard/custom MCP request headers to Streamable HTTP requests. |
| src/ModelContextProtocol.Core/Client/McpHeaderExtractor.cs | Extracts x-mcp-header-annotated args and mirrors them into HTTP headers. |
| src/ModelContextProtocol.Core/Client/McpHeaderEncoder.cs | Implements encoding/decoding rules for header-safe parameter values. |
| src/ModelContextProtocol.Core/Client/McpClientImpl.cs | Fixes protocol negotiation validation; caches tools for custom header emission. |
| src/ModelContextProtocol.Core/Client/McpClient.Methods.cs | Hooks tool discovery to allow caching tool definitions on list. |
| src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs | Adds server-side validation of standard/custom MCP headers for Streamable HTTP. |
334812c to
a9a17cf
Compare
a9a17cf to
888e978
Compare
Contributor
Author
|
Not a bug. Closing. |
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.
Fixes #1536
When
McpClientOptions.ProtocolVersionis explicitly set, the client incorrectly requires the server to respond with that exact version. If the server responds with a different but supported version (e.g., negotiating down fromDRAFT-2026-v1to2025-11-25), the client throwsMcpException: Server protocol version mismatch.Changes
AcceptsLowerServerProtocolVersion): Verifies the client accepts a server responding with a lower supported protocol version when the client explicitly requests a higher one.McpClientImpl.csto accept any version inSupportedProtocolVersions, regardless of whetherProtocolVersionwas explicitly set. The explicit version is still accepted as a fallback for forward compatibility.