|
1 | | -# Spring AI MCP Code Executor |
| 1 | +# MCP Code Executor Server |
2 | 2 |
|
3 | | -This project demonstrates the use of Spring AI's Model Context Protocol (MCP) to provide a code execution service. The service allows AI Agents to execute code in multiple programming languages through a structured API offering a dedicated isolated environment for your agent. |
| 3 | +A robust Model Context Protocol (MCP) server that enables AI agents to execute code across multiple programming languages in a secure, isolated environment. |
4 | 4 |
|
5 | | -## Features |
| 5 | +## Overview |
6 | 6 |
|
7 | | -- Execute code in multiple languages |
8 | | -- Handles compilation for compiled languages |
9 | | -- Clean and simple API |
10 | | -- Provides a dedicated isolated environment for secure code execution |
11 | | -- Supported languages: |
12 | | - - Java |
13 | | - - Python |
14 | | - - JavaScript |
15 | | - - TypeScript |
16 | | - - C++ |
| 7 | +This project implements a Model Context Protocol (MCP) server using Spring AI to provide code execution capabilities to AI agents. Think of MCP like a USB-C port for AI applications - it standardizes how AI models connect to different data sources and tools. This code executor server helps bridge the gap between language models and actual code execution. |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **Multi-language Support**: Execute code in Java, Python, JavaScript, TypeScript, and C++ |
| 12 | +- **Secure Execution**: Runs code in isolated environments with proper resource constraints |
| 13 | +- **MCP Integration**: Connects seamlessly with any MCP-compatible client like Claude Desktop |
| 14 | +- **Error Handling**: Provides detailed feedback for compilation and runtime errors |
| 15 | +- **Resource Management**: Automatically cleans up temporary files and enforces execution timeouts |
17 | 16 |
|
18 | 17 | ## How It Works |
19 | 18 |
|
20 | | -The application uses Spring Boot and Spring AI to expose code execution capabilities through the Model Context Protocol. The key components are: |
| 19 | +The server exposes a standardized MCP tool endpoint that allows AI assistants to: |
| 20 | + |
| 21 | +1. Submit code in a supported language |
| 22 | +2. Have it executed in a controlled environment |
| 23 | +3. Receive the execution output or error messages |
| 24 | + |
| 25 | +For compiled languages (Java, C++, TypeScript), the server automatically handles the compilation step before execution. |
| 26 | + |
| 27 | +## Getting Started |
| 28 | + |
| 29 | +### Prerequisites |
| 30 | + |
| 31 | +- Java 17 or higher |
| 32 | +- Python (for Python code execution) |
| 33 | +- Node.js and npm (for JavaScript/TypeScript) |
| 34 | +- g++ or compatible C++ compiler (for C++ code execution) |
| 35 | + |
| 36 | +### Running Locally |
| 37 | + |
| 38 | +```bash |
| 39 | +# Clone the repository |
| 40 | +git clone https://github.com/yourusername/mcp-code-executor-server.git |
| 41 | +cd mcp-code-executor-server |
| 42 | + |
| 43 | +# Build the project |
| 44 | +./mvnw clean package |
21 | 45 |
|
22 | | -1. **CodeExecutionService**: A service that creates temporary files with the provided code, executes them in the appropriate runtime, and returns the output. For compiled languages, it handles the compilation step automatically. |
| 46 | +# Run the server |
| 47 | +./mvnw spring-boot:run |
| 48 | +``` |
23 | 49 |
|
24 | | -2. **McpServerApplication**: The main Spring Boot application that registers the CodeExecutionService as a tool provider for AI systems. |
| 50 | +The server will start on port 8080 by default and register itself as an MCP server capable of executing code. |
| 51 | + |
| 52 | +### Connecting to Claude or Other MCP Clients |
| 53 | + |
| 54 | +1. Start your MCP client (e.g., Claude Desktop) |
| 55 | +2. Connect to the MCP server at `http://localhost:8080` |
| 56 | +3. The client will discover the available code execution tool |
| 57 | +4. You can now ask the AI to execute code through the MCP server |
25 | 58 |
|
26 | 59 | ## Containerized Deployment |
27 | 60 |
|
@@ -50,48 +83,29 @@ docker-compose logs -f |
50 | 83 | docker-compose down |
51 | 84 | ``` |
52 | 85 |
|
53 | | -The Docker setup includes: |
54 | | -- A non-root user for improved security |
55 | | -- Resource limits to prevent abuse |
56 | | -- Isolated execution environment |
57 | | -- Ephemeral storage for code execution |
58 | | - |
59 | | -## Safety Features |
60 | | - |
61 | | -- Execution timeouts (15 seconds by default) |
62 | | -- Temporary file cleanup |
63 | | -- Combined stdout/stderr output |
64 | | -- Error handling and compilation feedback |
| 86 | +## MCP Architecture |
65 | 87 |
|
66 | | -## Usage |
| 88 | +This project follows the Model Context Protocol's client-server architecture: |
67 | 89 |
|
68 | | -To run the application: |
69 | | - |
70 | | -```bash |
71 | | -./mvnw spring-boot:run |
72 | | -``` |
| 90 | +- **MCP Host**: AI applications like Claude Desktop that want to execute code |
| 91 | +- **MCP Client**: The protocol client that connects to our server |
| 92 | +- **MCP Server**: This application (exposes code execution capabilities through MCP) |
| 93 | +- **Resources**: The code execution service exposed as a standardized MCP tool |
73 | 94 |
|
74 | | -The MCP service will be available for AI systems to connect to and execute code. |
75 | | - |
76 | | -## API |
| 95 | +## Security Considerations |
77 | 96 |
|
78 | | -The service exposes the following tool endpoint through MCP: |
| 97 | +For production use, consider implementing: |
79 | 98 |
|
80 | | -- `executeCode(String language, String code)`: Execute code in the specified language |
| 99 | +- More restrictive resource limits (CPU, memory, execution time) |
| 100 | +- Stronger sandboxing for execution environments |
| 101 | +- Input validation and sanitization |
| 102 | +- Authentication and authorization for MCP connections |
| 103 | +- Network isolation for executed code |
81 | 104 |
|
82 | | -## Requirements |
| 105 | +## Contributing |
83 | 106 |
|
84 | | -- Java 17 or higher |
85 | | -- Python (for Python code execution) |
86 | | -- Node.js (for JavaScript and TypeScript code execution) |
87 | | -- g++ or compatible C++ compiler (for C++ code execution) |
88 | | -- TypeScript tools (for TypeScript code execution) |
89 | | - |
90 | | -## Security Considerations |
| 107 | +Contributions are welcome! Please feel free to submit a Pull Request. |
91 | 108 |
|
92 | | -This application is designed for demonstration purposes. In a production environment, consider: |
| 109 | +## License |
93 | 110 |
|
94 | | -- Adding resource limitations |
95 | | -- Implementing security sandboxing |
96 | | -- Adding authentication and authorization |
97 | | -- Restricting execution to trusted code only |
| 111 | +This project is licensed under the Apache License 2.0 - see the LICENSE file for details. |
0 commit comments