Skip to content

Conversation

halter73
Copy link
Contributor

@halter73 halter73 commented Oct 17, 2025

This fixes a regression reported in #821 that was caused by changes in #733 which caused resource template matching to break when there were multiple templated resources.

It's important for the auth pipeline to determine which resource is matched to make authorization decisions without executing the handlers associated with reading the resource. So rather than call McpServerResource.ReadAsync prior to running the filter pipeline, #733 wrongly assumed the first templated McpServerResource it saw would be able to handle any templated request. To fix this while still avoiding calling McpServerResource.ReadAsync just to see if the resource matched before auth filter can prevent it, this PR adds a new McpServerResource.CanReadUri method.

Unlike before, this will call Regex.Match twice for the matched resource. We could flow the Match to the eventual ReadAsync call via the RequestContext, but I don't think this one additional call is a huge concern though, considering that we already call Regex.Match O(N) times where N is the number of templated resources. I figure we'll probably use some sort of trie if/when we further optimize this path at which point we could look at preserving the matches.

Fixes #821

@halter73 halter73 requested a review from stephentoub October 17, 2025 21:43
/// </summary>
/// <param name="uri">The URI being evaluated for this resource.</param>
/// <returns><see langword="true"/> if the resource is able to handle the URI; otherwise, <see langword="false"/>.</returns>
public abstract bool CanReadUri(string uri);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't like the name CanReadUri. Would people prefer MatchesUri? Or something else?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Matches is much better than CanRead for this, imo.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with both. Others like HandlesUri would be fine, too.

@halter73 halter73 changed the title Add McpServerResource.CanReadUri Fix regression matching templated McpServerResources Oct 17, 2025
public override bool CanReadUri(string uri)
{
Throw.IfNull(uri);
return TryMatch(uri, out _);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is going to create the Match object if it does match. It'd be better if the Can path used IsMatch instead

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.

McpServerResource not working.

3 participants