This skill enables semantic code exploration and safe refactoring using LSP, helping you navigate definitions, references, and symbols across projects.
npx playbooks add skill lsp-client/lsp-client --skill lsp-client
Semantic code analysis using Language Server Protocol (LSP) clients. Provides language-aware code navigation, symbol search, and safe refactoring capabilities.
Three ways to use the library:
import anyio
from pathlib import Path
from lsp_client import Position
from lsp_client.clients.pyright import PyrightClient
async def main():
workspace = Path.cwd()
async with PyrightClient(workspace=workspace) as client:
definitions = await client.request_definition_locations(
file_path="src/main.py",
position=Position(line=10, character=5)
)
for def_loc in definitions:
file = client.from_uri(def_loc.uri)
print(f"Definition at {file}:{def_loc.range.start.line}")
anyio.run(main)
See scripts/custom_client_template.py for a complete template.
Key pattern - inherit from capability mixins:
from attrs import define
from lsp_client.clients.base import PythonClientBase
from lsp_client.capability.request import (
WithRequestHover,
WithRequestDefinition,
WithRequestReferences,
)
@define
class MyClient(
PythonClientBase,
WithRequestHover,
WithRequestDefinition,
WithRequestReferences,
):
...
Run standalone analysis scripts without writing code:
# Analyze symbol at position
python scripts/basic_analysis.py src/main.py 10 5
# Find all symbols in file
python scripts/find_all_symbols.py file src/main.py
# Search workspace symbols
python scripts/find_all_symbols.py workspace MyClass
# Safe rename with preview
python scripts/safe_rename.py src/main.py 10 5 new_name
Task: Understand how a function is used across the project
Approach:
client.request_definition_locations()client.request_references()client.request_hover()Script: python scripts/basic_analysis.py <file> <line> <char>
Task: Rename a symbol across the entire workspace
Approach:
client.request_rename_edits() client.apply_workspace_edit()Script: python scripts/safe_rename.py <file> <line> <char> <new_name>
Task: Find all classes/functions matching a pattern
Approach:
client.request_workspace_symbol_list(query="MyClass")client.request_document_symbol_list(file_path=...)Script:
python scripts/find_all_symbols.py workspace <query>python scripts/find_all_symbols.py file <path>See references/language_servers.md for full list.
Quick reference:
PyrightClient, PyreflyClient, BasedpyrightClient, TyClientRustAnalyzerClientTypescriptClient, DenoClientGoplsClientJdtlsClientAll clients support both local (subprocess) and container (Docker) modes.
The library uses capability mixins to compose exactly the features needed.
See references/capabilities.md for complete list.
Common combinations:
Basic navigation:
WithRequestDefinitionWithRequestReferencesWithRequestHoverIDE-like features:
WithRequestCompletionWithRequestSignatureHelpWithRequestCodeActionSymbol search:
WithRequestDocumentSymbol (file-level)WithRequestWorkspaceSymbol (project-wide)Refactoring:
WithRequestRenameWithRequestCodeActionWithRequestWorkspaceEditEvery client supports dual server backends:
from lsp_client.clients.pyright import PyrightClient
async with PyrightClient() as client:
# Uses local pyright-langserver if available
...
Pros: Fast startup, direct file access
Cons: Requires server installed locally
from lsp_client.clients.pyright import PyrightClient, PyrightContainerServer
async with PyrightClient(server=PyrightContainerServer()) as client:
# Uses ghcr.io/lsp-client/pyright:latest
...
Pros: Zero installation, consistent environment
Cons: Docker overhead, path translation
If no server specified, library tries:
server= argument (if provided)LSP uses 0-indexed line and character positions:
from lsp_client import Position
# Line 10, character 5 (0-indexed)
pos = Position(line=10, character=5)
LSP servers work with URIs. The client handles conversion:
# Server returns URI
location = await client.request_definition_locations(...)
# Convert back to file path
file_path = client.from_uri(location.uri)
Always use async with to ensure proper cleanup:
async with Client(...) as client:
# Client initialized, server running
result = await client.request_hover(...)
# Server automatically shut down
Clients come with sensible defaults. Override as needed:
@define
class MyClient(PyrightClient):
def create_default_config(self):
return {
"python": {
"analysis": {
"typeCheckingMode": "strict",
}
}
}
Customize server behavior with hooks:
from lsp_client.server.local import LocalServer
@define
class CustomServer(LocalServer):
async def setup(self, workspace):
# Before server starts
...
async def on_started(self, workspace, sender):
# After server ready
...
async def on_shutdown(self):
# Before cleanup
...
See library's examples/custom_hooks.py for details.
Symptom: "Could not find language server"
Solution: Either install locally or use container:
from lsp_client.clients.pyright import PyrightContainerServer
client = PyrightClient(server=PyrightContainerServer())
When using containers, always work with workspace-relative paths:
# Good
await client.request_hover(file_path="src/main.py", ...)
# Bad (absolute paths won't translate correctly)
await client.request_hover(file_path="/Users/me/project/src/main.py", ...)
basic_analysis.py - Hover, definition, references for a symbolfind_all_symbols.py - Document/workspace symbol searchsafe_rename.py - Preview and apply rename refactoringcustom_client_template.py - Template for creating custom clientscapabilities.md - Complete list of available LSP capabilitieslanguage_servers.md - Supported language servers and selection guideWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.