Expose Mcp
Goal
Turn a Python module into an MCP-compatible service that AI agents can discover and call - with zero MCP server code, no tool definitions, and no OpenAPI specs.
What You'll See
- Create a small Python module with public methods.
- Host it through Graftcode Gateway using Docker - the gateway automatically exposes an MCP endpoint.
- Connect an AI tool (Cursor or Claude Desktop) to the MCP endpoint.
- Ask the AI agent to call your methods - it discovers and invokes them through the Model Context Protocol.
Prerequisites
- Docker installed and running
- Python installed locally
- An AI tool with MCP support - for example Cursor or Claude Desktop
Step 1. Create a project folder
Create a new folder and initialize a Python project:
mkdir py-ai-backend cd py-ai-backend
Create module meta data file pyproject.toml:
[project] name = "energy-service" version = "1.0.0" requires-python = ">=3.8" description = "Test module for local directory analysis"
Step 2. Write a Python module with public methods
Create a file energy_price_calculator.py:
import random class EnergyPriceCalculator: @staticmethod def get_price(): return random.randint(100, 104) @staticmethod def calculate_bill(kwh_used): price_per_kwh = random.randint(100, 104) return kwh_used * price_per_kwh
This is a plain Python class - no decorators, no frameworks, no MCP-specific annotations. Any public method you write here will automatically become a callable MCP tool once hosted through Graftcode Gateway.
Step 3. Host it with Graftcode Gateway
Create a Dockerfile in the project root:
FROM python:3.13-bookworm WORKDIR /usr/app COPY ./energy_price_calculator.py /usr/app/energy-service/ COPY ./pyproject.toml /usr/app/energy-service/ RUN apt-get update \ && apt-get install -y wget \ && wget -O /usr/app/gg.deb https://github.com/grft-dev/graftcode-gateway/releases/latest/download/gg_linux_amd64.deb \ && dpkg -i /usr/app/gg.deb \ && rm /usr/app/gg.deb \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* EXPOSE 80 EXPOSE 81 CMD ["gg","--modules","./energy-service/"]
gg (Graftcode Gateway) reads your python module, discovers all public methods, and exposes them automatically - both as Grafts for app-to-app calls and for static methods also as MCP tools for AI agents. Port 80 handles service calls, port 81 serves Graftcode Vision and the MCP endpoint.
Build and run the container:
docker build --no-cache --pull -t py-ai-backend:test . docker run -d -p 80:80 -p 81:81 --name graftcode_mcp_demo_py py-ai-backend:test
Your Python service is now running with an MCP endpoint exposed automatically by Graftcode Gateway.
Step 4. Explore the service in Graftcode Vision
Open http://localhost:81/GV in your browser.
You will see all public methods from your Python module - their names, parameter types, and return types. Every method listed here is also available as an MCP tool that AI agents can discover and call. Graftcode Vision also provides:
- A "Try it out" button to call methods live, directly from the browser.
- A package manager command to install this service as a strongly-typed client in any other application.
Step 5. Connect an AI tool to the MCP endpoint
Graftcode Gateway exposes an MCP (Model Context Protocol) endpoint alongside your service automatically. Point your AI tool at it.
For Cursor, create or edit .cursor/mcp.json in your project root (or navigate to File > Preferences > Cursor Settings > Tools & MCP and press New MCP Server and add definition from below):
{ "mcpServers": { "energy-service": { "url": "http://localhost:81/mcp" } } }
The same can be applied For Claude Desktop, editing your claude_desktop_config.json.
The AI tool now sees your Python methods as callable tools - with their names, parameters, and return types - discovered automatically through MCP.
Step 6. Call your methods through AI
Open your AI tool and ask it to use your service. For example, in Cursor:
Note
"What is the current energy price?"
The AI agent discovers EnergyPriceCalculator.get_price() through MCP and calls it directly. You can also try:
Note
"Calculate the energy bill for 250 kWh"
The agent calls EnergyPriceCalculator.calculate_bill(250) and returns the result. No prompt engineering, no tool definitions in your code - MCP handles discovery and invocation automatically.
Step 7. Run with a Project Key (recommended for real-world usage)
Everything above works without any account - perfect for learning and local development. When you're ready for real-world usage, create a free account at portal.graftcode.com, set up a project, and copy its Project Key.
Then pass the key when starting your gateway:
CMD ["gg","--modules","./energy-service/", "--projectKey", "YOUR_PROJECT_KEY"]
A Project Key gives you:
- Stable MCP URL - AI tools connect to a permanent endpoint that doesn't change when you redeploy.
- Stable registry URL - consumers always find and update your Graft through a permanent address, so install commands don't change when you redeploy.
- Portal visibility - see all your gateways and exposed services in one place at gateways.graftcode.com.
- Access control - decide who can access your MCP endpoint and download your Grafts using package manager authentication and permissions.