Skip to content

Conversation

@halter73
Copy link
Contributor

This PR 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

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
Copy link
Contributor

Copilot AI left a 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 MessageContext base class and refactors RequestContext<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 McpServerImpl and integrates filters into McpSessionHandler for 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 ClientOAuthProvider where 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)

stephentoub
stephentoub previously approved these changes Jan 29, 2026
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.

3 participants