|
| 1 | +import type { SourceFile } from 'ts-morph'; |
| 2 | +import { Node, SyntaxKind } from 'ts-morph'; |
| 3 | + |
| 4 | +import type { Diagnostic, Transform, TransformContext, TransformResult } from '../../../types.js'; |
| 5 | +import { renameAllReferences } from '../../../utils/astUtils.js'; |
| 6 | +import { warning } from '../../../utils/diagnostics.js'; |
| 7 | +import { addOrMergeImport, isAnyMcpSpecifier } from '../../../utils/importUtils.js'; |
| 8 | + |
| 9 | +const REMOVED_ZOD_HELPERS: Record<string, string> = { |
| 10 | + schemaToJson: |
| 11 | + "Removed in v2. Use `fromJsonSchema()` from @modelcontextprotocol/server for JSON Schema, or your schema library's native conversion.", |
| 12 | + parseSchemaAsync: "Removed in v2. Use your schema library's validation directly (e.g., Zod's `.safeParseAsync()`).", |
| 13 | + getSchemaShape: "Removed in v2. These Zod-specific introspection helpers have no v2 equivalent. Use your schema library's native API.", |
| 14 | + getSchemaDescription: |
| 15 | + "Removed in v2. These Zod-specific introspection helpers have no v2 equivalent. Use your schema library's native API.", |
| 16 | + isOptionalSchema: |
| 17 | + "Removed in v2. These Zod-specific introspection helpers have no v2 equivalent. Use your schema library's native API.", |
| 18 | + unwrapOptionalSchema: |
| 19 | + "Removed in v2. These Zod-specific introspection helpers have no v2 equivalent. Use your schema library's native API." |
| 20 | +}; |
| 21 | + |
| 22 | +export const removedApisTransform: Transform = { |
| 23 | + name: 'Removed API handling', |
| 24 | + id: 'removed-apis', |
| 25 | + apply(sourceFile: SourceFile, _context: TransformContext): TransformResult { |
| 26 | + const diagnostics: Diagnostic[] = []; |
| 27 | + let changesCount = 0; |
| 28 | + |
| 29 | + changesCount += handleRemovedZodHelpers(sourceFile, diagnostics); |
| 30 | + changesCount += handleIsomorphicHeaders(sourceFile, diagnostics); |
| 31 | + changesCount += handleStreamableHTTPError(sourceFile, diagnostics); |
| 32 | + |
| 33 | + return { changesCount, diagnostics }; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +function handleRemovedZodHelpers(sourceFile: SourceFile, diagnostics: Diagnostic[]): number { |
| 38 | + interface Removal { |
| 39 | + importName: string; |
| 40 | + message: string; |
| 41 | + line: number; |
| 42 | + } |
| 43 | + |
| 44 | + const removals: Removal[] = []; |
| 45 | + |
| 46 | + for (const imp of sourceFile.getImportDeclarations()) { |
| 47 | + if (!isAnyMcpSpecifier(imp.getModuleSpecifierValue())) continue; |
| 48 | + const line = imp.getStartLineNumber(); |
| 49 | + for (const namedImport of imp.getNamedImports()) { |
| 50 | + const name = namedImport.getName(); |
| 51 | + const message = REMOVED_ZOD_HELPERS[name]; |
| 52 | + if (message) { |
| 53 | + removals.push({ importName: name, message, line }); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + for (const removal of removals) { |
| 59 | + for (const imp of sourceFile.getImportDeclarations()) { |
| 60 | + for (const namedImport of imp.getNamedImports()) { |
| 61 | + if (namedImport.getName() === removal.importName) { |
| 62 | + namedImport.remove(); |
| 63 | + if (imp.getNamedImports().length === 0 && !imp.getDefaultImport() && !imp.getNamespaceImport()) { |
| 64 | + imp.remove(); |
| 65 | + } |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + diagnostics.push(warning(sourceFile.getFilePath(), removal.line, `${removal.importName}: ${removal.message}`)); |
| 71 | + } |
| 72 | + |
| 73 | + return removals.length; |
| 74 | +} |
| 75 | + |
| 76 | +function handleIsomorphicHeaders(sourceFile: SourceFile, diagnostics: Diagnostic[]): number { |
| 77 | + let changesCount = 0; |
| 78 | + let foundImport: ReturnType<ReturnType<SourceFile['getImportDeclarations']>[0]['getNamedImports']>[0] | undefined; |
| 79 | + let foundImportDecl: ReturnType<SourceFile['getImportDeclarations']>[0] | undefined; |
| 80 | + |
| 81 | + for (const imp of sourceFile.getImportDeclarations()) { |
| 82 | + if (!isAnyMcpSpecifier(imp.getModuleSpecifierValue())) continue; |
| 83 | + for (const namedImport of imp.getNamedImports()) { |
| 84 | + if (namedImport.getName() === 'IsomorphicHeaders') { |
| 85 | + foundImport = namedImport; |
| 86 | + foundImportDecl = imp; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + if (foundImport) break; |
| 91 | + } |
| 92 | + |
| 93 | + if (!foundImport || !foundImportDecl) return 0; |
| 94 | + |
| 95 | + const line = foundImportDecl.getStartLineNumber(); |
| 96 | + |
| 97 | + renameAllReferences(sourceFile, 'IsomorphicHeaders', 'Headers'); |
| 98 | + changesCount++; |
| 99 | + |
| 100 | + foundImport.remove(); |
| 101 | + if (foundImportDecl.getNamedImports().length === 0 && !foundImportDecl.getDefaultImport() && !foundImportDecl.getNamespaceImport()) { |
| 102 | + foundImportDecl.remove(); |
| 103 | + } |
| 104 | + changesCount++; |
| 105 | + |
| 106 | + diagnostics.push( |
| 107 | + warning( |
| 108 | + sourceFile.getFilePath(), |
| 109 | + line, |
| 110 | + 'IsomorphicHeaders replaced with standard Web Headers API. Note: Headers uses .get()/.set() methods, not bracket access.' |
| 111 | + ) |
| 112 | + ); |
| 113 | + |
| 114 | + return changesCount; |
| 115 | +} |
| 116 | + |
| 117 | +function handleStreamableHTTPError(sourceFile: SourceFile, diagnostics: Diagnostic[]): number { |
| 118 | + let changesCount = 0; |
| 119 | + let foundImport: ReturnType<ReturnType<SourceFile['getImportDeclarations']>[0]['getNamedImports']>[0] | undefined; |
| 120 | + let foundImportDecl: ReturnType<SourceFile['getImportDeclarations']>[0] | undefined; |
| 121 | + |
| 122 | + for (const imp of sourceFile.getImportDeclarations()) { |
| 123 | + if (!isAnyMcpSpecifier(imp.getModuleSpecifierValue())) continue; |
| 124 | + for (const namedImport of imp.getNamedImports()) { |
| 125 | + if (namedImport.getName() === 'StreamableHTTPError') { |
| 126 | + foundImport = namedImport; |
| 127 | + foundImportDecl = imp; |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + if (foundImport) break; |
| 132 | + } |
| 133 | + |
| 134 | + if (!foundImport || !foundImportDecl) return 0; |
| 135 | + |
| 136 | + const line = foundImportDecl.getStartLineNumber(); |
| 137 | + const moduleSpec = foundImportDecl.getModuleSpecifierValue(); |
| 138 | + |
| 139 | + for (const node of sourceFile.getDescendantsOfKind(SyntaxKind.NewExpression)) { |
| 140 | + const expr = node.getExpression(); |
| 141 | + if (!Node.isIdentifier(expr) || expr.getText() !== 'StreamableHTTPError') continue; |
| 142 | + diagnostics.push( |
| 143 | + warning( |
| 144 | + sourceFile.getFilePath(), |
| 145 | + node.getStartLineNumber(), |
| 146 | + 'new StreamableHTTPError(statusCode, statusText, body?) → new SdkError(code, message, data?). ' + |
| 147 | + 'Constructor arguments differ — manual review required. Map HTTP status to SdkErrorCode enum value.' |
| 148 | + ) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + renameAllReferences(sourceFile, 'StreamableHTTPError', 'SdkError'); |
| 153 | + changesCount++; |
| 154 | + |
| 155 | + foundImport.remove(); |
| 156 | + if (foundImportDecl.getNamedImports().length === 0 && !foundImportDecl.getDefaultImport() && !foundImportDecl.getNamespaceImport()) { |
| 157 | + foundImportDecl.remove(); |
| 158 | + } |
| 159 | + |
| 160 | + const targetModule = resolveTargetModule(sourceFile, moduleSpec); |
| 161 | + const insertIndex = sourceFile.getImportDeclarations().length; |
| 162 | + addOrMergeImport(sourceFile, targetModule, ['SdkError', 'SdkErrorCode'], false, insertIndex); |
| 163 | + changesCount++; |
| 164 | + |
| 165 | + diagnostics.push( |
| 166 | + warning( |
| 167 | + sourceFile.getFilePath(), |
| 168 | + line, |
| 169 | + 'StreamableHTTPError replaced with SdkError. Constructor arguments differ — manual review required. ' + |
| 170 | + 'HTTP status is now in error.data?.status.' |
| 171 | + ) |
| 172 | + ); |
| 173 | + |
| 174 | + return changesCount; |
| 175 | +} |
| 176 | + |
| 177 | +function resolveTargetModule(sourceFile: SourceFile, originalModule: string): string { |
| 178 | + const imp = sourceFile.getImportDeclarations().find(i => { |
| 179 | + const spec = i.getModuleSpecifierValue(); |
| 180 | + return spec === '@modelcontextprotocol/client' || spec === '@modelcontextprotocol/server'; |
| 181 | + }); |
| 182 | + if (imp) return imp.getModuleSpecifierValue(); |
| 183 | + |
| 184 | + if (originalModule.includes('/client')) return '@modelcontextprotocol/client'; |
| 185 | + return '@modelcontextprotocol/server'; |
| 186 | +} |
0 commit comments