Update mcp_server
Browse files- mcp_server +41 -37
mcp_server
CHANGED
|
@@ -1,37 +1,41 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mcp.server.fastmcp import FastMCP
|
| 2 |
+
import sqlite3, json
|
| 3 |
+
|
| 4 |
+
mcp = FastMCP("EnterpriseData")
|
| 5 |
+
|
| 6 |
+
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 7 |
+
cur = conn.cursor()
|
| 8 |
+
|
| 9 |
+
cur.execute("""
|
| 10 |
+
CREATE TABLE Customers (
|
| 11 |
+
CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 12 |
+
Name TEXT,
|
| 13 |
+
Region TEXT,
|
| 14 |
+
LastOrderDate TEXT
|
| 15 |
+
)
|
| 16 |
+
""")
|
| 17 |
+
cur.executemany(
|
| 18 |
+
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
|
| 19 |
+
[
|
| 20 |
+
("Acme Corp", "Northeast", "2024-12-01"),
|
| 21 |
+
("Beta Inc", "West", "2025-06-01"),
|
| 22 |
+
("Gamma Co", "Northeast", "2023-09-15"),
|
| 23 |
+
("Delta LLC", "South", "2025-03-20"),
|
| 24 |
+
("Epsilon Ltd", "Northeast", "2025-07-10")
|
| 25 |
+
]
|
| 26 |
+
)
|
| 27 |
+
conn.commit()
|
| 28 |
+
|
| 29 |
+
@mcp.tool()
|
| 30 |
+
def query_database(sql: str) -> str:
|
| 31 |
+
"""Run a SQL query and return JSON."""
|
| 32 |
+
try:
|
| 33 |
+
cur.execute(sql)
|
| 34 |
+
cols = [d[0] for d in cur.description or []]
|
| 35 |
+
rows = [dict(zip(cols, r)) for r in cur.fetchall()]
|
| 36 |
+
return json.dumps(rows)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return json.dumps({"error": str(e)})
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
mcp.run(transport="stdio")
|