Skip to main content
Glama
blakeyoder

TypeScript Definitions MCP Server

by blakeyoder

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesThe TypeScript code to validate
expectedTypeNoOptional expected type to validate against

Implementation Reference

  • 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 }; }
  • 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) } ] }; }
  • 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"] } },
  • 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 }; }
  • 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); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/blakeyoder/typescript-definitions-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server