MCP Hello World Example
by wubbyweb
This project demonstrates a simple implementation of the Model Context Protocol (MCP) using a Flask server and OpenAI integration. It showcases how to create an MCP server with a number addition function and a client that uses it through OpenAI's function calling.
View on GitHub
Last updated: N/A
MCP Hello World Example
This project demonstrates a simple implementation of the Model Context Protocol (MCP) using a Flask server and OpenAI integration. It showcases how to create an MCP server that provides a number addition function and a client that can use this function through OpenAI's function calling capabilities.
Components
Server (server.py
)
- A Flask-based MCP server that implements:
- A simple addition function (
add_numbers
) - MCP protocol endpoints for function listing and execution
- JSON schema definition for the addition function
- Runs on
http://localhost:8080
- A simple addition function (
Client (client.py
)
- Integrates with OpenAI's API and the MCP server
- Demonstrates:
- Fetching available functions from the MCP server
- Converting MCP functions to OpenAI function specifications
- Making function calls through OpenAI's chat completions
- Processing and displaying results
Prerequisites
- Python 3.x
- OpenAI API key
Installation
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
Configuration
- Set your OpenAI API key:
export OPENAI_API_KEY="your-api-key-here"
Usage
- Start the MCP server:
python server.py
- In a separate terminal, run the client:
python client.py
The client will:
- Connect to the MCP server
- Fetch available functions
- Start a conversation with OpenAI's model
- Demonstrate function calling by adding two numbers (42 and 17)
- Display the results
Project Structure
.
├── README.md # Project documentation
├── requirements.txt # Python dependencies
├── server.py # MCP server implementation
└── client.py # OpenAI client implementation
Dependencies
python-dotenv
: Environment variable managementopenai
: OpenAI API clientflask
: Web server frameworkrequests
: HTTP client library
Example Output
When running the client, you'll see:
- Available MCP functions listing
- Model's decision to use the addition function
- Function execution results
- Final response from the model
Contributing
Feel free to submit issues and enhancement requests!