Demonstrates integration with OpenAI's Chat Completions API, allowing the language model to discover and call MCP tools (ping, add, subtract) through function calling.
Explore MCP
This repo runs a local MCP (Model Context Protocol) server, verifies tool connectivity with a client, and demonstrates OpenAI tool-calling integration.
Prerequisites
Python 3.13 (per
pyproject.toml).Linux/macOS shell (commands shown for bash).
An OpenAI API key.
Dependencies listed in
pyproject.toml.
Install dependencies
You can use uv, poetry, or pip. Pick one approach.
Option A: uv (fast, recommended)
Option B: poetry
Option C: pip + venv
Set OpenAI API key
The script utils/helpers.py expects your key in a file at:
~/.llm_secretswith content like:
Alternatively, you can set the environment variable directly in your shell before running the OpenAI test:
Note: call_my_mcp2.py imports get_api_key() and sets os.environ["OPENAI_API_KEY"] internally using the ~/.llm_secrets file. If that file is missing, set the env var as shown above or create the file.
Start the MCP server
This launches the FastAPI app serving the MCP endpoint at http://127.0.0.1:8000/mcp.
Expected log: uvicorn starts and listens on port 8000.
Health check: MCP client
In a separate terminal (while the server is running), verify connectivity and tool discovery.
Expected output: A tools list including ping, add, and subtract.
OpenAI integration test
With the server running and your OpenAI key available, run the LLM orchestration demo:
What it does:
Discovers MCP tools from the server.
Provides those tools to the OpenAI Chat Completions API.
Lets the model call
addand returns the final answer.
Expected: The printed result of adding two numbers (e.g., 68 for 23 + 45).
Notes
The script uses model
gpt-5. If this model is not available on your account, change it insidecall_my_mcp2.pyto a model you have access to (e.g.,gpt-4.1-minior another supported tools-capable model).Ensure the server URL in
call_my_mcp2.pyandmcp_client_check.pymatcheshttp://127.0.0.1:8000/mcp.If port 8000 is in use, edit
mcp_server.pyto run uvicorn on a different port and update the client scripts accordingly.
Files
mcp_server.py: FastAPI + FastMCP server exposing tools:ping,add,subtract.mcp_client_check.py: Async client that pings the server and lists tools.call_my_mcp2.py: Orchestrates an OpenAI chat with MCP tools.utils/helpers.py: Utility to load the OpenAI API key from~/.llm_secretsor set env vars from a JSON file.