validate_code
Check code syntax and formatting using linters like Prettier to identify errors before finalizing your code.
Instructions
Validates code syntax and formatting using available linters (e.g., Prettier). usage: providing code snippets to check for correctness before finalizing.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The code content to validate. | |
| language | Yes | The programming language (e.g., 'typescript', 'python', 'json'). |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"code": {
"description": "The code content to validate.",
"type": "string"
},
"language": {
"description": "The programming language (e.g., 'typescript', 'python', 'json').",
"type": "string"
}
},
"required": [
"code",
"language"
],
"type": "object"
}
Implementation Reference
- src/tools/validator.ts:19-69 (handler)The core handler function that executes the validate_code tool logic: writes code to temp file, runs Prettier check for JS/TS/etc., Python py_compile for Python, or warns for unsupported languages.export async function validateCodeHandler(args: any) { const { code, language } = args; // Create a temporary file to run validation against const tmpDir = os.tmpdir(); const fileName = `validate_${Date.now()}.${getExt(language)}`; const filePath = path.join(tmpDir, fileName); try { await fs.writeFile(filePath, code); // Default to Prettier for supported languages if (["javascript", "typescript", "json", "css", "html", "markdown", "yaml"].includes(language)) { try { // Check syntax and formatting await execAsync(`npx prettier --check "${filePath}"`); // If checking passed, it's valid code (mostly) return { content: [{ type: "text", text: "✅ Code is valid and formatted correctly." }] }; } catch (error: any) { // Prettier error output usually contains the syntax error details return { content: [{ type: "text", text: `❌ Validation Failed:\n${error.stdout || error.stderr || error.message}` }] }; } } // Fallback for other languages (mock validation for now, or add specific compilers like `python -m py_compile`) if (language === "python") { try { await execAsync(`python -m py_compile "${filePath}"`); return { content: [{ type: "text", text: "✅ Python syntax is valid." }] }; } catch (error: any) { return { content: [{ type: "text", text: `❌ Python Syntax Error:\n${error.stderr}` }] }; } } return { content: [{ type: "text", text: `⚠️ No specific validator configured for ${language}, but code was received.` }] }; } catch (err: any) { return { content: [{ type: "text", text: `System Error during validation: ${err.message}` }] }; } finally { // Cleanup try { await fs.unlink(filePath); } catch { } } }
- src/tools/validator.ts:10-17 (schema)Zod schema definition for the validate_code tool, specifying input parameters: code (string) and language (string).export const validateCodeSchema = { name: "validate_code", description: "Validates code syntax and formatting using available linters (e.g., Prettier). usage: providing code snippets to check for correctness before finalizing.", inputSchema: z.object({ code: z.string().describe("The code content to validate."), language: z.string().describe("The programming language (e.g., 'typescript', 'python', 'json')."), }) };
- src/index.ts:94-94 (registration)Registration of the validate_code tool in the main toolRegistry Map used by the stdio MCP server.["validate_code", { schema: validateCodeSchema, handler: validateCodeHandler }],
- src/server.ts:102-102 (registration)Registration of the validate_code tool in the toolRegistry Map used by the HTTP MCP server.["validate_code", { schema: validateCodeSchema, handler: validateCodeHandler }],
- src/tools/validator.ts:71-77 (helper)Helper function to determine file extension based on programming language, used for creating temporary files for validation.function getExt(lang: string): string { const map: Record<string, string> = { typescript: "ts", javascript: "js", python: "py", rust: "rs", go: "go", html: "html", css: "css", json: "json" }; return map[lang] || "txt"; }