Fix McpServerResource URI template matching for multiple templated resources #895
+58
−7
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.
Problem
When multiple templated resources were defined in a server, attempting to access a resource that matched the second or later template would fail with an "Unknown resource URI" error. For example:
Accessing
KBInfo://KB/docs2/topic-auth.md
would fail with:Root Cause
PR #733 introduced a regression in the resource matching logic. The code was setting
MatchedPrimitive
to the first templated resource without checking if the URI pattern actually matched. WhenReadAsync
returnednull
(indicating no match), the handler didn't fall through to try other templated resources, resulting in the error.Solution
This fix restores the correct behavior while maintaining filter support:
Filter logic: Continues to set
MatchedPrimitive
to the first templated resource as a "hint" - this is necessary for filters to accessMatchedPrimitive
Handler logic: Enhanced to try all templated resources:
MatchedPrimitive
if setThe fix ensures all templated resources are checked in sequence until a match is found, resolving the issue while maintaining backward compatibility with existing filter behavior.
Testing
Added regression test
MultipleTemplatedResources_MatchesCorrectResource
that verifies:Fixes #[issue-number]
Original prompt
This section details on the original issue you should resolve
<issue_title>McpServerResource not working.</issue_title>
<issue_description>Describe the bug
I just defined two resources to test,
To Reproduce
Use AI VS code like the Roo Code etc.
Ask some question wants to use this resource.
Expected behavior
Error accessing MCP resource:
MCP error -32602: Unknown resource URI: 'KBInfo://KB/docs2/topic-auth.md'
Logs
Additional context
None
</issue_description>
Comments on the Issue (you are @copilot in this section)
@stephentoub Can you share a full repro? How are you registering these resources with your server? @stephentoub > The MCP spec isn't clear on what level of templates should be supported, but I think most SDKs only support level 1.It should support through level 4...at least that was the intent. </comment_new>
<comment_new>@stephentoub
@halter73, I think this broke in #733. This loop:
got changed to this: