Skip to main content
Glama

fix

Automatically fix code issues including linting errors, TypeScript problems, formatting inconsistencies, import organization, and unused code to improve code quality.

Instructions

【自动修复】自动修复代码问题(Lint、TypeScript、格式化等)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeNo需要修复的代码
typeNo修复类型:lint, type, format, import, unused, all(默认 all)

Implementation Reference

  • The primary handler function for the 'fix' tool. It constructs an extensive prompt guiding the AI to fix code issues including lint errors, TypeScript types, formatting, imports, and unused code, then returns it as a text content response.
    export async function fix(args: any) {
      try {
        const code = args?.code || "";
        const type = args?.type || "all"; // lint, type, format, import, unused
    
        const message = `请自动修复以下代码问题:
    
    📝 **代码内容**:
    ${code || "请提供需要修复的代码"}
    
    🎯 **修复类型**:${type}
    
    ---
    
    ## 自动修复步骤
    
    ### 第一步:识别问题
    
    执行以下检查:
    \`\`\`bash
    # Lint 检查
    npm run lint
    
    # TypeScript 类型检查
    tsc --noEmit
    
    # 格式化检查
    npm run format:check
    \`\`\`
    
    ### 第二步:问题分类
    
    **1️⃣ Lint 错误**
    - ESLint 规则违反
    - 代码质量问题
    - 潜在 Bug
    
    **2️⃣ TypeScript 类型错误**
    - 类型不匹配
    - 缺少类型定义
    - 隐式 any
    
    **3️⃣ 格式化问题**
    - 缩进不一致
    - 引号风格
    - 分号使用
    - 换行规则
    
    **4️⃣ Import 问题**
    - 未使用的 import
    - 重复 import
    - Import 顺序混乱
    - 相对路径 vs 绝对路径
    
    **5️⃣ 未使用代码**
    - 未使用的变量
    - 未使用的函数
    - 死代码(Dead Code)
    
    ---
    
    ## 修复策略
    
    ### 🔧 Lint 错误修复
    
    **常见问题和修复:**
    
    1. **no-unused-vars**
    \`\`\`typescript
    // ❌ Before
    const unusedVar = 123;
    function test() {
      const result = compute();
      return 42;
    }
    
    // ✅ After
    function test() {
      return 42;
    }
    \`\`\`
    
    2. **no-console**
    \`\`\`typescript
    // ❌ Before
    console.log('debug info');
    
    // ✅ After (开发环境)
    if (process.env.NODE_ENV === 'development') {
      console.log('debug info');
    }
    
    // ✅ After (使用 logger)
    logger.debug('debug info');
    \`\`\`
    
    3. **prefer-const**
    \`\`\`typescript
    // ❌ Before
    let value = 10;
    const result = value * 2;
    
    // ✅ After
    const value = 10;
    const result = value * 2;
    \`\`\`
    
    ### 🔧 TypeScript 类型错误修复
    
    **常见问题和修复:**
    
    1. **隐式 any**
    \`\`\`typescript
    // ❌ Before
    function process(data) {
      return data.value;
    }
    
    // ✅ After
    function process(data: { value: string }): string {
      return data.value;
    }
    \`\`\`
    
    2. **类型不匹配**
    \`\`\`typescript
    // ❌ Before
    const num: number = "123";
    
    // ✅ After
    const num: number = 123;
    // 或
    const num: number = parseInt("123");
    \`\`\`
    
    3. **可能为 null/undefined**
    \`\`\`typescript
    // ❌ Before
    function getName(user) {
      return user.name.toUpperCase();
    }
    
    // ✅ After
    function getName(user: User | null): string {
      return user?.name?.toUpperCase() ?? 'Unknown';
    }
    \`\`\`
    
    ### 🔧 Import 优化
    
    **修复策略:**
    
    \`\`\`typescript
    // ❌ Before
    import { useState, useEffect, useMemo } from 'react';
    import { Button } from './components/Button';
    import React from 'react';
    import { formatDate } from '../utils/date';
    import { api } from '../../services/api';
    
    // ✅ After
    // 外部依赖
    import React, { useEffect, useMemo, useState } from 'react';
    
    // 内部模块(按层级从远到近)
    import { api } from '../../services/api';
    import { formatDate } from '../utils/date';
    import { Button } from './components/Button';
    \`\`\`
    
    ### 🔧 格式化修复
    
    **自动格式化:**
    \`\`\`bash
    # Prettier
    npm run format
    
    # ESLint 自动修复
    npm run lint:fix
    \`\`\`
    
    ---
    
    ## 批量修复命令
    
    **一键修复所有可自动修复的问题:**
    \`\`\`bash
    # 1. 格式化代码
    npm run format
    
    # 2. ESLint 自动修复
    npm run lint:fix
    
    # 3. 整理 import
    npx organize-imports-cli 'src/**/*.ts'
    
    # 4. 移除未使用的 import
    npx ts-unused-exports tsconfig.json --deleteUnusedFile
    
    # 5. TypeScript 类型检查
    tsc --noEmit
    \`\`\`
    
    ---
    
    ## 修复报告
    
    ### 📊 问题统计
    - Lint 错误: X 个
    - 类型错误: Y 个
    - 格式问题: Z 个
    - Import 问题: W 个
    
    ### ✅ 已自动修复
    1. [文件:行号] 问题描述 → 已修复
    2. [文件:行号] 问题描述 → 已修复
    
    ### ⚠️ 需要手动处理
    1. [文件:行号] 问题描述 → 修复建议
    2. [文件:行号] 问题描述 → 修复建议
    
    ### 📝 修复后的代码
    \`\`\`typescript
    // 完整的修复后代码
    \`\`\`
    
    ---
    
    ## 预防措施
    
    **配置自动修复:**
    
    \`\`\`.vscode/settings.json
    {
      "editor.formatOnSave": true,
      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true,
        "source.organizeImports": true
      },
      "eslint.validate": [
        "javascript",
        "typescript",
        "javascriptreact",
        "typescriptreact"
      ]
    }
    \`\`\`
    
    **Git Hooks(Husky):**
    \`\`\`json
    {
      "husky": {
        "hooks": {
          "pre-commit": "lint-staged"
        }
      },
      "lint-staged": {
        "*.{js,ts,tsx}": [
          "eslint --fix",
          "prettier --write"
        ]
      }
    }
    \`\`\`
    
    ---
    
    现在请开始分析代码问题并自动修复。`;
    
        return {
          content: [
            {
              type: "text",
              text: message,
            },
          ],
        };
      } catch (error) {
        const errorMessage =
          error instanceof Error ? error.message : String(error);
        return {
          content: [
            {
              type: "text",
              text: `❌ 自动修复失败: ${errorMessage}`,
            },
          ],
          isError: true,
        };
      }
    }
  • The input schema definition for the 'fix' tool registered in ListToolsRequestHandler, defining optional parameters 'code' and 'type'.
      name: "fix",
      description: "【自动修复】自动修复代码问题(Lint、TypeScript、格式化等)",
      inputSchema: {
        type: "object",
        properties: {
          code: {
            type: "string",
            description: "需要修复的代码",
          },
          type: {
            type: "string",
            description: "修复类型:lint, type, format, import, unused, all(默认 all)",
          },
        },
        required: [],
      },
    },
  • src/index.ts:501-502 (registration)
    Registration in the CallToolRequestSchema handler's switch statement, dispatching calls to the fix function.
    case "fix":
      return await fix(args);
  • Re-export of the fix tool function from its implementation file, making it available for import in src/index.ts.
    export { fix } from "./fix.js";
