CPPCon MCP Server
This project implements a Model Context Protocol (MCP) server that provides a tool to compile C++ code using the Compiler Explorer (godbolt.org) API. It allows clients to request the assembly output for given C++ code and compiler options, supporting both standard tool invocation and Server-Sent Events (SSE) for streaming results.
Features
- MCP Compliant: Built using
mcp-server[fastmcp], ensuring adherence to the Model Context Protocol. get_assemblyTool: Exposes a tool that compiles C++ code using MSVC (via godbolt.org) and returns the generated assembly.- SSE Support: Provides a Server-Sent Events endpoint (
/sse) for streaming compilation status and results. - Tool Discovery: Exposes a
/capabilitiesendpoint for clients to discover available tools and their schemas. - Client Messaging: Supports a
/messageendpoint for general client-to-server communication.
Setup
Prerequisites
- Python 3.11 or higher
Installation
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` -
Install dependencies:
pip install "mcp-server[fastmcp]" uvicorn requests pydantic
Running the Server
To start the MCP server, execute the server.py script:
python server.py
The server will run on http://127.0.0.1:8000 by default.
Usage
The server exposes several endpoints compliant with the Model Context Protocol.
1. Get Server Capabilities
Clients can discover the server's capabilities, including available tools, by sending a GET request to the /capabilities endpoint.
curl http://127.0.0.1:8000/capabilities
2. Invoke get_assembly Tool (Standard POST)
To get the assembly output for C++ code in a single request/response cycle, send a POST request to the /tool/get_assembly endpoint.
Example Request:
curl -X POST
-H "Content-Type: application/json"
-d '{"code": "int main() { return 0; }", "options": "/O2"}'
http://127.0.0.1:8000/tool/get_assembly
3. Stream get_assembly Results (Server-Sent Events)
For streaming compilation status and results, connect to the /sse endpoint using GET. The code and options for compilation are passed as URL query parameters. An optional sessionId can also be provided for client tracking.
Example Request:
curl -N "http://127.0.0.1:8000/sse?code=int%20main()%20%7B%20return%200%3B%20%7D&options=/O2&sessionId=my_compiler_session_123"
The server will stream events in the following format:
event: message
data: {"status": "starting_compilation", "sessionId": "my_compiler_session_123"}
event: result
data: {"type": "text", "text": "; Assembly output here..."}
event: message
data: {"status": "compilation_complete", "sessionId": "my_compiler_session_123"}
In case of an error, an event: error will be sent:
event: error
data: {"type": "server_error", "text": "Error details here...", "sessionId": "my_compiler_session_123"}
4. Send Client Messages
Clients can send general messages to the server via a POST request to the /message endpoint. These messages can be used for various client-initiated communications.
Example Request:
curl -X POST
-H "Content-Type: application/json"
-d '{"type": "client_ping", "sessionId": "my_compiler_session_123", "data": "Are you alive?"}'
http://127.0.0.1:8000/message
The server will respond with:
{"status": "message_received", "sessionId": "my_compiler_session_123"}
5. Test Server Using modelcontextprotocol/inspector
npx @modelcontextprotocol/inspector
6. Configure using VSCode http MCP server command create mcp.json and start tool
{
"servers": {
"my-mcp-server-4620e938": {
"url": "http://localhost:8000/sse",
"type": "http"
}
},
"inputs": []
}