代码分析与测试生成工具

NeuroDev MCP Server是一个基于AI的Python代码分析、测试生成与执行工具,提供全面的代码审查、智能测试生成和格式化功能,适用于Python开发工作流。

  • by ravikant1918
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 ravikant1918/neurodev-mcp

使用说明

🧠 NeuroDev MCP服务器

智能代码分析、测试生成和执行

一个强大的模型上下文协议(MCP)服务器,通过AI驱动的代码审查、智能测试生成和全面的测试执行来增强Python开发工作流程。

特性 • 安装 • 快速开始 • 工具 • 例子


✨ 特性

🔍 代码审查

  • 6强大的分析仪
    • pylint -代码质量和PEP8
    • flake8 -风格强制
    • mypy -类型检查
    • bandit -安全扫描
    • radon -复杂性度量
    • AST -海关检查
  • 实时问题检测
  • 安全漏洞扫描
  • 复杂性和可维护性得分

🧪 测试生成

  • 智能AST分析
    • 自动生成pytest测试
    • 快乐之路覆盖
    • 边缘案例处理
    • 异常测试
    • 型式验证试验
  • 支持函数和类
  • 类型提示感知

▶️ 测试执行

  • 综合测试
    • 孤立的环境
    • 覆盖率报告
    • 逐行分析
    • 超时保护
  • 详细的通过/失败结果
  • 性能指标

🎨 代码格式化

  • 自动格式化
    • black -观点化风格
    • autopep8 -PEP8合规性
  • 可配置的线路长度
  • 一致的代码风格
  • 一个命令格式化

📦 安装

快速安装

