Skip to content

Add Experimental MCP Support #3396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Add Experimental MCP Support #3396

wants to merge 2 commits into from

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Apr 16, 2025

Add Model Context Protocol (MCP) support to Ruby LSP

This PR adds support for the Model Context Protocol (MCP), enabling AI assistants to programmatically access Ruby project indexes and file contents that Ruby LSP already collects.

Connection Mechanism

The implementation uses a hybrid approach:

  1. Ruby LSP runs an HTTP-based MCP server powered by WEBrick on a dynamically assigned local port
  2. A lightweight stdio-to-HTTP bridge script (ruby-mcp-bridge) connects the editor's MCP client to our HTTP server
  3. Communication flows bidirectionally between AI assistants and the Ruby project's metadata

Architecture Diagram

Screenshot 2025-04-14 at 09 43 50

Editor Setup

VS Code Configuration

Add to .vscode/mcp.json:

{
  "inputs": [],
  "servers": {
    "rubyMcp": {
      "type": "stdio",
      "command": "${workspaceFolder}/.ruby-lsp/ruby-mcp-bridge"
    }
  }
}

See VS Code MCP documentation for more details.

Cursor Configuration

Add to .cursor/mcp.json:

{
  "mcpServers": {
    "rubyMcp": {
      "command": "./.ruby-lsp/ruby-mcp-bridge"
    }
  }
}

See Cursor MCP documentation for more details.

Notes For Manual Testing

This feature requires both the extension and server changes to work. So for testing:

  1. Run the extension with the subject project
  2. Update the subject project's Gemfile to have gem "ruby-lsp", github: "Shopify/ruby-lsp", branch: "mcp"
  3. Make sure the server is connected to the respective editors. When connected, the editor should display what tools are available
  4. In both editors, MCP servers are only used in "Agent" mode
  5. For VS Code, tool usage needs to be selected manually at each prompt

Known Issues

⚠️ Cursor-specific issue: We've identified that Cursor's MCP/LSP client implementation can occasionally cause hangs in the Ruby LSP process. The main thread gets stuck waiting on gets in the LSP communication channel. This issue may be specific to Cursor as it doesn't occur (or occurs much less frequently) in VS Code, and more often in certain projects (e.g. ruby/irb) than others. The cause may be related to how Cursor handles MCP/LSP requests and requires more investigation.

Supported Tools (TBD)

The MCP server provides four tools:

  1. get_classes_and_modules - Browse all indexed classes and modules in your project, with optional query filtering
  2. read_ruby_files - Access file contents, including from dependencies
    • I removed this because clients usually would ask additional permission to read files outside of the workspace, and Ruby MCP should not provide a way to get around that.
  3. get_methods_details - Get comprehensive information about methods (documentation, parameters, location, visibility)
  4. get_class_module_details - See class/module details including documentation, ancestry chains, and available methods

These tools enable AI assistants to understand Ruby code structure, navigate dependencies, and access relevant documentation that are not accessible through the editor's simple codebase indexing.

@st0012 st0012 added enhancement New feature or request vscode This pull request should be included in the VS Code extension's release notes server This pull request should be included in the server gem's release notes labels Apr 16, 2025
Copy link

graphite-app bot commented Apr 16, 2025

How to use the Graphite Merge Queue

Add the label graphite-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@st0012 st0012 force-pushed the mcp branch 4 times, most recently from ec7daf7 to 9a95003 Compare April 16, 2025 07:35
@andyw8
Copy link
Contributor

andyw8 commented Apr 29, 2025

Some early notes on how to try this with Zed:

  • Check out this branch locally (as a small modification is needed) and point your project's Gemfile to it.

  • Manually copy ruby-mcp-bridge into .ruby-lsp in your project (ensure it retains the +x permission) since we can't rely on the VS Code extension (a Zed extension should be fairly easy though).

  • Configure the project settings:

    // .zed/settings.json
    {
      "context_servers": {
        "mcp-servers-ruby-lsp": {
          "command": {
            "path": "/path/to/project/.ruby-lsp/ruby-mcp-bridge", // seems to need an absolute path
            "args": []
          },
        }
      }

Modify global_state.rb in Ruby LSP to make it Zed-aware:

def detects_ruby_mcp
  check_mcp_file(".vscode/mcp.json", ["servers", "rubyMcp"]) ||
    check_mcp_file(".cursor/mcp.json", ["mcpServers", "rubyMcp"]) ||
    check_mcp_file(".zed/settings.json", ["context_servers", "mcp-servers-ruby-lsp"])
end

Reload Zed, choose Tools in the Agent panel and if all well you should see get_classes_and_modules etc. listed:

Screenshot 2025-04-28 at 8 54 09 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes vscode This pull request should be included in the VS Code extension's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants