Skip to main content
Glama

get_completions

Generate code completion suggestions for Python at specific positions to improve editing accuracy and semantic understanding during development.

Instructions

Get code completion suggestions at position (1-based).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
lineYes
columnYes

Implementation Reference

  • The get_completions handler, which is registered as an MCP tool and calls the LSP client to fetch completions.
    async def get_completions(file_path: str, line: int, column: int) -> str:
        """Get code completion suggestions 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)
            completions = await client.get_completions(path, line - 1, column - 1)
    
            if not completions:
                return _not_found(f"No completions at {path.name}:{line}:{column}")
    
            kind_names = {
                1: "text", 2: "method", 3: "function", 4: "constructor",
                5: "field", 6: "variable", 7: "class", 8: "interface",
                9: "module", 10: "property", 11: "unit", 12: "value",
                13: "enum", 14: "keyword", 15: "snippet", 16: "color",
                17: "file", 18: "reference", 19: "folder", 20: "enum_member",
                21: "constant", 22: "struct", 23: "event", 24: "operator",
                25: "type_parameter"
            }
    
            items = []
            for item in completions[:30]:
                label = item.get("label", "?")
                kind = kind_names.get(item.get("kind", 0), "unknown")
                detail = item.get("detail", "")
                items.append({
                    "label": label,
                    "kind": kind,
                    "detail": detail or None
                })
    
            return _ok({
                "count": len(completions),
                "shown": len(items),
                "completions": items
            })
        except Exception as e:
            return _error(str(e))
  • The underlying LSP client method that sends the 'textDocument/completion' request to the ty language server.
    async def get_completions(
        self, file_path: str | Path, line: int, character: int
    ) -> list[dict[str, Any]]:
        """Get completion items at position."""
        file_path = Path(file_path).resolve()
        uri = file_path.as_uri()
    
        result = await self._send_request("textDocument/completion", {
            "textDocument": {"uri": uri},
            "position": {"line": line, "character": character}
        })
    
        if not result:
            return []
    
        # Result can be CompletionItem[] | CompletionList
        if isinstance(result, list):
            return result
        elif isinstance(result, dict) and "items" in result:
            return result["items"]
    
        return []

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