Compose alternate-result filters with typed server handlers#1718
Open
onatozmenn wants to merge 1 commit into
Open
Compose alternate-result filters with typed server handlers#1718onatozmenn wants to merge 1 commit into
onatozmenn wants to merge 1 commit into
Conversation
Signed-off-by: onatozmenn <onatozmen44@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Compose alternate-result behavior around the ordinary typed server pipeline instead of replacing it.
McpServerOptions.AddAlternateResultFilter<TParams, TResult>API.tools/callfilters, primitive matching, error handling, and normalization inside alternate-result filters.CallToolWithAlternateHandleras the low-level replacement path and reject incompatible combinations at startup.Motivation and Context
CallToolFiltersand alternate-result filters were previously mutually exclusive. As a result, enabling Tasks could displace validation, telemetry, and ASP.NET authorization filters.The tool-specific alternate APIs also meant that extending another built-in method would require adding another Core
<Method>WithAlternateHandler/filter pair. The new registration is keyed by method and typed by the existing request/result contract, so future extension-owned augmentation does not require a new Core API for each method.For task-backed calls, the task record is created first. The ordinary filters then run exactly once in background execution with
MatchedPrimitivepopulated and before the tool body. An authorization denial therefore fails the task without invoking the tool. A dedicated async DI scope keeps scoped filters and tool dependencies alive after the initiating HTTP request completes.This addresses the alternate-result composition and method-agnostic augmentation slice of #1704. Typed custom RPC registration and subscription grant processing remain coordinated follow-up work.
How Has This Been Tested?
AuthorizeAttributeTestsclass: 30 passed, 0 failed.dotnet format whitespace --verify-no-changesandgit diff --check.Breaking Changes
No stable API break. The new API is experimental. An explicit
CallToolWithAlternateHandlercombined with a method-keyed alternate-result filter now fails with an actionable startup error because the explicit handler is a full replacement.Types of changes
Checklist
Additional context
The first method-keyed consumer is Tasks wrapping
tools/call, but coverage also augmentsprompts/getandprompts/listto prove the seam is method-agnostic. Tests cover filter ordering, immediate and alternate serialization, cache/result normalization, unknown methods, type mismatches, explicit replacement collisions, exactly-once ordinary filters, scoped service lifetime/disposal, and ASP.NET authorization.