tritlo_lsp_mcp

tritlo_lsp_mcp

by Tritlo
An MCP server that bridges LLMs with LSP servers for code suggestions and analysis.

LSP MCP Server: Bridging Language Servers and LLMs

The LSP MCP Server is a powerful tool that enables interaction between Language Server Protocol (LSP) servers and Large Language Models (LLMs). By acting as a bridge, it allows LLMs to query LSP hover and completion providers, enhancing code suggestions and analysis capabilities.

Overview

The LSP MCP Server works by:

  1. Starting an LSP client that connects to an LSP server.
  2. Exposing MCP tools that send requests to the LSP server.
  3. Returning results in a format that LLMs can understand and use.

This integration empowers LLMs to leverage LSPs for more accurate and context-aware code suggestions.

Configuration

To configure the LSP MCP Server, use the following JSON structure:

{
  "mcpServers": {
    "lsp-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "tritlo/lsp-mcp",
        "<language-id>",
        "<path-to-lsp>",
        "<lsp-args>"
      ]
    }
  }
}

Features

MCP Tools

  • get_info_on_location: Retrieves hover information at a specific file location.
  • get_completions: Fetches completion suggestions at a specific file location.
  • get_code_actions: Retrieves code actions for a specific range in a file.
  • open_document: Opens a file in the LSP server for analysis.
  • close_document: Closes a file in the LSP server.
  • get_diagnostics: Retrieves diagnostic messages (errors, warnings) for open files.
  • start_lsp: Starts the LSP server with a specified root directory.
  • restart_lsp_server: Restarts the LSP server without restarting the MCP server.
  • set_log_level: Changes the server's logging verbosity level at runtime.

MCP Resources

  • 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.

Additional Features

  • Comprehensive logging system with multiple severity levels.
  • Colorized console output for better readability.
  • Runtime-configurable log level.
  • Detailed error handling and reporting.
  • Simple command-line interface.

Prerequisites

  • Node.js (v16 or later)
  • npm

For the demo server:

  • GHC (8.10 or later)
  • Cabal (3.0 or later)

Installation

Building the MCP Server

  1. Clone the repository:
    bash git clone https://github.com/your-username/lsp-mcp.git cd lsp-mcp

  2. Install dependencies:
    bash npm install

  3. Build the MCP server:
    bash npm run build

Testing

The project includes integration tests for TypeScript LSP support. These tests verify LSP operations like hover information, completions, diagnostics, and code actions.

Running Tests

To run the TypeScript LSP tests:

npm test

or specifically:

npm run test:typescript

Test Coverage

Tests verify the following functionality:

  • Initializing the TypeScript LSP with a mock project.
  • Opening TypeScript files for analysis.
  • Getting hover information for functions and types.
  • Getting code completion suggestions.
  • Getting diagnostic error messages.
  • Getting code actions for errors.

Usage

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

Starting the LSP Server

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:

{
  "tool": "start_lsp",
  "arguments": {
    "root_dir": "/path/to/your/project"
  }
}

Logging

The server includes a comprehensive logging system with 8 severity levels:

  • debug
  • info
  • notice
  • warning
  • error
  • critical
  • alert
  • emergency

By default, logs are sent to:

  1. Console output with color-coding.
  2. MCP notifications to the client (via the notifications/message method).

Viewing Debug Logs

For detailed debugging, you can:

  1. Use the claude --mcp-debug flag when running Claude:
    bash claude --mcp-debug

  2. Change the log level at runtime using the set_log_level tool:
    json { "tool": "set_log_level", "arguments": { "level": "debug" } }

API

The server provides the following MCP tools:

get_info_on_location

Retrieves hover information at a specific location in a file.

{
  "tool": "get_info_on_location",
  "arguments": {
    "file_path": "/path/to/your/file",
    "language_id": "haskell",
    "line": 3,
    "column": 5
  }
}

get_completions

Retrieves completion suggestions at a specific location in a file.

{
  "tool": "get_completions",
  "arguments": {
    "file_path": "/path/to/your/file",
    "language_id": "haskell",
    "line": 3,
    "column": 10
  }
}

get_code_actions

Retrieves code actions for a specific range in a file.

{
  "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
  }
}

start_lsp

Starts the LSP server with a specified root directory.

{
  "tool": "start_lsp",
  "arguments": {
    "root_dir": "/path/to/your/project"
  }
}

restart_lsp_server

Restarts the LSP server process without restarting the MCP server.

{
  "tool": "restart_lsp_server",
  "arguments": {
    "root_dir": "/path/to/your/project"
  }
}

open_document

Opens a file in the LSP server for analysis.

{
  "tool": "open_document",
  "arguments": {
    "file_path": "/path/to/your/file",
    "language_id": "haskell"
  }
}

close_document

Closes a file in the LSP server.

{
  "tool": "close_document",
  "arguments": {
    "file_path": "/path/to/your/file"
  }
}

get_diagnostics

Retrieves diagnostic messages for one or all open files.

{
  "tool": "get_diagnostics",
  "arguments": {
    "file_path": "/path/to/your/file"
  }
}

set_log_level

Sets the server's logging level.

{
  "tool": "set_log_level",
  "arguments": {
    "level": "debug"
  }
}

MCP Resources

Diagnostic Resources

Access diagnostic information via the lsp-diagnostics:// resource scheme.

Hover Information Resources

Access hover information via the lsp-hover:// resource scheme.

Code Completion Resources

Access code completion suggestions via the lsp-completions:// resource scheme.

Listing Available Resources

Use the MCP resources/list endpoint to discover available resources.

Subscribing to Resource Updates

Diagnostic resources support subscriptions for real-time updates.

Troubleshooting

  • Ensure the path to the LSP executable is correct.
  • Check the log file for detailed error messages.

License

MIT License

Extensions

The LSP-MCP server supports language-specific extensions that enhance its capabilities for different programming languages.

Available Extensions

  • Haskell: Provides specialized prompts for Haskell development, including typed-hole exploration guidance.

Using Extensions

Extensions are loaded automatically when you specify a language ID when starting the server.

Creating New Extensions

To create a new extension:

  1. Create a new TypeScript file in src/extensions/.
  2. Implement the Extension interface with optional functions.
  3. Export your implementation functions.

Acknowledgments

  • HLS team for the Language Server Protocol implementation.
  • Anthropic for the Model Context Protocol specification.

About

An MCP server that lets you interact with LSP servers.

Resources

Readme

License

MIT license

Features & Capabilities

Categories
mcp_server model_context_protocol typescript javascript haskell lsp code_completion diagnostics api_integration logging

Implementation Details

Stats

0 Views
2 GitHub Stars

Repository Info

Tritlo Organization

Similar MCP Servers

continuedev_continue by continuedev
25049
21423
9300