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/skills-catalog-uri-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/server': patch
---

Normalize path percent-encoding via `skillFileKey` when `SkillCatalog` resolves `resources/read` and `resources/directory/read` URIs. Previously the catalog looked up requests by raw `new URL(uri).href`, so equivalent spellings such as `Q%26A.md`, lowercase hex escapes like `Rembours%c3%a9.md`, and unencoded `^` paths produced by `new URL()` on Node 20/22 returned Resource Not Found (`-32602`).
27 changes: 21 additions & 6 deletions packages/server/src/ext/skills/catalogSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProtocolError, ProtocolErrorCode, ResourceNotFoundError } from '@modelcontextprotocol/core-internal';
import { canonicalUri, decodeUtf8Lossless, sha256Digest } from '@modelcontextprotocol/core-internal/ext/skills';
import { canonicalUri, decodeUtf8Lossless, sha256Digest, skillFileKey } from '@modelcontextprotocol/core-internal/ext/skills';

import type { CatalogSnapshot } from './catalogBuild';
import { trustedSources } from './internal';
Expand All @@ -13,6 +13,21 @@ export interface CatalogState {

const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error));

// Normalises a requested resource or directory URI below its authority so alternate
// percent-encodings (`Q%26A.md`, lowercase `%c3%a9`, raw `^` on Node 20/22) match the
// catalog's published entry, while rejecting URIs carrying a query or fragment.
function catalogUriKey(uri: string): string | undefined {
let url: URL;
try {
url = new URL(uri);
} catch {
return undefined;
}
const canonical = url.href;
if (/[#?]/.test(canonical)) return undefined;
return skillFileKey(`${url.protocol}//${url.host}`, canonical);
}

// A trusted source: the SDK built and checked the snapshot's entries.
export function catalogSource(
state: CatalogState,
Expand All @@ -31,8 +46,8 @@ export function catalogSource(
},
read: uri => readChecked(state.current, files, uri, warn),
async readDirectory(uri, { cursor }) {
const canonical = canonicalUri(uri);
const children = canonical === undefined ? undefined : state.current.dirs.get(canonical);
const key = catalogUriKey(uri);
const children = key === undefined ? undefined : state.current.dirs.get(key);
if (children === undefined) return;
const page = paginate(children, cursor, pageSize, child => child.uri);
return page.nextCursor === undefined ? { resources: page.items } : { resources: page.items, nextCursor: page.nextCursor };
Expand All @@ -52,10 +67,10 @@ async function readChecked(
uri: string,
warn: (warning: SkillWarning) => void
): Promise<SkillReadResult | undefined> {
const canonical = canonicalUri(uri);
const file = canonical === undefined ? undefined : snapshot.files.get(canonical);
const key = catalogUriKey(uri);
const file = key === undefined ? undefined : snapshot.files.get(key);
if (file === undefined) {
if (canonical !== undefined && snapshot.dirs.has(canonical)) {
if (key !== undefined && snapshot.dirs.has(key)) {
throw new ResourceNotFoundError(uri, `${uri} is a directory; use resources/directory/read`);
}
return undefined;
Expand Down
55 changes: 55 additions & 0 deletions packages/server/test/ext/skills/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,61 @@ describe('SkillCatalog serving', () => {
}
});

it('resolves equivalent percent-encodings, lowercase hex, and raw carets in resources/read and resources/directory/read', async () => {
const { mcp, catalog, files } = await loaded();
files['git-workflow/references/Q&A.md'] = '# Q&A\n';
files['git-workflow/references/x^y.md'] = '# Caret\n';
files['git-workflow/references/100%.md'] = '# Percent\n';
files['git-workflow/Q&A/note.md'] = '# Dir QA\n';
files['git-workflow/x^y/note.md'] = '# Dir Caret\n';
await catalog.reload();

for (const [requested, text] of [
['skill://git-workflow/references/Q&A.md', '# Q&A\n'],
['skill://git-workflow/references/Q%26A.md', '# Q&A\n'],
['skill://git-workflow/references/%51&A.md', '# Q&A\n'],
['skill://git-workflow/references/x^y.md', '# Caret\n'],
['skill://git-workflow/references/x%5Ey.md', '# Caret\n'],
['skill://acme/billing/refunds/templates/Rembours%c3%a9.md', '# Remboursement\n'],
['skill://git-workflow/references/100%25.md', '# Percent\n'],
['skill://git-workflow/references/100%.md', '# Percent\n']
] as const) {
expect(await resultOf(mcp, 'resources/read', { uri: requested })).toMatchObject({ contents: [{ text }] });
}

for (const rejected of [
'skill://git-workflow/references/Q&A.md?v=1',
'skill://git-workflow/references/Q&A.md?',
'skill://git-workflow/references/Q&A.md#section',
'skill://git-workflow/references/Q&A.md#'
]) {
expect(await errorOf(mcp, 'resources/read', { uri: rejected })).toMatchObject({ code: -32_602 });
}

for (const dirUri of ['skill://git-workflow/Q%26A', 'skill://git-workflow/x^y']) {
expect(await errorOf(mcp, 'resources/read', { uri: dirUri })).toMatchObject({
code: -32_602,
message: expect.stringMatching(/is a directory/)
});
expect(await resultOf(mcp, 'resources/directory/read', { uri: dirUri })).toMatchObject({
resources: [{ name: 'note.md' }]
});
}

for (const badDir of ['skill://git-workflow/Q&A?v=1', 'skill://git-workflow/Q&A#section']) {
expect(await errorOf(mcp, 'resources/directory/read', { uri: badDir })).toMatchObject({ code: -32_602 });
}

const prefixed = await loaded({ prefix: 'acme:prod' });
prefixed.files['git-workflow/references/Q&A.md'] = '# Prefixed Q&A\n';
await prefixed.catalog.reload();
expect(
await resultOf(prefixed.mcp, 'resources/read', { uri: 'skill://acme%3Aprod/git-workflow/references/Q%26A.md' })
).toMatchObject({
contents: [{ text: '# Prefixed Q&A\n' }]
});
});

it('answers -32602 when resources/read targets a directory', async () => {
const { mcp } = await loaded();
expect(await errorOf(mcp, 'resources/read', { uri: 'skill://acme/billing/refunds' })).toMatchObject({
Expand Down
Loading