Skip to content

Commit b73f20f

Browse files
ochafikclaude
andcommitted
feat: add .strict() transform and re-export JSON-RPC schemas
Add post-processing transform to append .strict() to JSON-RPC schemas: - JSONRPCRequestSchema - JSONRPCNotificationSchema - JSONRPCResultResponseSchema - JSONRPCErrorResponseSchema This makes them compatible with types.ts's stricter validation, allowing re-export. Total schemas re-exported: 49 Lines saved: ~452 (2600 → 2148) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5c8d559 commit b73f20f

File tree

3 files changed

+59
-75
lines changed

3 files changed

+59
-75
lines changed

scripts/generate-schemas.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ const FIELD_OVERRIDES: Record<string, Record<string, string>> = {
8989
*/
9090
const INTEGER_SCHEMAS = ['ProgressTokenSchema', 'RequestIdSchema'];
9191

92+
/**
93+
* Schemas that need .strict() added for stricter validation.
94+
*/
95+
const STRICT_SCHEMAS = [
96+
'JSONRPCRequestSchema',
97+
'JSONRPCNotificationSchema',
98+
'JSONRPCResultResponseSchema',
99+
'JSONRPCErrorResponseSchema',
100+
];
101+
92102
// =============================================================================
93103
// Pre-processing: Transform spec types to SDK-compatible hierarchy
94104
// =============================================================================
@@ -261,6 +271,7 @@ const AST_TRANSFORMS: Transform[] = [
261271
transformIntegerRefinements,
262272
transformUnionToEnum,
263273
applyFieldOverrides,
274+
addStrictToSchemas,
264275
];
265276

266277
/**
@@ -472,6 +483,25 @@ function applyFieldOverrides(sourceFile: SourceFile): void {
472483
}
473484
}
474485

486+
/**
487+
* Add .strict() to specified schemas for stricter validation.
488+
* This matches the SDK's behavior of rejecting unknown properties.
489+
*/
490+
function addStrictToSchemas(sourceFile: SourceFile): void {
491+
for (const schemaName of STRICT_SCHEMAS) {
492+
const varDecl = sourceFile.getVariableDeclaration(schemaName);
493+
if (!varDecl) continue;
494+
495+
const initializer = varDecl.getInitializer();
496+
if (!initializer) continue;
497+
498+
// Append .strict() to the schema
499+
const currentText = initializer.getText();
500+
varDecl.setInitializer(`${currentText}.strict()`);
501+
console.log(` ✓ Added .strict() to ${schemaName}`);
502+
}
503+
}
504+
475505
// =============================================================================
476506
// Main
477507
// =============================================================================

src/generated/sdk.schemas.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,33 @@ export const RequestSchema = z.object({
132132
*/
133133
export const JSONRPCNotificationSchema = NotificationSchema.extend({
134134
jsonrpc: z.literal('2.0')
135-
});
135+
}).strict();
136136

137137
/**
138138
* A successful (non-error) response to a request.
139139
*
140140
* @category JSON-RPC
141141
*/
142-
export const JSONRPCResultResponseSchema = z.object({
143-
jsonrpc: z.literal('2.0'),
144-
id: RequestIdSchema,
145-
result: ResultSchema
146-
});
142+
export const JSONRPCResultResponseSchema = z
143+
.object({
144+
jsonrpc: z.literal('2.0'),
145+
id: RequestIdSchema,
146+
result: ResultSchema
147+
})
148+
.strict();
147149

148150
/**
149151
* A response to a request that indicates an error occurred.
150152
*
151153
* @category JSON-RPC
152154
*/
153-
export const JSONRPCErrorResponseSchema = z.object({
154-
jsonrpc: z.literal('2.0'),
155-
id: RequestIdSchema.optional(),
156-
error: ErrorSchema
157-
});
155+
export const JSONRPCErrorResponseSchema = z
156+
.object({
157+
jsonrpc: z.literal('2.0'),
158+
id: RequestIdSchema.optional(),
159+
error: ErrorSchema
160+
})
161+
.strict();
158162

159163
/**
160164
* A response to a request, containing either the result or error.
@@ -1964,7 +1968,7 @@ export const ListToolsResultSchema = PaginatedResultSchema.extend({
19641968
export const JSONRPCRequestSchema = RequestSchema.extend({
19651969
jsonrpc: z.literal('2.0'),
19661970
id: RequestIdSchema
1967-
});
1971+
}).strict();
19681972

19691973
/**
19701974
* An error response that indicates that the server requires the client to provide additional information via an elicitation request.

src/types.ts

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ import {
7070
PromptListChangedNotificationSchema,
7171
ToolListChangedNotificationSchema,
7272
RootsListChangedNotificationSchema,
73+
// JSON-RPC schemas (with .strict() for validation)
74+
JSONRPCRequestSchema,
75+
JSONRPCNotificationSchema,
76+
JSONRPCResultResponseSchema,
77+
JSONRPCErrorResponseSchema,
7378
} from './generated/sdk.schemas.js';
7479

7580
// Alias RequestParamsSchema to BaseRequestParamsSchema for internal use
@@ -122,6 +127,10 @@ export {
122127
PromptListChangedNotificationSchema,
123128
ToolListChangedNotificationSchema,
124129
RootsListChangedNotificationSchema,
130+
JSONRPCRequestSchema,
131+
JSONRPCNotificationSchema,
132+
JSONRPCResultResponseSchema,
133+
JSONRPCErrorResponseSchema,
125134
};
126135

127136
export const LATEST_PROTOCOL_VERSION = '2025-11-25';
@@ -182,47 +191,15 @@ const RequestMetaSchema = z.looseObject({
182191
export const isTaskAugmentedRequestParams = (value: unknown): value is TaskAugmentedRequestParams =>
183192
TaskAugmentedRequestParamsSchema.safeParse(value).success;
184193

185-
// Note: RequestSchema, NotificationSchema, ResultSchema are re-exported from generated.
186-
// They include proper _meta typing with RELATED_TASK_META_KEY.
187-
188-
/**
189-
* A request that expects a response.
190-
*/
191-
export const JSONRPCRequestSchema = z
192-
.object({
193-
jsonrpc: z.literal(JSONRPC_VERSION),
194-
id: RequestIdSchema,
195-
...RequestSchema.shape
196-
})
197-
.strict();
194+
// Note: RequestSchema, NotificationSchema, ResultSchema, and JSON-RPC schemas
195+
// are re-exported from generated. They include proper _meta typing and .strict().
198196

199197
export const isJSONRPCRequest = (value: unknown): value is JSONRPCRequest => JSONRPCRequestSchema.safeParse(value).success;
200-
201-
/**
202-
* A notification which does not expect a response.
203-
*/
204-
export const JSONRPCNotificationSchema = z
205-
.object({
206-
jsonrpc: z.literal(JSONRPC_VERSION),
207-
...NotificationSchema.shape
208-
})
209-
.strict();
210-
211198
export const isJSONRPCNotification = (value: unknown): value is JSONRPCNotification => JSONRPCNotificationSchema.safeParse(value).success;
212-
213-
/**
214-
* A successful (non-error) response to a request.
215-
*/
216-
export const JSONRPCResultResponseSchema = z
217-
.object({
218-
jsonrpc: z.literal(JSONRPC_VERSION),
219-
id: RequestIdSchema,
220-
result: ResultSchema
221-
})
222-
.strict();
223-
224199
export const isJSONRPCResultResponse = (value: unknown): value is JSONRPCResultResponse =>
225200
JSONRPCResultResponseSchema.safeParse(value).success;
201+
export const isJSONRPCErrorResponse = (value: unknown): value is JSONRPCErrorResponse =>
202+
JSONRPCErrorResponseSchema.safeParse(value).success;
226203

227204
/**
228205
* Error codes defined by the JSON-RPC specification.
@@ -243,33 +220,6 @@ export enum ErrorCode {
243220
UrlElicitationRequired = -32042
244221
}
245222

246-
/**
247-
* A response to a request that indicates an error occurred.
248-
*/
249-
export const JSONRPCErrorResponseSchema = z
250-
.object({
251-
jsonrpc: z.literal(JSONRPC_VERSION),
252-
id: RequestIdSchema.optional(),
253-
error: z.object({
254-
/**
255-
* The error type that occurred.
256-
*/
257-
code: z.number().int(),
258-
/**
259-
* A short description of the error. The message SHOULD be limited to a concise single sentence.
260-
*/
261-
message: z.string(),
262-
/**
263-
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.).
264-
*/
265-
data: z.unknown().optional()
266-
})
267-
})
268-
.strict();
269-
270-
export const isJSONRPCErrorResponse = (value: unknown): value is JSONRPCErrorResponse =>
271-
JSONRPCErrorResponseSchema.safeParse(value).success;
272-
273223
export const JSONRPCMessageSchema = z.union([
274224
JSONRPCRequestSchema,
275225
JSONRPCNotificationSchema,

0 commit comments

Comments
 (0)