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
6 changes: 6 additions & 0 deletions .changeset/uri-template-query-matching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelcontextprotocol/core': patch
'@modelcontextprotocol/core-internal': patch
---

Match URI template query operators with omitted, partial, or out-of-order query parameters.
34 changes: 25 additions & 9 deletions packages/core-internal/src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ export class UriTemplate {
}

if (part.operator === '?' || part.operator === '&') {
for (let i = 0; i < part.names.length; i++) {
const name = part.names[i]!;
const prefix = i === 0 ? '\\' + part.operator : '&';
patterns.push({
pattern: prefix + this.escapeRegExp(name) + '=([^&]+)',
name
});
}
return patterns;
}

Expand All @@ -227,7 +219,7 @@ export class UriTemplate {

switch (part.operator) {
case '': {
pattern = part.exploded ? '([^/,]+(?:,[^/,]+)*)' : '([^/,]+)';
pattern = part.exploded ? '([^/?#,]+(?:,[^/?#,]+)*)' : '([^/?#,]+)';
break;
}
case '+':
Expand Down Expand Up @@ -256,10 +248,18 @@ export class UriTemplate {
UriTemplate.validateLength(uri, MAX_TEMPLATE_LENGTH, 'URI');
let pattern = '^';
const names: Array<{ name: string; exploded: boolean }> = [];
const queryNames: Array<{ name: string; exploded: boolean }> = [];

for (const part of this.parts) {
if (typeof part === 'string') {
pattern += this.escapeRegExp(part);
} else if (part.operator === '?' || part.operator === '&') {
for (const name of part.names) {
UriTemplate.validateLength(name, MAX_VARIABLE_LENGTH, 'Variable name');
queryNames.push({ name, exploded: part.exploded });
}

pattern += part.operator === '?' ? String.raw`(?:\?[^#]*)?` : '(?:&[^#]*)?';
} else {
const patterns = this.partToRegExp(part);
for (const { pattern: partPattern, name } of patterns) {
Expand All @@ -285,6 +285,22 @@ export class UriTemplate {
result[cleanName] = exploded && value.includes(',') ? value.split(',') : value;
}

if (queryNames.length > 0) {
const queryStart = uri.indexOf('?');
if (queryStart !== -1) {
const queryEnd = uri.indexOf('#', queryStart);
const query = uri.slice(queryStart + 1, queryEnd === -1 ? undefined : queryEnd);
const params = new URLSearchParams(query);

for (const { name, exploded } of queryNames) {
const value = params.get(name);
if (value !== null) {
result[name] = exploded && value.includes(',') ? value.split(',') : value;
}
}
}
}

return result;
}
}
28 changes: 28 additions & 0 deletions packages/core-internal/test/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ describe('UriTemplate', () => {
expect(template.variableNames).toEqual(['q', 'page']);
});

it('should match omitted query parameters', () => {
const template = new UriTemplate('/search{?q,page}');
const match = template.match('/search');
expect(match).toEqual({});
expect(template.variableNames).toEqual(['q', 'page']);
});

it('should match a subset of query parameters', () => {
const template = new UriTemplate('/search{?q,page,limit}');
const match = template.match('/search?q=test&limit=10');
expect(match).toEqual({ q: 'test', limit: '10' });
expect(template.variableNames).toEqual(['q', 'page', 'limit']);
});

it('should match query parameters in any order', () => {
const template = new UriTemplate('/search{?q,page,limit}');
const match = template.match('/search?limit=10&page=1&q=test');
expect(match).toEqual({ q: 'test', page: '1', limit: '10' });
expect(template.variableNames).toEqual(['q', 'page', 'limit']);
});

it('should stop simple path variables before query parameters', () => {
const template = new UriTemplate('dom://{pageId}{?selector,includeText}');
const match = template.match('dom://page-1?includeText=true');
expect(match).toEqual({ pageId: 'page-1', includeText: 'true' });
expect(template.variableNames).toEqual(['pageId', 'selector', 'includeText']);
});

it('should handle partial matches correctly', () => {
const template = new UriTemplate('/users/{id}');
expect(template.match('/users/123/extra')).toBeNull();
Expand Down
Loading