代码时间复杂度分析工具

一个通过静态分析估计代码Big-O时间复杂度的MCP服务器,支持多种编程语言,适用于AI编程助手和代码审查场景。

  • by Luzgan
  • TypeScript
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 Luzgan/time-complexity-mcp

使用说明

时间复杂度MCP

一个MCP服务器,通过静态分析估计代码的Big-O时间复杂度。它使用以下命令将源文件解析为AST 树保姆,检测循环、递归和已知的stdlib调用,然后使用行级注释报告每个函数的复杂性。

专为AI编码助手打造——与 克劳德代码 和 GitHub Copilot.

支持的语言

语言 扩展 语法
JavaScript .js, .mjs, .cjs, .jsx 树型javascript
TypeScript .ts, .tsx 树型字体
飞镖 .dart 供应商NAPI绑定
Kotlin .kt, .kts 树保姆科特林
Java .java 树保姆java
python .py 树栖蟒蛇
PHP .php 树保姆php
去吧 .go 树保姆走

它检测到什么

  • 循环嵌套 — for, while, do-while 具有深度跟踪功能。恒定边界循环(例如。, for i in range(10))被识别为O(1)。
  • 递归 --线性递归(O(n))与斐波那契(O(2^n))等分支递归。
  • 已知的stdlib方法 — .sort() 作为O(n log n), .filter()/.map() 作为O(n), .push()/.pop() 例如O(1)等。每种语言都有自己的模式。
  • 综合复杂性 --O(n)循环内的O(n”方法正确地报告O(n^2)。

工具

服务器公开了5个MCP工具:

工具 说明
analyze_file 分析源文件中的所有函数。返回每个带有推理和行注释的函数Big-O。
analyze_function 按名称或行号分析单个函数。
analyze_directory 扫描目录以查找所有支持的文件。返回包含热点(前5个最复杂的函数)的摘要。
analyze_github_repo 克隆GitHub仓库并分析复杂性。接受 owner/repo 或完整URL。需要 git 在PATH中。
get_supported_languages 列出支持的语言及其文件扩展名。

设置

从版本安装(推荐)

从以下网址下载适用于您平台的预构建捆绑包 最新版本:

平台 文件
macOS(苹果硅) time-complexity-mcp-darwin-arm64-v*.tar.gz
Linux x64 time-complexity-mcp-linux-x64-v*.tar.gz
Linux arm64 time-complexity-mcp-linux-arm64-v*.tar.gz
Windows x64 time-complexity-mcp-win32-x64-v*.zip

提取并配置:

# macOS / Linux
tar xzf time-complexity-mcp-darwin-arm64-v*.tar.gz
# Windows
Expand-Archive time-complexity-mcp-win32-x64-v*.zip

没有C++编译器或 npm install 必需的——只有Node.js 18+。这 analyze_github_repo 工具还需要 git 在PATH中。

从源代码安装

需要Node.js 18+和C++编译器(macOS上的Xcode CLI工具, build-essential 在Linux上)。

git clone https://github.com/Luzgan/time-complexity-mcp.git
cd time-complexity-mcp
npm install
npm run build

这 postinstall 该脚本自动构建供应商Dart语法。

使用Claude代码进行配置

添加到您的项目 .mcp.json (或 ~/.claude.json 全球访问):

{
  "mcpServers": {
    "time-complexity": {
      "type": "stdio",
      "command": "node",
      "args": ["/absolute/path/to/time-complexity-mcp/dist/index.js"]
    }
  }
}

然后重新启动Claude Code。工具 analyze_file, analyze_function, analyze_directory, analyze_github_repo,以及 get_supported_languages 将自动可用。

使用GitHub Copilot配置(VS代码)

增添 .vscode/mcp.json 在您的项目中:

{
  "servers": {
    "time-complexity": {
      "type": "stdio",
      "command": "node",
      "args": ["${workspaceFolder}/dist/index.js"]
    }
  }
}

如果MCP位于您的工作区之外,请更换 ${workspaceFolder}/dist/index.js 绝对路径。

用法示例

配置后,您的AI助手可以直接调用这些工具。

分析文件

> Analyze the complexity of src/utils/sort.ts

返回每个函数及其Big-O、推理和行级注释:

bubbleSort (lines 1-10): O(n^2)
  Found 2 variable-bound loop(s), max nesting depth: 2. Overall: O(n^2).

  Line annotations:
    Line 2: O(n) — for_statement loop (nesting depth: 1)
    Line 3: O(n^2) — for_statement loop (nesting depth: 2)

分析单个函数

> What's the complexity of the fibonacci function in recursion.py?

分析GitHub存储库

> Analyze the complexity of facebook/react

或者使用完整的URL:

> Analyze https://github.com/expressjs/express, focus on the lib/ directory

临时克隆仓库,对其进行分析,并返回带有仓库相关文件路径的结果。需要 git 安装。

扫描整个代码库

> Scan src/ for complexity hotspots

返回所有文件中最复杂的前5个函数的摘要:

Files analyzed: 27
Total functions: 150

Breakdown:
  O(1):       102
  O(n):        40
  O(n log n):   1
  O(n^2):       4
  O(n^3):       2
  O(2^n):       1

Hotspots:
  1. src/analyzer/base-analyzer.ts → walk: O(2^n)
  2. src/tools/analyze-directory.ts → analyzeDirectory: O(n^3)
  ...

建筑

src/
  index.ts                  # Entry point — stdio MCP transport
  server.ts                 # MCP tool registration
  analyzer/
    base-analyzer.ts        # Abstract base class (template method pattern)
    types.ts                # Core types (BigOComplexity, FunctionNode, etc.)
    complexity.ts           # Complexity arithmetic (max, multiply, fromDepth)
  languages/
    index.ts                # Language registry
    javascript/             # JS/TS analyzer
    dart/                   # Dart analyzer
    kotlin/                 # Kotlin analyzer
    java/                   # Java analyzer
    python/                 # Python analyzer
    php/                    # PHP analyzer
    go/                     # Go analyzer
  tools/                    # MCP tool implementations
  utils/                    # File I/O & formatting
vendor/
  tree-sitter-dart/         # Custom NAPI binding for Dart grammar
tests/
  *.test.ts                 # Per-language test suites (99 tests total)
  fixtures/                 # Sample source files

每个语言分析器实现9个模板方法 BaseAnalyzer:

getGrammar()              → tree-sitter grammar object
getFunctionNodeTypes()    → AST node types for functions
getLoopNodeTypes()        → AST node types for loops
getCallNodeTypes()        → AST node types for calls (e.g., "call_expression", "method_invocation", "call")
getKnownMethods()         → stdlib method complexity patterns
extractFunctionName()     → function name from AST node
extractParameters()       → parameter names from AST node
isConstantLoop()          → detect constant-bound loops
getCallName()             → function/method name from call node

发展

npm run build       # Compile TypeScript (also type-checks)
npm test            # Run all 99 tests
npm run test:watch  # Watch mode
npm run dev         # Run server via tsx (no build needed)

安全

  • 仅进行静态分析。 代码被解析为AST并被检查——永远不会被计算、执行或导入。
  • 只读文件访问。 读取源文件进行解析。没有任何内容被写入、修改或删除。
  • 网络访问(选择加入)。 这 analyze_github_repo 工具调用 git clone 获取公共GitHub仓库。所有其他工具都在本地运行,没有网络访问权限。克隆URL仅限于HTTPS GitHub URL。
  • 受信任的本地插件。 树保姆语法是从经过验证的源代码编译的NAPI插件。

许可证

麻省理工学院