Enables execution of C++ code using OnlineGDB's online compiler, including running code with custom input, testing solutions against multiple test cases, code optimization analysis, and test case generation for competitive programming problems.
Supports solving Data Structures and Algorithms problems from Codeforces through C++ code execution and automated testing capabilities.
Supports solving Data Structures and Algorithms problems from LeetCode through C++ code execution and automated testing capabilities.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@OnlineGDB MCP Server for C++ Code Executionexecute this C++ code to find the sum of two numbers: #include using namespace std; int main(){ int a,b; cin>>a>>b; cout<<a+b; return 0; }"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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
Clone and Setup
git clone <repository-url>
cd onlinegdb-mcp-server
npm installBuild the Project
npm run buildInstall 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 automaticallyUsage
Starting the Server
# Build the project first
npm run build
# Start the HTTP MCP server
npm startThe server will start on port 3000 (or PORT environment variable) and provide:
HTTP MCP Protocol endpoint:
POST /mcpREST API endpoints:
GET /tools/list,GET /healthWeb 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
Problem Analysis: Agent receives a coding problem
Solution Development: Agent writes initial C++ solution
Code Execution: Agent uses
execute_cpp_codeto test basic functionalityComprehensive Testing: Agent uses
submit_solutionwith multiple test casesError Analysis: If tests fail, agent analyzes errors and iterates
Optimization: Agent uses
optimize_codeto improve performanceFinal 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:mcpManual 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
Server won't start
Check if port 3000 is already in use
Set PORT environment variable:
PORT=3001 npm startEnsure dependencies are installed:
npm install
Puppeteer Installation Fails
Ensure you have the required system dependencies
Try
npm install puppeteer --unsafe-perm=true
OnlineGDB Not Loading
Check internet connection
OnlineGDB might be temporarily unavailable
Try increasing timeout limits
Code Execution Timeouts
Increase
timeLimitparameterCheck for