parse_code
Parse JavaScript/TypeScript code from files or strings into an AST for analysis. Prepares code for review, legacy code understanding, or further processing with other tools.
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 |
|---|---|---|---|
| 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. | |
| source | Yes | File path (./src/app.js) or code string ('const x = 1;') |
Input Schema (JSON Schema)
Implementation Reference
- src/index.ts:455-516 (handler)The handler function that implements the core logic of the parse_code tool. It parses JavaScript/TypeScript code from a file path or string input using the tree-hugger-js library, detects language if not specified, handles file reading, stores the AST and source code in the server's state (this.currentAST), and returns parsing success information or errors.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:180-201 (schema)The tool schema definition returned by ListToolsRequestSchema, including the name 'parse_code', detailed description with usage examples, and inputSchema specifying the required 'source' parameter and optional 'isFilePath' and 'language' parameters with their types and descriptions.{ 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"], }, },
- src/index.ts:413-414 (registration)The switch case in the CallToolRequestSchema request handler that registers and dispatches calls to the 'parse_code' tool by invoking the parseCode method with typed arguments.case "parse_code": return await this.parseCode(args as { source: string; isFilePath?: boolean; language?: string });