claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Claude Code integration with LSP (with example for Rust)

Open orsenkucher opened this issue 9 months ago • 10 comments

Hey, people! Today I've come up with idea of giving claude ability to navigate through quite large rust codebase using integration with rust-analyer. With regular text search it gets a bit lost in files. From my testing this helped a lot. Basically you need to rustup component add rust-analyzer and then use ra_tool.py and ra_tool.md reference in CLAUDE.md file.

Maybe this integration with LSPs can have more support directly from CLI tool. I like the idea of having model in shell and not in editor like Cursor or whatever. Would be interested to see what developers think on how this support can land into claude code.

Basic mvp for Rust lang is available here: https://github.com/orsenkucher/ra-tool

orsenkucher avatar Apr 08 '25 17:04 orsenkucher

this could be really awesome. I left a comment on your repo as couldn't immediately see how it works


similarly — the ability to attach to a debugger could be really powerful — if an agent could step through code and read the values into context at specific points

max-sixty avatar Apr 10 '25 19:04 max-sixty

Yeah, updated readme with correct instructions. Idea is just to talk Claude into reading script documentation. From there it knows how to use the tool. Thanks!

orsenkucher avatar Apr 10 '25 23:04 orsenkucher

If you have an LSP, you can set it up easily with https://github.com/isaacphi/mcp-language-server

tino avatar Apr 11 '25 08:04 tino

@tino Hey, I've tried it, it's awasome. I like that it's using MCP approach that is more natively integrated than bash commands. Response time is also a lot better as LSP is running in background, it provides great context around references in a scoped way. Though the context is sometimes too verbose, like giving over 2000 lines for references query that could have been shrunken to 150 lines with further exploration of code files that are relevant to Claude's Task. Also 'hover' feature is currenly missing, yet I think that this MCP server is a proper way to do LSP integration.

Given that Claude Code is all about working with code, I think LSP should be one of default tools for claude, or at least an extension that you can enable from claude mcp recommended list.

orsenkucher avatar Apr 12 '25 13:04 orsenkucher

Added those features and made context output cleaner in this PR: https://github.com/isaacphi/mcp-language-server/pull/9

orsenkucher avatar Apr 13 '25 02:04 orsenkucher

I'm surprised people keep finding my repo, it's hardly ready yet!

@orsenkucher Thank you for the PR, I'm taking a look through it and I'll leave you some comments/questions there. I'm determined to make this thing useful :) Issues are welcome if you have any other ideas/grievances

isaacphi avatar Apr 14 '25 01:04 isaacphi

Clojure user here, with a large code base. This is very interesting, as Claude sometimes has problems navigating my code, and LSP is well supported for Clojure.

jwr avatar Apr 25 '25 03:04 jwr

It's always off-putting when one sees Claude Code resorting to grep or rg to find some string (and the choice of search string it makes can be quite arbitrary).

One can sense that results won't be that good, and it may lead to false positives / false negatives and prematurely giving up, so it ends up changing strategies.

Having accurate code search in the first place would make things so more direct, exhaustive and fast.

So leveraging LSP when available would seem a significant improvement.

vemv avatar May 26 '25 16:05 vemv

There's also https://github.com/oraios/serena which adds support for LSP and more. It can be added to claude-code using

claude mcp add serena -- uvx --from git+https://github.com/oraios/serena -U serena-mcp-server --context ide-assistant --project $(pwd)

80avin avatar Jun 18 '25 20:06 80avin

I've had a lot of trouble getting either MCP-language-server or Serena working reliably with Claude on Mac OS. I started vibe coding a prototype lightweight solution for C++/clangd last night out of frustration. I would love to have this functionality in Claude Code.

jasondk avatar Jun 22 '25 18:06 jasondk

I’ve been using LSP hooks with Claude Code for a while and finally created a repo for it: https://github.com/teamchong/claude-code-lsp

Just put this together yesterday, so it’s very rough and bugs are expected. But the idea is “async signals” - Claude gets real-time diagnostics after every edit via ToolUse hooks (not MCP or Tools that Claude doesn’t always remember to use).

Demo of the concept: https://www.linkedin.com/posts/teamchong_async-signals-teaching-ai-to-see-error-panels-activity-7362090785611173888-YDLo

Feedback welcome!

teamchong avatar Aug 23 '25 23:08 teamchong

Closing since this now is possible with hooks!

catherinewu avatar Oct 17 '25 02:10 catherinewu

@catherinewu Could you elaborate how this is possible with hooks? Only solutions I have found were that on edit hook run build and that's it. I don't consider this LSP integration in a sense that Claude Code can plan changes based on real code understanding, nor at least rename a variable/class/symbol that is being used cross files.

chekrd avatar Oct 17 '25 08:10 chekrd

I’ve been using LSP hooks with Claude Code for a while and finally created a repo for it: https://github.com/teamchong/claude-code-lsp

Just put this together yesterday, so it’s very rough and bugs are expected. But the idea is “async signals” - Claude gets real-time diagnostics after every edit via ToolUse hooks (not MCP or Tools that Claude doesn’t always remember to use).

Demo of the concept: https://www.linkedin.com/posts/teamchong_async-signals-teaching-ai-to-see-error-panels-activity-7362090785611173888-YDLo

Feedback welcome!

This straight up doesn't use LSP servers. The name is misleading.

dhnaranjo avatar Oct 22 '25 01:10 dhnaranjo

I’ve been using LSP hooks with Claude Code for a while and finally created a repo for it: https://github.com/teamchong/claude-code-lsp Just put this together yesterday, so it’s very rough and bugs are expected. But the idea is “async signals” - Claude gets real-time diagnostics after every edit via ToolUse hooks (not MCP or Tools that Claude doesn’t always remember to use). Demo of the concept: https://www.linkedin.com/posts/teamchong_async-signals-teaching-ai-to-see-error-panels-activity-7362090785611173888-YDLo Feedback welcome!

This straight up doesn't use LSP servers. The name is misleading.

Yeah, you're right. The name doesn't match what it does anymore.

Claude Code has built-in LSP integration that uses your IDE's language servers, but it's not very reliable. That's why I built this few months ago.

I originally tried using LSP servers but realized it's not a good fit for how Claude Code works.

How VSCode uses LSP:

  • You open a file in the editor → textDocument/didOpen
  • As you type → textDocument/didChange (incremental updates)
  • Server keeps file in memory and auto-publishes diagnostics
  • You close the tab → textDocument/didClose

How Claude Code's PostToolUse hook works:

  • Claude edits a file and saves it to disk
  • Hook triggers after the file is already saved
  • That's it - no open event, no close event, no editing session

So with LSP I'd have to:

  • Read file from disk each time
  • Figure out when to send didOpen (first edit? always?)
  • Figure out when to send didClose (never? timeout?)
  • Manage keeping servers alive between edits with no lifecycle events

Direct CLI invocation (tsc, pyright, etc.) turned out simpler for one-time file checks.

I think there's probably a way to make LSP work properly with didSave, pull diagnostics, or an open-diag-close burst. but I am focusing on other things atm.

I should rename the project, thanks for your reply.

teamchong avatar Oct 22 '25 02:10 teamchong

I've also been experimenting here with LSP integration over MCP. Initially started it to support the official C# Language Server with its quirks but ended up using it with other Language Servers too. Hope it's of interest.

p1va avatar Oct 22 '25 13:10 p1va

@catherinewu can you please elaborate on how to use hooks for this use case? It is unclear to me why this issue was closed.

fitz-vivodyne avatar Oct 22 '25 14:10 fitz-vivodyne

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Oct 31 '25 14:10 github-actions[bot]