一个全面的 MCP(模型上下文协议)服务器,它将语言服务器协议(LSP)的功能与 Claude 结合起来,支持多种编程语言的智能代码分析、导航和开发辅助。
MCP 服务器的基础构建在经过实战考验的 vscode-jsonrpc 和 vscode-languageserver-protocol 库之上,提供与所有 VSCode 语言服务器 的兼容性。例如,安装 Kotlin 语言服务器:
brew install JetBrains/utils/kotlin-lsp
创建一个 MCP 服务器配置文件,定义你的语言服务器和项目。
[!NOTE] 提供了一个包含流行开发语言和多个项目的示例配置文件
lsp.json,作为入门指南。
语言服务器配置具有以下格式:
{
"servers": {
"language-id": { // 必需的唯一语言标识符
"command": "language-server-binary", // 必需的语言服务器二进制文件
"args": [ // 可选的语言服务器参数
"--stdio"
],
"configuration": {}, // 可选的语言服务器配置
"env": {}, // 可选的环境变量
"extensions": [ // 必需的语言服务器扩展
".extension"
],
"init": [], // 可选的语言服务器初始化命令
"projects": [ // 必需的语言服务器项目列表
{
"name": "project-name", // 必需的唯一项目名称
"description": "A description", // 可选的描述
"url": "https://github.com/org/project", // 可选的项目 URL
"path": "/Users/username/github/project" // 必需的本地项目路径
"patterns": { // 可选的排除或包含模式
"exclude": [
"**/directory",
"**/file.extension"
],
"include": [
"**/directory",
"**/file.extension"
],
}
}
],
"settings": { // 可选的语言服务器设置
"maxConcurrentFileReads": 1
"messageRequest": true,
"preloadFiles": true,
"rateLimitMaxRequests": 100,
"rateLimitWindowMs": 60000,
"registrationRequest": true,
"shutdownGracePeriodMs": 100,
"timeoutMs": 600000,
"workspace": true
}
}
}
}
语言服务器通常需要特定的配置才能发挥最佳效果。配置需求记录在每个服务器的官方仓库中。
例如,pyright-langserver 需要以下设置:
"configuration": {
"settings": {
"python": {
"analysis": {
"autoSearchPaths": true,
"diagnosticMode": "workspace"
}
}
}
}
这些设置控制 LSP 协议的行为和服务器兼容性:
maxConcurrentFileReads - 打开项目文件时同时读取的最大文件数,控制项目初始化期间的内存使用和性能(默认值:10)messageRequest - 控制语言服务器是否可以发送 window/showMessage 请求以显示用户对话框,禁用此选项以适应无头操作或自动化环境(默认值:true)preloadFiles - 控制项目文件是在项目初始化过程中还是之后加载(默认值:true)rateLimitMaxRequests - 在速率限制窗口内允许的最大请求数量,防止语言服务器因过多并发请求而过载(默认值:1_00)rateLimitWindowMs - 速率限制的时间窗口(毫秒),在此滑动窗口内计数请求(默认值:60000 - 1 分钟)registrationRequest - 控制语言服务器是否可以发送 client/registerCapability 请求以动态注册功能,禁用此选项以适应忽略客户端功能声明的服务器(默认值:true)shutdownGracePeriodMs - 发送关闭请求后等待的时间(毫秒),允许语言服务器完成清理操作(默认值:100)timeoutMs - 等待语言服务器初始化的最大时间(毫秒),防止挂起在无响应的服务器上(默认值:600000 - 10 分钟)workspace - 控制语言服务器初始化时是否发送 workspace/symbol 请求以测试工作区功能,禁用此选项以适应不支持工作区操作或导致初始化失败的服务器(默认值:true)文件模式使用了 fast-glob 语法。默认情况下,. 前缀、__ 双下划线前缀、bin、build、cache、coverage、dist、docs、excludes、log、node_modules、obj、out、target、temp、tests、tmp、vendor 和 venv 目录被排除。使用 include 或 exclude 模式来管理特定目录或文件(例如,**/dist,**/dist/**/*.d.ts,**/*.test.js)。
添加到你的 mcp.json MCP 服务器配置:
{
"mcpServers": {
"language-server": {
"command": "npx",
"args": [
"-y",
"@axivo/mcp-lsp"
],
"env": {
"LSP_FILE_PATH": "/Users/username/github/mcp-lsp/.claude/lsp.json"
}
}
}
}
LSP_FILE_PATH - 语言服务器配置 JSON 文件的绝对路径同时运行多个语言服务器以分析不同的项目:
✅ ansible (k3s-cluster) + typescript (k3s-cluster-actions)
✅ go (helm) + kotlin (ktor) + python (fastapi)
一个语言服务器一次只能运行一个项目:
❌ typescript (mcp-lsp) + typescript (typescript-sdk)
要切换项目,请使用所需项目名称重新启动语言服务器。
让 Claude 解释 LSP 工具如何工作:
typescript-sdk 项目的 TypeScript 语言服务器并检查服务器功能。为了开始进行代码审查,请让 Claude:
typescript-sdk 项目的 TypeScript 语言服务器并检查服务器功能。/Users/username/github/mcp-lsp/.claude/templates/code-review.md 模板。[!NOTE] 语言服务器的启动时间因语言和项目大小而异,通常对于拥有数千个文件的项目来说是几秒钟。某些语言服务器如
Kotlin可能需要几分钟来初始化大型项目。如果默认超时时间已达到,请相应地增加timeoutMs值。
一个公开会话使用DEVELOPER配置展示了 LSP 工具的能力,并解释了语义分析如何提供比传统基于文本的搜索方法更准确的编译器级别的理解。
查看 Claude 可用于系统化开发工作流程的模板。
start_server
language_id,project(可选)stop_server
language_idrestart_server
language_id,projectget_server_status
language_id(可选)get_server_capabilities
language_idget_server_projects
language_idget_project_files
language_id,project,limit(可选),offset(可选)get_project_symbols
language_id,project,query,limit(可选),offset(可选),timeout(可选)get_hover
file_path,line,characterget_symbol_definitions
file_path,line,characterget_symbol_references
file_path,line,character,include_declaration(可选)get_implementations
file_path,line,characterget_type_definitions
file_path,line,characterget_call_hierarchy
file_path,line,characterget_incoming_calls
item(来自 get_call_hierarchy)get_outgoing_calls
item(来自 get_call_hierarchy)get_type_hierarchy
file_path,line,characterget_supertypes
item(来自 get_type_hierarchy)get_subtypes
item(来自 get_type_hierarchy)get_completions
file_path,line,characterget_resolves
file_path,item(来自 get_completions)get_signature
file_path,line,characterget_inlay_hints
file_path,start_line,start_character,end_line,end_characterget_inlay_hint
file_path,item(来自 get_inlay_hints)get_symbols
file_path,limit(可选),offset(可选)get_highlights
file_path,line,characterget_folding_ranges
file_pathget_colors
file_pathget_diagnostics
file_pathget_links
file_pathget_link_resolves
file_path,item(来自 get_links)get_semantic_tokens
file_pathget_format
file_path**`get