validate_type_usage
Check TypeScript code for type errors and validate against expected types to ensure type safety in your project.
Instructions
Validate TypeScript code for type correctness
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "validate_type_usage"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The TypeScript code to validate | |
| expectedType | No | Optional expected type to validate against |
Implementation Reference
- src/type-validator.ts:11-79 (handler)Core implementation of the validate_type_usage tool logic using TypeScript Compiler API to create a temporary program, check syntactic/semantic diagnostics, and validate against an optional expected type.
validateTypeUsage(code: string, expectedType?: string): ValidationResult { const errors: ValidationError[] = []; const warnings: ValidationWarning[] = []; try { // Create a temporary source file for validation const sourceFile = ts.createSourceFile( "temp.ts", code, ts.ScriptTarget.Latest, true ); // Create a program with just this file const defaultCompilerHost = ts.createCompilerHost(this.compilerOptions); const compilerHost = { ...defaultCompilerHost, getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => { if (fileName === "temp.ts") { return sourceFile; } return defaultCompilerHost.getSourceFile(fileName, languageVersion); } }; const program = ts.createProgram(["temp.ts"], this.compilerOptions, compilerHost); // Get semantic diagnostics const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile); const syntacticDiagnostics = program.getSyntacticDiagnostics(sourceFile); // Convert diagnostics to our format for (const diagnostic of [...syntacticDiagnostics, ...semanticDiagnostics]) { const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); const position = diagnostic.start ? sourceFile.getLineAndCharacterOfPosition(diagnostic.start) : undefined; const errorInfo = { message, line: position ? position.line + 1 : undefined, column: position ? position.character + 1 : undefined, code: diagnostic.code?.toString() }; if (diagnostic.category === ts.DiagnosticCategory.Error) { errors.push(errorInfo); } else if (diagnostic.category === ts.DiagnosticCategory.Warning) { warnings.push(errorInfo); } } // If expected type is provided, validate against it if (expectedType && errors.length === 0) { const typeChecker = program.getTypeChecker(); this.validateExpectedType(sourceFile, typeChecker, expectedType, errors, warnings); } } catch (error) { errors.push({ message: `Validation failed: ${error instanceof Error ? error.message : String(error)}`, code: "VALIDATION_ERROR" }); } return { valid: errors.length === 0, errors, warnings }; } - src/mcp-server.ts:292-303 (handler)MCP server handler method for validate_type_usage tool that delegates to TypeValidator and formats the result as MCP response.
private async handleValidateTypeUsage(code: string, expectedType?: string) { const result = this.typeValidator.validateTypeUsage(code, expectedType); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } - src/mcp-server.ts:95-111 (registration)Tool registration in the ListTools response, defining name, description, and input schema.
name: "validate_type_usage", description: "Validate TypeScript code for type correctness", inputSchema: { type: "object", properties: { code: { type: "string", description: "The TypeScript code to validate" }, expectedType: { type: "string", description: "Optional expected type to validate against" } }, required: ["code"] } }, - src/mcp-server.ts:15-27 (schema)TypeScript interface defining the expected arguments for the validate_type_usage tool, used for internal type safety.
interface ToolArguments { lookup_type: { typeName: string; packageName?: string }; validate_type_usage: { code: string; expectedType?: string }; find_interfaces: { pattern: string }; get_package_types: { packageName: string }; validate_interface_implementation: { implementation: string; interfaceName: string; interfaceDefinition: string; }; check_type_compatibility: { sourceType: string; targetType: string }; reinitialize_indexer: { workingDir?: string }; } - src/mcp-server.ts:217-220 (registration)Tool dispatch registration in the CallToolRequest handler switch statement.
case "validate_type_usage": { const validateArgs = this.validateArgs<ToolArguments["validate_type_usage"]>(args); return await this.handleValidateTypeUsage(validateArgs.code, validateArgs.expectedType); }