Skip to content

Split configuration of request and message filters#1308

Merged
jeffhandley merged 14 commits intomainfrom
halter73/split-filters
Feb 20, 2026
Merged

Split configuration of request and message filters#1308
jeffhandley merged 14 commits intomainfrom
halter73/split-filters

Conversation

@halter73
Copy link
Contributor

@halter73 halter73 commented Feb 19, 2026

This adds WithMessageFilters and WithRequestFilters extension methods to IMcpServerBuilder which expose an IMcpMessageFilterBuilder and IMcpRequestFilterBuilder inline to their respective callbacks.

This avoids having too many similar looking, poorly sorted extension methods on IMcpServerBuilder. This also keeps incoming and outgoing message filters more closely associated.

This adds WithMessageFilters and WithRequestFilters extension methods to
IMcpServerBuilder which expose an IMcpMessageFilterBuilder and IMcpRequestFilterBuilder
to their respective callbacks. This avoids having to many similar looking,
poorly sorted extension methods on IMcpServerBuilder. This also keeps
incoming and outgoing message filters more closely associated.
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 refactors the filter configuration API for MCP servers by introducing a grouped builder pattern for configuring message and request filters. Instead of having many similarly-named Add*Filter extension methods directly on IMcpServerBuilder, the PR introduces two new methods (WithMessageFilters and WithRequestFilters) that accept callbacks providing specialized builder instances for configuring filters in their respective categories.

Changes:

  • Introduces IMcpMessageFilterBuilder and IMcpRequestFilterBuilder interfaces with corresponding implementations for grouping filter configuration
  • Adds WithMessageFilters() and WithRequestFilters() extension methods to organize filter registration
  • Restructures McpServerFilters to use nested McpMessageFilters and McpRequestFilters objects for better organization
  • Marks all existing individual filter methods as [Obsolete] while maintaining backward compatibility through delegation
  • Updates all tests, samples, and documentation to use the new API pattern

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ModelContextProtocol/McpServerBuilderExtensions.cs Adds new WithMessageFilters and WithRequestFilters methods; marks old methods obsolete
src/ModelContextProtocol/McpMessageFilterBuilderExtensions.cs New file with AddIncomingFilter and AddOutgoingFilter methods
src/ModelContextProtocol/McpRequestFilterBuilderExtensions.cs New file with all request-specific filter methods (AddListToolsFilter, AddCallToolFilter, etc.)
src/ModelContextProtocol/IMcpMessageFilterBuilder.cs New interface for message filter builder
src/ModelContextProtocol/IMcpRequestFilterBuilder.cs New interface for request filter builder
src/ModelContextProtocol/DefaultMcpMessageFilterBuilder.cs Default implementation of message filter builder
src/ModelContextProtocol/DefaultMcpRequestFilterBuilder.cs Default implementation of request filter builder
src/ModelContextProtocol.Core/Server/McpServerFilters.cs Restructured to use Message and Request nested properties
src/ModelContextProtocol.Core/Server/McpMessageFilters.cs New class containing message filter collections
src/ModelContextProtocol.Core/Server/McpRequestFilters.cs New class containing request-specific filter collections
src/ModelContextProtocol.Core/Server/McpServerImpl.cs Updated to access filters via nested properties
src/ModelContextProtocol.Core/Server/MessageContext.cs Updated XML documentation to reference new nested structure
src/ModelContextProtocol.Core/Server/McpServerOptions.cs Updated filter execution order documentation for clarity
src/ModelContextProtocol.AspNetCore/AuthorizationFilterSetup.cs Updated to use new nested filter properties
tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsMessageFilterTests.cs All tests updated to use new WithMessageFilters and WithRequestFilters APIs
tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsFilterTests.cs All tests updated to use new WithRequestFilters API
tests/ModelContextProtocol.ConformanceServer/Program.cs Updated to use new WithRequestFilters API
docs/concepts/filters.md Comprehensive documentation updates showing new API usage patterns

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jeffhandley jeffhandley added this to the Stable public API milestone Feb 19, 2026
Copy link
Collaborator

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still reviewing...

@jeffhandley jeffhandley added the breaking-change This issue or PR introduces a breaking change label Feb 19, 2026
Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling a bit with this. Maybe its because I haven't had time to dig into message/request filters yet, but I think I would have trouble figuring out how to set these up and use them.

Copy link
Contributor

Copilot AI commented Feb 19, 2026

@halter73 I've opened a new pull request, #1315, to work on those changes. Once the pull request is ready, I'll request review from you.

- We can add internal properties later to lazily check if the getters have been used to avoid allocations if we care
@jeffhandley jeffhandley merged commit d21aa9e into main Feb 20, 2026
11 checks passed
@jeffhandley jeffhandley deleted the halter73/split-filters branch February 20, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change This issue or PR introduces a breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments