Skip to main content
Glama

apply_code_action

Execute a specific code action identified by index to implement suggested fixes or improvements in Python files at precise locations.

Instructions

Apply a code action by index (from get_code_actions).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
lineYes
columnYes
action_indexYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The 'apply_code_action' tool is defined as an MCP tool and handles applying code actions to files using an LSP client.
    @mcp.tool()
    async def apply_code_action(
        file_path: str, line: int, column: int, action_index: int
    ) -> str:
        """Apply a code action by index (from get_code_actions)."""
        client = _get_client()
    
        path = Path(file_path).resolve()
        if not path.exists():
            return _error(f"File not found: {file_path}")
    
        try:
            await client.open_document(path)
    
            import asyncio
            await asyncio.sleep(0.3)
    
            diagnostics = client.get_diagnostics(path)
            relevant_diags = [
                d for d in diagnostics
                if d.range.start.line <= line - 1 <= d.range.end.line
            ]
    
            actions = await client.get_code_actions(
                path, line - 1, column - 1, line - 1, column, relevant_diags
            )
    
            if not actions:
                return _not_found(f"No actions at {path.name}:{line}:{column}")
    
            if action_index < 1 or action_index > len(actions):
                return _error(f"Invalid index. Choose 1-{len(actions)}")
    
            action = actions[action_index - 1]
    
            if not action.edit:
                return _error(f"Action '{action.title}' has no edits")
    
            all_edits = action.edit.get_all_edits()
            applied_files = []
    
            for uri, edits in all_edits.items():
                file_to_edit = _uri_to_path(uri)
                if file_to_edit.exists():
                    new_content = _apply_edits_to_file(file_to_edit, edits)
                    file_to_edit.write_text(new_content, encoding="utf-8")
                    applied_files.append(file_to_edit.name)
    
            return _ok({
                "applied": True,
                "action": action.title,
                "modified_files": applied_files
            })
    
        except Exception as e:
            return _error(str(e))
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It fails to state that this operation likely modifies the filesystem, whether it requires the file to be saved first, or what happens if the action_index is invalid. 'Apply' implies mutation but doesn't confirm safety or reversibility.

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 single sentence is efficiently structured with zero redundant words. However, given the complete absence of schema descriptions, this extreme brevity is inadequate rather than optimal—it sacrifices necessary parameter documentation for terseness.

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?

The tool has 4 undocumented parameters (0% schema coverage), no annotations, and likely complex IDE-side effects. While an output schema exists (reducing the description's burden for return values), the description is insufficient for parameter comprehension and operational context.

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

Parameters2/5

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

Schema description coverage is 0% (titles only, no descriptions). While the description mentions 'by index' (relating to action_index), it provides no semantic context for the other three required parameters (file_path, line, column) or their relationship to the code action resolution context.

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 uses a specific verb ('Apply') and resource ('code action') and clearly indicates the mechanism ('by index'). The parenthetical reference to 'get_code_actions' effectively distinguishes this from its sibling tool, clarifying this is the execution step while get_code_actions is the retrieval step.

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 parenthetical '(from get_code_actions)' implies a workflow dependency, suggesting this tool consumes indices from that sibling. However, it lacks explicit guidance on prerequisites (e.g., 'Call get_code_actions first') or when to use alternatives like get_edit_preview instead.

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/qinsehm1128/mcp-ty'

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