MCP Executor Server
A secure server that enables code execution in isolated Docker environments, supporting Python with strict security constraints including network isolation, limited filesystem access, and resource limitations.
README Documentation
MCP Executor Server
A secure, Docker-based code execution server that provides HTTP API endpoints for running Python code in isolated containers. Perfect for integration with automation tools like n8n, web applications, or any HTTP client.
๐ Features
- ๐ Secure Execution - Code runs in isolated Docker containers
- โก Fast Performance - Optimized for quick code execution
- ๐ก Real-time Streaming - Server-Sent Events (SSE) for live output
- ๐ HTTP API - RESTful endpoints for easy integration
- ๐ Health Monitoring - Built-in health check endpoint
- ๐ Python Support - Execute Python code with full library access
- ๐ง Docker Integration - Uses
my-llm-sandboxcontainer for execution
๐ Prerequisites
- Docker Desktop - Must be running and in Linux containers mode
- Node.js - Version 18+ recommended
- Docker Image -
my-llm-sandboximage must be available locally
๐ ๏ธ Installation & Setup
1. Clone and Navigate
cd src
2. Install Dependencies
npm install
3. Start the Server
Development Mode (Recommended):
npm run dev
Production Mode:
npm run build
npm start
The server will start on http://localhost:3000
๐ก API Endpoints
1. Execute Code
POST /execute
Execute Python code in a secure Docker container.
Request Body:
{
"code": "print('Hello, World!')",
"language": "python",
"timeout": 30,
"libraries": []
}
Response:
{
"content": [
{
"type": "text",
"text": "Hello, World!\n"
}
],
"exitCode": 0
}
2. Real-time Streaming
GET /sse
Connect to Server-Sent Events for real-time output streaming.
Usage:
- Open in browser:
http://localhost:3000/sse - Or use curl:
curl http://localhost:3000/sse
Output Format:
data: {"type":"stdout","data":"Hello, World!\n"}
data: {"type":"stderr","data":"Error message\n"}
3. Health Check
GET /health
Check if the server is running properly.
Response:
{
"status": "ok",
"message": "MCP Executor server is healthy",
"timestamp": "2025-07-10T18:12:11.711Z"
}
๐งช Testing
Using Postman
-
Test Health Check:
- Method:
GET - URL:
http://localhost:3000/health
- Method:
-
Test Code Execution:
- Method:
POST - URL:
http://localhost:3000/execute - Headers:
Content-Type: application/json - Body:
{ "code": "print('Hello from Postman!')", "language": "python" } - Method:
-
Test SSE Streaming:
- Method:
GET - URL:
http://localhost:3000/sse - Keep connection open while testing
/execute
- Method:
Using curl
# Health check
curl http://localhost:3000/health
# Execute code
curl -X POST http://localhost:3000/execute \
-H "Content-Type: application/json" \
-d '{"code": "print(\"Hello from curl!\")", "language": "python"}'
# SSE streaming
curl http://localhost:3000/sse
๐ง n8n Integration
Basic Setup
- Add HTTP Request Node
- Configure:
- Method:
POST - URL:
http://localhost:3000/execute - Headers:
Content-Type: application/json - Body (JSON):
{ "code": "print('Hello from n8n!')", "language": "python" } - Method:
Advanced Examples
Dynamic Code Execution:
{
"code": "{{ $json.code }}",
"language": "python"
}
Data Processing:
{
"code": "import json; data = {{ $json.data }}; print('Processed:', len(data))",
"language": "python"
}
File Operations:
{
"code": "with open('output.txt', 'w') as f: f.write('{{ $json.content }}'); print('File written')",
"language": "python"
}
๐ Project Structure
src/
โโโ index.ts # Main Express server
โโโ server.ts # Docker execution logic
โโโ utils/
โ โโโ sse.ts # Server-Sent Events handling
โโโ types/ # TypeScript type definitions
โโโ tools/ # Tool definitions
โโโ docs/ # Documentation
โโโ package.json # Dependencies and scripts
โโโ tsconfig.json # TypeScript configuration
โโโ Dockerfile # Docker configuration
โโโ README.md # This file
๐ Troubleshooting
Common Issues
-
Docker not running:
- Ensure Docker Desktop is running
- Check that it's in Linux containers mode
-
Container not found:
- Verify
my-llm-sandboximage exists:docker images - Rebuild if needed:
docker build -t my-llm-sandbox .
- Verify
-
Permission denied:
- Ensure Docker has proper permissions
- Check Docker socket access
-
Slow responses:
- First request may take 2-3 seconds (container startup)
- Subsequent requests should be fast (1-2 seconds)
Debug Mode
Enable detailed logging by checking the server console output for:
[STDOUT]- Docker container output[STDERR]- Docker container errorsExecuting code in Docker container...- Request processing
๐ Deployment
Docker Deployment
-
Build the image:
docker build -t mcp-executor . -
Run the container:
docker run -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock mcp-executor
Environment Variables
PORT- Server port (default: 3000)NODE_ENV- Environment (development/production)
๐ Examples
Simple Python Code
{
"code": "print('Hello, World!')",
"language": "python"
}
Mathematical Operations
{
"code": "import math; print(f'Pi: {math.pi}'); print(f'2^10: {2**10}')",
"language": "python"
}
Data Processing
{
"code": "data = [1, 2, 3, 4, 5]; print(f'Sum: {sum(data)}'); print(f'Average: {sum(data)/len(data)}')",
"language": "python"
}
File Operations
{
"code": "with open('test.txt', 'w') as f: f.write('Hello from Docker!'); print('File created')",
"language": "python"
}
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
๐ License
This project is licensed under the ISC License.
๐ Support
For issues and questions:
- Check the troubleshooting section
- Review the console logs
- Test with simple code examples
- Verify Docker setup
Happy coding! ๐