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