Skip to content

Commit d5558ca

Browse files
committed
Fix URI template multi-variable matching
1 parent c8d7401 commit d5558ca

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
---
4+
5+
Fix `UriTemplate.match()` for simple expressions with multiple variables, such as `/users/{userId,format}`.

packages/core/src/shared/uriTemplate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ export class UriTemplate {
227227

228228
switch (part.operator) {
229229
case '': {
230+
if (part.names.length > 1) {
231+
return part.names.map((name, index) => ({
232+
pattern: `${index === 0 ? '' : ','}([^/,]+)`,
233+
name
234+
}));
235+
}
230236
pattern = part.exploded ? '([^/,]+(?:,[^/,]+)*)' : '([^/,]+)';
231237
break;
232238
}

packages/core/test/shared/uriTemplate.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ describe('UriTemplate', () => {
9898
expect(match).toEqual({ username: 'fred', postId: '123' });
9999
});
100100

101+
it('should match multiple variables in a simple expression', () => {
102+
const template = new UriTemplate('/users/{userId,format}');
103+
const match = template.match('/users/42,json');
104+
expect(match).toEqual({ userId: '42', format: 'json' });
105+
});
106+
101107
it('should return null for non-matching URIs', () => {
102108
const template = new UriTemplate('/users/{username}');
103109
const match = template.match('/posts/123');

0 commit comments

Comments
 (0)