C++代码执行服务器

一个基于HTTP的模型上下文协议(MCP)服务器,通过OnlineGDB在线编译器执行C++代码,适用于解决数据结构和算法问题。

  • by ayushtiwari110
  • JavaScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 ayushtiwari110/gdb-MCP-Server

使用说明

用于C++代码执行的在线GDB MCP服务器

A. 基于HTTP 模型上下文协议(MCP)服务器,使AI代理能够使用OnlineGDB的在线编译器执行C++代码。非常适合解决Codeforce、LeetCode等竞争性编程网站的数据结构和算法(DSA)问题。

备注:此服务器使用HTTP传输而不是STDIO,以获得更好的可靠性和更容易的集成。

特性

核心功能

  • 执行C++代码:使用OnlineGDB编译器使用自定义输入运行C++代码
  • 提交解决方案:针对多个测试用例测试完整的解决方案
  • 代码优化:分析代码以提高性能
  • 测试用例生成:为问题生成全面的测试用例

代理能力

  • 自校正:代理可以执行代码、查看错误和迭代解决方案
  • 多个测试用例:根据各种输入验证解决方案
  • 性能分析:获取执行时间和内存使用情况反馈
  • 错误处理:详细的编译和运行时错误报告

安装

  1. 克隆和设置
git clone <repository-url>
cd onlinegdb-mcp-server
npm install
  1. 构建项目
npm run build
  1. 再进行 服务器需要Puppeteer来实现浏览器自动化:
# On Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y wget gnupg ca-certificates procps libxss1
sudo apt-get install -y libgconf-2-4 libxrandr2 libasound2 libpangocairo-1.0-0 libatk1.0-0 libcairo-gobject2 libgtk-3-0 libgdk-pixbuf2.0-0 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libasound2 libpangocairo-1.0-0 libatk1.0-0 libcairo-gobject2 libdrm2 libxkbcommon0 libgtk-3-0

# On macOS (using Homebrew)
brew install chromium

# On Windows
# Puppeteer will download Chromium automatically

用法

启动服务器

# Build the project first
npm run build

# Start the HTTP MCP server
npm start

服务器将在端口3000(或port环境变量)上启动,并提供:

  • HTTP MCP协议端点: POST /mcp
  • REST API端点: GET /tools/list, GET /health
  • web界面: GET /

可用工具

1.execute_cpp_code

使用可选输入执行C++代码:

{
  "name": "execute_cpp_code",
  "arguments": {
    "code": "#include<iostream>\nusing namespace std;\nint main(){\n    int n;\n    cin >> n;\n    cout << n * 2 << endl;\n    return 0;\n}",
    "input": "5",
    "timeLimit": 5
  }
}

2.提交解决方案

针对多个测试用例测试完整的解决方案:

{
  "name": "submit_solution",
  "arguments": {
    "problemName": "Two Sum",
    "code": "C++ solution code here",
    "testCases": [
      {
        "input": "4\n2 7 11 15\n9",
        "expectedOutput": "0 1"
      },
      {
        "input": "3\n3 2 4\n6",
        "expectedOutput": "1 2"
      }
    ],
    "functionSignature": "vector<int> twoSum(vector<int>& nums, int target)"
  }
}

3.优化代码

分析代码以提高性能:

{
  "name": "optimize_code",
  "arguments": {
    "code": "C++ code to analyze",
    "constraints": {
      "timeComplexity": "O(n log n)",
      "spaceComplexity": "O(1)",
      "memoryLimit": "256MB"
    }
  }
}

4.生成测试用例

为问题生成测试用例:

{
  "name": "generate_test_cases",
  "arguments": {
    "problemDescription": "Find the maximum subarray sum",
    "constraints": "1 ≤ n ≤ 10^5, -10^9 ≤ arr[i] ≤ 10^9",
    "numTestCases": 5
  }
}

与AI代理集成

Claude MCP配置

添加到您的Claude MCP配置中(使用HTTP传输而不是STDIO):

选项1:HTTP传输(推荐)

{
  "mcpServers": {
    "onlinegdb-cpp": {
      "transport": {
        "type": "http",
        "url": "http://localhost:3000/mcp"
      }
    }
  }
}

选项2:在MCP客户端中直接进行HTTP调用

// Make direct HTTP requests to the MCP endpoints
fetch('http://localhost:3000/mcp', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: "2.0",
    id: 1,
    method: "tools/list"
  })
})

代理工作流示例

  1. 问题分析:代理收到编码问题
  2. 解决方案开发:代理编写初始C++解决方案
  3. 代码执行:代理使用 execute_cpp_code 测试基本功能
  4. 综合测试:代理使用 submit_solution 具有多个测试用例
  5. 误差分析:如果测试失败,代理会分析错误并迭代
  6. 优化:代理使用 optimize_code 以提高性能
  7. 最终验证:代理对所有测试用例运行最终解决方案

代理对话示例

Human: Solve this problem: Given an array of integers, find two numbers that add up to a target sum.

Agent: I'll solve this step by step using the OnlineGDB MCP server.

First, let me implement a basic two-sum solution:

[Uses execute_cpp_code to test basic logic]

Now let me test it against multiple test cases:

[Uses submit_solution with comprehensive test cases]

The solution passed 4/5 test cases. Let me analyze the failure and optimize:

[Uses optimize_code to identify issues]

[Iterates and re-tests until all test cases pass]

代码模板

竞争性编程的基本C++模板

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Your code here

    return 0;
}

基于函数的模板

#include <iostream>
#include <vector>
using namespace std;

// Function signature here
vector<int> solve(vector<int>& arr, int target) {
    // Implementation
    return {};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Input parsing
    int n, target;
    cin >> n >> target;
    vector<int> arr(n);
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    // Call solution function
    vector<int> result = solve(arr, target);

    // Output formatting
    for(int i = 0; i < result.size(); i++) {
        cout << result[i];
        if(i < result.size() - 1) cout << " ";
    }
    cout << endl;

    return 0;
}

测试

运行综合测试

# Start the server in one terminal
npm start

# Run tests in another terminal
node test-final-mcp.js

# Or use the npm script
npm run test:mcp

手动测试

# Test health endpoint
curl http://localhost:3000/health

# Test MCP initialize
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"clientInfo":{"name":"test","version":"1.0.0"}}}'

# Test tools list
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'

故障排除

常见问题

  1. 服务器无法启动

    • 检查端口3000是否已在使用中
    • 设置PORT环境变量: PORT=3001 npm start
    • 确保安装了依赖项: npm install
  2. 木偶安装失败

    • 确保您具有所需的系统依赖关系
    • 尝试 npm install puppeteer --unsafe-perm=true
  3. 在线GDB未加载

    • 检查互联网连接
    • 在线GDB可能暂时不可用
    • 尝试增加超时限制
  4. 代码执行超时

    • 增加 timeLimit 参数
    • 检查