lsp-mcp服务器
一种MCP(模型上下文协议)服务器,将克劳德代码连接到语言服务器协议(LSP)服务器,实现语义代码智能功能。
概述
lsp-mcp服务器 充当Claude Code和语言服务器之间的桥梁,提供强大的代码智能功能:
- 转到定义 -导航到定义符号的位置
- 查找引用 -查找整个工作区中符号的所有用法
- 悬停信息 -获取类型信息和文档
- 代码补全 -获取智能代码建议
- 诊断 -来自语言服务器的访问错误、警告和提示
- 符号搜索 -在文档中或整个工作区中搜索符号
- 重命名 -在整个代码库中安全地重命名符号
- 代码操作 -应用快速修复、重构和组织导入
- 调用层次结构 -查看谁调用函数及其调用内容
- 类型层次 -探索类继承和接口实现
- 格式化文档 -使用语言服务器的格式化程序格式化代码
- 智能搜索 -单次通话中的综合符号分析
- 文件分析 -探索导入、导出和文件关系
┌─────────────┐ ┌──────────────────┐ ┌───────────────────┐
│ Claude Code │────▶│ lsp-mcp-server │────▶│ Language Servers │
│ (MCP) │◀────│ (this tool) │◀────│ (TypeScript, etc) │
└─────────────┘ └──────────────────┘ └───────────────────┘
stdio stdio/JSON-RPC stdio特性
- 29 MCP工具 用于全面的代码智能
- 支持10种语言 开箱即用:
- Types/JavaScript - python - 锈 - 去 - C/C++ - 红宝石 - PHP - 灵药 - 科特林 - Java
- 多根工作区 -为每个工作区的服务器实例提供适当的monorepo支持
- 基于推送的诊断 -来自语言服务器的实时错误/警告缓存
- 人性化的职位 -所有行号/列号均为1-索引
- 安全重命名 -在应用模拟运行模式之前预览更改
- 自动服务器管理 -服务器按需启动,并在崩溃时重新启动
- 可配置的 -自定义语言服务器、超时等
- 安全功能 -文件大小限制、工作区边界验证、绝对路径强制
安装
先决条件
- Node.js 18.0.0或更高
- 语言服务器 对于您要使用的语言:
# TypeScript/JavaScript
npm install -g typescript-language-server typescript
# Python
pip install python-lsp-server
# Rust
rustup component add rust-analyzer
# Go
go install golang.org/x/tools/gopls@latest
# C/C++
# Ubuntu/Debian:
sudo apt install clangd
# macOS:
brew install llvm
# Ruby
gem install solargraph
# PHP
npm install -g intelephense
# Elixir
mix escript.install hex elixir_ls
# Or download pre-built releases from:
# https://github.com/elixir-lsp/elixir-ls/releases
# Kotlin
# macOS:
brew install JetBrains/utils/kotlin-lsp
# Or download from:
# https://github.com/Kotlin/kotlin-lsp/releases
# Java (requires Java 20+, Maven, npm, protobuf)
git clone https://github.com/idelice/jls
cd jls && ./scripts/build.sh
# Add dist/ to PATH or symlink dist/lang_server_linux.sh as 'jls'安装lsp-mcp服务器
# Clone the repository
git clone
cd lsp-mcp-server
# Install dependencies
npm install
# Build
npm run build
# Verify installation
node dist/index.js --help全局安装(可选)
# Link globally for easy access
npm link
# Now you can run from anywhere
lsp-mcp-server使用Claude代码进行配置
1.添加到克劳德代码MCP设置
创建或编辑 .mcp.json 主目录中的文件:
地点: ~/.mcp.json (用户级别)或 .mcp.json 在项目根目录(项目级别)中
{
"mcpServers": {
"lsp": {
"command": "node",
"args": ["/absolute/path/to/lsp-mcp-server/dist/index.js"],
"env": {
"LSP_LOG_LEVEL": "info"
}
}
}
}或者,如果通过npm链接全局安装:
{
"mcpServers": {
"lsp": {
"command": "lsp-mcp-server"
}
}
}2.重新启动克劳德代码
更新配置后,重新启动Claude Code以加载新的MCP服务器。
3.验证安装
在克劳德代码中,问:
“使用lsp_server_status检查可用的语言服务器”
您应该看到一个显示服务器正在运行的响应。
4.强制使用LSP工具(推荐)
为了使Claude Code始终更喜欢LSP工具,而不是像这样的替代品 Grep 和 Glob 对于代码导航,请在全局设置中添加说明 ~/.claude/CLAUDE.md 文件:
## LSP Server - REQUIRED FIRST STEP
**BEFORE any code analysis, navigation, or codebase exploration, you MUST:**
1. Run `lsp_server_status` to check running servers
2. If the relevant language server is NOT running → run `lsp_start_server` immediately
3. ONLY AFTER the LSP server is running, proceed with analysis
This is a hard requirement, not a preference. Do NOT skip this step.
## LSP Tool Requirements
When LSP MCP tools are available, you MUST use them instead of alternatives:
| Task | REQUIRED Tool | FORBIDDEN Alternatives |
|------|---------------|----------------------|
| Find where X is defined | `lsp_goto_definition` | Grep, Read, Glob |
| Find where X is used | `lsp_find_references` | Grep |
| Find symbol by name | `lsp_workspace_symbols` or `lsp_find_symbol` | Glob, Grep |
| Understand file structure | `lsp_document_symbols` | Read entire file |
| Get type information | `lsp_hover` | Reading source code |
| Find implementations | `lsp_find_implementations` | Grep |
| Understand module API | `lsp_file_exports` | Read entire file |
| Check for errors | `lsp_diagnostics` | Running compiler manually |
| See file dependencies | `lsp_file_imports` or `lsp_related_files` | Grep for imports |
## Prohibited Patterns
When LSP is available, NEVER do these:
- NEVER use `Grep` to find function/class/symbol definitions
- NEVER use `Grep` to find where a symbol is referenced
- NEVER use `Glob` to find files containing a symbol name
- NEVER use `Read` to scan through a file looking for definitions
- NEVER use `Bash` with grep/rg/find for code navigation
These tools are still appropriate for:
- Searching for text/strings (not code symbols)
- Reading configuration files
- Reading documentation files
- File operations unrelated to code navigation
## LSP Tool Quick Reference
lsp_server_status#检查正在运行的内容 lsp_start_server#启动语言服务器 lsp_stop_server#停止语言服务器 lsp_goto_definition#跳转到定义符号的位置 lsp_goto_type_definition#跳转到类型定义 lsp_find_references#查找符号的所有用法 lsp_find_implements#查找具体实现 lsp_workspace_symbols#在项目中搜索符号 lsp_document_symbols#获取文件大纲 lsp_document_highlights#此文件中的每个事件(读/写分类) lsp_hover#获取符号的类型/docs lsp_signature_help#获取函数参数提示 lsp_inlay_hints#范围内的推断类型+参数名称 lsp_coompletions#获取代码补全 lsp_tdiagnostics#获取文件的错误/警告 lsp_workspace_dignostics#在打开的文件中获取错误/警告 lsp_index_files#预热:批处理打开工作区诊断文件 lsp_file_exports#获取模块的公共API lsp_file_imports#获取文件的导入/依赖关系(正则表达式、JS/TS) lsp_related_files#查找连接的文件(导入/由导入) lsp_folding_ranges#可折叠区域(函数、块、导入) lsp_selection_range#语义封闭范围(stmt/block/fn) lsp_renamed#跨代码库重命名符号 lsp_code_actions#获取/应用快速修复和重构 lsp_call_hierarchi#查看调用者和被调用者 lsp_type层次结构#参见类型继承 lsp_format_document#格式代码 lsp_smart_search#组合:定义+引用+悬停 lsp_find_symbol#按名称查找符号(可选范围为文件)
这确保了Claude Code将:
- 在分析代码之前,始终启动LSP服务器
- 使用语义LSP工具而不是基于文本的搜索进行代码导航
- 仅在非代码搜索(字符串、配置文件、文档)时使用Grep/Glob
5.安装LLM使用技能(推荐)
此存储库提供 SKILL.md --一个独立的、面向法学硕士的指南,教助理如何在29个选项之间做出选择 lsp_* 工具,它们的陷阱是什么,以及规范的工作流程是什么样子的。将其作为Claude Code技能安装,可以让模型按需加载该指南,而不需要将其粘贴到每个提示中。
除了上面的CLAUDE.md代码片段外,为什么还要安装它? CLAUDE.md代码段强制执行 *那* 使用LSP工具。 SKILL.md 教授 *怎么* 为了更好地使用它们——决策树、工作流、陷阱、输出形状、错误代码。两者相辅相成。
克劳德代码
在用户级别安装(在每个项目中都可用):
mkdir -p ~/.claude/skills/lsp-mcp-server
cp SKILL.md ~/.claude/skills/lsp-mcp-server/SKILL.md或者在项目级别(提交给特定的仓库,仅在其中可用):
mkdir -p .claude/skills/lsp-mcp-server
cp /path/to/lsp-mcp-server/SKILL.md .claude/skills/lsp-mcp-server/SKILL.md重新启动Claude Code(或启动新会话)。该技能是从其YAML frontmatter中自动发现的(name: lsp-mcp-server).克劳德将通过 Skill 每当代码导航、重构或诊断相关时,都可以使用该工具。
要验证,请询问Claude Code:
“你为LSP提供了哪些技能?”
你应该看看 lsp-mcp-server 上市的。
其他Claude/Anthropic SDK集成
SKILL.md 是带有YAML frontmatter的纯Markdown,因此它可以在任何可以发布Markdown文档的地方工作:
- Anthropic API/Claude代理SDK --通过加载 技能特征 或者将其包含在系统提示中。
- 自定义代理 --将内容复制到代理的系统提示或知识库中。
- 其他LLM CLI(Gemini CLI、Copilot CLI等) --将其放入客户端支持的任何技能/指令目录中,或将其作为参考上下文包含在内。
该文件是有意自包含的:没有外部链接可供遵循,也没有其他文件可供安装。一个Markdown文档就是全部技能。
保持更新
如果你升级 lsp-mcp-server (新工具,新行为),重新复制 SKILL.md 从新版本。例如,如果工具签名发生变化,未来的版本可能会使用过时的技能——将技能固定到您运行的服务器版本是保持一致的最简单方法。
可用工具
导航工具
lsp_goto_definition
导航到符号的定义。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
Output:
- definitions: Array of locations with path, line, column, and context示例提示: 转到/project/src/utils.ts中第42行第10列的函数定义
lsp_goto_type_definition
导航到符号的类型定义(对于查找定义变量类型的接口/类很有用)。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
Output:
- definitions: Array of type definition locations示例提示: 在/project/src/app.ts的第15行第5列找到变量的类型定义
参考工具
lsp_find_references
在工作空间中查找对符号的所有引用。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- include_declaration: Whether to include the declaration (default: true)
- limit: Maximum results (default: 100, max: 500)
- offset: Skip results for pagination (default: 0)
Output:
- references: Array of locations
- total_count: Total number of references found
- has_more: Whether there are more results示例提示: “在/project/src/services/user.ts的第5行找到对'UserService'类的所有引用”
lsp_find_implementations
查找接口或抽象方法的所有实现。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- limit: Maximum results (default: 50, max: 100)
Output:
- implementations: Array of implementation locations
- total_count: Total implementations found
- has_more: Whether there are more results示例提示: “在/project/src/types.ts的第10行找到接口的所有实现”
信息工具
lsp_hover
获取符号的悬停信息(类型信息、文档)。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
Output:
- contents: Markdown-formatted type information and documentation
- range: The range of the hovered symbol (optional)示例提示: “/project/src/main.ts中第25行第8列的变量类型是什么?”
lsp_signature_help
在调用表达式中获取函数/方法签名信息。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
Output:
- signatures: Array of function signatures with parameters
- active_signature: Index of the active signature
- active_parameter: Index of the active parameter示例提示: “/project/src/api.ts中第30行的函数调用的参数是什么?”
符号工具
lsp_document_symbols
获取文档中定义的所有符号(函数、类、变量等)。
Input:
- file_path: Absolute path to the source file
Output:
- symbols: Hierarchical array of symbols with name, kind, range, and children示例提示: “列出/project/src/components/Button.tsx中的所有符号”
lsp_workspace_symbols
按名称在整个工作空间中搜索符号。
Input:
- query: Search query (supports fuzzy matching)
- kinds: Filter by symbol kinds (optional): Class, Function, Interface, Variable, etc.
- limit: Maximum results (default: 50, max: 100)
Output:
- symbols: Array of matching symbols with path and location
- total_count: Total matches found
- has_more: Whether there are more results示例提示: 在工作区中搜索所有包含“Service”的类
lsp_find_symbol
按名称查找符号并获取有关它的全面信息-不需要文件路径。
Input:
- name: Symbol name to search for (supports fuzzy matching)
- kind: Filter to specific symbol kind (optional): Class, Function, Interface, etc.
- include: Array of what to include: 'hover', 'definition', 'references', 'implementations', 'incoming_calls', 'outgoing_calls' (default: ['hover', 'definition', 'references'])
- references_limit: Maximum references to return (default: 20)
Output:
- query: The symbol that was searched for
- match: The best matching symbol found
- matches_found: Number of total matches
- definition: Where the symbol is defined
- hover: Type information and documentation
- references: All usages of the symbol
- implementations: Implementations (for interfaces)
- incoming_calls: Functions that call this
- outgoing_calls: Functions this calls示例提示: “找到UserService类并向我显示其所有引用”
文件分析工具
lsp_file_exports
获取文件的公共API表面-所有导出的函数、类、接口和变量。
Input:
- file_path: Absolute path to the source file
- include_signatures: Include type signatures from hover (default: true, slower but more informative)
Output:
- file: The file path
- exports: Array of exported items with name, kind, line, column, and signature
- note: Additional information示例提示: “/project/src/utils/index.ts导出什么?”
lsp_file_imports
获取文件的所有导入和依赖关系。
Input:
- file_path: Absolute path to the source file
Output:
- file: The file path
- imports: Array of imports with module, line, symbols, is_type_only, is_dynamic
- note: Additional information示例提示: “/project/src/api/client.ts导入哪些模块?”
lsp_related_files
查找与给定文件连接的文件-它导入了什么以及导入了什么。
Input:
- file_path: Absolute path to the source file
- relationship: Which relationships to include: 'imports', 'imported_by', or 'all' (default: 'all')
Output:
- file: The file path
- imports: Array of files this file imports
- imported_by: Array of files that import this file
- note: Additional information示例提示: “哪些文件依赖于/project/src/services/auth.ts?”
诊断工具
lsp_diagnostics
获取文件的缓存诊断(错误、警告)。
Input:
- file_path: Absolute path to the source file
- severity_filter: Filter by severity - 'all', 'error', 'warning', 'info', 'hint' (default: 'all')
Output:
- diagnostics: Array of diagnostics with range, severity, message, and code
- summary: Count of errors, warnings, info, and hints
- note: Information about diagnostic caching示例提示: “显示/project/src/index.ts中的所有错误”
lsp_workspace_diagnostics
获取工作区中所有打开文件的诊断信息。
Input:
- severity_filter: Filter by severity - 'all', 'error', 'warning', 'info', 'hint' (default: 'all')
- limit: Maximum diagnostics to return (default: 50, max: 200)
- group_by: How to group results - 'file' or 'severity' (default: 'file')
Output:
- items: Array of diagnostics with file, line, column, severity, message, and context
- total_count: Total diagnostics found
- returned_count: Number returned (may be limited)
- files_affected: Number of files with diagnostics
- summary: Count of errors, warnings, info, and hints
- note: Information about diagnostic caching示例提示: “显示整个项目中的所有错误”
完井工具
lsp_completions
在某个位置获取代码完成建议。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- limit: Maximum suggestions (default: 20, max: 50)
Output:
- completions: Array of completion items with label, kind, detail, and documentation
- is_incomplete: Whether the list is incomplete示例提示: “/project/src/app.ts中第15行第10列有哪些可用的补全?”
重构工具
lsp_rename
在整个工作空间中重命名符号。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- new_name: The new name for the symbol
- dry_run: Preview changes without applying (default: true)
Output:
- changes: Map of file paths to arrays of edits
- files_affected: Number of files that would be modified
- edits_count: Total number of edits
- applied: Whether changes were applied
- original_name: The original symbol name (if available)示例提示: “将/project/src/api.ts中第20行的函数'getUserData'重命名为'fetchUserData'(先进行模拟运行)”
lsp_code_actions
在某个位置或范围内获取可用的代码操作(重构、快速修复),并可选择应用它们。
Input:
- file_path: Absolute path to the source file
- start_line: Start line number (1-indexed)
- start_column: Start column number (1-indexed)
- end_line: End line number (optional, defaults to start line)
- end_column: End column number (optional, defaults to start column)
- kinds: Filter by action kinds (optional): quickfix, refactor, refactor.extract, refactor.inline, source.organizeImports, etc.
- apply: If true, apply the action at action_index (default: false)
- action_index: Index of action to apply when apply=true (default: 0)
Output:
- actions: Array of available code actions with title, kind, and edits
- total_count: Number of available actions
- applied: The action that was applied (if apply=true and successful)示例提示: “/project/src/utils.ts中第50行的函数有哪些重构选项?”
示例提示: “对/project/src/api.ts中第15行的错误应用第一个快速修复”
lsp_format_document
使用语言服务器的格式化功能格式化文档。
Input:
- file_path: Absolute path to the source file
- tab_size: Spaces per tab (default: 2)
- insert_spaces: Use spaces instead of tabs (default: true)
- apply: Apply formatting to file (default: false)
Output:
- edits: Array of formatting edits with range and new_text
- edits_count: Number of edits
- applied: Whether edits were applied示例提示: “使用语言服务器格式化/project/src/messy-file.ts”
层次结构工具
lsp_call_hierarchy
获取函数的调用层次结构——谁调用它以及它调用什么。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- direction: 'incoming' (callers), 'outgoing' (callees), or 'both' (default: 'both')
Output:
- item: The call hierarchy item at the position
- incoming_calls: Array of functions that call this function
- outgoing_calls: Array of functions this function calls示例提示: “显示/project/src/server.ts中第100行调用handleRequest的所有函数”
lsp_type_hierarchy
获取类或接口的类型层次结构-超类型和子类型。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- direction: 'supertypes' (parents), 'subtypes' (children), or 'both' (default: 'both')
Output:
- item: The type hierarchy item at the position
- supertypes: Array of parent types/interfaces
- subtypes: Array of child types/implementations示例提示: “哪些类实现了/project/src/types.ts中第5行的Repository接口?”
组合工具
lsp_smart_search
在一次呼叫中结合多个LSP操作的综合符号搜索。
Input:
- file_path: Absolute path to the source file
- line: Line number (1-indexed)
- column: Column number (1-indexed)
- include: Array of what to include: 'hover', 'definition', 'references', 'implementations', 'incoming_calls', 'outgoing_calls' (default: ['hover', 'definition', 'references'])
- references_limit: Maximum references to return (default: 20)
Output:
- symbol_name: Name of the symbol
- hover: Type information and documentation
- definition: Where the symbol is defined
- references: All usages of the symbol
- implementations: Implementations (for interfaces)
- incoming_calls: Functions that call this
- outgoing_calls: Functions this calls示例提示: “给我一份对/project/src/processor.ts中第75行processData函数的完整分析,包括定义、所有引用和调用它的内容”
服务器管理工具
lsp_server_status
获取正在运行的语言服务器的状态。
Input:
- server_id: Specific server to check (optional, omit for all servers)
Output:
- servers: Array of server status objects with id, status, capabilities, uptime, etc.示例提示: “显示所有语言服务器的状态”
lsp_start_server
手动启动特定工作区的语言服务器。
Input:
- server_id: Server ID from configuration (e.g., 'typescript', 'python')
- workspace_root: Absolute path to the workspace/project root
Output:
- status: 'started'
- server_id: The server that was started
- workspace_root: The workspace root
- capabilities: List of supported capabilities示例提示: “启动/home/user/my项目的TypeScript语言服务器”
lsp_stop_server
停止正在运行的语言服务器。
Input:
- server_id: Server ID to stop
- workspace_root: Workspace root (optional, omit to stop all instances)
Output:
- status: 'stopped'
- server_id: The server that was stopped
- was_running: Whether the server was actually running示例提示: “停止Python语言服务器”
支持的语言
开箱即用支持以下语言:
| 语言 | 服务器 | 命令 | 文件扩展名 | 根模式 |
|---|---|---|---|---|
| Types/JavaScript | 打字语言服务器 | typescript-language-server --stdio | .ts, .tsx, .js, .jsx, .mjs, .cjs | tsconfig.json, jsconfig.json, package.json |
| python | pylsp | pylsp | .py, .pyi | pyproject.toml, setup.py, requirements.txt, Pipfile |
| 锈 | 锈蚀分析仪 | rust-analyzer | .rs | Cargo.toml |
| 去 | gopls | gopls serve | .go | go.mod, go.work |
| C/C++ | 声音 | clangd --background-index | .c, .h, .cpp, .hpp, .cc, .cxx | compile_commands.json, CMakeLists.txt, Makefile |
| 红宝石 | 太阳图 | solargraph stdio | .rb, .rake, .gemspec | Gemfile, .ruby-version, Rakefile |
| PHP | 智力障碍 | intelephense --stdio | .php, .phtml | composer.json, index.php |
| 灵药 | 灵丹妙药ls | elixir-ls | .ex, .exs, .heex, .leex | mix.exs, .formatter.exs |
| 科特林 | 科特林lsp | kotlin-lsp | .kt, .kts | build.gradle, build.gradle.kts, settings.gradle, settings.gradle.kts |
| Java | jls | jls | .java | pom.xml, build.gradle, build.gradle.kts, settings.gradle, settings.gradle.kts, BUILD, .classpath |
您可以通过提供自定义配置来添加其他语言(请参阅 配置).
配置
配置文件
在以下位置之一创建配置文件(按优先级顺序):
./.lsp-mcp.json(当前目录)./lsp-mcp.json(当前目录)~/.config/lsp-mcp/config.json(XDG配置)~/.lsp-mcp.json(主目录)
或设置 LSP_CONFIG_PATH 环境变量,用于指定自定义路径。
配置示例:
{
"servers": [
{
"id": "typescript",
"extensions": [".ts", ".tsx", ".js", ".jsx"],
"languageIds": ["typescript", "typescriptreact", "javascript", "javascriptreact"],
"command": "typescript-language-server",
"args": ["--stdio"],
"rootPatterns": ["tsconfig.json", "package.json"]
},
{
"id": "python",
"extensions": [".py"],
"languageIds": ["python"],
"command": "pylsp",
"args": [],
"rootPatterns": ["pyproject.toml", "setup.py", "requirements.txt"]
}
],
"requestTimeout": 30000,
"autoStart": true,
"logLevel": "info",
"idleTimeout": 1800000
}配置选项
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
servers | array | 内置默认值 | 语言服务器配置 |
requestTimeout | number | 30000 | 请求超时(毫秒) |
autoStart | boolean | true | 第一次请求时自动启动服务器 |
logLevel | string | “info” | 日志级别:调试、信息、警告、错误 |
idleTimeout | number | 18000000 | 停止服务器前的空闲超时(30分钟) |
服务器配置
每个服务器 servers 数组具有:
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | string | 是 | 服务器的唯一标识符 |
extensions | string\[\] | 是 | 此服务器处理的文件扩展名 |
languageIds | string\[\] | 是 | LSP语言标识符 |
command | string | Yes | 启动服务器的命令 |
args | string\[\] | Yes | 命令参数 |
env | object | 否 | 环境变量 |
initializationOptions | object | 否 | LSP初始化选项 |
rootPatterns | string\[\] | 否 | 表示项目根目录的文件/dir |
环境变量
| 变量 | 描述 |
|---|---|
LSP_LOG_LEVEL | 覆盖日志级别(调试、信息、警告、错误) |
LSP_CONFIG_PATH | 配置文件的路径 |
LSP_WORKSPACE_ROOT | 覆盖工作区根检测 |
安全功能
lsp-mcp服务器包括几种安全措施:
- 绝对路径强制 -所有文件路径必须是绝对的,以防止路径遍历攻击
- 工作区边界验证 -文件修改(重命名、格式化、代码操作)仅限于工作区根目录内
- 文件大小限制 -大于10MB的文件将被拒绝,以防止内存耗尽
- 不执行Shell -语言服务器是通过以下方式生成的
shell: false防止命令注入
Claude代码的使用示例
基本导航
“我在看/project/src/services/auth.ts。你能告诉我它是什么吗 validateToken 第45行的功能是什么?使用lsp_hover获取其文档。"“转到定义 UserRepository 在/project/src/controllers/user.ts的第23行第15列使用“查找用途
“找到所有地方 handleError 函数在我的代码库中被调用。它在/project/src/utils/error.ts的第10行定义“我想重构 Config 界面。首先,使用lsp_find_implementations查找其所有实现”代码质量
“使用lsp_tdiagnostics检查/project/src/index.ts是否有任何TypeScript错误”
“使用lsp_workspace_dignostics显示整个项目中的所有错误和警告”
安全重构
“我想重命名getData功能到fetchData。它位于/project/src/api.ts中的第50行。首先进行一次模拟运行,看看会有什么变化。"
“模拟运行看起来不错。现在通过将dry_run设置为false来应用重命名。”
代码探索
“列出/project/src/models/User.ts中的所有符号,以了解其结构”
在工作区中搜索名称中包含“Controller”的所有类
“找到UserService类并告诉我关于它的一切——定义、引用和调用它的东西”
文件分析
“/project/src/utils/index.ts导出什么?”
“哪些文件依赖于/project/src/services/auth.ts?使用lsp_related_files”
“显示/project/src/api/client.ts中的所有导入”
补全
“在/project/src/app.ts中第5列第30行的对象上有哪些可用方法?使用lsp_coompletions”
代码操作和重构
“对于/project/src/utils.ts中从第20行到第35行的代码选择,有哪些重构选项可用?”
“使用lsp_code_actions在/project/src/components/App.tsx中组织导入,并为source.organizeImports设置种类筛选器”
“对/project/src/api.ts中第15行的错误应用第一个快速修复”
理解代码流
“在/project/src/orders.ts的第50行显示processOrder函数的调用层次结构-我想看看是什么调用它”
“身份验证函数调用什么?使用带有传出方向的lsp_call_hierage”
“显示BaseRepository类的类型层次结构-它的子类型是什么?”
综合分析
“请对/project/src/services/user.ts中第10行的UserService类进行完整分析。我需要定义、所有引用、实现和调用层次结构。使用lsp_smart_search”
格式化
使用语言服务器格式化/project/src/unformatd.ts(先预览,不应用)
故障排除
找不到语言服务器
错误: Failed to start language server: typescript-language-server
解决方案: 安装语言服务器:
npm install -g typescript-language-server typescript未显示诊断
问题: lsp_diagnostics 返回空结果
说明: 诊断是基于推送的。当文件被打开或更改时,语言服务器会发送它们。
解决方案:
- 请先使用其他工具打开文件
- 等待服务器进行分析
- 再试一次
服务器反复崩溃
问题: 服务器不断崩溃并重新启动
解决方案:
- 检查
LSP_LOG_LEVEL=debug查看详细日志 - 验证语言服务器是否已正确安装
- 检查工作区是否具有有效的配置(例如,TypeScript的tsconfig.json)
位置误差
问题: “位置无效”错误
记得: 所有位置都是1索引(第一行是1,第一列是1),而不是0索引。
路径错误
问题: “文件路径必须是绝对的”错误
记得: 所有文件路径必须是绝对的(例如。, /home/user/project/src/file.ts,不 src/file.ts).
超时错误
问题: 请求超时
解决方案: 增加超时时间:
export LSP_REQUEST_TIMEOUT=60000 # 60 seconds或者在配置中:
{
"requestTimeout": 60000
}文件太大
问题: “文件太大”错误
说明: 大于10 MB的文件将被拒绝,以防止内存问题。
解决方案: 处理较小的文件或将大文件拆分为模块。
发展
建筑
npm run build # Compile TypeScript
npm run dev # Watch mode
npm run typecheck # Type-check only测试
npm test # Run unit tests
npm run test:watch # Watch mode交互式测试
使用MCP检查器进行交互式测试:
npx @modelcontextprotocol/inspector node dist/index.js代码检查
npm run lint # Check for issues
npm run lint:fix # Auto-fix issues建筑
多根工作区支持
服务器实例由以下键控 (serverId, workspaceRoot) 对。这意味着:
- 每个工作区都有自己的语言服务器实例
- Monoreps可以正确处理多个tsconfig.json文件
- 服务器设置按工作区隔离
诊断缓存
与其他基于请求的LSP功能不同,诊断是基于推送的:
- 语言服务器发送
publishDiagnostics通知 - lsp-mcp服务器将这些缓存在内存中
lsp_diagnostics和lsp_workspace_diagnostics从缓存中读取的工具
这意味着在打开文件后,无需明确请求即可立即进行诊断。
自动服务器生命周期
- 服务器在需要时自动启动(如果
autoStart: true) - 崩溃的服务器以指数级回退重新启动(5分钟内最多尝试3次)
- 空闲服务器在配置的超时后关闭
版本
看 对于当前版本。MCP服务器在启动时动态报告其版本,因此它始终与包同步。
许可证
麻省理工学院
贡献
欢迎投稿!在提交pull请求之前,请阅读贡献指南。
