|
| 1 | +# LSP Client |
| 2 | + |
| 3 | +[](https://python.org) |
| 4 | +[](LICENSE) |
| 5 | + |
| 6 | +A full-featured, well-typed, and easy-to-use Python client for the Language Server Protocol (LSP). This library provides a clean, async-first interface for interacting with language servers, supporting both local and Docker-based runtimes. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- **🚀 Async-first Design**: Built for high-performance concurrent operations |
| 11 | +- **🔧 Full LSP Support**: Comprehensive implementation of LSP 3.17 specification |
| 12 | +- **🐳 Docker Support**: Run language servers in isolated containers |
| 13 | +- **📝 Type Safety**: Full type annotations with Pydantic validation |
| 14 | +- **🧩 Modular Architecture**: Mixin-based capability system for easy extension |
| 15 | +- **🎯 Production Ready**: Robust error handling with tenacity retries |
| 16 | +- **📚 Well Documented**: Extensive documentation and examples |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +uv add lsp-client |
| 24 | +``` |
| 25 | + |
| 26 | +### Basic Usage |
| 27 | + |
| 28 | +```python |
| 29 | +from pathlib import Path |
| 30 | +import anyio |
| 31 | +from lsp_client import Position |
| 32 | +from lsp_client.clients.pyright import PyrightClient |
| 33 | + |
| 34 | +async def main(): |
| 35 | + # Use current directory as workspace |
| 36 | + async with PyrightClient(workspace=Path.cwd()) as client: |
| 37 | + # Find definition of something at line 11, character 28 in a file |
| 38 | + refs = await client.request_definition_locations( |
| 39 | + file_path="src/main.py", |
| 40 | + position=Position(11, 28) # line 12, character 29 (1-indexed) |
| 41 | + ) |
| 42 | + |
| 43 | + if not refs: |
| 44 | + print("No definition found.") |
| 45 | + return |
| 46 | + |
| 47 | + for ref in refs: |
| 48 | + print(f"Found definition at: {ref.uri}") |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + anyio.run(main) |
| 52 | +``` |
| 53 | + |
| 54 | +### Docker-based Language Server |
| 55 | + |
| 56 | +```python |
| 57 | +from pathlib import Path |
| 58 | +import anyio |
| 59 | +from lsp_client.clients.pyright import PyrightClient, PyrightServer, PyrightDockerRuntime |
| 60 | + |
| 61 | +async def main(): |
| 62 | + workspace = Path.cwd() |
| 63 | + async with PyrightClient( |
| 64 | + workspace=workspace, |
| 65 | + server=PyrightServer( |
| 66 | + runtime=PyrightDockerRuntime(mounts=[workspace]), |
| 67 | + ), |
| 68 | + ) as client: |
| 69 | + # Find definition of something at line 11, character 28 in a file |
| 70 | + refs = await client.request_definition_locations( |
| 71 | + file_path="src/main.py", |
| 72 | + position=Position(11, 28) |
| 73 | + ) |
| 74 | + |
| 75 | + if not refs: |
| 76 | + print("No definition found.") |
| 77 | + return |
| 78 | + |
| 79 | + for ref in refs: |
| 80 | + print(f"Found definition at: {ref.uri}") |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + anyio.run(main) |
| 84 | +``` |
| 85 | + |
| 86 | +## Supported Language Servers |
| 87 | + |
| 88 | +The library includes pre-configured clients for popular language servers: |
| 89 | + |
| 90 | +| Language Server | Module Path | Language | |
| 91 | +| --------------- | ---------------------------------- | --------------------- | |
| 92 | +| Pyright | `lsp_client.clients.pyright` | Python | |
| 93 | +| Pyrefly | `lsp_client.clients.pyrefly` | Python | |
| 94 | +| Rust Analyzer | `lsp_client.clients.rust_analyzer` | Rust | |
| 95 | +| Deno | `lsp_client.clients.deno` | TypeScript/JavaScript | |
| 96 | + |
| 97 | +## Contributing |
| 98 | + |
| 99 | +We welcome contributions! Please see our [Contributing Guide](docs/contribution/) for details. |
| 100 | + |
| 101 | +## License |
| 102 | + |
| 103 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 104 | + |
| 105 | +## Acknowledgments |
| 106 | + |
| 107 | +- Built on the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) specification |
| 108 | +- Uses [lsprotocol](https://github.com/microsoft/lsprotocol) for LSP type definitions |
| 109 | +- Inspired by [multilspy](https://github.com/microsoft/multilspy) and other LSP clients |
0 commit comments