Index
code_context_agent.tools.lsp ¶
LSP (Language Server Protocol) tools package.
Provides tools for semantic code analysis via real LSP JSON-RPC over stdio.
LspSessionManager ¶
Singleton session manager for LSP connections.
Maintains a pool of LSP client connections keyed by server kind and workspace path. This allows multiple tools to share the same LSP connection efficiently.
Session keys are formatted as "{server_kind}:{workspace_path}".
Example
manager = LspSessionManager() client = await manager.get_or_create("ts", "/path/to/project") symbols = await client.document_symbols("/path/to/file.ts") await manager.shutdown_all()
reset_instance classmethod ¶
Reset the singleton instance (for testing).
get_server_command_for_session ¶
Get the server command string that was used for a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier (format: "kind:workspace"). | required |
Returns:
| Type | Description |
|---|---|
str | None | The command string that successfully started the server, or None. |
Source code in src/code_context_agent/tools/lsp/session.py
get_or_create async ¶
Get existing session or create new one with fallback chain.
Tries each configured LSP server command in order. If one fails, falls back to the next. Stores which command succeeded for later inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_kind | str | Server type ("ts", "py", etc.). | required |
workspace_path | str | Absolute path to workspace root. | required |
startup_timeout | float | None | Maximum seconds to wait for server initialization. If None, uses the value from settings. | None |
Returns:
| Type | Description |
|---|---|
LspClient | Connected LspClient instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If all server commands fail to start. |
ValueError | If server kind is not supported. |
Source code in src/code_context_agent/tools/lsp/session.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_session ¶
Get an existing session by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier (format: "kind:workspace"). | required |
Returns:
| Type | Description |
|---|---|
LspClient | None | LspClient if session exists and is connected, None otherwise. |
Source code in src/code_context_agent/tools/lsp/session.py
list_sessions ¶
List all active session IDs.
Returns:
| Type | Description |
|---|---|
list[str] | List of session key strings. |
shutdown_session async ¶
Shutdown a specific session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier to shutdown. | required |
Returns:
| Type | Description |
|---|---|
bool | True if session was found and shutdown, False otherwise. |
Source code in src/code_context_agent/tools/lsp/session.py
shutdown_all async ¶
Shutdown all active LSP sessions.
Source code in src/code_context_agent/tools/lsp/session.py
lsp_definition async ¶
Go to definition of symbol at position.
Use this to find where a symbol is defined, useful for tracing dependencies and understanding code structure.
Requires: Call lsp_start first to create a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start. | required |
file_path | str | Absolute path to the file. | required |
line | int | 0-indexed line number. | required |
character | int | 0-indexed column number. | required |
Returns:
| Type | Description |
|---|---|
str | JSON array of Location objects pointing to definition(s). |
Example
defn = await lsp_definition("ts:/path/repo", "/path/repo/src/index.ts", 5, 12)
Returns: [{"uri": "file:///path/repo/src/utils.ts", "range": {...}}]¶
Source code in src/code_context_agent/tools/lsp/tools.py
lsp_diagnostics async ¶
Get diagnostic messages (errors, warnings) for a file.
USE THIS TOOL: - To find type errors, unresolved references, and other issues - To assess code quality of a specific file - To find potential bugs that static analysis catches
DO NOT USE: - Before calling lsp_start (will fail with "No active LSP session") - For non-language files (e.g., JSON, YAML) unless LSP supports them
Requires: Call lsp_start first. Works best with ty (Python) server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start. | required |
file_path | str | Absolute path to the file. | required |
Returns:
| Type | Description |
|---|---|
str | JSON with diagnostics array containing severity, message, range. |
Source code in src/code_context_agent/tools/lsp/tools.py
lsp_document_symbols async ¶
Get document symbol outline (functions, classes, methods, variables).
USE THIS TOOL: - To get a structural overview of a file without reading full contents - To find function/class names and their line ranges for targeted reading - To understand file organization before diving into implementation - To get accurate symbol positions for lsp_references or lsp_hover calls
DO NOT USE: - Before calling lsp_start (will fail with "No active LSP session") - For searching across multiple files (use rg_search instead) - For simple line counting or file metadata (use read_file_bounded)
Requires: Call lsp_start first to create a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start (format: "kind:workspace"). | required |
file_path | str | Absolute path to the file to analyze. | required |
Returns:
| Type | Description |
|---|---|
str | JSON with symbols array containing name, kind, range, and nested children. |
Output Size: ~100-500 bytes per symbol. Typical file: 1-5KB response.
Symbol Kinds (common values): - 5: Class - 6: Method - 12: Function - 13: Variable - 14: Constant - 23: Struct
Common Errors
- "No active LSP session": Call lsp_start first
- Empty symbols array: File may not be parseable or have no exports
- "File not found": Ensure file_path is absolute and exists
Example success
{"status": "success", "file": "/repo/src/index.ts", "symbols": [ {"name": "main", "kind": 12, "range": {"start": {"line": 10}}, "children": []} ], "count": 5}
Workflow tip
- Use lsp_document_symbols to find symbol names and line numbers
- Use read_file_bounded with start_line to read specific sections
- Use lsp_references to find where symbols are used
Source code in src/code_context_agent/tools/lsp/tools.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
lsp_hover async ¶
Get hover information at a position (docstrings, JSDoc, type info).
Retrieves documentation and type information for the symbol at the given position. This is how you extract docstrings and JSDoc comments.
Requires: Call lsp_start first to create a session.
Args: session_id: Session ID from lsp_start. file_path: Absolute path to the file. line: 0-indexed line number. character: 0-indexed column number.
Returns: JSON object with hover contents (often includes markdown documentation).
Example: >>> hover = await lsp_hover("ts:/path/repo", "/path/repo/src/utils.ts", 10, 5) >>> # Returns: {"contents": {"kind": "markdown", "value": "/** * Utility function..."}}
Source code in src/code_context_agent/tools/lsp/tools.py
lsp_references async ¶
Find all references to symbol at position (fan-in analysis).
Use this to understand how widely a symbol is used across the codebase. The number of unique referencing files indicates the symbol's centrality.
Requires: Call lsp_start first to create a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start. | required |
file_path | str | Absolute path to the file. | required |
line | int | 0-indexed line number. | required |
character | int | 0-indexed column number. | required |
include_declaration | bool | Whether to include the declaration itself. | True |
Returns:
| Type | Description |
|---|---|
str | JSON array of Location objects with uri and range. |
Example
refs = await lsp_references("ts:/path/repo", "/path/repo/src/api.ts", 25, 10)
Returns: [{"uri": "file:///path/repo/src/handler.ts", "range": {...}}, ...]¶
Source code in src/code_context_agent/tools/lsp/tools.py
lsp_shutdown async ¶
Shutdown an LSP server session.
Use this when you're done with a workspace to free resources. Sessions are automatically cleaned up when the agent finishes, but explicit shutdown is more efficient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start. | required |
Returns:
| Type | Description |
|---|---|
str | JSON object with shutdown status. |
Example
await lsp_shutdown("ts:/path/repo")
Returns:¶
Source code in src/code_context_agent/tools/lsp/tools.py
lsp_start async ¶
Start an LSP server and initialize workspace for semantic analysis.
USE THIS TOOL: - Before using any other lsp_* tools (required prerequisite) - Once per language per workspace (session is reused automatically) - When you need semantic analysis: symbols, references, definitions, hover
DO NOT USE: - If you already started an LSP session for this language/workspace combo - For simple text searches (use rg_search instead - much faster) - For repos without the target language (e.g., don't start "ts" for Python-only repo)
Supported server kinds: - "ts": TypeScript/JavaScript (typescript-language-server) - "py": Python (ty server, with pyright-langserver fallback)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_kind | str | Server type - "ts" for TypeScript/JS, "py" for Python. | required |
workspace_path | str | Absolute path to the repository/workspace root. | required |
Returns:
| Type | Description |
|---|---|
str | Session ID (format: "kind:path") for use in subsequent LSP calls. |
Output Size: ~150 bytes JSON response.
Resource Usage
- Memory: 100-500MB per server (depends on project size)
- Startup time: 2-30 seconds (indexing dependent)
- Sessions persist until lsp_shutdown or agent exit
Common Errors
- "typescript-language-server not found": npm install -g typescript-language-server
- "ty not found": uv tool install ty or pip install ty
- Timeout: Large projects may exceed startup_timeout, increase in config
- "No tsconfig.json": TypeScript server needs tsconfig.json in workspace
Example success
{"status": "success", "session_id": "ts:/path/repo", "message": "LSP server started..."}
Workflow
- lsp_start("ts", "/repo") # Start once
- lsp_document_symbols(session_id, file) # Use many times
- lsp_references(session_id, file, line, char)
- lsp_shutdown(session_id) # Optional cleanup
Source code in src/code_context_agent/tools/lsp/tools.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
lsp_workspace_symbols async ¶
Search for symbols across the entire workspace.
USE THIS TOOL: - To find a symbol by name when you don't know which file it's in - For broad searches like "all classes named Service" - After lsp_start when you need workspace-wide symbol search
DO NOT USE: - For symbols in a specific file (use lsp_document_symbols instead) - Before calling lsp_start
Requires: ty LSP server supports workspace/symbol. TypeScript LSP also supports it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session ID from lsp_start. | required |
query | str | Search query string (partial name matching). | required |
max_results | int | Maximum symbols to return (default 50). | 50 |
Returns:
| Type | Description |
|---|---|
str | JSON with symbols array containing name, kind, location. |