Repository navigation
Conversation
Adds two SEP-3371 extensibility capabilities to McpClient:
1. Client JSON-RPC message filters (stable):
- McpClientFilters, McpClientMessageFilters, McpClientMessageFilter,
McpClientMessageHandler, and McpClientMessageContext on
McpClientOptions.Filters.Message (IncomingFilters / OutgoingFilters).
- Intercepts all incoming and outgoing JSON-RPC messages in deterministic
registration order, sharing per-message state via Items and preserving
JsonRpcMessage.Context when replacing JsonRpcMessage with a context-less
message.
- Validates and snapshots filters before transport connect and composes
filter pipelines once per session in McpSessionHandler.
- Isolates per-attempt Items across MRTR input_required retries.
2. Client custom request handlers ([Experimental(MCPEXP002)]):
- McpClientRequestHandler and McpClientRequestContext<TParams> on
McpClientOptions.RequestHandlers for server-initiated extension requests.
- Validates against reserved core methods, duplicate registrations, and
already-registered built-in handlers before transport connect.
3. Shared McpSessionHandler pipeline improvements (client and server):
- Composes incoming/outgoing message filter pipelines once at construction.
- Fails pending requests immediately when an incoming filter throws on or
suppresses a response/error, or when an outgoing filter suppresses a
request without sending it.
- Prevents sending a duplicate JSON-RPC error reply if an incoming filter
throws after a response has already been sent for that request.
Refs modelcontextprotocol#2051
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This branch has not been deployed
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.
Part of #2051 (addresses two of the SEP-3371 extensibility gaps for
McpClient).Summary
1. Client JSON-RPC message filters (stable)
Adds incoming and outgoing JSON-RPC message filtering on
McpClientOptions.Filters.Message, mirroring the server-sideMcpMessageFilters/MessageContextmiddleware pattern:McpClientFilters/McpClientMessageFilters(McpClientOptions.Filters.Message.IncomingFilters&OutgoingFilters)McpClientMessageFilter/McpClientMessageHandlerdelegatesMcpClientMessageContextexposingClient,JsonRpcMessage(replacing it with a context-less message preserves the priorJsonRpcMessageContext, includingItemsand transport metadata), and per-messageItemsIClientTransport.ConnectAsyncin bothMcpClient.CreateAsyncandMcpClient.ResumeSessionAsync, and composed once per sessioninput_requiredretries cloneJsonRpcMessageContextwith fresh per-attemptItems(preserving pre-filter entries such asToolContextKey)docs/concepts/filters.mdwith client message filter usage, handshake caveats (ServerInfo/ServerCapabilitiesare unavailable duringserver/discover/initialize), and suppression/error semantics2. Client custom request handlers (
[Experimental(MCPEXP002)])Adds custom server-to-client request handler registration on
McpClientOptions.RequestHandlersfor extension methods:McpClientRequestHandler(rawJsonNode?handler + AOT-friendly genericCreate<TParams, TResult>overloads acceptingJsonTypeInfo<T>orJsonSerializerOptions)McpClientRequestContext<TParams>(derives fromMcpClientMessageContext, sharesItemswith incoming message filters, and enforcesJsonRpcRequestonJsonRpcMessage)docs/list-of-diagnostics.mdforMCPEXP0023. Shared
McpSessionHandlerpipeline improvements (affects both client and server)Because
McpClientandMcpServershareMcpSessionHandler, this PR also includes three shared pipeline improvements with both client- and server-side tests:McpSessionHandlerconstructor rather than rebuilt per message, so filter factory closures retain state across messages and null-returning factories fail at construction.JsonRpcResponse/JsonRpcErrormatching a pending request, or when an outgoing filter suppresses aJsonRpcRequestwithout sending it, the awaiting caller now fails immediately (with the filter's exception orInvalidOperationException) instead of waiting until cancellation. Previously, incoming filter exceptions on responses were only logged.nextthrows: Tracks per-requestResponseSentstate so an incoming filter throwing afternext(or after sending its own response) does not send a secondJsonRpcErrorwith the same request ID.Testing
tests/ModelContextProtocol.Tests/Client/McpClientMessageFilterTests.cstests/ModelContextProtocol.Tests/Client/McpClientRequestHandlerTests.csMcpSessionHandlerchanges intests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.csdotnet buildacross all projects and target frameworks (0 warnings, 0 errors)net8.0,net9.0(reflection-disabled/AOT), andnet10.0: 63/63 passed on each TFMModelContextProtocol.Tests(net10.0, non-manual): 3,717/3,717 passedModelContextProtocol.AspNetCore.Tests(net10.0, non-manual): 754/754 passeddotnet pack src/ModelContextProtocol.Core/ModelContextProtocol.Core.csproj -c Release(ApiCompat): succeededNote
This pull request description and code were generated with assistance from GitHub Copilot.