使用说明
打字mcp
由tsgo支持的AI编码代理的TypeScript类型检查和代码导航。
建筑
Coding Agent (Claude Code, etc.)
| MCP (stdio)
typescript-mcp (Go)
| LSP JSON-RPC (stdio)
tsgo (TypeScript 7, native Go compiler)
typescript-mcp 是一个桥接的Go MCP服务器
模型上下文协议 工具调用
tsgo的内置LSP
服务器。它为编码代理提供了真正的TypeScript类型检查和代码导航
而不依赖于正则表达式或启发式。
服务器生成 tsgo --lsp --stdio 作为子进程,通过以下方式进行通信
JSON-RPC,并将LSP响应转换为简洁、对代理友好的JSON。
先决条件
- 转到1.24+
- tsgo --全局安装或使用npx:
npm install -g @typescript/native-preview # or run without installing: npx @typescript/native-preview --version
安装
go install github.com/paulvanbrenk/typescript-mcp/cmd/typescript-mcp@latest
或者从源代码构建:
git clone https://github.com/paulvanbrenk/typescript-mcp.git
cd typescript-mcp
go build -o typescript-mcp ./cmd/typescript-mcp
Claude代码的配置
将以下内容添加到MCP配置中(.mcp.json 或克劳德代码设置):
{
"mcpServers": {
"typescript": {
"command": "typescript-mcp",
"args": []
}
}
}
如果 typescript-mcp 不在你的 PATH,使用二进制文件的完整路径。
工具参考
行号和列号为 1-基.
ts_诊断学
获取文件的TypeScript错误和警告。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 检查单个文件的绝对路径 |
tsconfig |
string | no | tsconfig.json的路径(如果省略,则自动检测) |
maxResults |
number | no | 返回的最大错误数(默认值50) |
请求示例:
{
"file": "/home/user/project/src/index.ts",
"maxResults": 10
}
示例响应:
{
"diagnostics": [
{
"file": "/home/user/project/src/index.ts",
"line": 12,
"column": 5,
"severity": "error",
"code": 2322,
"message": "Type 'string' is not assignable to type 'number'."
}
],
"totalCount": 1,
"truncated": false
}
ts_定义
转到符号的定义。返回文件和符号所在的位置 已定义,并带有源代码行的预览。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 绝对文件路径 |
line |
number | yes | 行号(从1开始) |
column |
number | yes | 列号(从1开始) |
tsconfig |
string | no | tsconfig.json的路径 |
请求示例:
{
"file": "/home/user/project/src/index.ts",
"line": 10,
"column": 15
}
示例响应:
[
{
"file": "/home/user/project/src/utils.ts",
"line": 3,
"column": 17,
"preview": "export function formatDate(date: Date): string {"
}
]
ts_hover
获取某个位置符号的类型信息和文档。返回 解析类型签名。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 绝对文件路径 |
line |
number | yes | 行号(从1开始) |
column |
number | yes | 列号(从1开始) |
tsconfig |
string | no | tsconfig.json的路径 |
请求示例:
{
"file": "/home/user/project/src/index.ts",
"line": 5,
"column": 10
}
示例响应(纯文本):
(property) AppConfig.port: number
响应是从悬停内容中提取的类型签名。标记语言 代码围栏被剥离,只返回类型信息。
ts_引用
查找整个项目中对符号的所有引用。返回每个位置 使用符号,包括声明。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 绝对文件路径 |
line |
number | yes | 行号(从1开始) |
column |
number | yes | 列号(从1开始) |
maxResults |
number | no | 返回的最大引用数(默认值50) |
tsconfig |
string | no | tsconfig.json的路径 |
请求示例:
{
"file": "/home/user/project/src/utils.ts",
"line": 3,
"column": 17
}
示例响应:
{
"references": [
{
"file": "/home/user/project/src/utils.ts",
"line": 3,
"column": 17,
"preview": "export function formatDate(date: Date): string {"
},
{
"file": "/home/user/project/src/index.ts",
"line": 10,
"column": 15,
"preview": "const result = formatDate(new Date());"
}
],
"totalCount": 2,
"truncated": false
}
ts_document_symbols
获取文件的符号轮廓。返回所有函数、类的树, 接口和变量及其类型。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 绝对文件路径 |
tsconfig |
string | no | tsconfig.json的路径 |
请求示例:
{
"file": "/home/user/project/src/utils.ts"
}
示例响应:
[
{
"name": "formatDate",
"kind": "function",
"line": 3,
"detail": "(date: Date) => string"
},
{
"name": "AppConfig",
"kind": "interface",
"line": 8,
"children": [
{
"name": "port",
"kind": "property",
"line": 9,
"detail": "number"
},
{
"name": "host",
"kind": "property",
"line": 10,
"detail": "string"
}
]
}
]
ts_rename
在整个项目中重命名符号。这个工具 写入磁盘 --所有文件 包含该符号的内容会自动更新(失败时会回滚)。LSP 在应用编辑后重新同步。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
string | yes | 绝对文件路径 |
line |
number | yes | 行号(从1开始) |
column |
number | yes | 列号(从1开始) |
newName |
string | yes | 符号的新名称 |
tsconfig |
string | no | tsconfig.json的路径 |
请求示例:
{
"file": "/home/user/project/src/store.ts",
"line": 42,
"column": 14,
"newName": "repository"
}
示例响应:
{
"newName": "repository",
"totalEdits": 9,
"changes": [
{
"file": "/home/user/project/src/actions.ts",
"edits": 8,
"preview": "import { repository } from '@/lib/store';"
},
{
"file": "/home/user/project/src/store.ts",
"edits": 1,
"preview": "export const repository = new Store();"
}
]
}
ts_project_info
获取TypeScript项目配置信息。返回tsconfig路径和项目 根目录。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
tsconfig |
string | no | tsconfig.json的路径 |
cwd |
string | no | tsconfig发现的工作目录 |
请求示例:
{
"cwd": "/home/user/project"
}
示例响应:
{
"tsconfigPath": "/home/user/project/tsconfig.json",
"projectRoot": "/home/user/project"
}
工作流示例
编辑检查修复周期
编辑TypeScript文件后,使用 ts_diagnostics 为了检查类型错误,
修复它们,然后再次验证:
- 编辑文件(例如,更改函数签名)
- 呼叫
ts_diagnostics使用文件路径获取错误 - 修复报告的错误
- 呼叫
ts_diagnostics再次确认零误差
代码探索
使用符号导航不熟悉的代码,悬停并转到定义:
- 呼叫
ts_document_symbols获取文件结构的概览 - 呼叫
ts_hover从一个有趣的符号看它的类型签名 - 呼叫
ts_definition跳到符号的源定义
安全重构
一步即可在整个项目中重命名符号:
- 呼叫
ts_rename带有符号的位置和新名称 - 呼叫
ts_diagnostics对受影响的文件进行正确性验证
或者,对于手动重构,请先找到所有用法:
- 呼叫
ts_references在您想要更改的符号上 - 编辑所有返回的位置
- 呼叫
ts_diagnostics对受影响的文件进行正确性验证
环境变量
| 变量 | 描述 |
|---|---|
TYPESCRIPT_MCP_DEBUG |
设置为 1 启用详细调试日志记录(使用zap开发日志记录器) |
发展
构建
go build ./cmd/typescript-mcp
测试
go test ./...
在本地运行
# Ensure tsgo is available
tsgo --version
# Run the MCP server (communicates over stdio)
./typescript-mcp
项目结构
cmd/typescript-mcp/ Entry point and MCP server setup
internal/
lsp/ LSP client and tsgo process management
client.go JSON-RPC connection, LSP method wrappers
process.go tsgo process lifecycle (spawn, stop, resolve)
docsync/ Document synchronization with the LSP server
sync.go Open/change/close notifications
uri.go File path <-> URI conversion
tools/ MCP tool handlers
tools.go Tool registration (schemas and descriptions)
diagnostics.go ts_diagnostics handler
definition.go ts_definition handler
hover.go ts_hover handler
references.go ts_references handler
rename.go ts_rename handler (write tool)
symbols.go ts_document_symbols handler
project.go ts_project_info handler
util.go Shared utilities (readLine)
cmd/test-client/ CLI for manual testing against real projects
许可证
看 许可证 了解详情。