Content
# Model Context Protocol (MCP) with OpenAI
A simple implementation demonstrating how to use Anthropic's Model Context Protocol (MCP) with the OpenAI API.
## What is MCP?
Model Context Protocol (MCP) is an open standard by Anthropic that allows large language models to access external data and tools through a standardized interface. It enables models to interact with tools and resources while maintaining context.
## Prerequisites
- Python 3.10 or higher
- OpenAI API key
## Quick Start Guide
### 1. Install Required Packages
```bash
pip install mcp openai
```
### 2. Set Up Your Project
Clone this repository or download the files:
- `server.py` - MCP server with example tools
- `simple_client.py` - Client that demonstrates OpenAI integration
### 3. Configure Your API Key
Update `simple_client.py` with your OpenAI API key:
```python
OPENAI_API_KEY = "your_openai_api_key_here"
```
### 4. Run the Demo
```bash
python simple_client.py
```
This will:
1. Start the MCP server with example tools
2. Demonstrate calling the echo tool
3. Demonstrate calling the add tool
4. Show how to chain multiple tools together
## How It Works
### MCP Server (`server.py`)
The server defines resources and tools:
```python
# Define a tool that echoes a message
@mcp.tool()
def echo(message: str) -> str:
"""Echoes back the message provided"""
return f"Echo: {message}"
# Define a tool that performs addition
@mcp.tool()
def add(a: int, b: int) -> int:
"""Adds two numbers together"""
return a + b
```
### OpenAI Integration
The client demonstrates how to:
1. Define tool specifications for OpenAI
2. Send function calls to the MCP server
3. Return tool results to OpenAI
Example tool definition for OpenAI:
```python
{
"type": "function",
"function": {
"name": "echo",
"description": "Echoes back the message provided",
"parameters": {
"type": "object",
"properties": {
"message": {"type": "string"}
},
"required": ["message"]
}
}
}
```
## Creating Your Own Tools
To add new tools to the MCP server:
1. Add a new function to `server.py` with the `@mcp.tool()` decorator
2. Define the input parameters and return type
3. Add the corresponding tool definition in your OpenAI client
Example:
```python
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiplies two numbers together"""
return a * b
```
## Resources
- [MCP Documentation](https://modelcontextprotocol.io/)
- [OpenAI Function Calling API](https://platform.openai.com/docs/guides/function-calling)
Connection Info
You Might Also Like
everything-claude-code
Complete Claude Code configuration collection - agents, skills, hooks,...
markitdown
MarkItDown-MCP is a lightweight server for converting URIs to Markdown.
servers
Model Context Protocol Servers
servers
Model Context Protocol Servers
cc-switch
All-in-One Assistant for Claude Code, Codex & Gemini CLI across platforms.
Time
A Model Context Protocol server for time and timezone conversions.