OnlineGDB MCP Server for C++ Code Execution

OnlineGDB MCP Server for C++ Code Execution

Enables AI agents to execute C++ code, test solutions against multiple test cases, analyze performance, and generate test cases using OnlineGDB's online compiler. Perfect for solving competitive programming problems with iterative self-correction capabilities.

Category
访问服务器

README

OnlineGDB MCP Server for C++ Code Execution

A HTTP-based Model Context Protocol (MCP) server that enables AI agents to execute C++ code using OnlineGDB's online compiler. Perfect for solving Data Structures and Algorithms (DSA) problems from competitive programming sites like Codeforces, LeetCode, etc.

Note: This server uses HTTP transport instead of STDIO for better reliability and easier integration.

Features

Core Functionality

  • Execute C++ Code: Run C++ code with custom input using OnlineGDB compiler
  • Submit Solutions: Test complete solutions against multiple test cases
  • Code Optimization: Analyze code for performance improvements
  • Test Case Generation: Generate comprehensive test cases for problems

Agent Capabilities

  • Self-Correction: Agent can execute code, see errors, and iterate on solutions
  • Multiple Test Cases: Validate solutions against various inputs
  • Performance Analysis: Get execution time and memory usage feedback
  • Error Handling: Detailed compilation and runtime error reporting

Installation

  1. Clone and Setup
git clone <repository-url>
cd onlinegdb-mcp-server
npm install
  1. Build the Project
npm run build
  1. Install Dependencies The server requires Puppeteer for browser automation:
# 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

Usage

Starting the Server

# Build the project first
npm run build

# Start the HTTP MCP server
npm start

The server will start on port 3000 (or PORT environment variable) and provide:

  • HTTP MCP Protocol endpoint: POST /mcp
  • REST API endpoints: GET /tools/list, GET /health
  • Web interface: GET /

Available Tools

1. execute_cpp_code

Execute C++ code with optional input:

{
  "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. submit_solution

Test a complete solution against multiple test cases:

{
  "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. optimize_code

Analyze code for performance improvements:

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

4. generate_test_cases

Generate test cases for a problem:

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

Integration with AI Agents

Claude MCP Configuration

Add to your Claude MCP configuration (use HTTP transport instead of STDIO):

Option 1: HTTP Transport (Recommended)

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

Option 2: Direct HTTP calls in your MCP client

// 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"
  })
})

Agent Workflow Example

  1. Problem Analysis: Agent receives a coding problem
  2. Solution Development: Agent writes initial C++ solution
  3. Code Execution: Agent uses execute_cpp_code to test basic functionality
  4. Comprehensive Testing: Agent uses submit_solution with multiple test cases
  5. Error Analysis: If tests fail, agent analyzes errors and iterates
  6. Optimization: Agent uses optimize_code to improve performance
  7. Final Validation: Agent runs final solution against all test cases

Example Agent Conversation

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]

Code Templates

Basic C++ Template for Competitive Programming

#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;
}

Function-Based Template

#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;
}

Testing

Run Comprehensive Tests

# 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

Manual Testing

# 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"}'

Troubleshooting

Common Issues

  1. Server won't start

    • Check if port 3000 is already in use
    • Set PORT environment variable: PORT=3001 npm start
    • Ensure dependencies are installed: npm install
  2. Puppeteer Installation Fails

    • Ensure you have the required system dependencies
    • Try npm install puppeteer --unsafe-perm=true
  3. OnlineGDB Not Loading

    • Check internet connection
    • OnlineGDB might be temporarily unavailable
    • Try increasing timeout limits
  4. Code Execution Timeouts

    • Increase timeLimit parameter
    • Check for

推荐服务器

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

官方
精选