-
Notifications
You must be signed in to change notification settings - Fork 776
Fix HTTP task filter composition #1722
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
PranavSenthilnathan
merged 8 commits into
modelcontextprotocol:main
from
PranavSenthilnathan:pranavsenthilnathan-fix-http-task-exception
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4460d6b
Fix HTTP task filter composition
PranavSenthilnathan 1854496
Preserve tool filter semantics with tasks
PranavSenthilnathan e4c8b51
Authorize tool calls before task dispatch
PranavSenthilnathan ed2b222
Document task filter ordering
PranavSenthilnathan 60ad05e
Log final alternate filter results
PranavSenthilnathan 5c69198
Address HTTP task filter review feedback
PranavSenthilnathan 646140d
Scope filter ordering validation to Tasks
PranavSenthilnathan 7d1341f
Move tool call lifecycle to separate file
PranavSenthilnathan 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
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
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,13 @@ | ||
| <Project> | ||
| <Target Name="PinMcpProjectReferencePackageVersions" | ||
| AfterTargets="_GetProjectReferenceVersions" | ||
| Condition="'$(MSBuildProjectName)' == 'ModelContextProtocol' | ||
| Or '$(MSBuildProjectName)' == 'ModelContextProtocol.AspNetCore' | ||
| Or '$(MSBuildProjectName)' == 'ModelContextProtocol.Extensions.Tasks'"> | ||
| <ItemGroup> | ||
| <_ProjectReferencesWithVersions Update="@(_ProjectReferencesWithVersions)"> | ||
| <ProjectVersion>[%(_ProjectReferencesWithVersions.ProjectVersion)]</ProjectVersion> | ||
| </_ProjectReferencesWithVersions> | ||
| </ItemGroup> | ||
| </Target> | ||
| </Project> |
32 changes: 32 additions & 0 deletions
32
src/ModelContextProtocol.AspNetCore/AuthorizationCallToolFilterGuardSetup.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,32 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using ModelContextProtocol.Protocol; | ||
| using ModelContextProtocol.Server; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore; | ||
|
|
||
| internal sealed class AuthorizationFiltersMarker; | ||
|
|
||
| internal sealed class AuthorizationCallToolFilterGuardSetup(AuthorizationFiltersMarker? marker = null) : IPostConfigureOptions<McpServerOptions> | ||
| { | ||
| public void PostConfigure(string? name, McpServerOptions options) | ||
| { | ||
| if (marker is not null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| #pragma warning disable MCPEXP002 // The guard must run before Tasks can dispatch the request. | ||
| options.Filters.Request.CallToolWithAlternateFilters.Insert(0, static async (context, next, cancellationToken) => | ||
| { | ||
| if (AuthorizationFilterSetup.HasAuthorizationMetadata(context.MatchedPrimitive)) | ||
| { | ||
| throw new InvalidOperationException( | ||
| "Authorization filter was not invoked for tools/call operation, but authorization metadata was found on the tool. " + | ||
| "Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters."); | ||
| } | ||
|
|
||
| return await next(context, cancellationToken); | ||
| }); | ||
| #pragma warning restore MCPEXP002 | ||
| } | ||
| } |
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
84 changes: 84 additions & 0 deletions
84
src/ModelContextProtocol.Core/Server/ComposedCallToolInvocationState.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,84 @@ | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| #pragma warning disable MCPEXP002 | ||
| internal sealed class ComposedCallToolInvocationState | ||
| { | ||
| private readonly object _sync = new(); | ||
| private readonly List<ToolCallLifecycle> _pendingOrdinaryLifecycles = []; | ||
| private bool _outerCompleted; | ||
| private bool _outerReturnedAlternate; | ||
|
|
||
| public IReadOnlyList<ToolCallLifecycle> RecordOrdinaryResult(CallToolResult result) => | ||
| RecordOrdinaryLifecycle(new(result, null, false)); | ||
|
|
||
| public IReadOnlyList<ToolCallLifecycle> RecordOrdinaryException( | ||
| Exception exception, | ||
| bool cancellationRequested) => | ||
| RecordOrdinaryLifecycle(new(null, exception, cancellationRequested)); | ||
|
|
||
| public IReadOnlyList<ToolCallLifecycle> CompleteOuter(ResultOrAlternate<CallToolResult> result) | ||
| { | ||
| lock (_sync) | ||
| { | ||
| if (result.IsAlternate) | ||
| { | ||
| _outerReturnedAlternate = true; | ||
| return DrainPendingLifecycles(); | ||
| } | ||
|
|
||
| _outerCompleted = true; | ||
| var exceptions = _pendingOrdinaryLifecycles | ||
| .Where(lifecycle => lifecycle.Exception is not null) | ||
| .ToArray(); | ||
| _pendingOrdinaryLifecycles.Clear(); | ||
| return exceptions.Length > 0 ? | ||
| exceptions : | ||
| [new(result.Result!, null, false)]; | ||
| } | ||
| } | ||
|
|
||
| public IReadOnlyList<ToolCallLifecycle> CompleteOuterException( | ||
| Exception exception, | ||
| bool cancellationRequested) | ||
| { | ||
| lock (_sync) | ||
| { | ||
| _outerCompleted = true; | ||
| _pendingOrdinaryLifecycles.Clear(); | ||
| return [new(null, exception, cancellationRequested)]; | ||
| } | ||
| } | ||
|
|
||
| private IReadOnlyList<ToolCallLifecycle> RecordOrdinaryLifecycle(ToolCallLifecycle lifecycle) | ||
| { | ||
| lock (_sync) | ||
| { | ||
| if (_outerReturnedAlternate) | ||
| { | ||
| return [lifecycle]; | ||
| } | ||
|
|
||
| if (!_outerCompleted) | ||
| { | ||
| _pendingOrdinaryLifecycles.Add(lifecycle); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
| } | ||
|
|
||
| private IReadOnlyList<ToolCallLifecycle> DrainPendingLifecycles() | ||
| { | ||
| if (_pendingOrdinaryLifecycles.Count == 0) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var lifecycles = _pendingOrdinaryLifecycles.ToArray(); | ||
| _pendingOrdinaryLifecycles.Clear(); | ||
| return lifecycles; | ||
| } | ||
| } | ||
| #pragma warning restore MCPEXP002 |
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
18 changes: 18 additions & 0 deletions
18
src/ModelContextProtocol.Core/Server/McpRequestInvocationFilter.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,18 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Delegate type for filtering a single incoming MCP request invocation. | ||
| /// </summary> | ||
| /// <typeparam name="TParams">The type of the parameters sent with the request.</typeparam> | ||
| /// <typeparam name="TResult">The type of the response returned by the handler.</typeparam> | ||
| /// <param name="context">The context for the current request.</param> | ||
| /// <param name="next">The next request handler in the pipeline for this invocation.</param> | ||
| /// <param name="cancellationToken">The cancellation token for the current request.</param> | ||
| /// <returns>The result of the filtered request invocation.</returns> | ||
| [Experimental(Experimentals.Subclassing_DiagnosticId, UrlFormat = Experimentals.Subclassing_Url)] | ||
| public delegate ValueTask<TResult> McpRequestInvocationFilter<TParams, TResult>( | ||
| RequestContext<TParams> context, | ||
| McpRequestHandler<TParams, TResult> next, | ||
| CancellationToken cancellationToken); |
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.