Behavior2/5

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

No annotations are provided, so the description carries full burden. While '自动修复' (automatic fix) implies mutation, it doesn't disclose important behavioral traits: whether changes are destructive or reversible, what permissions are needed, how errors are handled, or what the output looks like. The description is too brief to provide adequate transparency for a mutation tool.

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 extremely concise - just one sentence in Chinese that efficiently communicates the core functionality. Every word earns its place: '自动修复' establishes the action, '代码问题' specifies the target, and the parenthetical provides concrete examples. No wasted words or unnecessary elaboration.

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

Completeness2/5

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

For a mutation tool with no annotations and no output schema, the description is insufficient. It doesn't explain what the tool returns (fixed code? success status? error messages?), doesn't mention side effects, and provides minimal context about the fixing process. The 100% schema coverage helps with parameters, but overall completeness is inadequate for a tool that modifies code.

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%, providing good documentation for both parameters. The description adds marginal value by listing examples of fix types ('Lint、TypeScript、格式化等') that correspond to the 'type' parameter's enum values. However, it doesn't explain parameter interactions or provide additional context 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.

Purpose4/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: '自动修复代码问题' (automatically fix code problems) with specific examples of what types of problems (Lint, TypeScript, formatting). It uses a specific verb ('修复' - fix) and resource ('代码问题' - code problems). However, it doesn't explicitly differentiate from sibling tools like 'refactor' or 'resolve_conflict' which might have overlapping functionality.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It mentions the types of fixes available (lint, type, format, etc.) but doesn't specify when to choose this over sibling tools like 'refactor' for code improvements, 'resolve_conflict' for merge issues, or 'debug' for runtime problems. No prerequisites or exclusions 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/mybolide/mcp-probe-kit'

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