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
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| line | Yes | ||
| column | Yes |
Implementation Reference
- src/mcp_ty/server.py:471-513 (handler)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)) - src/mcp_ty/lsp_client.py:630-651 (handler)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 []