Skip to content

Commit 4d2ab8c

Browse files
ochafikclaude
andcommitted
feat: generate sdk.types.ts with SDK-compatible type hierarchy
Write pre-processed types to sdk.types.ts so types.ts can re-export them: 1. spec.types.ts → preProcessTypes() → sdk.types.ts 2. sdk.types.ts → ts-to-zod → sdk.schemas.ts 3. types.ts can now re-export from sdk.types.ts + sdk.schemas.ts Generated files: - sdk.types.ts: Types with `extends Request`/`Notification` (not JSONRPC*) - sdk.schemas.ts: Zod schemas matching SDK hierarchy - sdk.schemas.zod.test.ts: Integration tests This enables types.ts to become a thin re-export layer in a follow-up. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e223551 commit 4d2ab8c

File tree

6 files changed

+2603
-21
lines changed

6 files changed

+2603
-21
lines changed

scripts/generate-schemas.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ const __dirname = dirname(__filename);
3939
const PROJECT_ROOT = join(__dirname, '..');
4040

4141
const SPEC_TYPES_FILE = join(PROJECT_ROOT, 'src', 'spec.types.ts');
42+
const SDK_TYPES_FILE = join(PROJECT_ROOT, 'src', 'generated', 'sdk.types.ts');
4243
const GENERATED_DIR = join(PROJECT_ROOT, 'src', 'generated');
43-
const SCHEMA_OUTPUT_FILE = join(GENERATED_DIR, 'spec.schemas.ts');
44-
const SCHEMA_TEST_OUTPUT_FILE = join(GENERATED_DIR, 'spec.schemas.zod.test.ts');
44+
const SCHEMA_OUTPUT_FILE = join(GENERATED_DIR, 'sdk.schemas.ts');
45+
const SCHEMA_TEST_OUTPUT_FILE = join(GENERATED_DIR, 'sdk.schemas.zod.test.ts');
4546

4647
// =============================================================================
4748
// Pre-processing: Transform spec types to SDK-compatible hierarchy
@@ -98,10 +99,28 @@ async function main() {
9899

99100
// Read and pre-process spec types to match SDK hierarchy
100101
const rawSourceText = readFileSync(SPEC_TYPES_FILE, 'utf-8');
101-
const sourceText = preProcessTypes(rawSourceText);
102+
const sdkTypesContent = preProcessTypes(rawSourceText);
103+
104+
// Write pre-processed types to sdk.types.ts
105+
const sdkTypesWithHeader = `/**
106+
* SDK-compatible types generated from spec.types.ts
107+
*
108+
* This file is auto-generated by scripts/generate-schemas.ts
109+
* DO NOT EDIT MANUALLY
110+
*
111+
* Transformations applied:
112+
* - \`extends JSONRPCRequest\` → \`extends Request\`
113+
* - \`extends JSONRPCNotification\` → \`extends Notification\`
114+
*
115+
* This allows SDK types to omit jsonrpc/id fields, which are
116+
* handled at the transport layer.
117+
*/
118+
${sdkTypesContent.replace(/^\/\*\*[\s\S]*?\*\/\n/, '')}`;
119+
writeFileSync(SDK_TYPES_FILE, sdkTypesWithHeader, 'utf-8');
120+
console.log(`✅ Written: ${SDK_TYPES_FILE}`);
102121

103122
const result = generate({
104-
sourceText,
123+
sourceText: sdkTypesContent,
105124
keepComments: true,
106125
skipParseJSDoc: false,
107126
// Use PascalCase naming to match existing types.ts convention
@@ -121,17 +140,17 @@ async function main() {
121140
console.warn('⚠️ Warning: Circular dependencies detected in types');
122141
}
123142

124-
// Generate schema file with relative import to spec.types
125-
let schemasContent = result.getZodSchemasFile('../spec.types.js');
143+
// Generate schema file with relative import to sdk.types
144+
let schemasContent = result.getZodSchemasFile('./sdk.types.js');
126145
schemasContent = postProcess(schemasContent);
127146

128147
writeFileSync(SCHEMA_OUTPUT_FILE, schemasContent, 'utf-8');
129148
console.log(`✅ Written: ${SCHEMA_OUTPUT_FILE}`);
130149

131150
// Generate integration tests that verify schemas match TypeScript types
132151
const testsContent = result.getIntegrationTestFile(
133-
'../spec.types.js',
134-
'./spec.schemas.js',
152+
'./sdk.types.js',
153+
'./sdk.schemas.js',
135154
);
136155
if (testsContent) {
137156
const processedTests = postProcessTests(testsContent);

src/generated/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
/**
2-
* Generated Zod Schemas for MCP SDK
2+
* Generated Zod Schemas and Types for MCP SDK
33
*
4-
* This module provides auto-generated Zod schemas from spec.types.ts,
5-
* post-processed for SDK compatibility.
4+
* This module provides:
5+
* - sdk.types.ts: Pre-processed types with SDK-compatible hierarchy
6+
* - sdk.schemas.ts: Zod schemas generated from sdk.types.ts
67
*
7-
* @see spec.types.ts - MCP specification types
8-
* @see spec.schemas.ts - Auto-generated Zod schemas
9-
* @see ../types.ts - Production schemas with SDK extras
8+
* Pre-processing transforms:
9+
* - `extends JSONRPCRequest` → `extends Request`
10+
* - `extends JSONRPCNotification` → `extends Notification`
11+
*
12+
* This allows SDK types/schemas to omit jsonrpc/id fields, which are
13+
* handled at the transport layer.
14+
*
15+
* @see sdk.types.ts - Pre-processed types (SDK hierarchy)
16+
* @see sdk.schemas.ts - Generated Zod schemas
17+
* @see ../types.ts - Re-exports from here + SDK extras
1018
*/
1119

1220
// =============================================================================
@@ -200,10 +208,10 @@ export {
200208
ServerRequestSchema,
201209
ServerNotificationSchema,
202210
ServerResultSchema
203-
} from './spec.schemas.js';
211+
} from './sdk.schemas.js';
204212

205213
// =============================================================================
206-
// Spec Types (interfaces and type aliases)
214+
// SDK-Compatible Types (pre-processed from spec.types.ts)
207215
// =============================================================================
208216

209217
export type {
@@ -393,7 +401,7 @@ export type {
393401
ServerRequest,
394402
ServerNotification,
395403
ServerResult
396-
} from '../spec.types.js';
404+
} from './sdk.types.js';
397405

398406
// =============================================================================
399407
// SDK Constants (from types.ts, not spec)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Run: npm run generate:schemas
44
import { z } from 'zod/v4';
55

6-
import * as spec from '../spec.types.js';
7-
import * as generated from './spec.schemas.js';
6+
import * as spec from './sdk.types.js';
7+
import * as generated from './sdk.schemas.js';
88

99
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1010
function expectType<T>(_: T) {

0 commit comments

Comments
 (0)