Skip to content

Reauthorize tools changed by call-tool filters#1737

Open
PranavSenthilnathan wants to merge 2 commits into
modelcontextprotocol:mainfrom
PranavSenthilnathan:pranavsenthilnathan-fix-tool-reauthorization
Open

Reauthorize tools changed by call-tool filters#1737
PranavSenthilnathan wants to merge 2 commits into
modelcontextprotocol:mainfrom
PranavSenthilnathan:pranavsenthilnathan-fix-tool-reauthorization

Conversation

@PranavSenthilnathan

@PranavSenthilnathan PranavSenthilnathan commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add one ordinary authorization filter for each AddAuthorizationFilters() registration so intervening call-tool filters can replace and reauthorize the matched tool
  • remove the Tasks registration-order restriction because ordinary filters run inside task execution regardless of when they are registered
  • preserve authorization reevaluation at every explicit checkpoint, including changes to the matched tool or user
  • add regression coverage for tool replacement and for ordinary filters registered before Tasks

Note

Tool authorization is intentionally not performed at the outer task-dispatch level. A caller may register a filter that replaces the initially matched tool before the next authorization checkpoint. Authorizing outside the ordinary filter pipeline would instead authorize—and potentially reject—the original tool before that replacement can occur. For task-backed calls, this means task creation can precede authorization during background execution.

Closes #1733

Testing

  • dotnet build
  • dotnet test tests\ModelContextProtocol.AspNetCore.Tests\ModelContextProtocol.AspNetCore.Tests.csproj --no-build --filter "FullyQualifiedName~AuthorizeAttributeTests|FullyQualifiedName~HttpTaskIntegrationTests"
  • dotnet test --no-build (all ASP.NET Core tests pass; existing Core integration tests fail because TestServer.exe is not available on PATH)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 5feb0906-73fc-4e1a-8cf2-c9a99c588487
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 5feb0906-73fc-4e1a-8cf2-c9a99c588487
@PranavSenthilnathan
PranavSenthilnathan marked this pull request as ready for review July 25, 2026 02:56
{
#pragma warning disable MCPEXP002 // Authorization must run in the alternate-result pipeline before task dispatch.
options.Filters.Request.CallToolWithAlternateFilters.Insert(0, async (context, next, cancellationToken) =>
options.Filters.Request.CallToolFilters.Add(next => async (context, cancellationToken) =>

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.

This drops the authorization pass outside Tasks. An anonymous call to an [Authorize] tool now reaches CreateTaskAsync and gets a task ID, which regresses #1722 and #1733's requirement that denial happen before task creation. Can we keep one alternate-result authorization check before Tasks and add this ordinary checkpoint alongside it?

}

[Fact]
public async Task Authorize_Tool_ReauthorizesPrimitiveChangedByInterveningFilter()

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.

Can this also cover a task-backed call where an ordinary filter replaces an anonymous tool with an authorized one? The direct call proves the new checkpoint order, but not that the replacement is reauthorized inside Tasks.

Sign up for free to 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.

Reauthorize tools changed by intervening call-tool filters

3 participants