References a GitHub repository containing example code for a weather forecast functionality
References an article about building MCP servers with authentication using FastAPI
MCP (Model Context Protocol) Python Examples
This repository contains examples demonstrating how to build and interact with MCP servers and clients in Python. It covers basic integration, authentication, and an advanced example of a GenAI-powered agent that uses MCP tools.
Features
- Authenticated FastAPI Server: An example of integrating an MCP server into a FastAPI application with bearer token authentication.
- Standalone Tool Server: A standalone MCP server providing weather tools using the National Weather Service (NWS) API.
- Simple Python Client: A basic client for calling a specific tool on the authenticated server.
- GenAI Agent Client: An advanced client that uses Google's Gemini Pro to understand natural language queries and decide which tool to call on the weather server.
Prerequisites
- Python 3.10+
pip
andvirtualenv
(orpyenv
)make
(for Linux/macOS) or a standard Windows Command Prompt
Setup
- Clone the repository:
- Run the setup script:
This will create a Python virtual environment in a
venv
directory and install all the required dependencies.For Linux/macOS:For Windows: - Activate the virtual environment:
After the setup is complete, you need to activate the environment in your shell.For Linux/macOS:For Windows Command Prompt:
- Set up environment variables:
The GenAI agent client requires an API key from Google.A file named
.env.example
is included in the repository. Copy it to a new file named.env
.On Linux/macOS:On Windows Command Prompt:Add your Google AI API key to the.env
file:
How to Run the Examples
This project provides convenient scripts to simplify running the examples.
- For Linux and macOS users: A
Makefile
is included. Use commands likemake run-remote-server
. - For Windows users: A
run.bat
script is provided for the Command Prompt. Use commands likerun.bat run-remote-server
.
The following sections show commands for both environments. The main difference to be aware of is how environment variables are set:
- Windows (
cmd.exe
):set MCP_CLIENT_TOKEN=my_value
- Linux/macOS/PowerShell:
export MCP_CLIENT_TOKEN=my_value
Example 1: Authenticated FastAPI Server (greet
tool)
This example demonstrates an MCP server integrated with FastAPI that requires bearer token authentication.
1. Run the Server
The server will start on http://localhost:8000
.
2. Run the Client
In a new terminal, first set the required environment variable for the authentication token. For this example, any string will work.
For Linux/macOS:
For Windows Command Prompt:
Expected Output
(Optional) Testing with curl
You can also test the server endpoints directly with curl
.
- Unauthenticated request (will fail):Output:
{"detail":"Not authenticated"}
- Authenticated request (will succeed):Output:
Example 2: GenAI-Powered Weather Agent
This example showcases a more advanced use case: a standalone MCP server providing weather tools and a client that uses a Large Language Model (Gemini) to answer natural language questions by calling those tools.
1. Run the Weather Tool Server
This server provides get_forecast
and get_alerts
tools. It will run on http://localhost:8123
.
2. Run the Agent Client
In a new terminal, run the agent client. Make sure you have set your GENAI_API_KEY
in the .env
file as described in the Setup section.
The client will start an interactive chat loop. The helper scripts run example/client/client.py
.
Usage
Once the client is running, you can ask it questions about the weather.
Example Query:
Expected Output:
The client will show the internal reasoning of the model, including the tool call it decides to make. Note that for "Boston", the client code specifically uses hardcoded coordinates.
You can ask for other locations as well, but the model will need to infer the latitude and longitude.
References
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
A standardized interface for agent-to-agent communication that enables composability, supports streaming, and implements server-sent events (SSE) for real-time interaction.
Related MCP Servers
- -securityFlicense-qualityIntegrates with the AgentCraft framework to enable secure communication and data exchange between AI agents, supporting both premade and custom enterprise AI agents.Last updated -1Python
- -securityAlicense-qualityA Model Context Protocol server that allows AI agents to interact with n8n workflows through natural language, enabling workflow management and execution via SSE connections.Last updated -1765TypeScriptMIT License
- -securityFlicense-qualityA Model Context Protocol server that enables real-time communication using Server-Sent Events (SSE), providing standardized model management and resource templating capabilities.Last updated -TypeScript
- -securityFlicense-qualityAn auto-generated Multi-Agent Conversation Protocol Server that enables interaction with the IX-API (Internet Exchange API) through natural language, allowing for management of interconnection services in data centers.Last updated -Python