Skip to content

Commit 1f43d9c

Browse files
committed
fix(server): return -32602 for nonexistent resources (SEP-2164)
Per SEP-2164, the draft spec upgrades resource-not-found handling from SHOULD return -32002 (2025-11-25) to MUST return -32602 (Invalid params). The requested URI is included in the error's data.uri so clients can still distinguish not-found from other invalid-params errors. ProtocolErrorCode.ResourceNotFound (-32002) is deprecated but remains exported, since clients SHOULD still accept it from older servers. Closes #2194
1 parent 8d55531 commit 1f43d9c

6 files changed

Lines changed: 26 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modelcontextprotocol/server': patch
3+
'@modelcontextprotocol/core': patch
4+
---
5+
6+
Resource not found now returns `-32602` (Invalid params) per SEP-2164; `-32002` (`ProtocolErrorCode.ResourceNotFound`) is deprecated. The error includes the requested URI in `data.uri` so clients can still distinguish not-found from other invalid-params errors. Clients SHOULD continue to accept legacy `-32002` from older servers.

packages/core/src/types/enums.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export enum ProtocolErrorCode {
1111
InternalError = -32_603,
1212

1313
// MCP-specific error codes
14+
/**
15+
* Legacy error code for reads of nonexistent resources.
16+
*
17+
* @deprecated Per SEP-2164, servers MUST return {@link ProtocolErrorCode.InvalidParams}
18+
* (`-32602`, with the requested URI in `data.uri`) for nonexistent resources. This code
19+
* remains exported because clients SHOULD still accept `-32002` from older servers.
20+
*/
1421
ResourceNotFound = -32_002,
1522
/**
1623
* Processing the request requires a capability the client did not declare

packages/server/src/server/mcp.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ export class McpServer {
406406
}
407407
}
408408

409-
throw new ProtocolError(ProtocolErrorCode.ResourceNotFound, `Resource ${uri} not found`);
409+
// SEP-2164: nonexistent resources MUST return -32602 (Invalid params); the
410+
// requested URI is included in `data` so clients can distinguish not-found.
411+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Resource ${uri} not found`, { uri: uri.toString() });
410412
});
411413

412414
this._resourceHandlersInitialized = true;

test/e2e/requirements.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ export const REQUIREMENTS: Record<string, Requirement> = {
531531
},
532532
'resources:read:unknown-uri': {
533533
source: 'https://modelcontextprotocol.io/specification/2025-11-25/server/resources#error-handling',
534-
behavior: 'resources/read for an unknown URI returns JSON-RPC error -32002 (resource not found).'
534+
behavior: 'resources/read for an unknown URI returns JSON-RPC error -32602 (Invalid params) with the URI in error data.',
535+
note: 'SEP-2164: the draft spec upgrades this to MUST -32602 (2025-11-25 said SHOULD -32002); clients SHOULD still accept legacy -32002 from older servers.'
535536
},
536537
'resources:subscribe:capability-required': {
537538
source: 'https://modelcontextprotocol.io/specification/2025-11-25/server/resources#capabilities',

test/e2e/scenarios/resources.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,10 @@ verifies('resources:read:unknown-uri', async ({ transport }: TestArgs) => {
203203
await using _ = await wire(transport, makeServer, client);
204204

205205
await expect(client.readResource({ uri: 'file:///no-such-resource' })).rejects.toMatchObject({
206-
code: -32_002,
207-
message: expect.stringMatching(/not found|unknown/i)
206+
// SEP-2164: nonexistent resources return -32602 (Invalid params) with the URI in `data`
207+
code: -32_602,
208+
message: expect.stringMatching(/not found|unknown/i),
209+
data: { uri: 'file:///no-such-resource' }
208210
});
209211
});
210212

test/integration/test/server/mcp.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,8 +2728,10 @@ describe('Zod v4', () => {
27282728
}
27292729
})
27302730
).rejects.toMatchObject({
2731-
code: ProtocolErrorCode.ResourceNotFound,
2732-
message: expect.stringContaining('not found')
2731+
// SEP-2164: nonexistent resources return -32602 (Invalid params) with the URI in `data`
2732+
code: ProtocolErrorCode.InvalidParams,
2733+
message: expect.stringContaining('not found'),
2734+
data: { uri: 'test://nonexistent' }
27332735
});
27342736
});
27352737

0 commit comments

Comments
 (0)