From 4375e68e9669fdf5eb9626a322b67769ef0b065d Mon Sep 17 00:00:00 2001
From: David Soria Parra <167242713+dsp-ant@users.noreply.github.com>
Date: Wed, 20 Nov 2024 23:48:36 +0000
Subject: [PATCH] Documentation edits made through Mintlify web editor
---
docs/first-server/python.mdx | 78 +++++++++++++++---------------------
1 file changed, 33 insertions(+), 45 deletions(-)
diff --git a/docs/first-server/python.mdx b/docs/first-server/python.mdx
index 31fc673..3c7c5bb 100644
--- a/docs/first-server/python.mdx
+++ b/docs/first-server/python.mdx
@@ -6,15 +6,15 @@ description: "Create a simple MCP server in Python in 15 minutes"
Let's build your first MCP server in Python! We'll create a weather server that provides current weather data as a resource and lets Claude fetch forecasts using tools.
-This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWeatherMap](https://openweathermap.org/api) to follow along.
+ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWeatherMap](https://openweathermap.org/api) to follow along.
## Prerequisites
- You'll need Python 3.9 or higher:
-
+ You'll need Python 3.10 or higher:
+
```bash
python --version # Should be 3.9 or higher
pip --version
@@ -25,19 +25,19 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
```bash
mkdir weather-server
cd weather-server
- python -m venv venv
- source venv/bin/activate # On Windows use: venv\Scripts\activate
+ uv init --package --app --name weather-server
```
```bash
- pip install modelcontextprotocol requests python-dotenv
+ uv add mcp
```
Create `.env`:
+
```bash
OPENWEATHER_API_KEY=your-api-key-here
```
@@ -61,7 +61,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
import json
from datetime import datetime
from typing import Any, Dict, List, Optional
-
+
import requests
from dotenv import load_dotenv
from modelcontextprotocol import Server, StdioServerTransport
@@ -127,7 +127,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
print("Weather MCP server running on stdio", flush=True)
# Handler implementations will continue in next sections
-```
+ ```
@@ -298,6 +298,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
```
Create `setup.py`:
+
```python
from setuptools import setup
@@ -331,6 +332,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
) -> Dict[str, Any]:
# ...
```
+
Python type hints help catch errors early and improve code maintainability.
@@ -348,6 +350,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
}]
}
```
+
Resources provide data that Claude can access as context.
@@ -365,6 +368,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
}
}
```
+
Tools let Claude take actions through your server with validated inputs.
@@ -372,10 +376,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
## Best practices
-
+
```python
try:
response = self.session.get(...)
@@ -388,10 +389,7 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
```
-
+
```python
if not isinstance(args, dict) or "city" not in args:
raise McpError(
@@ -401,20 +399,14 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
```
-
+
```python
if not API_KEY:
raise ValueError("OPENWEATHER_API_KEY is required")
```
-
+
```python
API_CONFIG = {
"BASE_URL": "...",
@@ -432,12 +424,14 @@ This guide uses the OpenWeatherMap API. You'll need a free API key from [OpenWea
While this guide uses stdio transport, MCP supports multiple transport options:
### WebSocket
+
```python
from modelcontextprotocol import WebSocketServerTransport
transport = WebSocketServerTransport()
```
### SSE (Server-Sent Events)
+
```python
from modelcontextprotocol import SSEServerTransport
transport = SSEServerTransport("/events", response)
@@ -446,6 +440,7 @@ transport = SSEServerTransport("/events", response)
## Troubleshooting
### Installation issues
+
```bash
# Check Python version
python --version
@@ -456,6 +451,7 @@ pip install -r requirements.txt
```
### Runtime errors
+
```bash
# Enable debug logging
export MCP_LOG_LEVEL=DEBUG
@@ -463,6 +459,7 @@ python weather_server.py
```
### Type checking
+
```bash
# Install mypy
pip install mypy
@@ -493,7 +490,9 @@ mypy weather_server.py
1. Quit Claude completely
+
2. Start Claude again
+
3. Look for your weather server in the 🔌 menu
@@ -503,6 +502,7 @@ mypy weather_server.py
Ask Claude:
+
```
What's the current weather in San Francisco? Can you analyze the conditions and tell me if it's a good day for outdoor activities?
```
@@ -510,6 +510,7 @@ mypy weather_server.py
Ask Claude:
+
```
Can you get me a 5-day forecast for Tokyo and help me plan what clothes to pack for my trip?
```
@@ -517,6 +518,7 @@ mypy weather_server.py
Ask Claude:
+
```
Can you analyze the forecast for both Tokyo and San Francisco and tell me which city would be better for outdoor photography this week?
```
@@ -692,27 +694,21 @@ mypy weather_server.py
## Deployment
-
+
```dockerfile
FROM python:3.11-slim
-
+
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
-
+
COPY . .
-
+
CMD ["python", "weather_server.py"]
```
-
+
```ini
[Unit]
Description=Weather MCP Server
@@ -732,19 +728,11 @@ mypy weather_server.py
## Next steps
-
+
Learn more about the MCP architecture
-
+
Read more about the Python SDK
\ No newline at end of file