Download mcp_server.py from mgbam/AI_SQL: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/mgbam/AI_SQL/resolve/ff342dce9c21369d98a2f5ef9067f444d67a4cb9/mcp_server.py
- Command line
-
hf download hf://spaces/mgbam/AI_SQL@ff342dce9c21369d98a2f5ef9067f444d67a4cb9/mcp_server.py
-
curl -L -o mcp_server.py https://huggingface.co/spaces/mgbam/AI_SQL/resolve/ff342dce9c21369d98a2f5ef9067f444d67a4cb9/mcp_server.py
1.11 kB
| from mcp.server.fastmcp import FastMCP | |
| import sqlite3, json | |
| mcp = FastMCP("EnterpriseData") | |
| # In-memory SQLite | |
| 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() | |
| def query_database(sql: str) -> str: | |
| """Run SQL and return JSON rows.""" | |
| 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 e: | |
| return json.dumps({"error": str(e)}) | |
| if __name__ == "__main__": | |
| mcp.run(transport="stdio") | |