Skip to content

Commit 1eb80c4

Browse files
v2: add guard methods (modelcontextprotocol#1842)
1 parent 7ba58da commit 1eb80c4

File tree

5 files changed

+172
-3
lines changed

5 files changed

+172
-3
lines changed

docs/migration-SKILL.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Notes:
8686
| `JSONRPCError` | `JSONRPCErrorResponse` |
8787
| `JSONRPCErrorSchema` | `JSONRPCErrorResponseSchema` |
8888
| `isJSONRPCError` | `isJSONRPCErrorResponse` |
89-
| `isJSONRPCResponse` | `isJSONRPCResultResponse` |
89+
| `isJSONRPCResponse` (deprecated in v1) | `isJSONRPCResultResponse` (**not** v2's new `isJSONRPCResponse`, which correctly matches both result and error) |
9090
| `ResourceReference` | `ResourceTemplateReference` |
9191
| `ResourceReferenceSchema` | `ResourceTemplateReferenceSchema` |
9292
| `IsomorphicHeaders` | REMOVED (use Web Standard `Headers`) |
@@ -98,7 +98,7 @@ Notes:
9898
| `StreamableHTTPError` | REMOVED (use `SdkError` with `SdkErrorCode.ClientHttp*`) |
9999
| `WebSocketClientTransport` | REMOVED (use `StreamableHTTPClientTransport` or `StdioClientTransport`) |
100100

101-
All other symbols from `@modelcontextprotocol/sdk/types.js` retain their original names (e.g., `CallToolResultSchema`, `ListToolsResultSchema`, etc.).
101+
All other **type** symbols from `@modelcontextprotocol/sdk/types.js` retain their original names. **Zod schemas** (e.g., `CallToolResultSchema`, `ListToolsResultSchema`) are no longer part of the public API — they are internal to the SDK. For runtime validation, use type guard functions like `isCallToolResult` instead of `CallToolResultSchema.safeParse()`.
102102

103103
### Error class changes
104104

@@ -435,6 +435,13 @@ const tool = await client.callTool({ name: 'my-tool', arguments: {} });
435435

436436
Remove unused schema imports: `CallToolResultSchema`, `CompatibilityCallToolResultSchema`, `ElicitResultSchema`, `CreateMessageResultSchema`, etc., when they were only used in `request()`/`send()`/`callTool()` calls.
437437

438+
If `CallToolResultSchema` was used for **runtime validation** (not just as a `request()` argument), replace with the `isCallToolResult` type guard:
439+
440+
| v1 pattern | v2 replacement |
441+
| --------------------------------------------------- | -------------------------- |
442+
| `CallToolResultSchema.safeParse(value).success` | `isCallToolResult(value)` |
443+
| `CallToolResultSchema.parse(value)` | Use `isCallToolResult(value)` then cast, or use `CallToolResult` type |
444+
438445
## 12. Experimental: `TaskCreationParams.ttl` no longer accepts `null`
439446

440447
`TaskCreationParams.ttl` changed from `z.union([z.number(), z.null()]).optional()` to `z.number().optional()`. Per the MCP spec, `null` TTL (unlimited lifetime) is only valid in server responses (`Task.ttl`), not in client requests. Omit `ttl` to let the server decide.

docs/migration.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,18 @@ const result = await client.callTool({ name: 'my-tool', arguments: {} });
442442

443443
The return type is now inferred from the method name via `ResultTypeMap`. For example, `client.request({ method: 'tools/call', ... })` returns `Promise<CallToolResult | CreateTaskResult>`.
444444

445+
If you were using `CallToolResultSchema` for **runtime validation** (not just in `request()`/`callTool()` calls), use the new `isCallToolResult` type guard instead:
446+
447+
```typescript
448+
// v1: runtime validation with Zod schema
449+
import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
450+
if (CallToolResultSchema.safeParse(value).success) { /* ... */ }
451+
452+
// v2: use the type guard
453+
import { isCallToolResult } from '@modelcontextprotocol/client';
454+
if (isCallToolResult(value)) { /* ... */ }
455+
```
456+
445457
### Client list methods return empty results for missing capabilities
446458

447459
`Client.listPrompts()`, `listResources()`, `listResourceTemplates()`, and `listTools()` now return empty results when the server didn't advertise the corresponding capability, instead of sending the request. This respects the MCP spec's capability negotiation.
@@ -482,14 +494,16 @@ The following deprecated type aliases have been removed from `@modelcontextproto
482494
| `JSONRPCError` | `JSONRPCErrorResponse` |
483495
| `JSONRPCErrorSchema` | `JSONRPCErrorResponseSchema` |
484496
| `isJSONRPCError` | `isJSONRPCErrorResponse` |
485-
| `isJSONRPCResponse` | `isJSONRPCResultResponse` |
497+
| `isJSONRPCResponse` | `isJSONRPCResultResponse` (see note below) |
486498
| `ResourceReferenceSchema` | `ResourceTemplateReferenceSchema` |
487499
| `ResourceReference` | `ResourceTemplateReference` |
488500
| `IsomorphicHeaders` | Use Web Standard `Headers` |
489501
| `AuthInfo` (from `server/auth/types.js`) | `AuthInfo` (now re-exported by `@modelcontextprotocol/client` and `@modelcontextprotocol/server`) |
490502

491503
All other types and schemas exported from `@modelcontextprotocol/sdk/types.js` retain their original names — import them from `@modelcontextprotocol/client` or `@modelcontextprotocol/server`.
492504

505+
> **Note on `isJSONRPCResponse`:** v1's `isJSONRPCResponse` was a deprecated alias that only checked for *result* responses (it was equivalent to `isJSONRPCResultResponse`). v2 removes the deprecated alias and introduces a **new** `isJSONRPCResponse` with corrected semantics — it checks for *any* response (either result or error). If you are migrating v1 code that used `isJSONRPCResponse`, rename it to `isJSONRPCResultResponse` to preserve the original behavior. Use the new `isJSONRPCResponse` only when you want to match both result and error responses.
506+
493507
**Before (v1):**
494508

495509
```typescript

packages/core/src/exports/public/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ export { ProtocolError, UrlElicitationRequiredError } from '../../types/errors.j
104104
export {
105105
assertCompleteRequestPrompt,
106106
assertCompleteRequestResourceTemplate,
107+
isCallToolResult,
107108
isInitializedNotification,
108109
isInitializeRequest,
109110
isJSONRPCErrorResponse,
110111
isJSONRPCNotification,
111112
isJSONRPCRequest,
113+
isJSONRPCResponse,
112114
isJSONRPCResultResponse,
113115
isTaskAugmentedRequestParams,
114116
parseJSONRPCMessage
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { JSONRPC_VERSION } from './constants.js';
4+
import { isCallToolResult, isJSONRPCErrorResponse, isJSONRPCResponse, isJSONRPCResultResponse } from './guards.js';
5+
6+
describe('isJSONRPCResponse', () => {
7+
it('returns true for a valid result response', () => {
8+
expect(
9+
isJSONRPCResponse({
10+
jsonrpc: JSONRPC_VERSION,
11+
id: 1,
12+
result: {}
13+
})
14+
).toBe(true);
15+
});
16+
17+
it('returns true for a valid error response', () => {
18+
expect(
19+
isJSONRPCResponse({
20+
jsonrpc: JSONRPC_VERSION,
21+
id: 1,
22+
error: { code: -32_600, message: 'Invalid Request' }
23+
})
24+
).toBe(true);
25+
});
26+
27+
it('returns false for a request', () => {
28+
expect(
29+
isJSONRPCResponse({
30+
jsonrpc: JSONRPC_VERSION,
31+
id: 1,
32+
method: 'test'
33+
})
34+
).toBe(false);
35+
});
36+
37+
it('returns false for a notification', () => {
38+
expect(
39+
isJSONRPCResponse({
40+
jsonrpc: JSONRPC_VERSION,
41+
method: 'test'
42+
})
43+
).toBe(false);
44+
});
45+
46+
it('returns false for arbitrary objects', () => {
47+
expect(isJSONRPCResponse({ foo: 'bar' })).toBe(false);
48+
});
49+
50+
it('narrows the type correctly', () => {
51+
const value: unknown = {
52+
jsonrpc: JSONRPC_VERSION,
53+
id: 1,
54+
result: { content: [] }
55+
};
56+
if (isJSONRPCResponse(value)) {
57+
// Type should be narrowed to JSONRPCResponse
58+
expect(value.jsonrpc).toBe(JSONRPC_VERSION);
59+
expect(value.id).toBe(1);
60+
}
61+
});
62+
63+
it('agrees with isJSONRPCResultResponse || isJSONRPCErrorResponse', () => {
64+
const values = [
65+
{ jsonrpc: JSONRPC_VERSION, id: 1, result: {} },
66+
{ jsonrpc: JSONRPC_VERSION, id: 2, error: { code: -1, message: 'err' } },
67+
{ jsonrpc: JSONRPC_VERSION, id: 3, method: 'test' },
68+
{ jsonrpc: JSONRPC_VERSION, method: 'notify' },
69+
{ foo: 'bar' },
70+
null,
71+
42
72+
];
73+
for (const v of values) {
74+
expect(isJSONRPCResponse(v)).toBe(isJSONRPCResultResponse(v) || isJSONRPCErrorResponse(v));
75+
}
76+
});
77+
});
78+
79+
describe('isCallToolResult', () => {
80+
it('returns false for an empty object (content is required)', () => {
81+
expect(isCallToolResult({})).toBe(false);
82+
});
83+
84+
it('returns true for a result with content', () => {
85+
expect(
86+
isCallToolResult({
87+
content: [{ type: 'text', text: 'hello' }]
88+
})
89+
).toBe(true);
90+
});
91+
92+
it('returns true for a result with isError', () => {
93+
expect(
94+
isCallToolResult({
95+
content: [{ type: 'text', text: 'fail' }],
96+
isError: true
97+
})
98+
).toBe(true);
99+
});
100+
101+
it('returns true for a result with structuredContent', () => {
102+
expect(
103+
isCallToolResult({
104+
content: [],
105+
structuredContent: { key: 'value' }
106+
})
107+
).toBe(true);
108+
});
109+
110+
it('returns false for non-objects', () => {
111+
expect(isCallToolResult(null)).toBe(false);
112+
expect(isCallToolResult(42)).toBe(false);
113+
expect(isCallToolResult('string')).toBe(false);
114+
});
115+
116+
it('returns false for invalid content items', () => {
117+
expect(
118+
isCallToolResult({
119+
content: [{ type: 'invalid' }]
120+
})
121+
).toBe(false);
122+
});
123+
});

packages/core/src/types/guards.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import {
2+
CallToolResultSchema,
23
InitializedNotificationSchema,
34
InitializeRequestSchema,
45
JSONRPCErrorResponseSchema,
56
JSONRPCMessageSchema,
67
JSONRPCNotificationSchema,
78
JSONRPCRequestSchema,
9+
JSONRPCResponseSchema,
810
JSONRPCResultResponseSchema,
911
TaskAugmentedRequestParamsSchema
1012
} from './schemas.js';
1113
import type {
14+
CallToolResult,
1215
CompleteRequest,
1316
CompleteRequestPrompt,
1417
CompleteRequestResourceTemplate,
@@ -18,6 +21,7 @@ import type {
1821
JSONRPCMessage,
1922
JSONRPCNotification,
2023
JSONRPCRequest,
24+
JSONRPCResponse,
2125
JSONRPCResultResponse,
2226
TaskAugmentedRequestParams
2327
} from './types.js';
@@ -58,6 +62,25 @@ export const isJSONRPCResultResponse = (value: unknown): value is JSONRPCResultR
5862
export const isJSONRPCErrorResponse = (value: unknown): value is JSONRPCErrorResponse =>
5963
JSONRPCErrorResponseSchema.safeParse(value).success;
6064

65+
/**
66+
* Checks if a value is a valid {@linkcode JSONRPCResponse} (either a result or error response).
67+
* @param value - The value to check.
68+
*
69+
* @returns True if the value is a valid {@linkcode JSONRPCResponse}, false otherwise.
70+
*/
71+
export const isJSONRPCResponse = (value: unknown): value is JSONRPCResponse => JSONRPCResponseSchema.safeParse(value).success;
72+
73+
/**
74+
* Checks if a value is a valid {@linkcode CallToolResult}.
75+
* @param value - The value to check.
76+
*
77+
* @returns True if the value is a valid {@linkcode CallToolResult}, false otherwise.
78+
*/
79+
export const isCallToolResult = (value: unknown): value is CallToolResult => {
80+
if (typeof value !== 'object' || value === null || !('content' in value)) return false;
81+
return CallToolResultSchema.safeParse(value).success;
82+
};
83+
6184
/**
6285
* Checks if a value is a valid {@linkcode TaskAugmentedRequestParams}.
6386
* @param value - The value to check.

0 commit comments

Comments
 (0)