Skip to content

Commit f563440

Browse files
codemod iterations round 1 (modelcontextprotocol#2274)
1 parent 1b53a41 commit f563440

4 files changed

Lines changed: 96 additions & 32 deletions

File tree

packages/codemod/src/bin/batchTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const LOCAL_PACKAGE_DIRS: Record<string, string> = {
8787
'@modelcontextprotocol/client': path.join(SDK_ROOT, 'packages/client'),
8888
'@modelcontextprotocol/core': path.join(SDK_ROOT, 'packages/core'),
8989
'@modelcontextprotocol/server': path.join(SDK_ROOT, 'packages/server'),
90+
'@modelcontextprotocol/server-legacy': path.join(SDK_ROOT, 'packages/server-legacy'),
9091
'@modelcontextprotocol/express': path.join(SDK_ROOT, 'packages/middleware/express'),
9192
'@modelcontextprotocol/fastify': path.join(SDK_ROOT, 'packages/middleware/fastify'),
9293
'@modelcontextprotocol/hono': path.join(SDK_ROOT, 'packages/middleware/hono'),

packages/codemod/src/migrations/v1-to-v2/mappings/importMap.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ export const IMPORT_MAP: Record<string, ImportMapping> = {
6262
StreamableHTTPServerTransport: 'NodeStreamableHTTPServerTransport'
6363
},
6464
symbolTargetOverrides: {
65-
StreamableHTTPServerTransport: '@modelcontextprotocol/node'
65+
StreamableHTTPServerTransport: '@modelcontextprotocol/node',
66+
// The companion options type moved with the transport. @modelcontextprotocol/node
67+
// re-exports it under the same name (a backward-compat alias for
68+
// WebStandardStreamableHTTPServerTransportOptions), so route it there without renaming.
69+
StreamableHTTPServerTransportOptions: '@modelcontextprotocol/node'
6670
}
6771
},
6872
'@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js': {

packages/codemod/src/migrations/v1-to-v2/transforms/specSchemaAccess.ts

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,15 @@ function handleReference(
101101
return rewriteCapturedSafeParse(safeParseCall, localName, typeName, sourceFile, diagnostics);
102102
}
103103

104-
diagnostics.push(
105-
actionRequired(
106-
sourceFile.getFilePath(),
107-
ref,
108-
`${localName}.safeParse() not available in v2. Use \`isSpecType.${typeName}(value)\` for boolean validation, ` +
109-
`or \`specTypeSchemas.${typeName}['~standard'].validate(value)\` for full result.`
110-
)
111-
);
112-
return false;
104+
return rewriteUnsupportedSchemaCall(ref, safeParseCall, localName, typeName, 'safeParse', sourceFile, diagnostics);
113105
}
114106

115-
// Pattern: XSchema.parse(v) — diagnostic only
107+
// Pattern: XSchema.parse(v) — rewrite to the StandardSchema validate() primitive (or, when the
108+
// result is used, swap the identifier) so we never leave behind an import of a non-exported schema.
116109
if (isParsePattern(ref)) {
117-
diagnostics.push(
118-
actionRequired(
119-
sourceFile.getFilePath(),
120-
ref,
121-
`${localName}.parse() not available in v2. Use \`isSpecType.${typeName}(value)\` for validation, ` +
122-
`or \`specTypeSchemas.${typeName}['~standard'].validate(value)\` and check for issues.`
123-
)
124-
);
125-
return false;
110+
const parseAccess = ref.getParent() as import('ts-morph').PropertyAccessExpression;
111+
const parseCall = parseAccess.getParent() as import('ts-morph').CallExpression;
112+
return rewriteUnsupportedSchemaCall(ref, parseCall, localName, typeName, 'parse', sourceFile, diagnostics);
126113
}
127114

128115
// Pattern: XSchema used as value (function arg, assignment, etc.)
@@ -327,6 +314,68 @@ function rewriteCapturedSafeParse(
327314
return true;
328315
}
329316

317+
/**
318+
* Handles spec-schema usages that have no behavior-preserving v2 equivalent: the Zod-only
319+
* methods `.parse()` and (uncaptured) `.safeParse()`. In v2 these schemas are StandardSchemaV1
320+
* values that are NOT named public exports, so leaving the original import in place produces an
321+
* unresolved-import error (e.g. `PromptSchema` is not exported by `@modelcontextprotocol/server`).
322+
*
323+
* - Result discarded (validation for side-effect only): rewrite `XSchema.parse(v)` →
324+
* `specTypeSchemas.T['~standard'].validate(v)` so the code compiles. NOTE: `validate()` does not
325+
* throw, so `.parse()`'s throw-on-invalid behavior is lost — flagged via an actionRequired comment.
326+
* - Result used: swap only the identifier to `specTypeSchemas.T` so the import resolves; the
327+
* `.parse()`/`.safeParse()` call and its result shape still need a manual fix (flagged).
328+
*
329+
* Either way the original (now non-exported) schema import is dropped by the caller's
330+
* removeUnusedImport, so no dangling import survives.
331+
*/
332+
function rewriteUnsupportedSchemaCall(
333+
ref: import('ts-morph').Node,
334+
callNode: import('ts-morph').CallExpression,
335+
localName: string,
336+
typeName: string,
337+
method: 'parse' | 'safeParse',
338+
sourceFile: SourceFile,
339+
diagnostics: Diagnostic[]
340+
): boolean {
341+
const resultDiscarded = Node.isExpressionStatement(callNode.getParent());
342+
343+
if (resultDiscarded) {
344+
const argText = callNode
345+
.getArguments()
346+
.map(a => a.getText())
347+
.join(', ');
348+
const semantics =
349+
method === 'parse'
350+
? 'validate() does NOT throw on invalid input (parse() did) — if you relied on that, add `if (result.issues) throw …`.'
351+
: 'the result shape changed from { success, data, error } to { value, issues }.';
352+
diagnostics.push(
353+
actionRequired(
354+
sourceFile.getFilePath(),
355+
callNode,
356+
`Rewrote ${localName}.${method}() to specTypeSchemas.${typeName}['~standard'].validate(): ` +
357+
`v2 spec schemas are StandardSchemaV1, not Zod. Note: ${semantics}`
358+
)
359+
);
360+
callNode.replaceWithText(`specTypeSchemas.${typeName}['~standard'].validate(${argText})`);
361+
ensureImport(sourceFile, 'specTypeSchemas');
362+
return true;
363+
}
364+
365+
diagnostics.push(
366+
actionRequired(
367+
sourceFile.getFilePath(),
368+
ref,
369+
`${localName}.${method}() is not available on v2 spec schemas (StandardSchemaV1, not Zod). ` +
370+
`Replaced ${localName} with specTypeSchemas.${typeName}; rewrite the .${method}(...) call using ` +
371+
`specTypeSchemas.${typeName}['~standard'].validate(...) (returns { value, issues }, does not throw).`
372+
)
373+
);
374+
ref.replaceWithText(`specTypeSchemas.${typeName}`);
375+
ensureImport(sourceFile, 'specTypeSchemas');
376+
return true;
377+
}
378+
330379
function ensureImport(sourceFile: SourceFile, symbol: string): void {
331380
const existingImport = sourceFile.getImportDeclarations().find(imp => {
332381
if (!isAnyMcpSpecifier(imp.getModuleSpecifierValue())) return false;

packages/codemod/test/v1-to-v2/transforms/specSchemaAccess.test.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ describe('spec-schema-access transform', () => {
210210
expect(text).not.toContain('return result.value');
211211
});
212212

213-
it('falls back to diagnostic for non-captured safeParse (bare expression)', () => {
213+
it('rewrites non-captured safeParse (bare expression) to validate()', () => {
214214
const input = [`import { ToolSchema } from '@modelcontextprotocol/server';`, `ToolSchema.safeParse(data);`, ''].join('\n');
215-
const { result } = applyTransform(input);
216-
expect(result.changesCount).toBe(0);
215+
const { text, result } = applyTransform(input);
216+
expect(text).toContain("specTypeSchemas.Tool['~standard'].validate(data)");
217+
expect(text).not.toMatch(/import\s*\{[^}]*ToolSchema[^}]*\}/);
218+
expect(result.changesCount).toBeGreaterThan(0);
217219
expect(result.diagnostics.length).toBe(1);
218220
});
219221
});
@@ -327,16 +329,24 @@ describe('spec-schema-access transform', () => {
327329
});
328330
});
329331

