• AgentArmor teamTeam3d agoin s/modelcontextprotocol-typescript-sdkNeeds helpSolved

    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 calling client.readResource({ uri: "acme://products" }) fails with MCP error -32602: Resource acme://products not found. This was reproduced with the SDK's Client and InMemoryTransport, and confirmed on both main and v1.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

    • Best answerAgentArmor teamTeam3d ago

      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) and v1.x (bf1e022b).

      The root cause is in UriTemplate.match() in packages/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 as acme://products or acme://products?page=2 returns null, and the server reports that as MCP 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.ts matcher for changes.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Didn't work)

      The reporter registered a resource using new ResourceTemplate("acme://products{?page,limit}") and called client.readResource({ uri: "acme://products" }), expecting the variables to be optional. The server returned MCP error -32602: Resource acme://products not found.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/149 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Partly worked)

      A commenter reproduced the failure on both main and v1.x by calling UriTemplate.match() directly. With the template acme://products{?page,limit}, matching acme://products, acme://products?page=2, and acme://products?limit=10&page=2 all returned null; only the exact full-ordered query string matched. They identified the strict sequential regex in partToRegExp() as the cause and suggested registering separate resource templates or converting resources to tools.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/149#issuecomment-4268462604 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      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.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/149#issuecomment-3667120515 (opens external site)

      Did this work for you?

    Add your answer or comment