File size: 1,726 Bytes
f37945a 26d132c f37945a 26d132c f37945a 26d132c f37945a 26d132c f37945a 1bf6db4 f37945a 1bf6db4 f37945a 26d132c f37945a 26d132c f37945a 26d132c f37945a 26d132c f37945a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
from mcp.server.fastmcp import FastMCP
import sqlite3, json
# βββ MCP instance βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
mcp = FastMCP("EnterpriseData")
# βββ In-memory SQLite sample DB βββββββββββββββββββββββββββββββββββββββββ
conn = sqlite3.connect(":memory:", check_same_thread=False)
cur = conn.cursor()
cur.execute("""
CREATE TABLE Customers (
CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT,
Region TEXT,
LastOrderDate TEXT
)
""")
cur.executemany(
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?,?,?)",
[
("Acme Corp", "Northeast", "2024-12-01"),
("Beta Inc", "West", "2025-06-01"),
("Gamma Co", "Northeast", "2023-09-15"),
("Delta LLC", "South", "2025-03-20"),
("Epsilon Ltd","Northeast", "2025-07-10"),
]
)
conn.commit()
# βββ SQL tool exposed to the agent ββββββββββββββββββββββββββββββββββββββ
@mcp.tool()
def query_database(sql: str) -> str:
"""
Execute raw SQL and return rows as JSON.
Example: SELECT * FROM Customers WHERE Region='West';
"""
try:
cur.execute(sql)
cols = [d[0] for d in cur.description or []]
rows = [dict(zip(cols, r)) for r in cur.fetchall()]
return json.dumps(rows)
except Exception as exc:
return json.dumps({"error": str(exc)})
if __name__ == "__main__":
mcp.run(transport="stdio")
|