modelcontextprotocol / modelcontextprotocol/typescript-sdk

`UriTemplate.match()` returns `null` for multi-variable path expressions like `{userId,format}`

Open
#2,166 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug fix proposed P2 ready for work
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

Describe the bug
UriTemplate.match() returns null for any template that uses a multi-variable expression in the path — like {userId,format}. This is valid RFC 6570 syntax but the SDK fails to match the URI and returns null instead of extracting the variables.

This breaks MCP resource registration — any resource with a multi-variable path template silently never matches, so the resource handler is never called.

To Reproduce

Minimal case:

import { UriTemplate } from '@modelcontextprotocol/core';

const tpl = new UriTemplate('/users/{userId,format}');
console.log(tpl.match('/users/42,json'));
// actual:   null
// expected: { userId: '42', format: 'json' }

With McpServer:

import { McpServer } from '@modelcontextprotocol/server';

const server = new McpServer({ name: 'my-server', version: '1.0' });

server.resource('user-data', 'data://users/{userId,format}', async (uri) => {
  // never reached — match() returns null, so nothing routes here
  return { contents: [{ uri: uri.href, text: 'ok' }] };
});

Or run the repro test directly:

pnpm --filter "@modelcontextprotocol/test-integration" test -- test/bug-repros/bug2-uri-template.test.ts

Expected behavior
tpl.match('/users/42,json') should return { userId: '42', format: 'json' } — both variables extracted correctly.

Logs

match() result: null

FAIL  test/bug-repros/bug2-uri-template.test.ts
AssertionError:
  Expected both userId="42" and format="json" to be in the result.
  Got: null

- Expected: { "format": "json", "userId": "42" }
+ Received: null

Additional context

Root cause is in packages/core/src/shared/uriTemplate.ts inside partToRegExp() (~line 224). Two problems:

  1. The regex pattern used for the default operator is [^/,]+ — this rejects commas, so a URI like /42,json doesn't match at all.
  2. Only part.name (the first variable) is pushed to the patterns array — so even if the regex matched, format would never be extracted.
const name = part.name; // only the first variable name — rest are ignored
pattern = part.exploded ? '([^/,]+(?:,[^/,]+)*)' : '([^/,]+)'; // comma rejected
patterns.push({ pattern, name }); // one group for all names

The fix would be: for multi-name parts with the '' operator, push one capture group per variable name and put a literal , between them in the assembled regex, so /42,json matches and both groups are captured.

RFC 6570 §3.2.2 covers this — simple string expansion with a comma-separated variable list is spec-valid.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/core/src/shared/uriTemplate.ts at partToRegExp(), around line 224, and inspect the repro at test/bug-repros/bug2-uri-template.test.ts. Run pnpm --filter "@modelcontextprotocol/test-integration" test -- test/bug-repros/bug2-uri-template.test.ts. Done means UriTemplate.match('/users/42,json') returns both userId and format as expected and the repro passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.