A minimal demonstration of the Model Context Protocol (MCP) using Python. This project shows how to:
- Create an MCP server that exposes a calculator tool
- Build a client that uses OpenAI's gpt-4o-mini to intelligently call the tool
- Demonstrate model-driven tool calling through MCP
The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). It enables AI models to securely access tools, data sources, and services through a unified interface.
mcp-demo/
βββ server.py # MCP server with calculator tool
βββ client.py # Client that uses OpenAI to call the tool
βββ requirements.txt # Python dependencies
βββ .env # Your API keys (create this)
βββ README.md # This file
β
Simple Calculator Tool - Performs add, subtract, multiply, divide operations
β
Proper JSON Schema - Well-defined input validation
β
Structured Output - Clean, formatted results
β
Lightweight Model - Uses OpenAI's gpt-4o-mini (fast & cost-effective)
β
Natural Language - Ask questions in plain English
β
Clean Code - Well-commented, easy to understand
- Python 3.10 or higher
- OpenAI API key (get one at https://platform.openai.com/api-keys)
python -m venv venv
source venv/bin/activate # On macOS/Linux
# or
venv\Scripts\activate # On Windowspip install -r requirements.txtCreate a .env file in the project root:
echo "OPENAI_API_KEY=your-api-key-here" > .envOr copy the example:
cp .env.example .env
# Then edit .env and add your actual API keyThe client automatically starts the server and runs a demo calculation:
python client.pyπ Starting MCP Client Demo
==================================================
β
Connected to MCP server
π Available tools: 1
β’ calculator: Perform basic arithmetic operations: add, subtract, multiply, divide
π¬ User prompt: What is 156 multiplied by 27?
π€ Asking OpenAI gpt-4o-mini to solve the problem...
β
Model decided to use a tool!
π§ Tool Call Details:
Name: calculator
Arguments: {'operation': 'multiply', 'a': 156, 'b': 27}
βοΈ Executing tool via MCP server...
β¨ Tool Result:
Result: 156 multiply 27 = 4212
π― Final Answer:
156 multiplied by 27 equals 4212.
==================================================
β
Demo completed successfully!
- Exposes a
calculatortool via MCP protocol - Defines a JSON schema for input validation
- Handles arithmetic operations: add, subtract, multiply, divide
- Returns structured results
- Connects to the MCP server via stdio
- Discovers available tools from the server
- Sends a natural language prompt to OpenAI
- OpenAI analyzes the prompt and decides to call the calculator tool
- Client executes the tool via MCP
- Returns the result to OpenAI for a natural language response
User Question β OpenAI (gpt-4o-mini) β Tool Call Decision
β
MCP Client
β
MCP Server β Calculator Tool
β
Result β OpenAI β Natural Answer
Edit the user_prompt in client.py:
# Examples:
user_prompt = "What is 156 multiplied by 27?"
user_prompt = "Calculate 100 divided by 5"
user_prompt = "Add 45 and 67"
user_prompt = "What's 89 minus 23?"In server.py, add more tools to the list_tools() function:
@app.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(name="calculator", ...),
Tool(name="weather", ...), # Add your new tool
]Then handle them in call_tool():
@app.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "calculator":
# ... existing code
elif name == "weather":
# ... your new tool logic- Make sure you created a
.envfile - Verify it contains:
OPENAI_API_KEY=sk-...
- Activate your virtual environment
- Run
pip install -r requirements.txt
- The client automatically starts the server
- Make sure
server.pyis in the same directory - Check that Python is in your PATH
- Transport: stdio (standard input/output)
- Format: JSON-RPC 2.0
- Tools: JSON Schema for validation
- Provider: OpenAI
- Model: gpt-4o-mini
- Features: Function calling, tool use
MIT - Feel free to use this demo for learning and building your own MCP tools!
Happy Building! π