Skip to main content
Glama

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."),
        }),
      },

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