Skip to main content
Glama
qckfx

Tree-Hugger-JS MCP Server

by qckfx

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

TableJSON Schema
NameRequiredDescriptionDefault
sourceYesFile path (./src/app.js) or code string ('const x = 1;')
isFilePathNoWhether source is a file path (true) or code string (false). Defaults to auto-detect.
languageNoLanguage to use (javascript, typescript, jsx, tsx). Auto-detected if not provided.

Implementation Reference

  • 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,
        };
      }
    }
  • 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"],
      },
    },
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It explains that the tool loads code into an 'AST state' and is a prerequisite for other tools, which adds useful context. However, it lacks details on error handling, performance implications, or what happens if called multiple times, leaving some behavioral aspects unclear.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and front-loaded with the core purpose and prerequisite note, followed by helpful examples. However, the examples are somewhat lengthy and could be more concise, as they repeat similar use cases, slightly reducing efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity as a prerequisite for analysis, no output schema, and no annotations, the description is mostly complete. It covers purpose, usage, and provides examples, but lacks details on return values or error handling, which would enhance completeness for a foundational tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description adds minimal value beyond the schema, as it doesn't explain parameter interactions or provide additional semantics. The examples illustrate usage but don't enhance parameter understanding beyond what's in the schema descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Parse JavaScript/TypeScript code from file or string and load it into the AST state.' It specifies the verb (parse), resource (JavaScript/TypeScript code), and distinguishes it from siblings by noting it 'must be called before using other analysis tools,' establishing its foundational role in the workflow.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit usage guidance: 'Must be called before using other analysis tools.' It distinguishes this tool from siblings by positioning it as a prerequisite for tools like get_functions or analyze_scopes, and the examples illustrate when to use it (e.g., for parsing files or strings, with explicit language settings).

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/qckfx/tree-hugger-js-mcp'

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