TypeScript提取器MCP服务器
用于提取TypeScript导入和方法体的MCP(模型上下文协议)服务器。此工具作为具有身份验证的HTTP服务器运行,并提供从TypeScript文件中提取特定方法/函数及其相关导入和类属性的工具。
特性
- 基于HTTP的MCP服务器:使用流式HTTP传输进行有状态通信
- 认证:通过客户端注册进行承载令牌身份验证
- 列表方法工具:发现TypeScript文件中的所有方法/函数
- 提取方法工具:提取方法及其依赖关系
- 读取文件工具:读取整个文件内容
- 智能导入过滤:仅返回实际使用的项目相关导入
- 类别属性提取:从方法的类和引用的类中提取属性
- 综合方法检测:处理类方法(实例和静态)、独立函数、箭头函数和接口/类型方法签名
- 纯文本输出:返回可读代码,导入、属性和方法体之间有明确的分隔
建筑
这是一个基于HTTP的MCP服务器,它:
- 在可配置端口上运行(默认值:4001)
- 需要客户端注册才能进行身份验证
- 使用承载令牌实现API安全
- 将令牌持久化
.mcp-tokens.json
安装
npm install
npm run build运行服务器
npm start服务器将在端口4001上启动。您可以自定义端口:
PORT=3000 npm start快速开始
1.启动服务器
npm start2.注册客户
curl -X POST http://localhost:4001/register \
-H "Content-Type: application/json" \
-d '{"clientId": "my-client"}'保存返回的令牌。
3.列出文件中的方法
curl -X POST http://localhost:4001/mcp \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "list_typescript_methods",
"arguments": {"filePath": "test-examples/sample.ts"}
}
}'4.提取方法
curl -X POST http://localhost:4001/mcp \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "extract_typescript_method",
"arguments": {
"filePath": "test-examples/sample.ts",
"methodName": "fetchUser"
}
}
}'API终点
| 端点 | 方法 | 身份验证 | 描述 |
|---|---|---|---|
/register | POST | 否 | 注册客户端并获取Bearer令牌 |
/mcp | POST | 承载 | 呼叫MCP工具 |
/mcp | GET | 承载 | SSE流媒体端点 |
可用工具
list_typescript_methods
列出TypeScript文件中的所有方法和函数名。
参数:
filePath(string,必填):TypeScript文件的路径
例子:
curl -X POST http://localhost:4001/mcp \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "list_typescript_methods",
"arguments": {"filePath": "src/app.ts"}
}
}'extract_typescript_method
提取一个方法/函数及其相关的导入和类属性。
参数:
filePath(string,必填):TypeScript文件的路径methodName(string,必填):要提取的方法/函数的名称
例子:
curl -X POST http://localhost:4001/mcp \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "extract_typescript_method",
"arguments": {
"filePath": "src/app.ts",
"methodName": "handleRequest"
}
}
}'输出格式:
Method Name: handleRequest
File Path: src/app.ts
Line Number: 45
Imports:
import { helper } from './utils';
Properties/Constants:
private config: Config;
static MAX_RETRIES = 3;
Method Body:
async handleRequest(req: Request): Promise {
// method body
}read_file
读取并返回文件的全部内容。
参数:
filePath(string,必填):文件路径
例子:
curl -X POST http://localhost:4001/mcp \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "read_file",
"arguments": {"filePath": "src/app.ts"}
}
}'输出格式:
[Full file contents]支持的TypeScript构造
- 类方法(实例方法)
- 静态类方法
- 独立函数声明
- 分配给变量的箭头函数
- 分配给变量的函数表达式
- 接口方法签名
- 类型别名方法签名
发展
# Watch mode for development
npm run watch
# Build for production
npm run build
# Run tests
npm test运作原理
- 解析:用途
ts-morph将TypeScript文件解析为AST - 定位:在所有支持的构造中查找请求的方法
- 分析:标识方法体中使用的所有标识符
- 筛选器导入:仅返回与项目相关的导入(
./,../,src/)使用 - 提取属性:对于类方法,从类和引用的类中提取属性
- 格式:返回包含导入、属性和方法体的结构化输出
用例
- 学习与探索:快速了解具体方法的工作原理及其所依赖的内容
- 代码审查:提取方法及其依赖关系,以便进行重点审查
- 文档:生成示例,显示具有所需导入的方法实现
- 重构:在将代码移动到不同文件之前,请查看确切的依赖关系
- 代码导航:列出文件中的所有方法以了解其结构
项目结构
context-tree/
├── src/
│ ├── index.ts # MCP HTTP server
│ └── extractor.ts # TypeScript extraction logic
├── test-examples/
│ ├── sample.ts # Example TypeScript file
│ └── test-extractor.ts # Test script
├── build/ # Compiled JavaScript
├── package.json
├── tsconfig.json
└── .mcp-tokens.json # Stored authentication tokens依赖项
@modelcontextprotocol/sdk:MCP协议实施ts-morph:TypeScript AST操作express:Web服务器zod:架构验证
文档
QUICKSTART.md-几分钟内起床跑步USAGE.md-带示例的详细使用指南PROJECT_SUMMARY.md-架构和技术细节
许可证
麻省理工学院
