by Tritlo
lsp-mcp is an MCP (Model Context Protocol) server for interacting with LSP (Language Server Protocol) interface, acting as a bridge that allows LLMs to query LSP Hover and Completion providers.
lsp-mcp is a Model Context Protocol (MCP) server designed to bridge the gap between Large Language Models (LLMs) and Language Server Protocol (LSP) servers. It allows LLMs to interact with LSP servers to obtain rich context information, such as hover details, code completion suggestions, and code actions, thereby enhancing the LLM's ability to provide accurate code-related assistance.
To use lsp-mcp, you first need to install Node.js (v16 or later) and npm. You can then build the server by cloning the repository, installing dependencies (npm install
), and running the build command (npm run build
).
To run the MCP server, you execute it via npx
and provide the language, path to the LSP executable, and any LSP arguments:
npx tritlo/lsp-mcp <language> /path/to/lsp [lsp-args...]
For example:
npx tritlo/lsp-mcp haskell /usr/bin/haskell-language-server-wrapper lsp
Important: With version 0.2.0 and later, you must explicitly start the LSP server by calling the start_lsp
tool before using any LSP functionality. This ensures proper initialization with the correct root directory.
lsp-mcp offers a comprehensive set of features to facilitate interaction between LLMs and LSP servers:
get_info_on_location
: Retrieve hover information at a specific file location.get_completions
: Obtain code completion suggestions.get_code_actions
: Get code actions for a specified range.open_document
: Open a file in the LSP server for analysis.close_document
: Close a file in the LSP server.get_diagnostics
: Get diagnostic messages (errors, warnings) for open files.start_lsp
: Start the LSP server with a specified root directory.restart_lsp_server
: Restart the LSP server without restarting the MCP server.set_log_level
: Change the server's logging verbosity level at runtime.lsp-diagnostics://
: Access diagnostic messages with real-time updates via subscriptions.lsp-hover://
: Retrieve hover information at specific file locations.lsp-completions://
: Get code completion suggestions at specific positions.lsp-mcp is primarily designed to empower LLMs with enhanced code understanding and generation capabilities. Specific use cases include:
Q: What are the prerequisites for running lsp-mcp? A: You need Node.js (v16 or later) and npm. For the demo server, GHC (8.10 or later) and Cabal (3.0 or later) are also required.
Q: How do I start the LSP server with lsp-mcp?
A: You must explicitly call the start_lsp
tool with the desired root directory before using any other LSP functionality.
Q: How can I view detailed debug logs?
A: You can use the claude --mcp-debug
flag when running Claude to see all MCP traffic, or change the log level at runtime using the set_log_level
tool to debug
.
Q: What is the difference between using MCP Tools and MCP Resources? A: Both approaches provide the same data in the same format. Tools offer a direct way to fetch information, while resources provide a more RESTful approach. Diagnostic resources also support subscriptions for real-time updates.
Q: Can I extend lsp-mcp for other languages?
A: Yes, lsp-mcp supports language-specific extensions. You can create a new TypeScript file in src/extensions/
and implement the Extension
interface to provide custom tools, resource handlers, and prompts for your desired language.
An MCP (Model Context Protocol) server for interacting with LSP (Language Server Protocol) interface. This server acts as a bridge that allows LLMs to query LSP Hover and Completion providers.
The MCP Server works by:
This enables LLMs to utilize LSPs for more accurate code suggestions.
{
"mcpServers": {
"lsp-mcp": {
"type": "stdio",
"command": "npx",
"args": [
"tritlo/lsp-mcp",
"<language-id>",
"<path-to-lsp>",
"<lsp-args>"
]
}
}
}
get_info_on_location
: Get hover information at a specific location in a fileget_completions
: Get completion suggestions at a specific location in a fileget_code_actions
: Get code actions for a specific range in a fileopen_document
: Open a file in the LSP server for analysisclose_document
: Close a file in the LSP serverget_diagnostics
: Get diagnostic messages (errors, warnings) for open filesstart_lsp
: Start the LSP server with a specified root directoryrestart_lsp_server
: Restart the LSP server without restarting the MCP serverset_log_level
: Change the server's logging verbosity level at runtimelsp-diagnostics://
resources for accessing diagnostic messages with real-time updates via subscriptionslsp-hover://
resources for retrieving hover information at specific file locationslsp-completions://
resources for getting code completion suggestions at specific positionsFor the demo server:
Clone this repository:
git clone https://github.com/your-username/lsp-mcp.git
cd lsp-mcp
Install dependencies:
npm install
Build the MCP server:
npm run build
The project includes integration tests for the TypeScript LSP support. These tests verify that the LSP-MCP server correctly handles LSP operations like hover information, completions, diagnostics, and code actions.
To run the TypeScript LSP tests:
npm test
or specifically:
npm run test:typescript
The tests verify the following functionality:
The test project is located in test/ts-project/
and contains TypeScript files with intentional errors to test diagnostic feedback.
Run the MCP server by providing the path to the LSP executable and any arguments to pass to the LSP server:
npx tritlo/lsp-mcp <language> /path/to/lsp [lsp-args...]
For example:
npx tritlo/lsp-mcp haskell /usr/bin/haskell-language-server-wrapper lsp
With version 0.2.0 and later, you must explicitly start the LSP server by calling the start_lsp
tool before using any LSP functionality. This ensures proper initialization with the correct root directory, which is especially important when using tools like npx:
{
"tool": "start_lsp",
"arguments": {
"root_dir": "/path/to/your/project"
}
}
The server includes a comprehensive logging system with 8 severity levels:
debug
: Detailed information for debugging purposesinfo
: General informational messages about system operationnotice
: Significant operational eventswarning
: Potential issues that might need attentionerror
: Error conditions that affect operation but don't halt the systemcritical
: Critical conditions requiring immediate attentionalert
: System is in an unstable stateemergency
: System is unusableBy default, logs are sent to:
notifications/message
method)For detailed debugging, you can:
Use the claude --mcp-debug
flag when running Claude to see all MCP traffic between Claude and the server:
claude --mcp-debug
Change the log level at runtime using the set_log_level
tool:
{
"tool": "set_log_level",
"arguments": {
"level": "debug"
}
}
The default log level is info
, which shows moderate operational detail while filtering out verbose debug messages.
The server provides the following MCP tools:
Gets hover information at a specific location in a file.
Parameters:
file_path
: Path to the filelanguage_id
: The programming language the file is written in (e.g., "haskell")line
: Line numbercolumn
: Column positionExample:
{
"tool": "get_info_on_location",
"arguments": {
"file_path": "/path/to/your/file",
"language_id": "haskell",
"line": 3,
"column": 5
}
}
Gets completion suggestions at a specific location in a file.
Parameters:
file_path
: Path to the filelanguage_id
: The programming language the file is written in (e.g., "haskell")line
: Line numbercolumn
: Column positionExample:
{
"tool": "get_completions",
"arguments": {
"file_path": "/path/to/your/file",
"language_id": "haskell",
"line": 3,
"column": 10
}
}
Gets code actions for a specific range in a file.
Parameters:
file_path
: Path to the filelanguage_id
: The programming language the file is written in (e.g., "haskell")start_line
: Start line numberstart_column
: Start column positionend_line
: End line numberend_column
: End column positionExample:
{
"tool": "get_code_actions",
"arguments": {
"file_path": "/path/to/your/file",
"language_id": "haskell",
"start_line": 3,
"start_column": 5,
"end_line": 3,
"end_column": 10
}
}
Starts the LSP server with a specified root directory. This must be called before using any other LSP-related tools.
Parameters:
root_dir
: The root directory for the LSP server (absolute path recommended)Example:
{
"tool": "start_lsp",
"arguments": {
"root_dir": "/path/to/your/project"
}
}
Restarts the LSP server process without restarting the MCP server. This is useful for recovering from LSP server issues or for applying changes to the LSP server configuration.
Parameters:
root_dir
: (Optional) The root directory for the LSP server. If provided, the server will be initialized with this directory after restart.Example without root_dir (uses previously set root directory):
{
"tool": "restart_lsp_server",
"arguments": {}
}
Example with root_dir:
{
"tool": "restart_lsp_server",
"arguments": {
"root_dir": "/path/to/your/project"
}
}
Opens a file in the LSP server for analysis. This must be called before accessing diagnostics or performing other operations on the file.
Parameters:
file_path
: Path to the file to openlanguage_id
: The programming language the file is written in (e.g., "haskell")Example:
{
"tool": "open_document",
"arguments": {
"file_path": "/path/to/your/file",
"language_id": "haskell"
}
}
Closes a file in the LSP server when you're done working with it. This helps manage resources and cleanup.
Parameters:
file_path
: Path to the file to closeExample:
{
"tool": "close_document",
"arguments": {
"file_path": "/path/to/your/file"
}
}
Gets diagnostic messages (errors, warnings) for one or all open files.
Parameters:
file_path
: (Optional) Path to the file to get diagnostics for. If not provided, returns diagnostics for all open files.Example for a specific file:
{
"tool": "get_diagnostics",
"arguments": {
"file_path": "/path/to/your/file"
}
}
Example for all open files:
{
"tool": "get_diagnostics",
"arguments": {}
}
Sets the server's logging level to control verbosity of log messages.
Parameters:
level
: The logging level to set. One of: debug
, info
, notice
, warning
, error
, critical
, alert
, emergency
.Example:
{
"tool": "set_log_level",
"arguments": {
"level": "debug"
}
}
In addition to tools, the server provides resources for accessing LSP features including diagnostics, hover information, and code completions:
The server exposes diagnostic information via the lsp-diagnostics://
resource scheme. These resources can be subscribed to for real-time updates when diagnostics change.
Resource URIs:
lsp-diagnostics://
- Diagnostics for all open fileslsp-diagnostics:///path/to/file
- Diagnostics for a specific fileImportant: Files must be opened using the open_document
tool before diagnostics can be accessed.
The server exposes hover information via the lsp-hover://
resource scheme. This allows you to get information about code elements at specific positions in files.
Resource URI format:
lsp-hover:///path/to/file?line={line}&column={column}&language_id={language_id}
Parameters:
line
: Line number (1-based)column
: Column position (1-based)language_id
: The programming language (e.g., "haskell")Example:
lsp-hover:///home/user/project/src/Main.hs?line=42&column=10&language_id=haskell
The server exposes code completion suggestions via the lsp-completions://
resource scheme. This allows you to get completion candidates at specific positions in files.
Resource URI format:
lsp-completions:///path/to/file?line={line}&column={column}&language_id={language_id}
Parameters:
line
: Line number (1-based)column
: Column position (1-based)language_id
: The programming language (e.g., "haskell")Example:
lsp-completions:///home/user/project/src/Main.hs?line=42&column=10&language_id=haskell
To discover available resources, use the MCP resources/list
endpoint. The response will include all available resources for currently open files, including:
Diagnostic resources support subscriptions to receive real-time updates when diagnostics change (e.g., when files are modified and new errors or warnings appear). Subscribe to diagnostic resources using the MCP resources/subscribe
endpoint.
Note: Hover and completion resources don't support subscriptions as they represent point-in-time queries.
You can choose between two approaches for accessing LSP features:
get_diagnostics
, get_info_on_location
, and get_completions
tools for a simple, direct way to fetch information.lsp-diagnostics://
, lsp-hover://
, and lsp-completions://
resources for a more RESTful approach.Both approaches provide the same data in the same format and enforce the same requirement that files must be opened first.
MIT License
The LSP-MCP server supports language-specific extensions that enhance its capabilities for different programming languages. Extensions can provide:
Currently, the following extensions are available:
Extensions are loaded automatically when you specify a language ID when starting the server:
npx tritlo/lsp-mcp haskell /path/to/haskell-language-server-wrapper lsp
All extension-provided features are namespaced with the language ID. For example, the Haskell extension's typed-hole prompt is available as haskell.typed-hole-use
.
To create a new extension:
Create a new TypeScript file in src/extensions/
named after your language (e.g., typescript.ts
)
Implement the Extension interface with any of these optional functions:
getToolHandlers()
: Provide custom tool implementationsgetToolDefinitions()
: Define custom tools in the MCP APIgetResourceHandlers()
: Implement custom resource handlersgetSubscriptionHandlers()
: Implement custom subscription handlersgetUnsubscriptionHandlers()
: Implement custom unsubscription handlersgetResourceTemplates()
: Define custom resource templatesgetPromptDefinitions()
: Define custom prompts for language tasksgetPromptHandlers()
: Implement custom prompt handlersExport your implementation functions
The extension system will automatically load your extension when the matching language ID is specified.
Please log in to share your review and rating for this MCP.