This project demonstrates Model Context Protocol (MCP) tool calling capabilities. It shows how to build and connect MCP tools that can be chained together intelligently by language models.
This is a practical example of MCP tool calling:
- MCP Server with two tools (web search + weather)
- MCP Client that connects tools to GPT-5
- Tool Chaining - where one tool's output becomes another's input
- Automatic Tool Selection - the model decides which tools to use
The interesting part is watching how the model automatically chains tools together to solve complex queries.
You: What's the weather in Tokyo?
Assistant: Weather in Tokyo: clear sky, 15°C (feels like 13°C), humidity 45%.
You: What is the weather in the city where Zohran Mamdani was elected as mayor in 2025?
Assistant: Weather in New York City: clear sky, 7.2°C (feels like 4.3°C), humidity 52%.
Here's the tool calling sequence:
- Tool Call 1:
tavily_searchwith query "Zohran Mamdani elected mayor 2025" - Tool Response: Found he was elected mayor of NYC
- Tool Call 2:
get_weatherwith city "New York City" - Final Response: Combined result from both tool calls
This demonstrates automatic tool orchestration - the model analyzed the query and determined it needed two sequential tool calls.
You: What's the weather in Paris and find recent news about France
You: Check weather in London and search for best restaurants there
You: Find where the Taj Mahal is located and tell me the weather there
This demonstrates Model Context Protocol (MCP) - a standard for connecting language models to tools. Here's the architecture:
MCP Server (mcp_server.py):
- Implements MCP protocol using FastMCP
- Provides tool definitions and execution
- Handles stdio transport
MCP Client (mcp_client.py):
- Connects to MCP server
- Translates MCP tools to OpenAI function calling format
- Handles tool orchestration with GPT-5
Tool Selection Logic: GPT-5 analyzes each query and decides:
- Which tools are needed
- What arguments to pass
- What order to call them in
- How to use results from one tool in another
This all happens automatically through the OpenAI function calling API.
You need two free API keys:
- Tavily: Go to tavily.com and sign up
- OpenWeatherMap: Sign up at openweathermap.org
pip install fastmcp requests python-dotenv openaiCreate a .env file:
OPENAI_API_KEY=your_openai_key_here
OPENWEATHER_API_KEY=your_weather_key_here
TAVILY_API_KEY=your_tavily_key_herepython mcp_client.pyThat's it! Start asking questions.
- Searches the entire internet
- Gets AI-powered answers
- Way smarter than Google for specific questions
- Perfect for current events, research, finding locations
- Real-time weather for any city
- Temperature, humidity, conditions
- Works worldwide
- Handles different city name formats
├── mcp_server.py # The tools (web search + weather)
├── mcp_client.py # Chat interface with GPT-5
├── requirements.txt # What to install
└── README.md # This file
Without MCP: Language models are isolated - they can't access real-time data or perform actions.
With MCP: Language models become interfaces to any API or service - web search, weather, databases, file systems, etc.
The Power: Complex queries that require multiple data sources can be handled in a single conversation turn through automatic tool orchestration.
To see MCP tool calling in action:
- Single tool queries: "What's the weather in London?" (uses weather tool only)
- Chained tool queries: "What's the weather where [person] lives?" (search → weather)
- Complex queries: "Weather and news about [location]" (uses both tools independently)
Watch the console output to see the actual tool calls being made.
Adding new tools is straightforward:
- Define the tool with
@mcp.tool()decorator - Add the function that implements the tool logic
- Restart the server - the client will automatically discover new tools
Example tool structure:
@mcp.tool()
def my_new_tool(param: str) -> str:
"""Tool description for the language model"""
# Your tool logic here
return resultThe language model will automatically learn to use new tools based on their descriptions and parameters.
This demonstrates the extensibility of MCP for building custom tool ecosystems.