ts mcp服务器
 ](https://www.npmjs.com/package/ts-mcp-server) ](https://www.npmjs.com/package/ts-mcp-server) 
轻量级 模型上下文协议 (MCP)服务器 TypeScript和JavaScript重构和代码智能每个工具都直接映射到 tsserver protocol命令——输出是TypeScript编译器未经修改的原始响应。重命名符号、提取函数、在文件之间移动声明、重新组织导入、导航类型层次结构、探索调用图、在工作区中搜索符号、将AI生成的代码映射到正确的位置、发现哪些错误代码具有自动修复功能等等——每一个 import, require,重新导出,并在整个代码库中自动更新引用。
为什么
AI编码助手可以读写代码,但他们很难做到 结构性变化 这会波及许多文件。重命名函数、提取助手、移动React组件或重新组织文件夹意味着更新每个涉及它的引用和导入。错过一个,构建就会中断。
ts-mcp-server 提供任何兼容MCP的客户端-- VS代码副本, 克劳德桌面, 光标, 帆板运动, 继续,以及其他——执行这些重构的能力 正确和完全,使用TypeScript自己的编译器基础架构。
特性
- 40工具 --每个都以1:1的比例映射到本地
tsserver协议命令
重构(14个工具)
- 重命名符号 --变量、函数、类、类型、属性、接口、枚举——每个文件中的所有引用都会更新
- 重命名/移动文件和文件夹 --所有导入路径自动更新
- 提取功能 --将代码范围提取到具有自动检测参数和返回类型的新函数中
- 提取常量 --将表达式提取到具有推断类型的命名常量中
- 提取类型 --将内联类型注释提取到命名类型别名中
- 推断返回类型 --向函数添加显式返回类型注释,由TypeScript推断
- 移动符号 --将顶级声明移动到另一个文件,所有导入都会自动重新连接
- 内联变量 --将所有引用替换为变量的初始值设定项并删除声明
- 组织导入 --排序、合并和删除未使用的导入
- 格式 --根据TypeScript的格式化规则格式化一系列代码
- 获取代码修复 --检索特定诊断的可用自动修复程序(缺少导入、类型不匹配等)
- 获取组合代码修复 --对文件中的特定错误代码应用全部修复操作
- 获取诊断信息 --检索任何文件的类型错误、警告和建议
- 查找所有引用 --在整个项目中查找符号的每次使用
- 地图代码 --将AI生成的代码片段映射到文件中,用名称替换匹配的声明或附加新的声明
- 获取支持的代码修复 --列出具有可用自动修复的每个错误代码,可选地将其范围限定在项目中
代码智能(24种工具)
- 快捷信息 --任何符号的完整类型信息、文档和JSDoc标签(悬停信息)
- 导航树 --文件的完整层次结构(所有声明及其嵌套)
- 转到定义 --跳转到声明符号的位置
- 定义和界限跨度 --类似定义,但也返回查询符号的文本跨度
- 查找源定义 --导航到实际的TypeScript源代码,而不是
.d.ts申报文件 - 转到类型定义 --跳转到类型的定义,而不是变量的声明
- 转到实施 --查找接口或抽象类的具体实现
- 导航到符号 --按名称搜索工作区范围内的符号
- 文件引用 --查找导入给定文件的每个文件(反向依赖关系图)
- 准备呼叫层次结构 --获取函数/方法的调用层次结构入口点
- 来电 --查找函数的所有调用者(“谁调用这个?”)
- 呼出电话 --查找函数的所有被调用者(“这调用什么?”)
- 项目信息 --获取tsconfig.json路径、文件列表和语言服务状态
- 完工信息 --在某个位置自动完成建议
- 完成条目详细信息 --完成项目的完整文件和类型签名
- 签名帮助 --调用站点的函数参数信息和重载
- 文档亮点 --文件中出现的所有符号,具有读/写区别
- 获取适用的重构工具 --发现某个位置或选择中可用的重构
- 选择范围 --为智能扩展/收缩选择获取语义上有意义的选择范围
- 转到重构建议 --移动符号时获取建议的目标文件
- 文档注释模板 --为函数/方法生成JSDoc注释模板
- 轮廓跨度 --获取文件中的可折叠区域
- 镶嵌提示 --获取一个范围的镶嵌提示(参数名称、推断类型)
- TODO评论 --在文件中查找所有TODO/FIXME/HACK注释
设计原则
- 纯tsserver输出 --每个工具都会返回未经修改的原始数据
tsserver响应为JSON - 预览模式 --在应用任何东西之前,先看看会发生什么变化
- 自动项目发现 —
tsconfig.json自动检测;无需配置 - 多项目支持 --monorepos、项目引用和复合构建开箱即用
- 跨平台 --Windows、macOS和Linux
运作原理
在引擎盖下, ts-mcp-server 与TypeScript通信 tsserver 通过节点IPC——与VS Code使用的协议相同。每个工具都是一个薄薄的包装:
- 将您的输入直接传递给
tsserver协议命令 - 返回原始响应--无格式、无分组、无筛选
重构工具:
| 工具 | tsserver命令 |
|---|---|
rename | rename-full → renameLocations-full |
renameFileOrDirectory | getEditsForFileRename-full |
references | references |
getDiagnostics | semanticDiagnosticsSync + suggestionDiagnosticsSync |
organizeImports | organizeImports-full |
getCodeFixes | getCodeFixes |
extractFunction | getEditsForRefactor-full |
extractConstant | getEditsForRefactor-full |
extractType | getEditsForRefactor-full |
inferReturnType | getEditsForRefactor-full |
moveSymbol | getEditsForRefactor-full |
inlineVariable | getEditsForRefactor-full |
format | format |
mapCode | mapCode |
getSupportedCodeFixes | getSupportedCodeFixes |
代码智能工具:
| 工具 | tsserver命令 |
|---|---|
quickinfo | quickinfo |
navtree | navtree |
definition | definition |
typeDefinition | typeDefinition |
implementation | implementation |
navto | navto |
fileReferences | fileReferences |
prepareCallHierarchy | prepareCallHierarchy |
provideCallHierarchyIncomingCalls | provideCallHierarchyIncomingCalls |
provideCallHierarchyOutgoingCalls | provideCallHierarchyOutgoingCalls |
projectInfo | projectInfo |
completionInfo | completionInfo |
completionEntryDetails | completionEntryDetails |
signatureHelp | signatureHelp |
documentHighlights | documentHighlights |
getApplicableRefactors | getApplicableRefactors |
getCombinedCodeFix | getCombinedCodeFix |
getOutliningSpans | getOutliningSpans |
todoComments | todoComments |
docCommentTemplate | docCommentTemplate |
provideInlayHints | provideInlayHints |
definitionAndBoundSpan | definitionAndBoundSpan |
findSourceDefinition | findSourceDefinition |
selectionRange | selectionRange |
getMoveToRefactoringFileSuggestions | getMoveToRefactoringFileSuggestions |
没有正则表达式,没有自定义路径解析,没有启发式,没有输出格式。TypeScript编译器完成了所有的工作。
快速开始
安装
npx ts-mcp-server配置您的MCP客户端
添加 ts-mcp-server 根据客户的MCP配置。
VS Code (.vscode/mcp.json):
{
"servers": {
"ts-mcp-server": {
"command": "npx",
"args": ["ts-mcp-server"]
}
}
}克劳德桌面 (claude_desktop_config.json):
{
"mcpServers": {
"ts-mcp-server": {
"command": "npx",
"args": ["ts-mcp-server"]
}
}
}光标,风帆,继续 --使用相同的方法遵循每个客户端的MCP服务器文档 npx ts-mcp-server 命令。
禁用单个工具
通过将其名称设置为,可以单独禁用每个工具 "false" 在 env MCP配置的块。默认情况下,工具处于启用状态;仅将工具明确设置为 "false" 在启动时跳过。
VS Code (.vscode/mcp.json):
{
"servers": {
"ts-mcp-server": {
"command": "npx",
"args": ["ts-mcp-server"],
"env": {
"todoComments": "false",
"getOutliningSpans": "false",
"docCommentTemplate": "false"
}
}
}
}克劳德桌面 (claude_desktop_config.json):
{
"mcpServers": {
"ts-mcp-server": {
"command": "npx",
"args": ["ts-mcp-server"],
"env": {
"todoComments": "false",
"getOutliningSpans": "false",
"docCommentTemplate": "false"
}
}
}
}中的工具名称 env 必须与中列出的工具名称完全匹配 工具参考 下面(例如。, "quickinfo", "getDiagnostics", "extractFunction").任何其他值(包括完全省略键)都会使该工具处于启用状态。
工具参考
rename
重命名Types/JavaScript符号并更新整个项目中的所有引用。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 包含符号的文件路径(绝对或相对于cwd) |
line | number | ✅ | 符号出现的从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
newName | string | ✅ | 符号的新名称 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
rename file="src/utils/helpers.ts" line=5 offset=17 newName="formatCurrency"
rename file="src/components/Button.tsx" line=10 offset=17 newName="PrimaryButton"
rename file="src/types.ts" line=3 offset=11 newName="UserProfile"
rename file="src/utils/helpers.ts" line=5 offset=17 newName="formatCurrency" preview=true______________________________________________________________________
renameFileOrDirectory
重命名或移动TypeScript/JavaScript文件或目录,并更新整个项目中的所有导入路径。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
from | string | ✅ | 当前文件或目录路径(相对于cwd的绝对或相对路径) |
to | string | ✅ | 新文件或目录路径(相对于cwd的绝对或相对路径) |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
renameFileOrDirectory from="src/utils/helpers.ts" to="src/utils/string-helpers.ts"
renameFileOrDirectory from="src/Button.tsx" to="src/components/ui/Button.tsx"
renameFileOrDirectory from="src/components/primitives" to="src/components/ui"
renameFileOrDirectory from="src/old-name.ts" to="src/new-name.ts" preview=true______________________________________________________________________
references
查找项目中符号的所有用法。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 符号出现的从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
references file="src/utils/helpers.ts" line=5 offset=17
references file="src/types.ts" line=3 offset=11______________________________________________________________________
getDiagnostics
获取文件的所有错误、警告和建议。将语义诊断和建议诊断作为单独的数组返回。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
示例:
getDiagnostics file="src/utils/helpers.ts"
getDiagnostics file="src/components/Button.tsx"注: 未使用的代码诊断(未使用的变量、未使用的导入)仅在以下情况下显示tsconfig.json有noUnusedLocals和noUnusedParameters启用。
______________________________________________________________________
organizeImports
对文件中未使用的导入进行排序、合并和删除。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
organizeImports file="src/utils/helpers.ts"
organizeImports file="src/components/Button.tsx" preview=true______________________________________________________________________
getCodeFixes
获取文件中某个范围内特定错误代码的可用代码修复。使用 getDiagnostics 首先发现错误代码和范围,然后将其传递到此处。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 基于1的诊断范围起始线 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 基于1的诊断范围终点线 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
errorCodes | number[] | ✅ | 用于修复的诊断错误代码 |
示例:
# Get fixes for a "Cannot find name" error (code 2304) at line 10
getCodeFixes file="src/app.ts" startLine=10 startOffset=1 endLine=10 endOffset=20 errorCodes=[2304]
# Get fixes for multiple error codes
getCodeFixes file="src/app.ts" startLine=5 startOffset=1 endLine=5 endOffset=30 errorCodes=[2304, 2552]______________________________________________________________________
getCombinedCodeFix
获取一个组合代码修复,在一个操作中跨文件应用修复的所有实例。返回完整的文件编辑集作为 CombinedCodeActions 回应。使用 getCodeFixes 首先发现可用 fixId 值,然后传递 fixId 在这里获取整个文件的组合修复。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
fixId | string | ✅ | 来自代码修复的fixId(例如。, "fixMissingImport", "unusedIdentifier", "inferFromUsage") |
示例:
# Get the combined "add all missing imports" fix for a file
getCombinedCodeFix file="src/app.ts" fixId="fixMissingImport"
# Get the combined "remove all unused variables" fix for a file
getCombinedCodeFix file="src/app.ts" fixId="unusedIdentifier"______________________________________________________________________
extractFunction
将选定的代码范围提取到新函数中。TypeScript自动检测参数和返回类型。回应包括 renameFilename / renameLocation 所以你可以跟进 rename 给函数起一个有意义的名字。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 从1开始选择 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 基于1的选择结束行 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Extract lines 10-15 into a function
extractFunction file="src/app.ts" startLine=10 startOffset=1 endLine=15 endOffset=1
# Preview the extraction
extractFunction file="src/app.ts" startLine=10 startOffset=1 endLine=15 endOffset=1 preview=true______________________________________________________________________
extractConstant
将所选表达式提取到命名常量中。TypeScript推断类型。回应包括 renameFilename / renameLocation 所以你可以跟进 rename 给常量一个有意义的名称。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 表达式的从1开始的起始行 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 表达式的1基结束行 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Extract an expression into a constant
extractConstant file="src/app.ts" startLine=8 startOffset=12 endLine=8 endOffset=35
# Preview the extraction
extractConstant file="src/app.ts" startLine=8 startOffset=12 endLine=8 endOffset=35 preview=true______________________________________________________________________
moveSymbol
将顶级声明(函数、类、类型、常量)移动到另一个文件。整个项目中的所有导入都会自动重新连接。如果目标文件不存在,tsserver会创建它。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 源文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 声明的起始行从1开始 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 声明的基于1的结束行 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
targetFile | string | ✅ | 目标文件路径(绝对或相对于cwd) |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Move a function to a utility file
moveSymbol file="src/app.ts" startLine=20 startOffset=1 endLine=35 endOffset=2 targetFile="src/utils/helpers.ts"
# Move a type to a shared types file
moveSymbol file="src/components/Button.tsx" startLine=1 startOffset=1 endLine=5 endOffset=2 targetFile="src/types.ts"
# Preview the move
moveSymbol file="src/app.ts" startLine=20 startOffset=1 endLine=35 endOffset=2 targetFile="src/utils/helpers.ts" preview=true______________________________________________________________________
inlineVariable
内联一个变量——用变量的初始化器替换所有引用并删除声明。位置必须位于其声明或任何用法中的变量名上。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 变量的从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Inline a variable
inlineVariable file="src/app.ts" line=12 offset=7
# Preview the inlining
inlineVariable file="src/app.ts" line=12 offset=7 preview=true______________________________________________________________________
extractType
将内联类型注释提取到命名类型别名中。选择要提取的类型跨度。回应包括 renameFilename / renameLocation 所以你可以跟进 rename 为该类型赋予一个有意义的名称。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | span类型的基于1的起始行 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | span类型的基于1的端线 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Extract an inline object type into a type alias
# Given: function process(user: { id: number; name: string }) { ... }
# Select the span "{ id: number; name: string }"
extractType file="src/app.ts" startLine=5 startOffset=26 endLine=5 endOffset=56
# Preview the extraction
extractType file="src/app.ts" startLine=5 startOffset=26 endLine=5 endOffset=56 preview=true______________________________________________________________________
inferReturnType
向函数添加一个显式的返回类型注释,由TypeScript推断。位置必须位于函数名或声明关键字上(function, async,箭头函数变量名)。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 函数的从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
# Add return type to a function that currently has none
# Given: function greet(name: string) { return `Hello, ${name}!`; }
# After: function greet(name: string): string { return `Hello, ${name}!`; }
inferReturnType file="src/app.ts" line=10 offset=10
# Preview the change
inferReturnType file="src/app.ts" line=10 offset=10 preview=truequickinfo
获取给定位置符号的完整类型信息、文档和JSDoc标签。这是“悬停”信息。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
quickinfo file="src/utils/helpers.ts" line=5 offset=17
quickinfo file="src/types.ts" line=3 offset=11______________________________________________________________________
navtree
获取文件的完整层次结构——所有类、函数、变量、接口、类型别名、枚举及其嵌套。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
示例:
navtree file="src/utils/helpers.ts"
navtree file="src/components/Button.tsx"______________________________________________________________________
definition
转到符号的定义。返回声明符号的文件位置。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
definition file="src/app.ts" line=10 offset=5
definition file="src/components/Button.tsx" line=3 offset=15______________________________________________________________________
typeDefinition
导航到类型的定义,而不是变量的声明。给定 const user: UserProfile = ..., definition 转到变量,但是 typeDefinition 转到 UserProfile 界面。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
typeDefinition file="src/app.ts" line=10 offset=12
typeDefinition file="src/services/api.ts" line=5 offset=8______________________________________________________________________
implementation
查找接口或抽象类的具体实现。给定一个接口 Serializable,返回实现它的每个类。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
implementation file="src/types.ts" line=1 offset=18
implementation file="src/interfaces/repository.ts" line=3 offset=18______________________________________________________________________
navto
按名称搜索工作区范围内的符号。获取搜索字符串,并返回所有项目文件中的匹配符号及其位置和类型。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
searchValue | string | ✅ | 要搜索的符号名称或前缀 |
file | string | -- | 项目上下文的可选文件(绝对或相对于cwd) |
maxResultCount | number | -- | 要返回的最大结果数 |
currentFileOnly | boolean | -- | 如果 true,仅搜索指定的文件 |
示例:
navto searchValue="User" file="src/app.ts"
navto searchValue="handle" file="src/app.ts" maxResultCount=10
navto searchValue="Button" file="src/components/Button.tsx" currentFileOnly=true______________________________________________________________________
fileReferences
查找导入或引用给定文件的每个文件。单个文件的反向依赖关系图。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
示例:
fileReferences file="src/utils/helpers.ts"
fileReferences file="src/types.ts"______________________________________________________________________
prepareCallHierarchy
在某个位置获取调用层次结构项——调用层次结构查询的入口点。返回函数/方法名称、种类、文件位置和跨度。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
prepareCallHierarchy file="src/services/api.ts" line=10 offset=17
prepareCallHierarchy file="src/utils/helpers.ts" line=5 offset=17______________________________________________________________________
provideCallHierarchyIncomingCalls
查找在给定位置调用函数的所有函数/方法。回答“谁叫这个?”
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
provideCallHierarchyIncomingCalls file="src/services/api.ts" line=10 offset=17
provideCallHierarchyIncomingCalls file="src/utils/helpers.ts" line=5 offset=17______________________________________________________________________
provideCallHierarchyOutgoingCalls
查找给定位置的函数调用的所有函数/方法。回答“这叫什么?”
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
provideCallHierarchyOutgoingCalls file="src/services/api.ts" line=10 offset=17
provideCallHierarchyOutgoingCalls file="src/utils/helpers.ts" line=5 offset=17______________________________________________________________________
projectInfo
获取tsconfig.json路径、项目中文件的完整列表以及语言服务是否处于活动状态。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
needFileNameList | boolean | -- | 如果 true,包括项目中所有文件的列表(默认值: true) |
示例:
projectInfo file="src/app.ts"
projectInfo file="src/app.ts" needFileNameList=false______________________________________________________________________
completionInfo
在某个位置获取自动完成建议。返回所有可能的补全及其类型、排序文本和插入文本。有助于了解某个位置可用的符号、方法或属性。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
prefix | string | -- | 筛选完成项的可选前缀 |
triggerCharacter | string | -- | 触发完成的字符(例如。, ., ", ', ` `, /, @, <, #, `) |
示例:
completionInfo file="src/app.ts" line=10 offset=15
completionInfo file="src/app.ts" line=10 offset=15 prefix="get"
completionInfo file="src/app.ts" line=10 offset=15 triggerCharacter="."______________________________________________________________________
completionEntryDetails
获取特定完成项的完整详细信息——文档、完整类型签名、JSDoc标签和代码操作(如自动导入)。用作后续 completionInfo.
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
entryNames | string[] | ✅ | 获取详细信息的完成条目名称 |
示例:
completionEntryDetails file="src/app.ts" line=10 offset=15 entryNames=["map","filter"]
completionEntryDetails file="src/app.ts" line=5 offset=10 entryNames=["useState"]______________________________________________________________________
signatureHelp
在调用站点获取函数/方法签名信息。返回每个重载的参数名称、类型和文档。当光标位于函数调用括号内时使用。
| 参数 | 类型 | 必填 | 说明 | ||
|---|---|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) | ||
line | number | ✅ | 从1开始的行号 | ||
offset | number | ✅ | 基于1的字符偏移量(在函数调用括号内) | ||
triggerReason | object | -- | 可选: `{ kind: "invoked" \ | "retrigger" \ | "characterTyped", triggerCharacter?: string }` |
示例:
signatureHelp file="src/app.ts" line=12 offset=20
signatureHelp file="src/app.ts" line=12 offset=20 triggerReason={"kind":"invoked"}______________________________________________________________________
documentHighlights
查找文件(或文件集)中出现的所有符号。区分读和写引用。比 references 当你只需要本地事件时。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
filesToSearch | string[] | -- | 可选:将搜索限制为这些文件 |
示例:
documentHighlights file="src/app.ts" line=10 offset=5
documentHighlights file="src/app.ts" line=10 offset=5 filesToSearch=["src/app.ts","src/utils.ts"]______________________________________________________________________
getApplicableRefactors
了解某个位置或选择中可用的重构。在尝试重构之前使用,看看有什么可能。返回可用重构器的列表及其操作名称和描述。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 从1开始选择 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 基于1的选择结束行 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
triggerReason | string | — | "invoked" 或 "implicit" |
示例:
getApplicableRefactors file="src/app.ts" startLine=10 startOffset=1 endLine=15 endOffset=1
getApplicableRefactors file="src/app.ts" startLine=8 startOffset=12 endLine=8 endOffset=35______________________________________________________________________
docCommentTemplate
为某个位置的函数、方法或类生成JSDoc注释模板。返回模板文本 @param, @returns等等。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
docCommentTemplate file="src/utils/helpers.ts" line=10 offset=1
docCommentTemplate file="src/services/api.ts" line=25 offset=10______________________________________________________________________
getOutliningSpans
获取文件的代码折叠区域。返回代码块的层次结构,包括其种类(注释、区域、代码、导入)。有助于理解文件结构和复杂性。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
示例:
getOutliningSpans file="src/app.ts"
getOutliningSpans file="src/components/Button.tsx"______________________________________________________________________
provideInlayHints
获取范围的嵌体提示(内联类型注释)。显示推断类型、调用站点的参数名称和返回类型。有助于理解TypeScript在没有显式类型注释的情况下推断什么。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
start | number | ✅ | 起始偏移量(基于0的字符位置) |
length | number | ✅ | 范围长度(字符) |
示例:
# Get inlay hints for the first 1000 characters of a file
provideInlayHints file="src/app.ts" start=0 length=1000
# Get inlay hints for a specific range
provideInlayHints file="src/utils/helpers.ts" start=500 length=200______________________________________________________________________
todoComments
在文件中查找所有TODO、FIXME、HACK和其他配置的注释标记。返回每个匹配注释的位置和文本。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
descriptors | {text: string, priority: number}[] | ✅ | 要搜索的评论标记数组(例如TODO、FIXME) |
示例:
# Find all TODO and FIXME comments
todoComments file="src/app.ts" descriptors=[{"text":"TODO","priority":1},{"text":"FIXME","priority":0}]
# Find TODO, FIXME, and HACK comments
todoComments file="src/app.ts" descriptors=[{"text":"TODO","priority":2},{"text":"FIXME","priority":1},{"text":"HACK","priority":0}]______________________________________________________________________
definitionAndBoundSpan
喜欢 definition,而且还返回所查询符号的文本跨度。有助于准确理解哪些字符构成符号。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
definitionAndBoundSpan file="src/app.ts" line=10 offset=5
definitionAndBoundSpan file="src/types.ts" line=3 offset=11______________________________________________________________________
findSourceDefinition
导航到实际的TypeScript源代码,而不是 .d.ts 声明文件。当使用具有源映射的库时,或者当您想查看实现而不仅仅是类型声明时,这很有用。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的行号 |
offset | number | ✅ | 行上基于1的字符偏移 |
示例:
findSourceDefinition file="src/app.ts" line=10 offset=5
findSourceDefinition file="src/services/api.ts" line=3 offset=15______________________________________________________________________
selectionRange
获取语义上有意义的选择范围,以便进行智能扩展/收缩选择。返回表示逐渐增大的语法构造(表达式→ 声明→ 块→ 功能)。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
locations | {line: number, offset: number}[] | ✅ | 用于获取选择范围的位置数组 |
示例:
selectionRange file="src/app.ts" locations=[{"line":10,"offset":5}]
selectionRange file="src/app.ts" locations=[{"line":10,"offset":5},{"line":20,"offset":10}]______________________________________________________________________
format
根据TypeScript的格式化规则格式化一系列代码。应用一致的缩进、间距和换行。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
line | number | ✅ | 从1开始的范围起始线 |
offset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 基于1的范围结束线 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
options | object | -- | 格式选项(tabSize、indentSize等) |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
示例:
format file="src/app.ts" line=1 offset=1 endLine=50 endOffset=1
format file="src/app.ts" line=10 offset=1 endLine=20 endOffset=1 preview=true
format file="src/app.ts" line=1 offset=1 endLine=100 endOffset=1 options={"tabSize":4}______________________________________________________________________
getMoveToRefactoringFileSuggestions
将符号移动到另一个文件时,获取建议的目标文件。返回建议的新文件名和作为良好目标的现有文件。以前用这个 moveSymbol 选择最佳目标位置。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
startLine | number | ✅ | 声明的起始行从1开始 |
startOffset | number | ✅ | 基于1的起始字符偏移 |
endLine | number | ✅ | 声明的基于1的结束行 |
endOffset | number | ✅ | 基于1的结束字符偏移 |
示例:
getMoveToRefactoringFileSuggestions file="src/app.ts" startLine=20 startOffset=1 endLine=35 endOffset=2
getMoveToRefactoringFileSuggestions file="src/components/Button.tsx" startLine=1 startOffset=1 endLine=5 endOffset=2______________________________________________________________________
getSupportedCodeFixes
返回具有可用自动修复的所有错误代码的列表。在调用之前将其用作发现工具 getCodeFixes -它告诉您tsserver可以修复哪些错误代码。可以选择将查询范围限定到特定文件的项目。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | -- | 可选文件路径(相对于cwd的绝对或相对路径)。如果提供,则范围为文件的项目。 |
示例:
# Get all fixable error codes globally
getSupportedCodeFixes
# Get fixable error codes scoped to a specific project
getSupportedCodeFixes file="src/app.ts"______________________________________________________________________
mapCode
将AI生成的代码片段映射到一个文件中,用名称替换匹配的声明或附加新的声明。专为AI代码生成工作流程而设计,您可以在不重复声明的情况下将新代码合并到现有文件中。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file | string | ✅ | 文件路径(绝对或相对于cwd) |
contents | string[] | ✅ | 要映射到文件中的代码片段。每个都是独立解析的。函数和类按名称匹配。 |
focusLocations | object[][] | -- | 嵌套数组 {start, end} span(基于1的行/偏移量)用于启用基于名称的匹配。若无此项,代码将始终附加至文件末尾。 |
preview | boolean | ✅ | 如果 true,返回更改而不应用 |
匹配是如何工作的:
- 没有
focusLocations→ 代码始终附加到文件末尾(未尝试匹配) - 随着
focusLocations→ TypeScript在指向的作用域中搜索具有匹配名称的声明 - 匹配工作适用于: 函数、类、方法、接口(具有
.name财产) - 匹配不适用于:
const/let/var声明(VariableStatement没有.name) - 找到匹配项后,将替换从第一个匹配语句到最后一个匹配语句的范围
- 当未找到匹配项时,代码将附加到作用域的末尾
限制:
- 与多人通话
contents条目只应用第一个匹配项——每个声明调用一次以替换多个 const/let/var不支持替换;改用标准文件编辑
示例:
# Replace an existing function (focusLocations enables name-based matching)
mapCode file="src/utils.ts" contents=["export function add(a: number, b: number, c = 0) { return a + b + c; }"] focusLocations=[[{"start":{"line":1,"offset":1},"end":{"line":1,"offset":1}}]]
# Append a new function (no focusLocations — always appends)
mapCode file="src/utils.ts" contents=["export function multiply(a: number, b: number) { return a * b; }"]
# Preview before applying
mapCode file="src/utils.ts" contents=["export function add(a: number, b: number) { return a + b; }"] focusLocations=[[{"start":{"line":1,"offset":1},"end":{"line":1,"offset":1}}]] preview=true支持的语言和框架
ts-mcp-server 适用于TypeScript语言服务理解的任何项目:
- TypeScript (
.ts,.tsx,.mts,.cts) - JavaScript (
.js,.jsx,.mjs,.cjs) - 反应 / Next.js / 混音 / 星的
- 视图 (脚本块)
- Node.js / 快速 / 快车 / NestJS
- Angular
- 斯维尔特 (脚本块)
- 电子
- React Native
- 单体仓库 (Turborepo、Nx、Lerna、pnpm工作区)
如果你的项目有 tsconfig.json (或 jsconfig.json),它工作。
系统要求
| 要求 | 版本 |
|---|---|
| Node.js | 22或更高版本(当前LTS) |
| TypeScript | 6.x(作为依赖项自动安装) |
| 操作系统 | Windows、macOS、Linux |
不需要额外的依赖关系或全局工具。服务器捆绑了它需要的一切。
常见问题解答
它在没有 tsconfig.json? 对。TypeScript会创建一个推断的项目,但显式配置会给出更好的结果。
它更新了吗 package.json 还是非代码文件? 否。它更新Types/JavaScript导入和导出语句,以及中与路径相关的条目 tsconfig.json (files, include, exclude, paths).
我可以在纯JavaScript项目中使用它吗? 对。添加一个 jsconfig.json (相当于 tsconfig.json 和 allowJs: true)服务器将发现您的项目。
它是否适用于路径别名(@/components/...)? 对。 tsserver 解析中定义的路径别名 tsconfig.jsons paths 和 baseUrl 设置。
输出是否被修改或格式化? 不是的。每个工具都会返回未经修改的原始数据 tsserver 响应序列化为JSON。没有任何内容被截断、简化、分组或过滤。
