Skip to content

Add support for message-level filters to McpServer - #1207

Merged
halter73 merged 7 commits into
mainfrom
halter73/message-filters
Jan 30, 2026
Merged

Add support for message-level filters to McpServer#1207
halter73 merged 7 commits into
mainfrom
halter73/message-filters

Conversation

@halter73

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

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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
FileDescription
src/ModelContextProtocol.Core/Server/MessageContext.csNew base class providing context for message filters with Server, Services, User, Items, and JsonRpcMessage properties
src/ModelContextProtocol.Core/Server/RequestContext.csRefactored to extend MessageContext, maintaining backward compatibility while inheriting common context properties
src/ModelContextProtocol.Core/Server/McpMessageHandler.csNew delegate type for message-level handlers that process MessageContext
src/ModelContextProtocol.Core/Server/McpMessageFilter.csNew delegate type for wrapping message handlers in filter pipeline
src/ModelContextProtocol.Core/JsonRpcMessageFilter.csInternal delegate for low-level JSON-RPC message filtering
src/ModelContextProtocol.Core/Server/McpServerFilters.csAdds IncomingMessageFilters and OutgoingMessageFilters collections to store registered filters
src/ModelContextProtocol.Core/Server/McpServerImpl.csImplements BuildMessageFilterPipeline method to construct filter chains and passes them to McpSessionHandler
src/ModelContextProtocol.Core/McpSessionHandler.csIntegrates incoming and outgoing message filters into message handling and sending pipelines
src/ModelContextProtocol/McpServerBuilderExtensions.csAdds public API methods for registering message filters with comprehensive XML documentation
src/ModelContextProtocol.Core/Client/McpClientImpl.csUpdates constructor to pass null filters (clients don't support message filters)
src/ModelContextProtocol.Core/Server/StreamableHttpPostTransport.csReorders Context initialization to occur before conditional RelatedTransport assignment
src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.csFixes bug where auth server metadata wasn't stored after successful token refresh
docs/concepts/filters.mdComprehensive documentation of message filters including usage examples, execution order, and best practices
tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.csExtensive test coverage including filter ordering, context access, handler skipping, exception handling, and message manipulation
tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTest.csChanges class from public to abstract (best practice for base test classes)

Comment threadsrc/ModelContextProtocol/McpServerBuilderExtensions.cs Outdated
Comment threadsrc/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Comment threadsrc/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Comment threadsrc/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Comment threadsrc/ModelContextProtocol.Core/Server/MessageContext.cs Outdated
Comment threadsrc/ModelContextProtocol/McpServerBuilderExtensions.cs
stephentoub
stephentoub previously approved these changes Jan 29, 2026
Comment threadsrc/ModelContextProtocol.Core/McpSessionHandler.cs
@halter73
halter73 merged commit 7d48d39 into mainJan 30, 2026
9 of 10 checks passed
@halter73
halter73 deleted the halter73/message-filters branch January 30, 2026 23:19
@jeffhandleyjeffhandley linked an issue Feb 3, 2026 that may be closed by this pull request
Sign up for freeto 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.

Add global transport-level filters

3 participants

@halter73@stephentoub