DEV Community

Creating a Custom MCP Server with Flask to Power AI Code Execution

🚀 Understanding MCP (Machine Control Protocol)
MCP is a lightweight protocol that allows applications like Claude, VSCode, or ChatGPT to interact with code execution environments. Through a simple HTTP interface, these clients can send code, have it executed remotely, and receive the result — making automation and dynamic code evaluation much more flexible.

In this guide, you’ll learn how to build a minimal MCP-compliant server using Flask in Python, deploy it on Render, and connect it with any compatible tool.

🧰 Technologies Used

Lightweight web server: Python + Flask
Cloud hosting for deployment: Render.com

Open repository for source code: GitHub

🛠 Step 1: Build the MCP Server Using Python
a. Set Up Your Environment

mkdir flask-mcp-server
cd flask-mcp-server
python3 -m venv env
source env/bin/activate
pip install flask

Enter fullscreen mode Exit fullscreen mode

b. Create app.py

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/mcp', methods=['POST'])
def handle_mcp():
    data = request.get_json()
    code = data.get("code")
    language = data.get("language")

    if language == "python":
        try:
            local_vars = {}
            exec(code, {}, local_vars)
            return jsonify({"output": local_vars})
        except Exception as e:
            return jsonify({"error": str(e)})
    return jsonify({"error": "Unsupported language"}), 400

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Enter fullscreen mode Exit fullscreen mode

☁️ Step 2: Deploying to Render.com
Push your code to GitHub.

Sign up on Render.com.

Create a new Web Service with these options:

Build command: pip install -r requirements.txt

Start command: python app.py

Render will generate a URL like:
https://flask-mcp.onrender.com/mcp

🤖 Step 3: Test with an MCP-Compatible Client
Once your server is live, try sending Python code from Claude or a tool like Postman.

Sample Claude Prompt:

Use this MCP endpoint: https://flask-mcp.onrender.com/mcp
Send this Python code:
print("Hello MCP from Claude")

Enter fullscreen mode Exit fullscreen mode

📁 GitHub:
https://github.com/fabipm/mcp-flask-server

Included:

  • app.py Flask backend
  • Instructions for deploying to Render
  • Sample payload for testing

Top comments (0)