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-rfc6570-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelcontextprotocol/core-internal': patch
'@modelcontextprotocol/server': patch
---

Align `UriTemplate.expand()` value encoding with RFC 6570 section 3.2.1: percent-encode `!'()*` in strict variable expansions (`{var}`, `{?q}`, `{&q}`, `{.var}`, `{/var}`) and preserve valid existing `%HH` triplets in reserved (`+`) and fragment (`#`) expansions while still encoding lone or malformed `%` sequences.
8 changes: 6 additions & 2 deletions packages/core-internal/src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ export class UriTemplate {
private encodeValue(value: string, operator: string): string {
UriTemplate.validateLength(value, MAX_VARIABLE_LENGTH, 'Variable value');
if (operator === '+' || operator === '#') {
return encodeURI(value);
// RFC 6570 §3.2.1: reserved and fragment expansions preserve existing %HH triplets
// while percent-encoding lone or malformed '%' sequences.
return encodeURI(value).replaceAll(/%25([0-9A-Fa-f]{2})/g, '%$1');
}
return encodeURIComponent(value);
// RFC 6570 §3.2.1: strict expansions only leave unreserved characters unencoded;
// encodeURIComponent leaves sub-delimiters !'()* unencoded.
return encodeURIComponent(value).replaceAll(/[!'()*]/g, char => `%${char.codePointAt(0)!.toString(16).toUpperCase()}`);
}

private expandPart(
Expand Down
38 changes: 38 additions & 0 deletions packages/core-internal/test/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,42 @@ describe('UriTemplate', () => {
expect(elapsed).toBeLessThan(100);
});
});

describe('RFC 6570 section 3.2.1 value encoding', () => {
it("percent-encodes sub-delimiters !'()* in strict expansions", () => {
expect(new UriTemplate('{var}').expand({ var: "!'()*" })).toBe('%21%27%28%29%2A');
expect(new UriTemplate('search{?q}').expand({ q: 'hello!' })).toBe('search?q=hello%21');
expect(new UriTemplate('search{&q}').expand({ q: "a'b(c)d*e" })).toBe('search&q=a%27b%28c%29d%2Ae');
expect(new UriTemplate('item{.ext}').expand({ ext: 'v(1)!' })).toBe('item.v%281%29%21');
expect(new UriTemplate('item{/part}').expand({ part: '*test*' })).toBe('item/%2Atest%2A');
});

it('keeps RFC 6570 unreserved characters and percent-encodes existing %HH in strict expansions', () => {
expect(new UriTemplate('{var}').expand({ var: 'A-Z_a.z~09' })).toBe('A-Z_a.z~09');
expect(new UriTemplate('{var}').expand({ var: 'My%20File.txt' })).toBe('My%2520File.txt');
});

it('preserves existing %HH triplets in reserved (+) and fragment (#) expansions', () => {
expect(new UriTemplate('file:///docs/{+path}').expand({ path: 'My%20File.txt' })).toBe('file:///docs/My%20File.txt');
expect(new UriTemplate('docs{#frag}').expand({ frag: 'Part%20One' })).toBe('docs#Part%20One');
expect(new UriTemplate('file:///docs/{+path}').expand({ path: 'dir%2fname' })).toBe('file:///docs/dir%2fname');
expect(new UriTemplate('file:///docs/{+path}').expand({ path: 'café%20bar' })).toBe('file:///docs/caf%C3%A9%20bar');
});

it('still percent-encodes lone and malformed % sequences in reserved and fragment expansions', () => {
expect(new UriTemplate('{+val}').expand({ val: '100%' })).toBe('100%25');
expect(new UriTemplate('{+val}').expand({ val: '50% off' })).toBe('50%25%20off');
expect(new UriTemplate('{+val}').expand({ val: '%2G' })).toBe('%252G');
expect(new UriTemplate('{+val}').expand({ val: '%2' })).toBe('%252');
expect(new UriTemplate('{+val}').expand({ val: '%%20' })).toBe('%25%20');
expect(new UriTemplate('{+val}').expand({ val: '%2520' })).toBe('%2520');
expect(new UriTemplate('{+val}').expand({ val: '%25' })).toBe('%25');
expect(new UriTemplate('{#frag}').expand({ frag: '50%&20' })).toBe('#50%25&20');
});

it("leaves reserved sub-delimiters !'()* unencoded in reserved and fragment expansions", () => {
expect(new UriTemplate('{+val}').expand({ val: "!'()*" })).toBe("!'()*");
expect(new UriTemplate('{#frag}').expand({ frag: 'hello!' })).toBe('#hello!');
});
});
});
Loading