To query Language Server Protocol (LSP) servers from the command line for debugging or exploration, developers typically use specialized tools or scripts that handle the LSP protocol's intricacies. Here are the most effective approaches:
1. CLI Tool: lsp (by @josa42)
The best dedicated CLI tool for interacting with LSP servers is josa42/lsp. It abstracts the protocol details, allowing you to send requests and view responses easily.
Features:
- Supports sending arbitrary LSP requests (e.g.,
textDocument/definition, textDocument/completion).
- Handles protocol headers (e.g.,
Content-Length) automatically.
- Works with any LSP server (e.g.,
pyright, tsserver, clangd).
Example Usage:
# Install (requires Go)
go install github.com/josa42/lsp@latest
# Start an LSP server (e.g., pyright) and send an 'initialize' request
lsp start --cmd "pyright --stdio"
# Send a request (e.g., textDocument/completion)
lsp request textDocument/completion '{
"textDocument": {"uri": "file:///path/to/file.py"},
"position": {"line": 0, "character": 0}
}'
2. Custom Scripts for Manual Interaction
If you prefer scripting, use Python, Node.js, or similar languages to handle the LSP protocol. For example, here's a minimal Python script to communicate with an LSP server:
import subprocess
import json
def send_lsp_request(server_process, method, params):
request = {
"jsonrpc": "2.0",
"id": 1,
"method": method,
"params": params
}
body = json.dumps(request)
header = f"Content-Length: {len(body)}\r\n\r\n"
server_process.stdin.write(header.encode())
server_process.stdin.write(body.encode())
server_process.stdin.flush()
# Start the LSP server
server = subprocess.Popen(
["pyright", "--stdio"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
# Send an 'initialize' request
send_lsp_request(server, "initialize", {
"processId": None,
"rootUri": None,
"capabilities": {}
})
# Read response (simplified)
response = server.stdout.read(1024)
print(response.decode())
3. Neovim (with Lua)
Neovim’s built-in LSP client can act as a proxy for debugging. Use its Lua API to send requests:
-- Start a language server (e.g., pyright)
local server = require('lspconfig').pyright
server.setup({})
-- Send a request (e.g., textDocument/documentSymbol)
local params = vim.lsp.util.make_document_symbol_params()
vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params)
4. Raw Communication with socat or nc
If the server listens on a socket (rare), use socat or netcat. For example:
# Start a server listening on a socket
socat TCP-LISTEN:1234,reuseaddr,fork EXEC:"pyright --stdio"
# Connect and send a raw LSP request
nc localhost 1234
Note: You must manually format requests with LSP headers (e.g., Content-Length: 123\r\n\r\n).
5. Editor Logs and Tracing
Most editors (VS Code, Neovim) support logging LSP traffic:
- VS Code: Set
"logToFile": true in your settings.json to capture LSP logs.
- Neovim: Use
:LspInfo and :LspLog to inspect communication.
Summary
- Best CLI tool: Use
josa42/lsp for simplicity.
- Scripting: Python/Node.js for full control.
- Neovim: Leverage Lua APIs for ad-hoc testing.
- Manual: Use
socat/nc for socket-based servers (less common).
These methods allow you to debug and explore LSP server behavior effectively.