Code Analysis MCP Server

Code Analysis MCP Server

A modular MCP server that provides tools for file operations, regex-based code searching, and structural analysis of functions and classes across multiple programming languages. It also includes AI-powered features for intelligently updating files according to architectural changes.

Category
访问服务器

README

Code Analysis MCP Server

A modular MCP (Model Context Protocol) server for code analysis with file operations, code search, and structure analysis capabilities.

Features

📁 File Operations

  • read_file: Read contents of any code file
  • list_files: List files in directories with pattern matching
  • file_info: Get detailed file information (size, type, line count)

🔍 Code Search

  • search_code: Search for patterns in code using regex
  • find_definition: Find symbol definitions (functions, classes, variables)

📊 Code Analysis

  • analyze_structure: Analyze code structure (imports, classes, functions)

Installation

# Clone the repository
git clone https://github.com/yourusername/code-mcp.git
cd code-mcp

# Create virtual environment
python -m venv venv

# Activate environment
source venv/bin/activate  # On Unix/macOS
venv\Scripts\activate     # On Windows

# Install dependencies
pip install -r requirements.txt

Usage

1. With Claude Desktop

Add to your Claude Desktop configuration file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "code-analyzer": {
      "command": "python",
      "args": ["/absolute/path/to/code-mcp/server.py"]
    }
  }
}

Then restart Claude Desktop.

2. With Continue.dev (VS Code)

Add to your Continue configuration:

{
  "models": [...],
  "mcpServers": {
    "code-analyzer": {
      "command": "python",
      "args": ["/absolute/path/to/code-mcp/server.py"]
    }
  }
}

3. With Other MCP Clients

Any MCP-compatible client can use this server by pointing to the server.py file.

Available Tools

📖 read_file

Read the contents of a file.

{
  "tool": "read_file",
  "arguments": {
    "path": "src/main.py",
    "encoding": "utf-8"  // optional, default: utf-8
  }
}

📂 list_files

List files in a directory with optional pattern matching.

{
  "tool": "list_files",
  "arguments": {
    "directory": "./src",      // optional, default: current dir
    "pattern": "*.py",         // optional, default: *
    "recursive": true          // optional, default: false
  }
}

ℹ️ file_info

Get detailed information about a file.

{
  "tool": "file_info",
  "arguments": {
    "path": "src/main.py"
  }
}

🔍 search_code

Search for patterns in code files using regex.

{
  "tool": "search_code",
  "arguments": {
    "pattern": "def.*test",        // regex pattern
    "directory": "./src",          // optional
    "file_pattern": "*.py",        // optional
    "case_sensitive": false        // optional, default: true
  }
}

🎯 find_definition

Find where a symbol is defined.

{
  "tool": "find_definition",
  "arguments": {
    "symbol": "MyClass",
    "directory": "./src",          // optional
    "language": "python"           // optional: python, javascript
  }
}

🏗️ analyze_structure

Analyze the structure of a code file.

{
  "tool": "analyze_structure",
  "arguments": {
    "path": "src/main.py",
    "include_docstrings": true     // optional, default: false
  }
}

🤖 update_with_architecture

Compare old and new architecture versions and intelligently update the new file.

{
  "tool": "update_with_architecture",
  "arguments": {
    "old_file": "src/legacy/module.py",    // Reference file (old architecture)
    "new_file": "src/modern/module.py",    // Target file (will be updated)
    "backup": true                         // optional, default: true
  }
}

AI Configuration

To use the AI-powered tools, you need to configure your API keys:

  1. Copy .env.example to .env:

    cp .env.example .env
    
  2. Edit .env and add your API keys:

    AI_PROVIDER=openai
    OPENAI_API_KEY=your-openai-api-key
    # or
    AI_PROVIDER=anthropic  
    ANTHROPIC_API_KEY=your-anthropic-api-key
    

Thinking Models Support

The tool automatically handles "thinking" models (like o1, o1-preview) that include reasoning in their responses:

  • Thinking sections are automatically removed
  • Only the actual code is extracted
  • Supports various thinking formats: <think>, [thinking], etc.
  1. Install AI dependencies:

    pip install openai anthropic
    
  2. Test LLM connectivity:

    ./test_llm.sh
    # or
    python tests/test_llm.py
    

Examples

In Claude Desktop

After configuring, you can ask Claude:

  • "Read the file src/main.py"
  • "Search for all functions that contain 'test' in the src directory"
  • "Find where the class 'UserModel' is defined"
  • "Analyze the structure of app.py"
  • "List all Python files in the project"

Programmatic Usage

# Example of calling tools programmatically
import asyncio
from mcp import Client

async def main():
    client = Client()
    
    # Read a file
    result = await client.call_tool("read_file", {
        "path": "src/main.py"
    })
    
    # Search for patterns
    result = await client.call_tool("search_code", {
        "pattern": "TODO|FIXME",
        "directory": "./",
        "recursive": True
    })
    
    # Analyze structure
    result = await client.call_tool("analyze_structure", {
        "path": "src/main.py",
        "include_docstrings": True
    })

asyncio.run(main())

Architecture

The server follows a modular architecture:

├── server.py          # Main MCP server
├── tools/             # Tool definitions
│   ├── file_tools.py  # File operations
│   └── code_tools.py  # Code analysis tools
├── handlers/          # Request handlers
│   ├── file_handler.py
│   ├── search_handler.py
│   └── analyze_handler.py
└── core/              # Core services
    ├── file_system.py # File system operations
    └── code_parser.py # Code parsing logic

Supported Languages

  • Python (.py)
  • JavaScript/TypeScript (.js, .ts, .jsx, .tsx)
  • Java (.java)
  • C/C++ (.c, .cpp, .h)
  • Go (.go)
  • Rust (.rs)
  • Ruby (.rb)
  • And more...

Security

  • File access is restricted to prevent directory traversal
  • Large files are handled efficiently with streaming
  • Search results are limited to prevent memory issues

Contributing

Feel free to submit issues and enhancement requests!

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选