2.2 — Building Your First MCP Server — File System & Database Access
Building Your First MCP Server
This lesson teaches you to build a production-ready MCP server in Python. You'll create a "Crypto Price Tracker" MCP that fetches live cryptocurrency prices, allowing Claude to analyze crypto markets in real-time.
Setup: MCP SDK Installation
pip install mcp
This installs Anthropic's MCP SDK (server and client libraries).
Building Crypto Price Tracker MCP
File Structure:
crypto-mcp/
├── server.py (MCP server)
├── requirements.txt
└── CLAUDE.md
Step 1: Write server.py
import asyncio
import json
from mcp.server import Server
from mcp.types import Tool, Resource, TextContent
import httpx
app = Server("crypto-tracker")
# Resource: Live crypto prices
@app.resource("crypto://prices")
async def get_crypto_prices():
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.coingecko.com/api/v3/simple/price",
params={"ids": "bitcoin,ethereum,cardano", "vs_currencies": "usd,pkr"}
)
prices = response.json()
return TextContent(
mime_type="application/json",
text=json.dumps(prices, indent=2)
)
# Tool: Analyze crypto trends
@app.tool("analyze_crypto_trend")
async def analyze_trend(crypto: str, days: int = 7):
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://api.coingecko.com/api/v3/coins/{crypto}/market_chart",
params={"vs_currency": "usd", "days": days}
)
data = response.json()
prices = data["prices"]
current = prices[-1][1]
previous = prices[0][1]
change = ((current - previous) / previous) * 100
return f"{crypto.capitalize()} changed {change:.2f}% in last {days} days"
if __name__ == "__main__":
asyncio.run(app.run())
Step 2: requirements.txt
mcp>=0.1.0
httpx>=0.24.0
Step 3: Test Server
python server.py
Server runs on localhost:3000. Claude Code can now call it.
Using Your MCP Server
From Claude Code:
claude run "What's the current price of Bitcoin in PKR?"
Claude (via your MCP server):
- Calls
crypto://pricesresource - Gets live data from CoinGecko API
- Returns: "Bitcoin is currently PKR 5,400,000"
Analyze crypto trends:
claude run "Ethereum's trend over 30 days?"
Claude calls analyze_crypto_trend(ethereum, 30) tool, returns analysis.
Pakistan-Specific MCP: JazzCash Integration
Pakistani developer builds "JazzCash MCP" (payments):
from mcp.server import Server
import requests
app = Server("jazzcash-integration")
@app.tool("process_payment")
async def process_payment(phone: str, amount: int, description: str):
# Call JazzCash API
payload = {
"pp_merchant_id": "YOUR_MERCHANT_ID",
"pp_password": "YOUR_PASSWORD",
"pp_amount": amount,
"pp_bill_ref": description,
"pp_cell": phone
}
response = requests.post(
"https://sandbox.jazzcash.com.pk/ApplicationAPI/API/Payment/DoTransaction",
json=payload
)
return response.json()
@app.tool("check_payment_status")
async def check_status(transaction_id: str):
# Check payment status
response = requests.post(
"https://sandbox.jazzcash.com.pk/ApplicationAPI/API/Payment/QueryTransaction",
json={"transaction_id": transaction_id}
)
return response.json()
if __name__ == "__main__":
asyncio.run(app.run())
Now Claude can process payments in real-time:
claude run "Process PKR 1,000 payment from 03001234567 for invoice INV-001"
Claude calls MCP tool → JazzCash API → Payment processed. All seamlessly.
Practice Lab
Task 1: Build Crypto MCP — Create the Crypto Price Tracker MCP. Test that it works: (1) Run server, (2) Call it from Claude Code, (3) Verify live prices are fetched.
Task 2: Extend with Pakistan — Add a new tool to Crypto MCP: convert_to_pkr(amount_usd) that converts USD prices to PKR using live exchange rates (XE.com API).
Pakistan Example
Bilal builds "Stock Market MCP" for Pakistani stocks (PSX):
@app.resource("psx://stocks")
async def get_psx_stocks():
# Fetch PSX data (would integrate with PSX API if available)
stocks = {
"HBL": 210.50, # HBL Bank
"LUCK": 545.80, # Lucky Cement
"ENGRO": 128.75 # Engro Chemical
}
return TextContent(text=json.dumps(stocks))
@app.tool("analyze_psx_stock")
async def analyze_stock(symbol: str):
# Returns analysis
return f"{symbol} is a strong performer in PSX"
Pakistani investors use this MCP through Claude: "Show me top 5 PSX stocks to buy today". Claude queries MCP, analyzes market data, returns recommendations.
Result: Real-time PSX analysis powered by AI.
Lesson Summary
Building Your First MCP Server Quiz
4 questions to test your understanding. Score 60% or higher to pass.