-
Notifications
You must be signed in to change notification settings - Fork 614
Add support for message-level filters to McpServer #1207
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
base: main
Are you sure you want to change the base?
Conversation
This commit adds support for server-side message filters that can intercept and process incoming and outgoing JSON-RPC messages. It also includes: - Message filter infrastructure (McpMessageFilter, McpMessageHandler) - MessageContext for accessing message metadata during filter execution - Extended RequestContext with message filter support - McpServerBuilderExtensions for registering message filters - Comprehensive tests for message filter functionality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds comprehensive message-level filtering infrastructure to the MCP Server, enabling interception of all incoming and outgoing JSON-RPC messages before they reach request-specific handlers or are sent to clients.
Changes:
- Introduces
MessageContextbase class and refactorsRequestContext<TParams>to extend it, providing unified context for both message and request filters - Adds message filter infrastructure (
McpMessageFilter,McpMessageHandler,JsonRpcMessageFilter) that operates at the JSON-RPC protocol level - Implements filter pipeline building in
McpServerImpland integrates filters intoMcpSessionHandlerfor both incoming and outgoing messages - Provides public API methods (
AddIncomingMessageFilter,AddOutgoingMessageFilter,AddMessageFilter) with comprehensive documentation - Includes extensive test coverage demonstrating filter ordering, context access, handler skipping, and additional message sending capabilities
- Fixes an unrelated bug in
ClientOAuthProviderwhere auth server metadata wasn't stored after successful token refresh
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/ModelContextProtocol.Core/Server/MessageContext.cs |
New base class providing context for message filters with Server, Services, User, Items, and JsonRpcMessage properties |
src/ModelContextProtocol.Core/Server/RequestContext.cs |
Refactored to extend MessageContext, maintaining backward compatibility while inheriting common context properties |
src/ModelContextProtocol.Core/Server/McpMessageHandler.cs |
New delegate type for message-level handlers that process MessageContext |
src/ModelContextProtocol.Core/Server/McpMessageFilter.cs |
New delegate type for wrapping message handlers in filter pipeline |
src/ModelContextProtocol.Core/JsonRpcMessageFilter.cs |
Internal delegate for low-level JSON-RPC message filtering |
src/ModelContextProtocol.Core/Server/McpServerFilters.cs |
Adds IncomingMessageFilters and OutgoingMessageFilters collections to store registered filters |
src/ModelContextProtocol.Core/Server/McpServerImpl.cs |
Implements BuildMessageFilterPipeline method to construct filter chains and passes them to McpSessionHandler |
src/ModelContextProtocol.Core/McpSessionHandler.cs |
Integrates incoming and outgoing message filters into message handling and sending pipelines |
src/ModelContextProtocol/McpServerBuilderExtensions.cs |
Adds public API methods for registering message filters with comprehensive XML documentation |
src/ModelContextProtocol.Core/Client/McpClientImpl.cs |
Updates constructor to pass null filters (clients don't support message filters) |
src/ModelContextProtocol.Core/Server/StreamableHttpPostTransport.cs |
Reorders Context initialization to occur before conditional RelatedTransport assignment |
src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs |
Fixes bug where auth server metadata wasn't stored after successful token refresh |
docs/concepts/filters.md |
Comprehensive documentation of message filters including usage examples, execution order, and best practices |
tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs |
Extensive test coverage including filter ordering, context access, handler skipping, exception handling, and message manipulation |
tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.cs |
Changes class from public to abstract (best practice for base test classes) |
This PR adds support for server-side message filters that can intercept
and process incoming and outgoing JSON-RPC messages. It also includes: