Introduction to Model Context Protocol (MCP)
James Chapman
AI Curriculum Manager, DataCamp


![]()
![]()

from mcp.server.fastmcp import FastMCP import sqlite3 mcp = FastMCP("Timezone Converter") # Create connection at startup; reuse in handlers conn = sqlite3.connect("timezones.db") conn.row_factory = sqlite3.Row@mcp.resource("db://timezones") def get_locations() -> str: try: cursor = conn.execute("SELECT timezone FROM locations") rows = cursor.fetchall() return "\n".join(r["timezone"] for r in rows) except sqlite3.Error as e: return f"Error: {e}"
@mcp.tool() def lookup_locations(prefix: str) -> str: """Find timezones that contain the given prefix.""" try: cursor = conn.execute( "SELECT timezone FROM locations WHERE timezone LIKE ? LIMIT 50", (f"%{prefix}%",)) rows = cursor.fetchall() return "\n".join(r["timezone"] for r in rows) except sqlite3.Error as e: return f"Error: {e}"if __name__ == "__main__": try: mcp.run(transport="stdio") finally: conn.close()
?) queries: never string-format user or LLM input into SQLSELECT timezone FROM locations WHERE timezone LIKE ? LIMIT 50
Introduction to Model Context Protocol (MCP)