Day 21
Building a Custom MCP Server
Write your own MCP server in Python to expose any API or data source to Claude.
0 / 7 tasks
Minimal MCP server
python
# server.py
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
app = Server("my-server")
@app.list_tools()
async def list_tools():
return [
Tool(
name="get_weather",
description="Get current weather for a city",
inputSchema={
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "get_weather":
city = arguments["city"]
# Your actual logic here
weather = fetch_weather(city)
return [TextContent(type="text", text=f"Weather in {city}: {weather}")]
if __name__ == "__main__":
import asyncio
asyncio.run(stdio_server(app))