Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/uri-template-fragment-delimiter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/core': patch
---

Match URI template fragment expressions with their required `#` delimiter outside the captured variable, including empty fragment values.
7 changes: 5 additions & 2 deletions packages/core-internal/src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,14 @@ export class UriTemplate {
pattern = part.exploded ? '([^/,]+(?:,[^/,]+)*)' : '([^/,]+)';
break;
}
case '+':
case '#': {
case '+': {
pattern = '(.+)';
break;
}
case '#': {
pattern = '#(.*)';
break;
}
case '.': {
pattern = String.raw`\.([^/,]+)`;
break;
Expand Down
15 changes: 15 additions & 0 deletions packages/core-internal/test/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ describe('UriTemplate', () => {
expect(template.expand({ var: '/test' })).toBe('X#/test');
expect(template.variableNames).toEqual(['var']);
});

it.each(['intro', '/chapter?view=full', ''])('should match fragment value %j without its delimiter', section => {
const template = new UriTemplate('file:///doc{#section}');
expect(template.match(template.expand({ section }))).toEqual({ section });
});

it('should reject a fragment value without the fragment delimiter', () => {
const template = new UriTemplate('file:///doc{#section}');
expect(template.match('file:///docintro')).toBeNull();
});

it('should preserve reserved expansion matching without a fragment delimiter', () => {
const template = new UriTemplate('file:///doc{+section}');
expect(template.match('file:///docintro')).toEqual({ section: 'intro' });
});
});

describe('label expansion', () => {
Expand Down
Loading