-
Notifications
You must be signed in to change notification settings - Fork 682
Split configuration of request and message filters #1308
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7202e4a
Split configuration of request and message filters
halter73 f93a06c
Update docs/concepts/filters.md
halter73 fd9233b
Lazily construct McpServerFilters
halter73 c9b27e4
Add back intro detail to filters.md
halter73 138cf02
Remove confusing phrase from filters.md
halter73 854776f
Use common obsoletion pattern for
halter73 85956c7
Remove setters from McpServerOptions.Filters and Handlers
halter73 4bb9d9b
Merge branch 'main' into halter73/split-filters
halter73 8b16ee4
Use IList for filter collections in options
halter73 46bb1ab
Fix McpServerImpl to react to IList change
halter73 76b3f63
Merge branch 'main' into halter73/split-filters
halter73 f74855e
Remove repetitive calls to WithRequestFilters and WithMessageFilters …
halter73 b119fa6
Merge branch 'main' into halter73/split-filters
halter73 cac7710
Fix bad merge
halter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Provides grouped message filter collections. | ||
| /// </summary> | ||
| public sealed class McpMessageFilters | ||
| { | ||
| /// <summary> | ||
| /// Gets the filters for all incoming JSON-RPC messages. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters intercept all incoming JSON-RPC messages before they are processed by the server, | ||
| /// including requests, notifications, responses, and errors. The filters can perform logging, | ||
| /// authentication, rate limiting, or other cross-cutting concerns that apply to all message types. | ||
| /// </para> | ||
| /// <para> | ||
| /// Message filters are applied before request-specific filters. If a message filter does not call | ||
| /// the next handler in the pipeline, the default handlers will not be executed. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpMessageFilter> IncomingFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for all outgoing JSON-RPC messages. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters intercept all outgoing JSON-RPC messages before they are sent to the client, | ||
| /// including responses, notifications, and errors. The filters can perform logging, | ||
| /// redaction, auditing, or other cross-cutting concerns that apply to all message types. | ||
| /// </para> | ||
| /// <para> | ||
| /// If a message filter does not call the next handler in the pipeline, the message will not be sent. | ||
| /// Filters may also call the next handler multiple times with different messages to emit additional | ||
| /// server-to-client messages. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpMessageFilter> OutgoingFilters { get; } = []; | ||
| } | ||
157 changes: 157 additions & 0 deletions
157
src/ModelContextProtocol.Core/Server/McpRequestFilters.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Provides grouped request-specific filter collections. | ||
| /// </summary> | ||
| public sealed class McpRequestFilters | ||
| { | ||
| /// <summary> | ||
| /// Gets the filters for the list-tools handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters wrap handlers that return a list of available tools when requested by a client. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ToolsList"/> requests. It supports pagination through the cursor mechanism, | ||
| /// where the client can make repeated calls with the cursor returned by the previous call to retrieve more tools. | ||
| /// </para> | ||
| /// <para> | ||
| /// These filters work alongside any tools defined in the <see cref="McpServerTool"/> collection. | ||
| /// Tools from both sources will be combined when returning results to clients. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpRequestFilter<ListToolsRequestParams, ListToolsResult>> ListToolsFilters { get; } = []; | ||
|
halter73 marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the call-tool handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that are invoked when a client makes a call to a tool that isn't found in the <see cref="McpServerTool"/> collection. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ToolsCall"/> requests. The handler should implement logic to execute the requested tool and return appropriate results. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<CallToolRequestParams, CallToolResult>> CallToolFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the list-prompts handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters wrap handlers that return a list of available prompts when requested by a client. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.PromptsList"/> requests. It supports pagination through the cursor mechanism, | ||
| /// where the client can make repeated calls with the cursor returned by the previous call to retrieve more prompts. | ||
| /// </para> | ||
| /// <para> | ||
| /// These filters work alongside any prompts defined in the <see cref="McpServerPrompt"/> collection. | ||
| /// Prompts from both sources will be combined when returning results to clients. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpRequestFilter<ListPromptsRequestParams, ListPromptsResult>> ListPromptsFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the get-prompt handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that are invoked when a client requests details for a specific prompt that isn't found in the <see cref="McpServerPrompt"/> collection. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.PromptsGet"/> requests. The handler should implement logic to fetch or generate the requested prompt and return appropriate results. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<GetPromptRequestParams, GetPromptResult>> GetPromptFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the list-resource-templates handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that return a list of available resource templates when requested by a client. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ResourcesTemplatesList"/> requests. It supports pagination through the cursor mechanism, | ||
| /// where the client can make repeated calls with the cursor returned by the previous call to retrieve more resource templates. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<ListResourceTemplatesRequestParams, ListResourceTemplatesResult>> ListResourceTemplatesFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the list-resources handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that return a list of available resources when requested by a client. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ResourcesList"/> requests. It supports pagination through the cursor mechanism, | ||
| /// where the client can make repeated calls with the cursor returned by the previous call to retrieve more resources. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<ListResourcesRequestParams, ListResourcesResult>> ListResourcesFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the read-resource handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that are invoked when a client requests the content of a specific resource identified by its URI. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ResourcesRead"/> requests. The handler should implement logic to locate and retrieve the requested resource. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<ReadResourceRequestParams, ReadResourceResult>> ReadResourceFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the complete-handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// These filters wrap handlers that provide auto-completion suggestions for prompt arguments or resource references in the Model Context Protocol. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.CompletionComplete"/> requests. The handler processes auto-completion requests, returning a list of suggestions based on the | ||
| /// reference type and current argument value. | ||
| /// </remarks> | ||
| public List<McpRequestFilter<CompleteRequestParams, CompleteResult>> CompleteFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the subscribe-to-resources handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters wrap handlers that are invoked when a client wants to receive notifications about changes to specific resources or resource patterns. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ResourcesSubscribe"/> requests. The handler should implement logic to register the client's interest in the specified resources | ||
| /// and set up the necessary infrastructure to send notifications when those resources change. | ||
| /// </para> | ||
| /// <para> | ||
| /// After a successful subscription, the server should send resource change notifications to the client | ||
| /// whenever a relevant resource is created, updated, or deleted. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpRequestFilter<SubscribeRequestParams, EmptyResult>> SubscribeToResourcesFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the unsubscribe-from-resources handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters wrap handlers that are invoked when a client wants to stop receiving notifications about previously subscribed resources. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.ResourcesUnsubscribe"/> requests. The handler should implement logic to remove the client's subscriptions to the specified resources | ||
| /// and clean up any associated resources. | ||
| /// </para> | ||
| /// <para> | ||
| /// After a successful unsubscription, the server should no longer send resource change notifications | ||
| /// to the client for the specified resources. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpRequestFilter<UnsubscribeRequestParams, EmptyResult>> UnsubscribeFromResourcesFilters { get; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets the filters for the set-logging-level handler pipeline. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// These filters wrap handlers that process <see cref="RequestMethods.LoggingSetLevel"/> requests from clients. When set, it enables | ||
| /// clients to control which log messages they receive by specifying a minimum severity threshold. | ||
| /// The filters can modify, log, or perform additional operations on requests and responses for | ||
| /// <see cref="RequestMethods.LoggingSetLevel"/> requests. | ||
| /// </para> | ||
| /// <para> | ||
| /// After handling a level change request, the server typically begins sending log messages | ||
| /// at or above the specified level to the client as notifications/message notifications. | ||
| /// </para> | ||
| /// </remarks> | ||
| public List<McpRequestFilter<SetLevelRequestParams, EmptyResult>> SetLoggingLevelFilters { get; } = []; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.