Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 2 min read

I Built an MCP Server in 50 Lines of Python. Here Is How.

TL;DR: The Model Context Protocol (MCP) is the standard for giving AI agents access to tools. But most tutorials overcomplicate it. Here is how to build a fully functional MCP server in 50 lines of Python: weather lookup

TL;DR: The Model Context Protocol (MCP) is the standard for giving AI agents access to tools. But most tutorials overcomplicate it. Here is how to build a fully functional MCP server in 50 lines of Python: weather lookup, SQLite database, file system access. Copy, paste, run.

What MCP Actually Is

MCP is a protocol that lets AI agents discover and call tools. Instead of every agent needing a bespoke integration, MCP defines a standard way for servers to describe what they offer. The agent reads this manifest and calls tools as needed. No OpenAPI specs. No REST wrappers.

The 50-Line Server

import json, sys, sqlite3
from pathlib import Path

def get_weather(city):
data = {"Berlin": 14, "London": 11, "Tokyo": 22}
temp = data.get(city)
if temp is None:
return {"error": f"Unknown city: {city}"}
return {"city": city, "temperature_c": temp}

def query_db(sql):
conn = sqlite3.connect("data.db")
cur = conn.cursor()
cur.execute(sql)
if sql.upper().startswith("SELECT"):
rows = cur.fetchall()
cols = [d[0] for d in cur.description]
return {"columns": cols, "rows": rows}
conn.commit()
return {"affected": cur.rowcount}

def read_file(path):
p = Path(path)
if not p.exists():
return {"error": f"File not found: {path}"}
return {"path": path, "content": p.read_text()}

MANIFEST = {"tools": [
{"name": "get_weather", "inputSchema":
{"type": "object", "properties": {"city": {"type": "string"}}},
"description": "Get current temperature."},
{"name": "query_db", "inputSchema":
{"type": "object", "properties": {"sql": {"type": "string"}}},
"description": "Run SQL query."},
{"name": "read_file", "inputSchema":
{"type": "object", "properties": {"path": {"type": "string"}}},
"description": "Read a file."},
]}

TOOLS = {"get_weather": get_weather,
"query_db": query_db, "read_file": read_file}

def handle_request(req):
if req["method"] == "tools/list": return MANIFEST
if req["method"] == "tools/call":
fn = TOOLS[req["params"]["name"]]
args = req["params"].get("arguments", {})
return {"content": [{"type": "text",
"text": json.dumps(fn(**args))}]}

for line in sys.stdin:
print(json.dumps(handle_request(json.loads(line))))That is the entire server. Fifty lines. No dependencies beyond Python's standard library.

How It Works

Every MCP server speaks JSON-RPC over stdin/stdout. The agent sends a request, the server responds. No HTTP framework. Two methods: tools/list returns the manifest, tools/call invokes a tool.

Connecting to an Agent

Point Claude Desktop, Cursor, Facio, or Codex at python server.py. Tools appear automatically. No additional integration code needed.

Adding Real APIs

Replace the weather mock with a real OpenWeatherMap call. Same interface, same manifest. The agent does not know the implementation changed. This is MCP's core value: the agent sees tools, not APIs.

Security in Production

Restrict filesystem paths to a whitelist. Sandbox SQL to read-only. Add authentication for network-exposed servers. Rate-limit tool calls.

Why This Matters

Before MCP: custom REST wrappers per integration. With MCP: one server, many agents. Self-documenting. Schema-safe. Fifty lines of Python, three tools, unlimited agents.
I build MCP-native AI agent infrastructure at centerbit. If you are building tool servers for AI agents, we should talk.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.