Skip to main content
Glama
qckfx

Tree-Hugger-JS MCP Server

by qckfx

transform_code

Apply multiple code transformations in a single operation to refactor JavaScript/TypeScript, including renaming, replacing patterns, inserting code, and removing unused imports.

Instructions

Apply multiple transformations in a single operation. Most powerful tool for complex refactoring workflows.

Examples: • API refactor: [{type: 'rename', parameters: {oldName: 'getData', newName: 'fetchData'}}, {type: 'removeUnusedImports'}] • Environment update: [{type: 'replaceIn', parameters: {nodeType: 'string', pattern: /localhost/g, replacement: 'api.production.com'}}, {type: 'removeUnusedImports'}] • Add logging: [{type: 'insertAfter', parameters: {pattern: 'function_declaration', text: 'console.log("Function called");'}}, {type: 'removeUnusedImports'}] • Bulk rename: [{type: 'rename', parameters: {oldName: 'user', newName: 'customer'}}, {type: 'rename', parameters: {oldName: 'id', newName: 'customerId'}}] • Legacy migration: [{type: 'replaceIn', parameters: {nodeType: 'call_expression', pattern: /XMLHttpRequest/g, replacement: 'fetch'}}, {type: 'removeUnusedImports'}]

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationsYesArray of transformation operations applied in sequence. Use preview:true first!
previewNoReturn preview only without applying changes (default: false). ALWAYS preview complex transformations first.

Implementation Reference

  • The main handler function for the 'transform_code' tool. It applies a sequence of transformation operations (rename, removeUnusedImports, replaceIn, insertBefore, insertAfter) to the current AST using tree-hugger-js transformer, optionally in preview mode, and updates the AST state if not preview.
    private async transformCode(args: { operations: TransformOperation[]; preview?: boolean }) {
      if (!this.currentAST) {
        return {
          content: [{
            type: "text",
            text: "No AST loaded. Please use parse_code first.",
          }],
          isError: true,
        };
      }
    
      try {
        let transformer = this.currentAST.tree.transform();
        
        for (const op of args.operations) {
          switch (op.type) {
            case "rename":
              if (!op.parameters.oldName || !op.parameters.newName) {
                throw new Error("Rename operation requires oldName and newName parameters");
              }
              transformer = transformer.rename(op.parameters.oldName, op.parameters.newName);
              break;
            case "removeUnusedImports":
              transformer = transformer.removeUnusedImports();
              break;
            case "replaceIn":
              if (!op.parameters.nodeType || !op.parameters.pattern || !op.parameters.replacement) {
                throw new Error("ReplaceIn operation requires nodeType, pattern, and replacement parameters");
              }
              transformer = transformer.replaceIn(op.parameters.nodeType, op.parameters.pattern, op.parameters.replacement);
              break;
            case "insertBefore":
              if (!op.parameters.pattern || !op.parameters.text) {
                throw new Error("InsertBefore operation requires pattern and text parameters");
              }
              transformer = transformer.insertBefore(String(op.parameters.pattern), op.parameters.text);
              break;
            case "insertAfter":
              if (!op.parameters.pattern || !op.parameters.text) {
                throw new Error("InsertAfter operation requires pattern and text parameters");
              }
              transformer = transformer.insertAfter(String(op.parameters.pattern), op.parameters.text);
              break;
            default:
              throw new Error(`Unknown operation type: ${op.type}`);
          }
        }
    
        const result = transformer.toString();
        
        if (!args.preview) {
          this.currentAST.sourceCode = result;
          this.currentAST.tree = parse(result);
          this.currentAST.timestamp = new Date();
        }
    
        const transformResult: TransformResult = {
          operation: "transform_code",
          parameters: {}, // TransformOperation parameters don't include operations array
          preview: result.slice(0, 500) + (result.length > 500 ? '...' : ''),
          timestamp: new Date(),
        };
        
        this.transformHistory.push(transformResult);
    
        return {
          content: [{
            type: "text",
            text: `${args.preview ? 'Preview: ' : ''}Applied ${args.operations.length} transformations\n\n${args.preview ? 'Preview:\n' : 'Result:\n'}${result}`,
          }],
        };
      } catch (error) {
        return {
          content: [{
            type: "text",
            text: `Error transforming code: ${error instanceof Error ? error.message : String(error)}`,
          }],
          isError: true,
        };
      }
    }
  • The input schema definition for the 'transform_code' tool, defining the expected parameters: operations array (with type and parameters) and optional preview boolean.
    {
      name: "transform_code",
      description: "Apply multiple transformations in a single operation. Most powerful tool for complex refactoring workflows.\n\nExamples:\n• API refactor: [{type: 'rename', parameters: {oldName: 'getData', newName: 'fetchData'}}, {type: 'removeUnusedImports'}]\n• Environment update: [{type: 'replaceIn', parameters: {nodeType: 'string', pattern: /localhost/g, replacement: 'api.production.com'}}, {type: 'removeUnusedImports'}]\n• Add logging: [{type: 'insertAfter', parameters: {pattern: 'function_declaration', text: 'console.log(\"Function called\");'}}, {type: 'removeUnusedImports'}]\n• Bulk rename: [{type: 'rename', parameters: {oldName: 'user', newName: 'customer'}}, {type: 'rename', parameters: {oldName: 'id', newName: 'customerId'}}]\n• Legacy migration: [{type: 'replaceIn', parameters: {nodeType: 'call_expression', pattern: /XMLHttpRequest/g, replacement: 'fetch'}}, {type: 'removeUnusedImports'}]",
      inputSchema: {
        type: "object",
        properties: {
          operations: {
            type: "array",
            items: {
              type: "object",
              properties: {
                type: {
                  type: "string",
                  enum: ["rename", "removeUnusedImports", "replaceIn", "insertBefore", "insertAfter"]
                },
                parameters: {
                  type: "object",
                  description: "Parameters: rename{oldName,newName}, replaceIn{nodeType,pattern,replacement}, insert{pattern,text}"
                }
              },
              required: ["type"]
            },
            description: "Array of transformation operations applied in sequence. Use preview:true first!"
          },
          preview: {
            type: "boolean",
            description: "Return preview only without applying changes (default: false). ALWAYS preview complex transformations first."
          }
        },
        required: ["operations"],
      },
    },
  • src/index.ts:437-438 (registration)
    The switch case registration in the CallToolRequestHandler that dispatches calls to the transformCode handler function.
    case "transform_code":
      return await this.transformCode(args as { operations: TransformOperation[]; preview?: boolean });
