代码图工具

一个TypeScript代码图工具,通过AST分析提取代码结构并构建可查询的图数据库,支持AI编程代理的语义搜索和图形遍历功能。

  • by couzic
  • TypeScript
  • 本地部署
  • 8 星标
  • 更新于
在 GitHub 查看 couzic/ts-graph-mcp

使用说明

ts图mcp

一个TypeScript代码图工具,将代码结构提取到可查询的 数据库,与用于AI编码代理的MCP集成。

它的作用

ts图使用AST分析解析TypeScript源代码并构建图 代码库结构的数据库。该图捕获了代码符号(函数, 类、接口、类型、变量)及其关系(调用、导入、, 类型使用、继承)。

包括语义搜索。 首次运行时,ts图下载一个嵌入 模型(~30MB),并为所有符号生成嵌入。AI代理可以通过以下方式进行搜索 概念(“用户验证”、“数据库查询”)不仅仅是精确的符号名称。

AI代理通过以下方式查询图形 searchGraph MCP工具用于:

  • 按概念查找代码(语义搜索)
  • 遍历调用图(谁称之为?这叫什么?)
  • 查找符号之间的路径

需求

Node.js 24或更高版本。存储使用内置 node:sqlite 模块,所以 无需编译——任何平台上都没有构建工具。

快速开始

1.配置

创建 ts-graph-mcp.config.json 在项目根目录中:

{
  "packages": [
    { "name": "main", "tsconfig": "./tsconfig.json" }
  ],
  "server": {
    "port": 4000
  }
}

对于monorepos,列出多个包:

{
  "packages": [
    { "name": "shared", "tsconfig": "./shared/tsconfig.json" },
    { "name": "frontend", "tsconfig": "./frontend/tsconfig.json" },
    { "name": "backend", "tsconfig": "./backend/tsconfig.json" }
  ],
  "server": {
    "port": 4000
  }
}

2.启动HTTP服务器

npx ts-graph-mcp

服务器在第一次运行时为您的项目建立索引并监视更改。

3.配置克劳德代码

claude mcp add ts-graph-mcp -- npx -y ts-graph-mcp --mcp

或手动输入 .mcp.json:

{
  "mcpServers": {
    "ts-graph-mcp": {
      "command": "npx",
      "args": ["-y", "ts-graph-mcp", "--mcp"]
    }
  }
}

注: 首先启动HTTP服务器。MCP包装器连接到它。

MCP工具:searchGraph

将语义搜索与图遍历相结合的统一搜索。

查询模式

// Find code by concept (semantic search)
{ topic: "user authentication" }

// What does handleRequest call? (forward traversal)
{ from: { symbol: "handleRequest" } }

// Who calls saveUser? (backward traversal)
{ to: { symbol: "saveUser" } }

// How does A reach B? (path finding)
{ from: { symbol: "handleRequest" }, to: { symbol: "saveUser" } }

参数

参数 必填 说明
topic 否* 独立语义搜索(不可与 from/to)
from 否* 起点: { symbol } 或 { query } 可选 file_path
to 否* 终点: { symbol } 或 { query } 可选 file_path
max_nodes 否 输出限制(默认值:50)

*至少一个 topic, from,或 to 是必需的。 topic 是独立的 只是——它不能与 from/to.

输出示例

## Symbols matching "validation" (semantic search)

validateInput (Function) - src/validation.ts [score: 0.847]
checkUserData (Function) - src/user.ts [score: 0.721]
## Graph

handleRequest --CALLS--> validate --CALLS--> saveUser

## Nodes

validate:
  type: Function
  file: src/service.ts
  offset: 10, limit: 5
  snippet:
    10: export function validate(data: Input) {
  > 11:   return saveUser(data);
    12: }

CLI选项

ts-graph-mcp              # Start HTTP server
ts-graph-mcp --mcp        # Start MCP stdio server
ts-graph-mcp --reindex    # Force clean reindex

配置参考

必需的

字段 描述
packages 数组 { name, tsconfig }
server.port HTTP服务器端口(无默认值)

可选的

字段 描述 默认值
embedding.enabled 启用语义搜索 true
embedding.preset 嵌入模型 "nomic-embed-text-v1.5"
storage.type 数据库类型 "sqlite"
storage.path 数据库文件路径 .ts-graph-mcp/graph.db
watch.debounce 启用取消公告 true
watch.debounceInterval 去抖动延迟(ms) 300
watch.polling 使用轮询(针对Docker/WSL2) false
watch.pollingInterval 轮询间隔(ms) 1000
watch.excludeDirectories 要跳过的目录 []
watch.silent 禁止重新索引日志 false

嵌入模型

预设 大小 尺寸 注释
nomic-embed-text-v1.5 ~30MB 768 默认,快速有效
qwen3-0.6b ~650MB 1024 质量更高,速度更慢
qwen3-4b ~4GB 2560 最高质量,需要更多RAM
jina-embeddings-v2-base-code ~30MB 768 针对代码进行了优化

加 .ts-graph-mcp/ 对你 .gitignore.

纱线PnP支持

ts图适用于Yarn 4-PnP单仓库。当 .pnp.cjs 检测到,模块 分辨率使用Yarn的PnP API。

要求:

  • 使用基本包导入(@libs/utils,不 @libs/utils/date)
  • 声明依赖关系 workspace:* 协议

支持的类型

节点: 函数、类、方法、接口、TypeAlias、变量、综合类型、特征、规范、测试套件、测试

边缘: 调用、实现、扩展、获取、返回、HAS_TYPE、HAS_PROPERTY、derive_FROM、ALIAS_FOR、引用、包含、指定、验证BY

发展

npm run check        # Run tests, build, and lint
npm test             # Run tests
npm run build        # Compile TypeScript

项目结构

ts-graph-mcp/
├── http/          # HTTP server, database, ingestion, queries
├── mcp/           # MCP stdio wrapper
├── shared/        # Shared types
├── ui/            # Web UI (React + Vite)
└── main.ts        # Entry point

贡献

看 建筑.md 技术内部构件和 CLAUDE.md 获取代码风格指南。

许可证

麻省理工学院