time-complexity-mcp

time-complexity-mcp

An MCP server for static Big-O time complexity analysis using tree-sitter AST parsing. Supports JavaScript, TypeScript, Python, Java, Kotlin, and Dart.

Category
访问服务器

README

Time Complexity MCP

An MCP server that estimates Big-O time complexity of your code through static analysis. It parses source files into ASTs using tree-sitter, detects loops, recursion, and known stdlib calls, then reports per-function complexity with line-level annotations.

Built for AI coding assistants — works with Claude Code and GitHub Copilot.

Supported Languages

Language Extensions Grammar
JavaScript .js, .mjs, .cjs, .jsx tree-sitter-javascript
TypeScript .ts, .tsx tree-sitter-typescript
Dart .dart vendor NAPI binding
Kotlin .kt, .kts tree-sitter-kotlin
Java .java tree-sitter-java
Python .py tree-sitter-python
PHP .php tree-sitter-php
Go .go tree-sitter-go

What It Detects

  • Loop nestingfor, while, do-while with depth tracking. Constant-bound loops (e.g., for i in range(10)) are recognized as O(1).
  • Recursion — linear recursion (O(n)) vs branching recursion like fibonacci (O(2^n)).
  • Known stdlib methods.sort() as O(n log n), .filter()/.map() as O(n), .push()/.pop() as O(1), etc. Each language has its own patterns.
  • Combined complexity — an O(n) method inside an O(n) loop correctly reports O(n^2).

Tools

The server exposes 5 MCP tools:

Tool Description
analyze_file Analyze all functions in a source file. Returns per-function Big-O with reasoning and line annotations.
analyze_function Analyze a single function by name or line number.
analyze_directory Scan a directory for all supported files. Returns a summary with hotspots (top 5 most complex functions).
analyze_github_repo Clone a GitHub repo and analyze complexity. Accepts owner/repo or full URL. Requires git in PATH.
get_supported_languages List supported languages with file extensions.

Setup

Install from Release (recommended)

Download the prebuilt bundle for your platform from the latest release:

Platform File
macOS (Apple Silicon) 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

Extract and configure:

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

No C++ compiler or npm install required — just Node.js 18+. The analyze_github_repo tool also requires git in PATH.

Install from Source

Requires Node.js 18+ and a C++ compiler (Xcode CLI tools on macOS, build-essential on Linux).

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

The postinstall script automatically builds the vendor Dart grammar.

Configure with Claude Code

Add to your project's .mcp.json (or ~/.claude.json for global access):

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

Then restart Claude Code. The tools analyze_file, analyze_function, analyze_directory, analyze_github_repo, and get_supported_languages will be available automatically.

Configure with GitHub Copilot (VS Code)

Add to .vscode/mcp.json in your project:

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

If the MCP lives outside your workspace, replace ${workspaceFolder}/dist/index.js with the absolute path.

Usage Examples

Once configured, your AI assistant can call the tools directly.

Analyze a file

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

Returns each function with its Big-O, reasoning, and line-level annotations:

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)

Analyze a single function

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

Analyze a GitHub repository

> Analyze the complexity of facebook/react

or with a full URL:

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

Clones the repo temporarily, analyzes it, and returns results with repo-relative file paths. Requires git installed.

Scan an entire codebase

> Scan src/ for complexity hotspots

Returns a summary with the top 5 most complex functions across all files:

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)
  ...

Architecture

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

Each language analyzer implements 9 template methods from 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

Development

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)

Security

  • Static analysis only. Code is parsed into ASTs and inspected — never evaluated, executed, or imported.
  • Read-only file access. Source files are read for parsing. Nothing is written, modified, or deleted.
  • Network access (opt-in). The analyze_github_repo tool invokes git clone to fetch public GitHub repos. All other tools run locally with no network access. Clone URLs are restricted to HTTPS GitHub URLs only.
  • Trusted native addons. Tree-sitter grammars are compiled NAPI addons from verified sources.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选