parse_code
Parse JavaScript or TypeScript code from files or strings into an AST structure for subsequent code analysis and transformation tasks.
Instructions
Parse JavaScript/TypeScript code from file or string and load it into the AST state. Must be called before using other analysis tools.
Examples: • Parse a React component: parse_code('./src/UserProfile.jsx') • Parse code string: parse_code('function hello() { return "world"; }') • Parse with explicit language: parse_code('./config.js', language='javascript') • Analyze legacy code: parse_code('./old-script.js') then use other tools to understand structure • Code review prep: parse_code('./feature.ts') then get_functions() to review all functions
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source | Yes | File path (./src/app.js) or code string ('const x = 1;') | |
| isFilePath | No | Whether source is a file path (true) or code string (false). Defaults to auto-detect. | |
| language | No | Language to use (javascript, typescript, jsx, tsx). Auto-detected if not provided. |
Implementation Reference
- src/index.ts:455-516 (handler)The main execution logic for the parse_code tool. Parses JavaScript/TypeScript code from a file path or string using tree-hugger-js, detects language, handles errors, stores the AST in state, and returns parsing summary.private async parseCode(args: { source: string; isFilePath?: boolean; language?: string }) { try { let sourceCode: string; let filePath: string | undefined; let isFile: boolean = args.isFilePath ?? false; // Auto-detect if it's a file path if (args.isFilePath === undefined) { isFile = !args.source.includes('\n') && !args.source.includes(';') && args.source.length < 200; } let tree: TreeHugger; if (isFile) { const resolvedPath = resolve(args.source); if (!existsSync(resolvedPath)) { return { content: [{ type: "text", text: `File not found: ${resolvedPath}`, }], isError: true, }; } // Let tree-hugger handle file reading and language detection tree = parse(resolvedPath, { language: args.language }); sourceCode = readFileSync(resolvedPath, 'utf-8'); filePath = resolvedPath; } else { sourceCode = args.source; tree = parse(sourceCode, { language: args.language }); } this.currentAST = { tree, filePath, sourceCode, language: args.language || 'auto-detected', timestamp: new Date(), }; return { content: [{ type: "text", text: `Successfully parsed ${filePath || 'code string'}\n` + `Language: ${this.currentAST.language}\n` + `Lines: ${sourceCode.split('\n').length}\n` + `Characters: ${sourceCode.length}\n` + `Parse errors: ${tree.root.hasError ? 'Yes' : 'No'}\n` + `Root node type: ${tree.root.type}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error parsing code: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } }
- src/index.ts:183-200 (schema)Input schema defining parameters for the parse_code tool: source (required string), isFilePath (optional boolean), language (optional string).inputSchema: { type: "object", properties: { source: { type: "string", description: "File path (./src/app.js) or code string ('const x = 1;')" }, isFilePath: { type: "boolean", description: "Whether source is a file path (true) or code string (false). Defaults to auto-detect." }, language: { type: "string", description: "Language to use (javascript, typescript, jsx, tsx). Auto-detected if not provided." } }, required: ["source"], },
- src/index.ts:180-201 (registration)Registration of the parse_code tool in the tools list returned by ListToolsRequestSchema handler, including name, description, and inputSchema.{ name: "parse_code", description: "Parse JavaScript/TypeScript code from file or string and load it into the AST state. Must be called before using other analysis tools.\n\nExamples:\n• Parse a React component: parse_code('./src/UserProfile.jsx')\n• Parse code string: parse_code('function hello() { return \"world\"; }')\n• Parse with explicit language: parse_code('./config.js', language='javascript')\n• Analyze legacy code: parse_code('./old-script.js') then use other tools to understand structure\n• Code review prep: parse_code('./feature.ts') then get_functions() to review all functions", inputSchema: { type: "object", properties: { source: { type: "string", description: "File path (./src/app.js) or code string ('const x = 1;')" }, isFilePath: { type: "boolean", description: "Whether source is a file path (true) or code string (false). Defaults to auto-detect." }, language: { type: "string", description: "Language to use (javascript, typescript, jsx, tsx). Auto-detected if not provided." } }, required: ["source"], }, },