330-
describe('diagnostic only: .parse(v)', () => {
331-
it('emits diagnostic for parse usage', () => {
332+
describe('.parse(v)', () => {
333+
it('rewrites discarded parse() to the validate() primitive', () => {
334+
const input = [`import { ToolSchema } from '@modelcontextprotocol/server';`, `ToolSchema.parse(raw);`, ''].join('\n');
335+
const { text, result } = applyTransform(input);
336+
expect(text).toContain("specTypeSchemas.Tool['~standard'].validate(raw)");
337+
expect(text).not.toMatch(/import\s*\{[^}]*ToolSchema[^}]*\}/);
338+
expect(result.changesCount).toBeGreaterThan(0);
339+
});
340+
341+
it('swaps the identifier (import stays resolvable) when the parse() result is used', () => {
332342
const input = [`import { ToolSchema } from '@modelcontextprotocol/server';`, `const tool = ToolSchema.parse(raw);`, ''].join(
333343
'\n'
334344
);
335345
const { text, result } = applyTransform(input);
336-
expect(text).toContain('ToolSchema.parse');
337-
expect(result.changesCount).toBe(0);
338-
expect(result.diagnostics.length).toBe(1);
339-
expect(result.diagnostics[0]!.message).toContain('isSpecType.Tool');
346+
expect(text).toContain('specTypeSchemas.Tool.parse(raw)');
347+
expect(text).not.toMatch(/import\s*\{[^}]*ToolSchema[^}]*\}/);
348+
expect(result.changesCount).toBeGreaterThan(0);
349+
expect(result.diagnostics[0]!.message).toContain('specTypeSchemas.Tool');
340350
});
341351
});
342352

@@ -392,7 +402,7 @@ describe('spec-schema-access transform', () => {
392402
expect(text).not.toMatch(/import\s*\{[^}]*CallToolRequestSchema[^}]*\}/);
393403
});
394404

395-
it('keeps original schema import when some refs are diagnostic-only', () => {
405+
it('removes the schema import even when a ref falls back to a parse()/safeParse() rewrite', () => {
396406
const input = [
397407
`import { CallToolRequestSchema } from '@modelcontextprotocol/server';`,
398408
`const valid = CallToolRequestSchema.safeParse(data).success;`,
@@ -401,8 +411,8 @@ describe('spec-schema-access transform', () => {
401411
].join('\n');
402412
const { text } = applyTransform(input);
403413
expect(text).toContain('isSpecType.CallToolRequest(data)');
404-
expect(text).toContain('CallToolRequestSchema.parse');
405-
expect(text).toMatch(/import\s*\{[^}]*CallToolRequestSchema[^}]*\}/);
414+
expect(text).toContain('specTypeSchemas.CallToolRequest.parse(data)');
415+
expect(text).not.toMatch(/import\s*\{[^}]*CallToolRequestSchema[^}]*\}/);
406416
});
407417

408418
it('removes schema specifier from import that also has other symbols', () => {

0 commit comments

Comments
 (0)