Skip to content

Commit 6f302eb

Browse files
docs(core): qualify ResourceNotFoundError fromError reconstruction; fix cross-bundle example
- JSDoc / changeset / migration.md now state that ProtocolError.fromError reconstructs ResourceNotFoundError for either code only when error.data carries `uri`; a bare -32002 without data.uri stays a generic ProtocolError. - migration.md example: drop the outer `instanceof ProtocolError` gate (it defeats the cross-bundle case the prose describes); the example now feeds code+data straight into fromError and tests the locally-created result.
1 parent d57d832 commit 6f302eb

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

.changeset/resource-not-found-32602.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ identically without changes.
1717
`ProtocolErrorCode.ResourceNotFound` (`-32002`) remains importable as receive-tolerated
1818
vocabulary; clients should accept both `-32602` and `-32002` from peers (the
1919
specification's backwards-compatibility clause). The new typed `ResourceNotFoundError`
20-
class carries `data.uri`, and `ProtocolError.fromError` recognises both codes by the
21-
`data.uri` shape.
20+
class carries `data.uri`, and `ProtocolError.fromError` reconstructs it for either code
21+
when `error.data` carries `uri` — a bare `-32002` without `data.uri` stays a generic
22+
`ProtocolError`.

docs/migration.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,18 +1256,21 @@ with the `allowInputRequired: true` request option plus the `withInputRequired()
12561256
revisions, so v1.x peers see no change. An interim `-32002` emission that shipped in earlier v2 alphas is reverted: the era encode seam maps any handler-thrown `-32002` to `-32602` on the wire, so a handler written against the older alpha behaves identically without changes.
12571257

12581258
`ProtocolErrorCode.ResourceNotFound` (`-32002`) **remains importable** as receive-tolerated vocabulary: clients should accept both `-32602` and `-32002` from peers (the specification's backwards-compatibility clause). The new typed `ResourceNotFoundError` class carries the URI on
1259-
`.uri`, and `ProtocolError.fromError` recognizes both codes by the `data.uri` shape — recognize peers' errors by their code and `error.data`, not by `instanceof`, which does not survive bundling. Servers must not return an empty `contents` array for a non-existent resource (an
1260-
empty array is ambiguous between "exists but empty" and "does not exist").
1259+
`.uri`, and `ProtocolError.fromError` reconstructs it for either code when `error.data` carries `uri` (a bare `-32002` without `data.uri` stays a generic `ProtocolError`) — recognize peers' errors by their code and `error.data`, not by `instanceof`, which does not survive
1260+
bundling. Servers must not return an empty `contents` array for a non-existent resource (an empty array is ambiguous between "exists but empty" and "does not exist").
12611261

12621262
```typescript
12631263
import { ProtocolError, ResourceNotFoundError } from '@modelcontextprotocol/client';
12641264

12651265
try {
12661266
await client.readResource({ uri: 'file:///nope' });
12671267
} catch (error) {
1268-
const typed = error instanceof ProtocolError ? ProtocolError.fromError(error.code, error.message, error.data) : undefined;
1269-
if (typed instanceof ResourceNotFoundError) {
1270-
console.log('not found:', typed.uri);
1268+
// fromError reconstructs the typed class from code + data alone, so this
1269+
// works even when `error` crossed a bundle boundary and `instanceof` on
1270+
// the thrown object would not match.
1271+
const e = error as ProtocolError;
1272+
if (ProtocolError.fromError(e.code, e.message, e.data) instanceof ResourceNotFoundError) {
1273+
console.log('not found:', (e.data as { uri: string }).uri);
12711274
}
12721275
}
12731276
```

packages/core/src/types/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ export class ProtocolError extends Error {
7878
* `-32602` with `data.uri` is resource-not-found; any other `-32602` is an
7979
* ordinary Invalid Params). For backwards compatibility, clients should also
8080
* accept `-32002` as resource not found — earlier SDK builds emitted that
81-
* code, and {@linkcode ProtocolError.fromError} recognises both. Do not rely
82-
* on `instanceof` — it does not work across separately bundled copies of the
81+
* code, and {@linkcode ProtocolError.fromError} reconstructs this class for
82+
* either code **when `error.data` carries `uri`** (a bare `-32002` without
83+
* `data.uri` stays a generic {@linkcode ProtocolError}). Do not rely on
84+
* `instanceof` — it does not work across separately bundled copies of the
8385
* SDK.
8486
*/
8587
export class ResourceNotFoundError extends ProtocolError {

0 commit comments

Comments
 (0)