```bash

# Clone the repository
git clone https://github.com/ravikant1918/neurodev-mcp.git
cd neurodev-mcp

# Create virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\\Scripts\\activate

# Install the package
pip install -e .
\`\`\`

### **Verify Installation**

\`\`\`bash
# Run tests (should show 15/15 passing)
python test_installation.py

# Test the server
python -m neurodev_mcp.server
\`\`\`

<details>
<summary><b>📁 Project Structure</b> (click to expand)</summary>

\`\`\`
neurodev-mcp/
├─ neurodev_mcp/              # 📦 Main package
│   ├─ __init__.py            # Package exports
│   ├─ server.py              # MCP server entry point
│   ├─ analyzers/             # 🔍 Code analysis
│   │   ├─ __init__.py
│   │   └─ code_analyzer.py   # Multi-tool static analysis
│   ├─ generators/            # 🧪 Test generation
│   │   ├─ __init__.py
│   │   └─ test_generator.py  # AST-based test creation
│   └─ executors/             # ▶️ Test execution
│       ├─ __init__.py
│       └─ test_executor.py   # Test running & formatting
├─ pyproject.toml             # Project configuration
├─ README.md                  # This file
├─ test_installation.py       # Installation validator
├─ examples.py                # Usage examples
└─ requirements.txt           # Dependencies

🚀 快速开始

步骤1:配置MCP客户端

🖥️ Claude Desktop

编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "neurodev-mcp": {
      "command": "/absolute/path/to/neurodev-mcp/.venv/bin/python",
      "args": ["-m", "neurodev_mcp.server"]
    }
  }
}

💡 提示: 替换 /absolute/path/to/neurodev-mcp 与你的实际路径

🔧 Cline (VSCode)

添加到MCP设置中:

{
  "neurodev-mcp": {
    "command": "python",
    "args": ["-m", "neurodev_mcp.server"]
  }
}
🐍 Standalone Usage

直接运行服务器:

# Using the module
python -m neurodev_mcp.server

# Or as a command (if installed)
neurodev-mcp

步骤2:重新启动客户端

重新启动Claude Desktop或重新加载VSCode以加载服务器。

第三步:开始使用! 🎉

使用您的AI助手尝试以下命令:

  • “检查此Python代码是否存在问题”
  • “为此功能生成单元测试”
  • “以覆盖率运行这些测试”
  • “将此代码格式化为PEP8标准”

🌐 运输选项

NeuroDev MCP支持不同用例的多种传输协议:

STDIO(默认)-本地CLI

非常适合与Claude Desktop或Cline等MCP客户进行本地开发:

# Default STDIO transport
neurodev-mcp

# Or explicitly specify STDIO
neurodev-mcp --transport stdio

配置(克劳德桌面):

{
  "mcpServers": {
    "neurodev-mcp": {
      "command": "neurodev-mcp",
      "args": ["--transport", "stdio"]
    }
  }
}

SSE(服务器发送事件)-Web集成

对于基于web的集成和HTTP流:

# Run with SSE on default port (8000)
neurodev-mcp --transport sse

# Custom host and port
neurodev-mcp --transport sse --host 0.0.0.0 --port 3000

终点:

  • SSE流: http://localhost:8000/sse
  • 信息: http://localhost:8000/messages (职位)

Web客户端示例:

const sse = new EventSource('http://localhost:8000/sse');

sse.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Received:', data);
};

// Send message
fetch('http://localhost:8000/messages', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    method: 'tools/call',
    params: {
      name: 'code_review',
      arguments: { code: 'def test(): pass', analyzers: ['pylint'] }
    }
  })
});

运输比较

运输 用例 最适合
工作室 本地CLI客户端 Claude Desktop、Cline、本地开发
上海证券交易所 Web集成 浏览器应用程序、webhooks、远程客户端

🛠️ 可用工具

1. code_review

🔍 使用多种静态分析工具进行全面的代码分析

输入:

{
  "code": "def calculate(x):\n    return x * 2",
  "analyzers": ["pylint", "flake8", "mypy", "bandit", "radon", "ast"]
}

输出:

  • 每个分析器的详细问题报告
  • 安全漏洞
  • 复杂性度量
  • 代码质量分数
  • 逐行建议

2. generate_tests

🧪 使用AST分析生成智能pytest测试

输入:

{
  "code": "def add(a: int, b: int) -> int:\n    return a + b",
  "module_name": "calculator",
  "save": false
}

输出:

  • 完整的pytest测试套件
  • 多个测试用例(快乐路径、边缘用例、异常)
  • 型式验证试验
  • 准备运行测试代码

3. run_tests

▶️ 使用覆盖率报告执行pytest测试

输入:

{
  "test_code": "def test_add():\n    assert add(1, 2) == 3",
  "source_code": "def add(a, b):\n    return a + b",
  "timeout": 30
}

输出:

  • 通过/失败状态
  • 覆盖率
  • 线路覆盖详细信息
  • 执行时间
  • 详细的stdout/stderr

4. format_code

🎨 将Python代码自动格式化为PEP8标准

输入:

{
  "code": "def   messy(  x,y  ):\n        return x+y",
  "line_length": 88
}

输出:

  • 格式优美的代码
  • 符合PEP8标准
  • 风格一致
  • 变化检测

💡 用法示例

示例1:完成代码审查工作流

You: "Review this code for issues and security problems"

[paste code]

AI: [Uses code_review tool]
    → Finds 3 style issues
    → Detects 1 security vulnerability
    → Suggests complexity improvements
    
You: "Fix those issues and show me the updated code"

AI: [Provides fixed code with explanations]

示例2:测试生成和执行

You: "Generate tests for this function and run them"

def divide(a: float, b: float) -> float:
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

AI: [Uses generate_tests tool]
    → Creates 5 test cases
    → Includes edge cases (zero, negative numbers)
    → Tests exception handling
    
    [Uses run_tests tool]
    → 5/5 tests passing ✓
    → 100% code coverage
    → All edge cases handled

示例3:代码格式

You: "Format this messy code"

def   calculate(  x,y,z  ):
        result=x+y+z
        if result>10:
                    return   True
        return False

AI: [Uses format_code tool]
    → Applies black formatting
    → Returns clean, PEP8-compliant code

def calculate(x, y, z):
    result = x + y + z
    if result > 10:
        return True
    return False

📋 需求

包装 版本 用途
mcp ≥0.9.0 模型上下文协议SDK
pylint ≥3.0.0 代码质量分析
flake8 ≥7.0.0 样式检查
mypy ≥1.7.0 静态类型检查
bandit ≥1.7.5 安全扫描
radon ≥6.0.1 复杂性度量
black ≥23.12.0 代码格式化
autopep8 ≥2.0.4 PEP8格式化
pytest ≥7.4.3 测试框架
pytest-cov ≥4.1.0 覆盖率报告
pytest-timeout ≥2.2.0 测试超时

python 3.8或更高


🧪 发展

运行测试

# Run installation tests
python test_installation.py

# Run examples
python examples.py

# Run pytest (if you add tests)
pytest

用作图书馆

from neurodev_mcp import CodeAnalyzer, TestGenerator, TestExecutor
import asyncio

# Analyze code
code = "def hello(): print('world')"
result = asyncio.run(CodeAnalyzer.analyze_ast(code))

# Generate tests
tests = TestGenerator.generate_tests(code, "mymodule")

# Run tests
output = TestExecutor.run_tests(test_code, source_code)

❓ 故障排除

Server not appearing in MCP client?
  • ✅ 检查配置中的路径是否为 绝对的
  • ✅ 确保Python可执行文件路径正确
  • ✅ 重新启动克劳德桌面或VSCode 完全
  • ✅ 检查服务器日志是否有错误
Import or module errors?
# Reinstall the package
pip install -e .

# Verify installation
python -c "from neurodev_mcp import CodeAnalyzer; print('✓ OK')"

# Run installation tests
python test_installation.py
Tests failing?
  • ✅ 确保已安装Python 3.8+
  • ✅ 激活虚拟环境: source .venv/bin/activate
  • ✅ 重新安装依赖关系: pip install -e .
  • ✅ 运行: python test_installation.py 诊断
Performance issues?
  • 一些分析器(pylint、mypy)在处理大文件时可能会很慢
  • 使用特定分析仪: "analyzers": ["flake8", "ast"]
  • 增加大型测试套件的超时时间
  • 考虑缓存结果(未来功能)

🤝 贡献

欢迎投稿!方法如下:

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature/amazing-feature
  3. 进行更改
  4. 运行测试: python test_installation.py
  5. 承诺: git commit -m 'Add amazing feature'
  6. 推: git push origin feature/amazing-feature
  7. 打开拉取请求

未来的增强功能

  • []附加分析器(pydocstyle、秃鹫)
  • []结果缓存以提高性能
  • []配置文件支持
  • []Web仪表板
  • []多语言支持
  • []CI/CD管道

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。


🙏 致谢


📞 支持


准备好加速你的Python开发了! 🚀

由以下材料制成❤️ NeuroDev团队

⭐ GitHub上的明星 • 🐛 报告Bug • ✨ 请求功能