Skip to main content
Glama
codedrop-codes

Refactory

refactory_fix_imports

Fix broken require() paths in project files after module extraction. Scans consumers using pure path resolution; optionally preview changes with dry-run mode.

Instructions

Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
moduleDirYesDirectory containing extracted modules
projectDirNoProject root to scan for consumers
dryRunNoReport changes without writing (default: false)

Implementation Reference

  • Main handler for fixImports: scans broken require() paths in the module directory and in consumer files under projectDir, resolves them by finding the target file by basename, and rewrites require() paths. Supports dryRun mode.
    function fixImports({ moduleDir, projectDir, dryRun = false }) {
      const fixed = [];
      const errors = [];
      const modDirAbs = path.resolve(moduleDir);
      const projDirAbs = path.resolve(projectDir);
    
      // Phase 1: fix broken requires inside moduleDir
      const broken = scanBrokenRequires({ moduleDir: modDirAbs });
      for (const b of broken) {
        const target = findTarget(modDirAbs, b.require);
        if (!target) {
          errors.push(`No target found for "${b.require}" in ${b.file}:${b.line}`);
          continue;
        }
        const newPath = relativeRequirePath(b.file, target);
        if (newPath === b.require) continue;
        if (!dryRun) {
          let src = fs.readFileSync(b.file, "utf8");
          // Replace this specific require string (escape for regex)
          const escaped = b.require.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
          src = src.replace(
            new RegExp(`(require\\s*\\(\\s*['"])${escaped}(['"]\\s*\\))`, "g"),
            `$1${newPath}$2`
          );
          fs.writeFileSync(b.file, src, "utf8");
        }
        fixed.push({ file: b.file, old: b.require, new: newPath });
      }
    
      // Phase 2: scan project consumers outside moduleDir
      const projFiles = collectFiles(projDirAbs).filter(
        (f) => !f.startsWith(modDirAbs + path.sep)
      );
      for (const file of projFiles) {
        const src = fs.readFileSync(file, "utf8");
        let m;
        REQUIRE_RE.lastIndex = 0;
        while ((m = REQUIRE_RE.exec(src)) !== null) {
          const reqPath = m[2];
          if (!reqPath.startsWith(".")) continue;
          const resolved = resolveRequire(file, reqPath);
          if (resolved) continue; // already works
          // Try to find it in moduleDir
          const target = findTarget(modDirAbs, reqPath);
          if (!target) continue; // not a module-related require
          const newPath = relativeRequirePath(file, target);
          if (newPath === reqPath) continue;
          if (!dryRun) {
            let content = fs.readFileSync(file, "utf8");
            const escaped = reqPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
            content = content.replace(
              new RegExp(`(require\\s*\\(\\s*['"])${escaped}(['"]\\s*\\))`, "g"),
              `$1${newPath}$2`
            );
            fs.writeFileSync(file, content, "utf8");
          }
          fixed.push({ file, old: reqPath, new: newPath });
        }
      }
    
      return { fixed, errors };
    }
  • Input schema for refactory_fix_imports tool: accepts moduleDir (required), projectDir, and dryRun (boolean, default false).
    {
      name: "refactory_fix_imports",
      description: "Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.",
      inputSchema: {
        type: "object",
        properties: {
          moduleDir: { type: "string", description: "Directory containing extracted modules" },
          projectDir: { type: "string", description: "Project root to scan for consumers" },
          dryRun: { type: "boolean", description: "Report changes without writing (default: false)" },
        },
        required: ["moduleDir"],
      },
    },
  • src/server.js:153-165 (registration)
    Tool name 'refactory_fix_imports' is defined in the TOOLS array with description and inputSchema.
    {
      name: "refactory_fix_imports",
      description: "Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.",
      inputSchema: {
        type: "object",
        properties: {
          moduleDir: { type: "string", description: "Directory containing extracted modules" },
          projectDir: { type: "string", description: "Project root to scan for consumers" },
          dryRun: { type: "boolean", description: "Report changes without writing (default: false)" },
        },
        required: ["moduleDir"],
      },
    },
  • src/server.js:211-211 (registration)
    Case handler in the CallToolRequestSchema switch statement that dispatches 'refactory_fix_imports' to the fixImports() function.
    case "refactory_fix_imports": result = fixImports(args); break;
  • src/server.js:33-35 (registration)
    Import of fixImports from src/tools/fix-imports.js into the server.
    const { fixImports, scanBrokenRequires, generateReexport } = require("./tools/fix-imports");
    const { decompose } = require("./tools/decompose");
Behavior3/5

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

No annotations exist, so the description must carry the burden. It discloses it is mechanical and reports via dryRun, but does not specify that files are modified when not in dry-run mode or other side effects.

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

Conciseness5/5

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

The description is two concise sentences with no filler, front-loaded with purpose and supplemented with a distinguishing statement about no LLM needed.

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

Completeness3/5

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

No output schema, so description should explain outcomes. It states what the tool does but omits details like backup behavior, error handling, or whether it scans subdirectories. Adequate for a simple tool but could be improved.

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 coverage is 100% with parameter descriptions. The description adds context (after extraction, pure path resolution) but does not significantly augment the existing parameter documentation.

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 it fixes broken require() paths after module extraction, specifying it is mechanical (pure path resolution, no LLM). This differentiates it clearly from siblings like refactory_extract or refactory_analyze.

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

Usage Guidelines3/5

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

The description implies use after module extraction but does not explicitly state when to use vs. alternatives or provide exclusions. No when-not or alternative tool names are mentioned.

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/codedrop-codes/refactory'

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