Back to MCP directory
publicPublicdnsLocal runtime

Python Code Explorer

一个Python MCP服务器,用于提取和分析Python代码结构,专注于文件间的导入/导出关系,提供轻量级实现,无需代理系统。

article

README

🚀 Python 代码分析工具

本项目是一款强大的Python代码分析工具,提供了丰富的代码分析功能,并且全面支持Model Context Protocol (MCP) 标准。借助标准化接口,它能与各类MCP兼容客户端无缝对接,为开发者带来更安全、高效的代码分析体验。

🚀 快速开始

安装依赖项

安装项目所需的Python包:

pip install mcp[yaml]

克隆仓库

克隆此仓库到本地:

git clone https://github.com/yourusername/python-code-analyzer.git
cd python-code-analyzer

启动代理或服务器

根据需求启动原生MCP实现的代理或MCP SDK实现的服务器。

✨ 主要特性

功能模块

  • 代码提取:精准提取代码关键信息。
  • 依赖分析:深入剖析代码依赖关系。
  • 资源提供:有效提供代码所需资源。
  • 自定义工具:支持用户根据需求定制工具。

MCP 集成

本项目提供两种方式支持MCP协议:

  • 原生 MCP 实现:位于agent.py中的直接JSON - RPC接口,与MCP兼容。
  • MCP SDK 实现:位于server.py中,使用官方的Python MCP SDK,功能更强大丰富。

显著优势

  • 标准化接口:使您的工具可供任何MCP兼容客户端使用。
  • 增强安全:内置权限模型和资源控制,保障代码安全。
  • 更好的LLM集成:与Claude Desktop和其他LLM平台无缝集成。
  • 改进的开发人员体验:提供全面的工具支持,例如MCP Inspector。

MCP 协议版本

此实现支持MCP协议版本0.7.0。有关更多详细信息,请参阅官方文档

📦 安装指南

安装项目所需的Python包:

pip install mcp[yaml]

克隆此仓库到本地:

git clone https://github.com/yourusername/python-code-analyzer.git
cd python-code-analyzer

💻 使用示例

原生 MCP 实现(agent.py)

启动代理

# 以默认设置启动
python agent.py

# 指定自定义名称
python agent.py --name "My Code Explorer"

# 使用特定的.env文件
python agent.py --env-file .env.production

开发模式

使用MCP CLI在开发模式下启动代理:

# 安装MCP CLI
pip install "mcp[cli]"

# 在开发模式下启动代理并带有Inspector UI
mcp dev agent.py

MCP SDK 实现(server.py)

启动服务器

# 以默认设置启动服务器
python server.py

# 指定自定义名称
python server.py --name "Custom Code Explorer"

# 使用特定的配置文件
python server.py --config config.yaml

在Claude Desktop中集成

将此代理安装到Claude Desktop:

# 安装代理到Claude Desktop
mcp install agent.py

# 指定自定义名称和配置文件
mcp install agent.py --name "Python Code Analyzer" -f config.yaml

自定义部署

对于自定义部署,可以直接使用MCP服务器:

from server import Server

# 配置服务器
server.name = "Custom Python Analyzer"

# 启动服务器
server.run()

扩展功能

添加更多工具

可以在server.py中添加其他工具,通过装饰器@mcp.tool()

@mcp.tool()
def analyze_imports(target_file: str) -> Dict[str, Any]:
    """分析Python文件中的所有导入模块"""
    # 实现代码
    return {
        "file": target_file,
        "imports": [],  # 导入模块列表
        "analysis": ""  # 导入分析结果
    }

添加资源端点

可以添加资源端点来提供数据:

@mcp.resource("python_stats://{directory}")
def get_stats(directory: str) -> Dict[str, Any]:
    """获取指定目录中Python文件的统计信息"""
    from pathlib import Path
    stats = {
        "directory": directory,
        "file_count": 0,
        "total_lines": 0,
        "average_lines": 0
    }
    
    files = list(Path(directory).glob("**/*.py"))
    stats["file_count"] = len(files)
    
    if files:
        total_lines = 0
        for file in files:
            with open(file, "r") as f:
                total_lines += len(f.readlines())
        stats["total_lines"] = total_lines
        stats["average_lines"] = total_lines / len(files)
    
    return stats

示例代码

使用 MCP 客户端

可以在examples/mcp_client_example.py中找到示例:

from mcp.client import Client, Transport

# 连接到服务器
client = Client(Transport.subprocess(["python", "server.py"]))
client.initialize()

# 列出所有可用工具
for tool in client.tools:
    print(f"Tool: {tool.name}")

# 使用 get_code 工具
result = client.tools.get_code(target_file="path/to/your/file.py")
print(f"找到 {len(result['referenced_files'])} 个引用文件")

# 关闭连接
client.shutdown()

运行示例:

python examples/mcp_client_example.py [optional_target_file.py]

原生 MCP 实现示例

使用agent.py中的功能:

# 提取代码并分析依赖关系
python agent.py extract --file /path/to/your/script.py

# 分析资源
python agent.py analyze-resources --source /path/to/resource

📚 详细文档

配置

原生 MCP 实现配置

可以在config.yaml中找到默认配置:

log_level: INFO
bind: 0.0.0.0
port: 5000

MCP SDK 实现配置

可以在server_config.yaml中找到默认配置:

name: Python Code Analyzer Server
bind: 0.0.0.0
port: 8000
tools:
  enabled:
    analyze_imports: true
    get_stats: true

项目结构

  • agent.py - 原生 MCP 实现的代理
  • server.py - MCP SDK 实现的服务器
  • examples/ - 示例代码
  • config.yaml - 默认配置文件
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client