Python MCP server example with BMI calculator and weather retrieval functionalities
MCP (Model Context Protocol) is a universal adapter designed to facilitate seamless integration between AI applications and various data sources and tools through a standardized protocol. The MCP server acts as the intermediary layer, enabling AI applications like Claude Desktop, Continue, Cursor, and others to interact with specific functionalities such as calculating Body Mass Index (BMI) and fetching weather information. This document provides comprehensive documentation for the implementation and usage of the MCP server.
The MCP server is equipped with two core features: a BMI calculator tool and a weather fetcher tool. These tools are implemented using Python functions annotated with the @mcp.tool()
decorator, allowing them to be seamlessly integrated into AI workflows via the MCP protocol. The BMI calculation function takes weight in kilograms (kg) and height in meters (m), while the weather fetching function retrieves current weather data for a specified city.
@mcp.tool()
def calculate_bmi(weight_kg: float, height_m: float) -> float:
""" Calculate the BMI given weight in kg and height in meters"""
return weight_kg / (height_m ** 2)
@mcp.tool()
async def fetch_weather(city: str) -> str:
""" Fetch current weather for a city"""
async with httpx.AsyncClient() as client:
response = await client.get(f"https://api.weather.com/{city}")
return response.text
These functions demonstrate how the MCP server can expose functionality to AI applications through a standardized protocol, enabling them to perform specific tasks such as data processing and external API interactions.
The architecture of the MCP server is designed to be modular and flexible. The core function decorators and their execution are handled by the mcp.tool()
annotation, which defines the callable methods exposed over the MCP protocol. The implementation uses fastmcp
, a framework that simplifies the process of creating MCP-compatible services.
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
graph TD
A[MCP Server] -->|Exposes| B[Functionality]
B --> C[BMI Calculation]
B --> D[Weather Fetching]
C --> E[User Input Parameters]
D --> F[External APIs/Tools]
style A fill:#f3e5f5
The protocol flow diagram illustrates the interaction between an AI application, the MCP server, and external data sources or tools. The AI application uses an MCP client to send requests to the MCP server, which then communicates with appropriate functions or external services as defined in the architecture.
To set up and run the MCP server, follow these steps:
Install Dependencies:
pip install httpx fastmcp
Run the Server:
python server.py
These instructions provide a clear path for developers to rapidly deploy and test the MCP server in their AI workflows.
Health Monitoring System
Weather-Driven Application
The MCP server is compatible with multiple AI applications and clients as outlined in the following matrix:
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
This compatibility matrix highlights which functions of the MCP server are supported by various clients, ensuring seamless integration and alignment with developer requirements.
The performance and compatibility matrix for the MCP server are as follows:
Function | Response Time (ms) | Data Source | External Dependencies |
---|---|---|---|
BMI Calculation | 10-20 | Local Cache | None |
Weather Fetching | 50-100 | OpenWeather API | HTTPS Requests |
This matrix provides developers with an understanding of the performance characteristics and external dependencies associated with each functional component, facilitating informed decision-making.
The configuration file for customizing the MCP server includes specifying the command to start the server along with any required environment variables. An example is provided below:
{
"mcpServers": {
"[server-name]": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-[name]"],
"env": {
"API_KEY": "your-api-key"
}
}
}
}
Additional security measures such as secure API key management, access control policies, and log monitoring should be implemented to protect the server from unauthorized usage.
Q: How do I integrate MCP with my AI application?
A: Use an MCP client library in your AI application codebase to communicate with the MCP server over a standardized protocol.
Q: Which AI clients are supported by this MCP server?
A: The support matrix indicates that Claude Desktop, Continue, and Cursor can interact with the BMC calculator and weather fetching tools.
Q: Can I use this server with multiple AI applications simultaneously?
A: Yes, the MCP protocol enables concurrent requests from different clients without conflicting operations on shared resources.
Q: How does security work in this setup?
A: Secure API keys and access control policies ensure that only authorized client applications can interact with the server.
Q: What is the impact of external dependencies like OpenWeather API?
A: External service performance may affect latency, but caching strategies can mitigate such issues.
Contributions to this project are welcome from both individual contributors and organizations. Please ensure that all contributions adhere to the following guidelines:
The MCP ecosystem encompasses not only this server but also other tools, clients, and resources designed to enhance AI application capabilities. Developers are encouraged to explore the broader community for additional support and collaboration opportunities.
By leveraging the functionality of this MCP server, developers can significantly boost the utility and flexibility of their AI applications through seamless data source integration and tool usage.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration