Skip to main content
Glama

Apply Code Action

apply_code_action

Apply quick fixes and refactorings in Svelte code by selecting available actions to resolve issues or improve code structure.

Instructions

Apply a code action (quick fix, refactoring) by its title. Use get_code_actions first to see available actions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesAbsolute path to the file
startLineYesStart line (1-based)
actionTitleYesTitle of the code action to apply (case-insensitive partial match)
endLineNoEnd line (1-based)
kindNoFilter by kind: quickfix, refactor, etc.

Implementation Reference

  • The handler function for 'apply_code_action' which fetches, identifies, resolves, and applies the code action.
    async ({
      filePath,
      startLine,
      actionTitle,
      endLine,
      kind,
    }): Promise<ToolResult> => {
      try {
        const { actions, error } = await fetchCodeActions(
          lsp,
          filePath,
          startLine,
          endLine,
          kind
        );
        if (error) return textResult(error);
        if (!actions) return textResult("No code actions available.");
    
        // Find matching action by partial title
        let match = actions.find((a: any) =>
          (a.title ?? "")
            .toLowerCase()
            .includes(actionTitle.toLowerCase())
        );
    
        // Exact match fallback
        if (!match) {
          match = actions.find(
            (a: any) =>
              (a.title ?? "").toLowerCase() === actionTitle.toLowerCase()
          );
        }
    
        if (!match) {
          return textResult(
            `No code action matching '${actionTitle}' found. Use get_code_actions to see available actions.`
          );
        }
    
        if (match.disabled) {
          return textResult(
            `Code action '${match.title}' is disabled: ${match.disabled.reason}.`
          );
        }
    
        // If action has a direct edit, use it. Otherwise resolve.
        let edit = match.edit;
        if (!edit) {
          const resolved = await lsp.request("codeAction/resolve", match);
          if (!resolved) {
            return textResult(
              `Failed to resolve code action '${match.title}'.`
            );
          }
          edit = resolved.edit;
        }
    
        if (!edit) {
          if (match.command) {
            return textResult(
              `Code action '${match.title}' requires command execution which is not supported.`
            );
          }
          return textResult(
            `Code action '${match.title}' returned no edits.`
          );
        }
    
        const matchTitle = match.title ?? actionTitle;
        const applied = await applyWorkspaceEdit(lsp, edit);
        const summary = formatWorkspaceEdit(edit, `Applied '${matchTitle}'`);
        return textResult(
          applied
            ? summary
            : `(dry-run, edits NOT applied)\n\n${summary}`
        );
      } catch (ex) {
        return textResult(formatError(ex));
      }
    }
  • Registration of the 'apply_code_action' tool with its schema definition.
    server.registerTool(
      "apply_code_action",
      {
        title: "Apply Code Action",
        description:
          "Apply a code action (quick fix, refactoring) by its title. Use get_code_actions first to see available actions.",
        inputSchema: z.object({
          filePath: z.string().describe("Absolute path to the file"),
          startLine: z.number().describe("Start line (1-based)"),
          actionTitle: z
            .string()
            .describe(
              "Title of the code action to apply (case-insensitive partial match)"
            ),
          endLine: z.number().optional().describe("End line (1-based)"),
          kind: z
            .string()
            .optional()
            .describe("Filter by kind: quickfix, refactor, etc."),
        }),
      },
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. It mentions applying 'quick fix, refactoring' which implies mutation, but doesn't disclose critical behavioral traits: whether changes are reversible, what permissions are needed, if it modifies files in-place, what happens on failure, or what the response contains. For a mutation tool with zero annotation coverage, this leaves significant gaps.

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?

Two concise sentences with zero waste. The first sentence states the core purpose, the second provides crucial workflow guidance. Every word earns its place, and information is front-loaded appropriately.

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?

Given this is a mutation tool with no annotations and no output schema, the description should do more. While it provides good purpose clarity and excellent usage guidelines, it lacks behavioral transparency about the mutation effects, error handling, and response format. The 100% schema coverage helps, but for a tool that modifies code, more context about the operation's nature and consequences is needed.

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 5 parameters thoroughly. The description adds minimal value beyond the schema - it mentions applying by title (implied by actionTitle parameter) and references get_code_actions for discovery. No additional parameter semantics are provided 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 verb ('apply') and resource ('code action'), specifying it works by title. It distinguishes from get_code_actions by indicating that tool should be used first to see available actions. However, it doesn't explicitly differentiate from other sibling tools like rename_symbol or format_document that might also modify code.

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: 'Use get_code_actions first to see available actions.' This clearly indicates when to use this tool (after discovering actions) and references the alternative tool for discovery. It establishes a clear workflow dependency.

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/adainrivers/SvelteLS.MCP'

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