Skip to main content
Glama

get_code_actions

Retrieve quick fixes and refactorings for Python code at specific positions to improve code quality and resolve issues.

Instructions

Get available quick fixes and refactorings at position (1-based).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
lineYes
columnYes

Implementation Reference

  • This function is the MCP tool handler 'get_code_actions', which gets diagnostics for a file path and calls the underlying LSP client to retrieve code actions.
    @mcp.tool()
    async def get_code_actions(file_path: str, line: int, column: int) -> str:
        """Get available quick fixes and refactorings at position (1-based)."""
        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}")
    
            action_list = []
            for i, action in enumerate(actions, 1):
                action_list.append({
                    "index": i,
                    "title": action.title,
                    "kind": action.kind or None,
                    "has_edit": action.edit is not None
                })
    
            return _ok({"count": len(actions), "actions": action_list})
    
        except Exception as e:
            return _error(str(e))
  • This is the underlying LSP client method that performs the actual communication with the language server to get code actions.
    async def get_code_actions(
        self, file_path: str | Path, start_line: int, start_char: int,
        end_line: int, end_char: int, diagnostics: list[Diagnostic] | None = None
    ) -> list[CodeAction]:
        """Get available code actions (quick fixes, refactorings) for a range."""
        file_path = Path(file_path).resolve()
        uri = file_path.as_uri()
    
        context: dict[str, Any] = {"diagnostics": []}
        if diagnostics:
            for diag in diagnostics:
                context["diagnostics"].append({
                    "range": diag.range.to_dict(),
                    "message": diag.message,
                    "severity": diag.severity,
                    "source": diag.source,
                    "code": diag.code
                })
    
        result = await self._send_request("textDocument/codeAction", {
            "textDocument": {"uri": uri},
            "range": {
                "start": {"line": start_line, "character": start_char},
                "end": {"line": end_line, "character": end_char}
            },
            "context": context
        })
    
        if not result:
            return []
    
        actions = []
        for item in result:

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