Python Language Server Protocol implementation providing code intelligence features for Python development
—
Core server functionality for starting, configuring, and managing the Python LSP Server. Provides multiple communication modes and comprehensive command-line interface for different deployment scenarios.
Functions for starting the LSP server in different communication modes (stdio, TCP, WebSocket).
def start_io_lang_server(rfile, wfile, check_parent_process, handler_class):
"""
Start LSP server using stdio streams.
Parameters:
- rfile: readable stream for input
- wfile: writable stream for output
- check_parent_process: bool, auto-shutdown when parent dies
- handler_class: PythonLSPServer class or subclass
"""
def start_tcp_lang_server(bind_addr, port, check_parent_process, handler_class):
"""
Start LSP server using TCP socket.
Parameters:
- bind_addr: str, IP address to bind to
- port: int, port number to bind to
- check_parent_process: bool, auto-shutdown when parent dies
- handler_class: PythonLSPServer class or subclass
"""
def start_ws_lang_server(port, check_parent_process, handler_class):
"""
Start LSP server using WebSocket.
Requires 'python-lsp-server[websockets]' installation for websockets support.
Uses asyncio with ThreadPoolExecutor for concurrent message processing.
Parameters:
- port: int, port number for WebSocket server
- check_parent_process: bool, auto-shutdown when parent dies
- handler_class: PythonLSPServer class or subclass
Raises:
- ImportError: if websockets package not installed
"""Main LSP server implementation handling protocol communication and request dispatching.
class PythonLSPServer:
def __init__(self, rx, tx, check_parent_process=False, consumer=None, *, endpoint_cls=None):
"""
Initialize LSP server.
Parameters:
- rx: readable stream for input
- tx: writable stream for output
- check_parent_process: bool, monitor parent process
- consumer: optional message consumer
- endpoint_cls: optional endpoint class override
"""
def start(self):
"""Start the server and begin processing requests."""
def consume(self, message):
"""
Process a single LSP message.
Parameters:
- message: dict, LSP message to process
"""
def capabilities(self):
"""
Return server capabilities.
Returns:
dict: LSP server capabilities
"""Main CLI entry point and argument configuration.
def main():
"""Main CLI entry point."""
def add_arguments(parser):
"""
Add command-line arguments to ArgumentParser.
Parameters:
- parser: argparse.ArgumentParser instance
"""--tcp: Use TCP server instead of stdio--ws: Use WebSocket server instead of stdio--host: Bind address (default: 127.0.0.1)--port: Port number (default: 2087)--check-parent-process: Auto-shutdown when parent process dies--log-config: Path to JSON logging configuration file--log-file: Redirect logs to file instead of stderr-v/--verbose: Increase verbosity (can be repeated)-V/--version: Show version informationServer configuration constants.
LINT_DEBOUNCE_S = 0.5 # Lint debounce interval in seconds
PARENT_PROCESS_WATCH_INTERVAL = 10 # Parent process check interval
MAX_WORKERS = 64 # Maximum worker threads
PYTHON_FILE_EXTENSIONS = (".py", ".pyi") # Supported file extensions
CONFIG_FILEs = ("pycodestyle.cfg", "setup.cfg", "tox.ini", ".flake8") # Config filesfrom pylsp.python_lsp import PythonLSPServer, start_io_lang_server
import sys
# Start with stdio
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
start_io_lang_server(stdin, stdout, False, PythonLSPServer)from pylsp.python_lsp import start_tcp_lang_server, PythonLSPServer
# Custom server class with additional functionality
class CustomLSPServer(PythonLSPServer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Custom initialization
# Start TCP server
start_tcp_lang_server("0.0.0.0", 8080, True, CustomLSPServer)from pylsp.python_lsp import start_ws_lang_server, PythonLSPServer
# Requires websockets package: pip install 'python-lsp-server[websockets]'
# Uses asyncio event loop with ThreadPoolExecutor (max_workers=10)
# Handles multiple concurrent WebSocket connections
try:
start_ws_lang_server(2087, False, PythonLSPServer)
except ImportError as e:
print("WebSocket support not available. Install with: pip install 'python-lsp-server[websockets]'")WebSocket server architecture:
websockets library for WebSocket protocol handlingThreadPoolExecutor with 10 max workers for request processing# Basic stdio server
pylsp
# TCP server with verbose logging
pylsp --tcp --host 0.0.0.0 --port 8080 -vv
# WebSocket server with custom log file
pylsp --ws --port 2087 --log-file /var/log/pylsp.log
# Server with parent process monitoring
pylsp --check-parent-processInstall with Tessl CLI
npx tessl i tessl/pypi-python-lsp-server