TypeScript LSP服务器

为AI代理提供TypeScript语言服务器协议功能的MCP服务器,使AI代理能够获取代码中的类型信息和执行其他LSP操作。

  • by jaenster
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 jaenster/ts-lsp-mcp

使用说明

ts lsp mcp

MCP服务器向AI代理公开TypeScript LSP类功能。

赋予AI代理与IDE用户相同的“此位置的类型是什么?”权限。

快速开始

安装到Claude代码中

# Install to current project (recommended)
npx ts-lsp-mcp install cc

# Or install globally for all projects
npx ts-lsp-mcp install cc --global

就是这样!MCP服务器现在可供Claude Code使用。

卸载

npx ts-lsp-mcp uninstall cc

手动安装

全局npm安装

npm install -g ts-lsp-mcp
ts-lsp-mcp install cc

手动配置

添加到您的Claude Code MCP配置中(.mcp.json 在项目或 ~/.claude/settings.json 全球):

{
  "mcpServers": {
    "ts-lsp-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "ts-lsp-mcp", "serve", "--stdio"]
    }
  }
}

HTTP/SSE模式

对于远程客户端或调试:

ts-lsp-mcp --http --port 3000

终点:

  • GET /sse -SSE连接
  • POST /message -发送消息
  • GET /health -健康检查

可用工具

工具 说明
getTypeAtPosition 获取特定文件中的TypeScript类型:line:col
getDefinition 转到定义
getReferences 查找所有参考文献
getHover 获取悬停文档
getCompletions 获取自动完成建议
getDiagnostics 获取类型错误和警告
traceType 跟踪类型的来源及其组成方式
runTypeTests 从以下位置运行类型断言 @ts-lsp-mcp 评论
checkInlineCode 不创建文件的内联TypeScript类型检查

型式试验断言

在代码中添加类型断言:

// @ts-lsp-mcp expect-type: string
const name = user.name;

// @ts-lsp-mcp expect-type: User
const user = createUser({ name: 'Alice' });

// @ts-lsp-mcp expect-error: 2322
const bad: number = "oops";  // Should have error 2322

运行测试:

runTypeTests({ file: "src/types.ts" })
runTypeTests({ pattern: "**/*.type-test.ts" })

示例用法

在位置获取类型

支持统一 file:line:col 格式:

{
  "tool": "getTypeAtPosition",
  "arguments": {
    "file": "src/user.ts:10:5"
  }
}

或单独参数:

{
  "tool": "getTypeAtPosition",
  "arguments": {
    "file": "src/user.ts",
    "line": 10,
    "col": 5
  }
}

答复:

{
  "type": "User",
  "expanded": "{ id: number; name: string; email: string }",
  "symbol": "user",
  "kind": "variable"
}

检查类型错误

{
  "tool": "getDiagnostics",
  "arguments": {
    "file": "src/user.ts"
  }
}

类型检查内联代码

{
  "tool": "checkInlineCode",
  "arguments": {
    "code": "const x: number = 'bad';"
  }
}

答复:

{
  "valid": false,
  "diagnostics": [{
    "line": 1,
    "col": 7,
    "code": 2322,
    "message": "Type 'string' is not assignable to type 'number'."
  }]
}

特性

  • 多项目支持:适用于具有多个tsconfigs的monorepos
  • 自动发现:自动查找tsconfig.json
  • 智能文件分辨率:接受绝对、相对或唯一文件名
  • 虚拟文件:使用键入检查未保存的代码 content 参数
  • 高效:长期存在的守护进程缓存TypeScript项目

CLI选项

ts-lsp-mcp [command] [options]

Commands:
  serve              Start the MCP server (default)
  install <target>   Install into an AI assistant (cc, claude-code, claude)
  uninstall <target> Uninstall from an AI assistant

Serve options:
  --stdio           Use stdio transport (default)
  --http            Use HTTP/SSE transport
  --port <port>     HTTP server port (default: 3000)
  --host <host>     HTTP server host (default: 127.0.0.1)
  --debug           Enable debug logging

Install options:
  --global          Install globally (user scope) instead of project
  --name <name>     Custom name for the MCP server (default: ts-lsp-mcp)

General:
  -V, --version     Output version number
  -h, --help        Display help

许可证

麻省理工学院