MCP UriTemplate rejects partial or reordered query parameters with error -32602?
In the TypeScript MCP SDK, defining a resource as
new ResourceTemplate("acme://products{?page,limit}")and then callingclient.readResource({ uri: "acme://products" })fails withMCP error -32602: Resource acme://products not found. This was reproduced with the SDK's Client and InMemoryTransport, and confirmed on bothmainandv1.x. Why does partial or out-of-order query-parameter matching fail for ResourceTemplates?Originally asked on GitHub: https://github.com/modelcontextprotocol/typescript-sdk/issues/149
4 answers
Still open: as of the last comments in this thread, no release fix has been confirmed, although a contributor said PR #1083 should resolve this issue. A later comment confirmed the bug on both
main(b8886e77) andv1.x(bf1e022b).The root cause is in
UriTemplate.match()inpackages/core/src/shared/uriTemplate.ts.partToRegExp()emits a fixed-order, all-required regex for{?page,limit}: roughly\?page=([^&]+)&limit=([^&]+)as the matcher. Because every query parameter is required and must appear in declaration order, a request such asacme://productsoracme://products?page=2returnsnull, and the server reports that asMCP error -32602: Resource not found. Per RFC 6570, query parameters are optional and unordered, so this is a matcher limitation rather than an invalid template.Workarounds confirmed in the thread: register separate resource templates for each query-parameter combination you need to support, or convert those dynamic resources to tools until the matcher is fixed. If you need optional or arbitrary-order query parameters, follow PR #1083 or watch the
uriTemplate.tsmatcher for changes.Did this work for you?What a GitHub user tried on GitHub (Didn't work)
The reporter registered a resource using
new ResourceTemplate("acme://products{?page,limit}")and calledclient.readResource({ uri: "acme://products" }), expecting the variables to be optional. The server returnedMCP error -32602: Resource acme://products not found.Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/149 (opens external site)
Did this work for you?What a GitHub user tried on GitHub (Partly worked)
A commenter reproduced the failure on both
mainandv1.xby callingUriTemplate.match()directly. With the templateacme://products{?page,limit}, matchingacme://products,acme://products?page=2, andacme://products?limit=10&page=2all returnednull; only the exact full-ordered query string matched. They identified the strict sequential regex inpartToRegExp()as the cause and suggested registering separate resource templates or converting resources to tools.Did this work for you?What @mgyarmathy tried on GitHub (Outcome unclear)
A contributor pointed to PR #1083 and said it should resolve this issue along with issue #1079, but the thread does not confirm that the PR was merged.
Did this work for you?
Related questions
- MCP TypeScript SDK StreamableHttp without sticky sessions in multi-replica deployments?
- MCP stdio server: SyntaxError Unexpected token in deserializeMessage when using console.log
- MCP typescript-sdk: tool inputSchema/outputSchema reject JSON Schema type 'array'?
- Use Zod union/intersection as MCP tool paramsSchema in typescript-sdk?
- MCP Inspector: "Invalid transport type: undefined" when connecting to an SSE server