Behavior4/5

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

With no annotations provided, the description carries full burden and does well by explaining key behavioral traits: it handles multiple transformations in sequence (implied order matters), supports preview mode for safety, and includes concrete examples of destructive operations like renaming and replacing. However, it doesn't explicitly mention error handling, rollback capabilities, or performance implications.

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 with a clear purpose statement followed by practical examples, making it front-loaded and informative. However, it could be more concise by integrating the 'preview' advice into the main text rather than relying on the schema, and the examples are lengthy but necessary for clarity.

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 (batch transformations, no output schema, no annotations), the description is mostly complete: it explains purpose, usage, and provides examples. Gaps include lack of output format details, error handling, and explicit prerequisites (e.g., code must be parsed first). The examples compensate well for the missing output schema.

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

Parameters4/5

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

Schema description coverage is 100%, so the baseline is 3. The description adds significant value by providing five detailed examples that illustrate how to structure the 'operations' array with different 'type' and 'parameters' combinations, clarifying semantics beyond the schema's enum and descriptions. It doesn't explain the 'preview' parameter beyond what the schema states.

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: 'Apply multiple transformations in a single operation' with the specific context of 'complex refactoring workflows.' It distinguishes itself from sibling tools like rename_identifier and remove_unused_imports by emphasizing batch processing capabilities rather than single operations.

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 guidance on when to use this tool: for 'complex refactoring workflows' and 'most powerful tool for complex refactoring.' It implicitly suggests alternatives through sibling tool names (e.g., use rename_identifier for single renames), and the input schema reinforces this with 'Use preview:true first!' for safety.

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