veeravel86/mcp_client
If you are the rightful owner of mcp_client and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This document provides a comprehensive overview of a Model Context Protocol (MCP) server implementation with OpenAI integration, designed to facilitate seamless interaction through both web and CLI interfaces.
MCP Client & Server
A complete implementation of the Model Context Protocol (MCP) with OpenAI integration, featuring both CLI and web-based interfaces.
š Features
- MCP Client with OpenAI Integration - Connect to MCP servers and use tools via GPT-4
- Web-Based GUI - Modern, responsive interface for easy interaction
- CLI Interface - Command-line version for terminal enthusiasts
- Real-Time Logging - View complete request/response logs with pretty JSON formatting
- Built-in MCP Server - Example server with weather tools and math operations
- Tool & Prompt Support - Full MCP implementation with tools, resources, and prompts
š Table of Contents
- Prerequisites
- Installation
- Quick Start
- Usage
- MCP Server Tools
- Architecture
- Configuration
- Development
- Troubleshooting
š§ Prerequisites
Before you begin, ensure you have:
- Python 3.8+ installed on your system
- OpenAI API Key - Get one from OpenAI Platform
- Git (for cloning the repository)
š„ Installation
1. Clone the Repository
git clone https://github.com/veeravel86/mcp_client.git
cd mcp_client
2. Create Virtual Environment
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
3. Install Dependencies
pip install -r requirements.txt
4. Set Up Environment Variables
# Set your OpenAI API key
export OPENAI_API_KEY="your-api-key-here"
# Optional: Set custom MCP server path
export MCP_SERVER_SCRIPT="/path/to/mcp_server.py"
For persistent setup, add to your shell profile (~/.bashrc
, ~/.zshrc
):
echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.zshrc
source ~/.zshrc
š Quick Start
Option 1: Web Interface (Recommended)
# Make sure virtual environment is activated
source venv/bin/activate
# Start the web server
python mcp_client_web.py
Then open your browser to: http://localhost:5001
Option 2: Command Line Interface
# Make sure virtual environment is activated
source venv/bin/activate
# Run the CLI client
python mcp_client.py
Type your queries and press Enter. Type quit
, exit
, or q
to exit.
š Usage
Using the Web Interface
-
Start the Server
python mcp_client_web.py
-
Open Browser
- Navigate to
http://localhost:5001
- Navigate to
-
Connect to MCP Server
- Click the "Connect to Server" button
- Wait for connection confirmation
-
View Logs (Optional)
- Click "š View Logs" to see detailed request/response data
- Click "šļø Clear Logs" to clear the log history
-
Start Chatting
- Type your query in the input field
- Press Enter or click "Send"
- Watch tool executions in real-time
Example Queries
# Math operations
"Add 25 and 17"
"What's 100 plus 250?"
# Weather information
"What's the weather forecast for New York?"
"Get weather alerts for California"
# Simple tests
"Echo hello world"
š ļø MCP Server Tools
The included MCP server provides these tools:
1. echo
- Purpose: Echo back a message
- Parameters:
message
(string) - Example: "Echo hello world"
2. add_numbers
- Purpose: Add two numbers
- Parameters:
a
(number),b
(number) - Example: "Add 25 and 17"
3. get_weather_forecast
- Purpose: Get weather forecast for coordinates
- Parameters:
latitude
(number),longitude
(number) - Example: "What's the weather in New York?" (OpenAI knows NYC coordinates)
- API: Uses National Weather Service API
4. get_weather_alerts
- Purpose: Get active weather alerts for a US state
- Parameters:
state
(2-letter state code) - Example: "Get weather alerts for California"
- API: Uses National Weather Service API
Prompts
The server also provides pre-written prompt templates:
- math_problem_solver - Solve math problems step-by-step
- calculate_total - Calculate totals with breakdown
- analyze_weather - Analyze weather conditions
šļø Architecture
āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
ā Web GUI ā āāāā¶ ā MCP Client ā āāāā¶ ā OpenAI ā
ā (Browser) ā āāāā ā (Python) ā āāāā ā GPT-4 ā
āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
ā ā
ā ā
ā¼ ā¼
āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
ā MCP Server ā ā Tool Call ā
ā (Python) ā ā Decision ā
āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
ā
ā¼
āāāāāāāāāāāāāāā
ā External ā
ā APIs (NWS) ā
āāāāāāāāāāāāāāā
Flow:
- User enters query in web interface
- Client sends query + tool schemas to OpenAI
- OpenAI decides which tools to call
- Client executes tools on MCP server
- Server returns results to client
- Client sends results back to OpenAI
- OpenAI generates natural language response
- Response displayed to user
āļø Configuration
Environment Variables
Variable | Description | Required | Default |
---|---|---|---|
OPENAI_API_KEY | Your OpenAI API key | Yes | None |
MCP_SERVER_SCRIPT | Path to MCP server script | No | ./mcp_server.py |
Port Configuration
By default, the web server runs on port 5001
. To change:
Edit mcp_client_web.py
line 240:
app.run(debug=True, host='0.0.0.0', port=5001, use_reloader=False)
š» Development
Project Structure
mcp_client/
āāā mcp_client.py # CLI client
āāā mcp_client_web.py # Web server (Flask)
āāā mcp_client_gui.py # Tkinter GUI (legacy)
āāā mcp_server.py # MCP server with tools
āāā requirements.txt # Python dependencies
āāā .env.example # Example environment config
āāā .gitignore # Git ignore rules
āāā templates/
ā āāā index.html # Web interface HTML
āāā README.md # This file
Adding New Tools
To add a tool to the MCP server:
- Define the tool in
mcp_server.py
:
@app.list_tools()
async def handle_list_tools() -> list[Tool]:
return [
# ... existing tools ...
Tool(
name="your_tool_name",
description="What your tool does",
inputSchema={
"type": "object",
"properties": {
"param1": {
"type": "string",
"description": "Parameter description"
}
},
"required": ["param1"]
}
)
]
- Implement the handler:
@app.call_tool()
async def handle_call_tool(name: str, arguments: dict) -> list[TextContent]:
if name == "your_tool_name":
param1 = arguments.get("param1")
# Your logic here
return [TextContent(type="text", text=f"Result: {result}")]
- Restart the server and it's ready to use!
š Troubleshooting
Common Issues
1. Port Already in Use
Error: Address already in use
or Port 5001 is in use
Solution:
- Change the port in
mcp_client_web.py
- Or kill the process using the port:
lsof -ti:5001 | xargs kill
2. OpenAI API Key Not Found
Error: OPENAI_API_KEY environment variable is required
Solution:
export OPENAI_API_KEY="your-key-here"
# Or add to .env file
3. Module Not Found
Error: ModuleNotFoundError: No module named 'mcp'
Solution:
# Ensure virtual environment is activated
source venv/bin/activate
# Reinstall dependencies
pip install -r requirements.txt
4. Connection Failed
Error: Client can't connect to MCP server
Solution:
- Check that
MCP_SERVER_SCRIPT
path is correct - Ensure
python3
is available in PATH - Check server logs for errors
5. Tkinter Not Available (GUI)
Error: ImportError: No module named '_tkinter'
Solution: Use the web interface instead (mcp_client_web.py
), which doesn't require Tkinter.
š¦ Dependencies
- mcp - Model Context Protocol SDK
- openai - OpenAI Python client
- flask - Web framework
- flask-cors - CORS support
- httpx - HTTP client for async requests
- pydantic - Data validation
- python-dotenv - Environment variable management
š¤ Contributing
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name
- Make your changes
- Commit:
git commit -m "Add feature"
- Push:
git push origin feature-name
- Create a Pull Request
š License
This project is open source and available under the MIT License.
š Acknowledgments
- Built with Model Context Protocol
- Powered by OpenAI GPT-4
- Weather data from National Weather Service API
š Support
For issues, questions, or contributions:
- Open an issue on GitHub
- Check the MCP Documentation
Made with ā¤ļø using Claude Code and MCP