Skip to content

Commit fba3e0e

Browse files
ochafikclaude
andcommitted
refactor: inline JSONRPCResponse into JSONRPCMessage during pre-processing
Add inlineJSONRPCResponse() transform that: 1. Replaces JSONRPCResponse with JSONRPCResultResponse | JSONRPCErrorResponse in JSONRPCMessage 2. Removes JSONRPCResponse type alias from generated types This makes the generated JSONRPCMessage match the local schema structure (4 members), enabling better type compatibility. JSONRPCResponseSchema remains defined locally in types.ts as a convenience union. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 60ebd53 commit fba3e0e

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

scripts/generate-schemas.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,15 @@ function preProcessTypes(content: string): string {
156156
// Transform 4: Update Request.params to use RequestParams
157157
updateRequestParamsType(sourceFile);
158158

159-
// Transform 5: Convert JSDoc comments to @description tags for .describe() generation
159+
// Transform 5: Inline JSONRPCResponse into JSONRPCMessage and remove JSONRPCResponse
160+
// (types.ts will define these locally with proper schema unions)
161+
inlineJSONRPCResponse(sourceFile);
162+
163+
// Transform 6: Convert JSDoc comments to @description tags for .describe() generation
160164
// (Must run before injectDerivedCapabilityTypes so inline types get @description)
161165
convertJsDocToDescription(sourceFile);
162166

163-
// Transform 6: Add derived capability types (extracts from parent interfaces)
167+
// Transform 7: Add derived capability types (extracts from parent interfaces)
164168
injectDerivedCapabilityTypes(sourceFile);
165169

166170
return sourceFile.getFullText();
@@ -284,6 +288,42 @@ function updateRequestParamsType(sourceFile: SourceFile): void {
284288
}
285289
}
286290

291+
/**
292+
* Inline JSONRPCResponse into JSONRPCMessage and remove JSONRPCResponse type.
293+
* This allows types.ts to define these as schema unions locally.
294+
*
295+
* Transforms:
296+
* type JSONRPCMessage = JSONRPCRequest | JSONRPCNotification | JSONRPCResponse;
297+
* type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse;
298+
* Into:
299+
* type JSONRPCMessage = JSONRPCRequest | JSONRPCNotification | JSONRPCResultResponse | JSONRPCErrorResponse;
300+
* (JSONRPCResponse removed)
301+
*/
302+
function inlineJSONRPCResponse(sourceFile: SourceFile): void {
303+
// Find and update JSONRPCMessage
304+
const messageType = sourceFile.getTypeAlias('JSONRPCMessage');
305+
if (messageType) {
306+
const typeNode = messageType.getTypeNode();
307+
if (typeNode) {
308+
const text = typeNode.getText();
309+
// Replace JSONRPCResponse with its components
310+
const newType = text.replace(
311+
'JSONRPCResponse',
312+
'JSONRPCResultResponse | JSONRPCErrorResponse'
313+
);
314+
messageType.setType(newType);
315+
console.log(' ✓ Inlined JSONRPCResponse into JSONRPCMessage');
316+
}
317+
}
318+
319+
// Remove JSONRPCResponse type alias
320+
const responseType = sourceFile.getTypeAlias('JSONRPCResponse');
321+
if (responseType) {
322+
responseType.remove();
323+
console.log(' ✓ Removed JSONRPCResponse type (defined locally in types.ts)');
324+
}
325+
}
326+
287327
/**
288328
* Add derived capability types by extracting nested properties from parent interfaces.
289329
* This creates concrete interface definitions that ts-to-zod can generate schemas for.

src/generated/sdk.schemas.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ export const JSONRPCErrorResponseSchema = z
161161
.strict()
162162
.describe('A response to a request that indicates an error occurred.');
163163

164-
/** @description A response to a request, containing either the result or error. */
165-
export const JSONRPCResponseSchema = z
166-
.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema])
167-
.describe('A response to a request, containing either the result or error.');
168-
169164
/* Empty result */
170165
/**
171166
* @description A response that indicates success but carries no data.
@@ -2428,7 +2423,7 @@ To update this file, run: npm run fetch:spec-types */ /* JSON-RPC types */
24282423
* @category JSON-RPC
24292424
*/
24302425
export const JSONRPCMessageSchema = z
2431-
.union([JSONRPCRequestSchema, JSONRPCNotificationSchema, JSONRPCResponseSchema])
2426+
.union([JSONRPCRequestSchema, JSONRPCNotificationSchema, JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema])
24322427
.describe(
24332428
'This file is automatically generated from the Model Context Protocol specification.\n\nSource: https://github.com/modelcontextprotocol/modelcontextprotocol\nPulled from: https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/main/schema/draft/schema.ts\nLast updated from commit: 35fa160caf287a9c48696e3ae452c0645c713669\n\nDO NOT EDIT THIS FILE MANUALLY. Changes will be overwritten by automated updates.\nTo update this file, run: npm run fetch:spec-types'
24342429
);

src/generated/sdk.types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @category JSON-RPC
1919
*/
20-
export type JSONRPCMessage = JSONRPCRequest | JSONRPCNotification | JSONRPCResponse;
20+
export type JSONRPCMessage = JSONRPCRequest | JSONRPCNotification | JSONRPCResultResponse | JSONRPCErrorResponse;
2121

2222
/** @internal */
2323
export const LATEST_PROTOCOL_VERSION = 'DRAFT-2026-v1';
@@ -150,9 +150,6 @@ export interface JSONRPCErrorResponse {
150150
error: Error;
151151
}
152152

153-
/** @description A response to a request, containing either the result or error. */
154-
export type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse;
155-
156153
// Standard JSON-RPC error codes
157154
export const PARSE_ERROR = -32700;
158155
export const INVALID_REQUEST = -32600;

src/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ import {
197197
// Derived capability schemas (generated from extracted types)
198198
ClientTasksCapabilitySchema,
199199
ServerTasksCapabilitySchema,
200-
// JSON-RPC schemas
201-
JSONRPCResponseSchema,
202200
// Progress
203201
ProgressNotificationParamsSchema,
204202
} from './generated/sdk.schemas.js';
@@ -343,7 +341,6 @@ export {
343341
SamplingMessageContentBlockSchema,
344342
ClientTasksCapabilitySchema,
345343
ServerTasksCapabilitySchema,
346-
JSONRPCResponseSchema,
347344
ProgressNotificationParamsSchema,
348345
};
349346

@@ -421,14 +418,15 @@ export enum ErrorCode {
421418
UrlElicitationRequired = -32042
422419
}
423420

421+
export const JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]);
422+
424423
export const JSONRPCMessageSchema = z.union([
425424
JSONRPCRequestSchema,
426425
JSONRPCNotificationSchema,
427426
JSONRPCResultResponseSchema,
428427
JSONRPCErrorResponseSchema
429428
]);
430429

431-
432430
/* Cancellation */
433431
/**
434432
* This notification can be sent by either side to indicate that it is cancelling a previously-issued request.

0 commit comments

Comments
 (0)