mgbam commited on
Commit
1f7c21d
·
verified ·
1 Parent(s): c8534fa

Create mcp_server

Browse files
Files changed (1) hide show
  1. mcp_server +51 -0
mcp_server ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mcp.server.fastmcp import FastMCP
2
+ import sqlite3
3
+ import json
4
+
5
+ # Initialize MCP server
6
+ mcp = FastMCP("EnterpriseData")
7
+
8
+ # Create in-memory SQLite database
9
+ conn = sqlite3.connect(":memory:", check_same_thread=False)
10
+ cursor = conn.cursor()
11
+
12
+ # Create Customers table
13
+ cursor.execute("""
14
+ CREATE TABLE Customers (
15
+ CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
16
+ Name TEXT,
17
+ Region TEXT,
18
+ LastOrderDate TEXT
19
+ )
20
+ """)
21
+
22
+ # Seed sample data
23
+ cursor.executemany(
24
+ "INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
25
+ [
26
+ ("Acme Corp", "Northeast", "2024-12-01"),
27
+ ("Beta Inc", "West", "2025-06-01"),
28
+ ("Gamma Co", "Northeast", "2023-09-15"),
29
+ ("Delta LLC", "South", "2025-03-20"),
30
+ ("Epsilon Ltd", "Northeast", "2025-07-10")
31
+ ]
32
+ )
33
+ conn.commit()
34
+
35
+ # Expose SQL tool to the AI
36
+ @mcp.tool()
37
+ def query_database(sql: str) -> str:
38
+ """Executes raw SQL and returns JSON results."""
39
+ try:
40
+ cur = conn.cursor()
41
+ cur.execute(sql)
42
+ rows = cur.fetchall()
43
+ columns = [desc[0] for desc in cur.description or []]
44
+ results = [dict(zip(columns, row)) for row in rows]
45
+ return json.dumps(results)
46
+ except Exception as e:
47
+ return json.dumps({"error": str(e)})
48
+
49
+ # Start MCP server
50
+ if __name__ == "__main__":
51
+ mcp.run(transport="stdio")