Update mcp_server.py
Browse files- mcp_server.py +18 -9
mcp_server.py
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from mcp.server.fastmcp import FastMCP
|
| 2 |
import sqlite3, json
|
|
|
|
| 3 |
|
| 4 |
-
# βββ MCP instance βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
mcp = FastMCP("EnterpriseData")
|
| 6 |
|
| 7 |
-
# βββ
|
| 8 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 9 |
cur = conn.cursor()
|
| 10 |
|
|
@@ -16,7 +23,6 @@ CREATE TABLE Customers (
|
|
| 16 |
LastOrderDate TEXT
|
| 17 |
)
|
| 18 |
""")
|
| 19 |
-
|
| 20 |
cur.executemany(
|
| 21 |
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?,?,?)",
|
| 22 |
[
|
|
@@ -25,17 +31,14 @@ cur.executemany(
|
|
| 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 |
-
# βββ
|
| 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 []]
|
|
@@ -44,5 +47,11 @@ def query_database(sql: str) -> str:
|
|
| 44 |
except Exception as exc:
|
| 45 |
return json.dumps({"error": str(exc)})
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
mcp.run(transport="stdio")
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
mcp_server.py β MCP tool server exposing:
|
| 3 |
+
β’ query_database β SQLite sample
|
| 4 |
+
β’ query_salesforce β Salesforce via CData JDBC
|
| 5 |
+
Add more connectors under connectors/ and register the same way.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
from mcp.server.fastmcp import FastMCP
|
| 9 |
import sqlite3, json
|
| 10 |
+
from connectors.salesforce_connector import sf_query # β new connector
|
| 11 |
|
|
|
|
| 12 |
mcp = FastMCP("EnterpriseData")
|
| 13 |
|
| 14 |
+
# βββ in-memory SQLite sample βββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 16 |
cur = conn.cursor()
|
| 17 |
|
|
|
|
| 23 |
LastOrderDate TEXT
|
| 24 |
)
|
| 25 |
""")
|
|
|
|
| 26 |
cur.executemany(
|
| 27 |
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?,?,?)",
|
| 28 |
[
|
|
|
|
| 31 |
("Gamma Co", "Northeast", "2023-09-15"),
|
| 32 |
("Delta LLC", "South", "2025-03-20"),
|
| 33 |
("Epsilon Ltd","Northeast", "2025-07-10"),
|
| 34 |
+
],
|
| 35 |
)
|
| 36 |
conn.commit()
|
| 37 |
|
| 38 |
+
# βββ SQLite tool βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
@mcp.tool()
|
| 40 |
def query_database(sql: str) -> str:
|
| 41 |
+
"""Run arbitrary SQL against the Customers SQLite table."""
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
cur.execute(sql)
|
| 44 |
cols = [d[0] for d in cur.description or []]
|
|
|
|
| 47 |
except Exception as exc:
|
| 48 |
return json.dumps({"error": str(exc)})
|
| 49 |
|
| 50 |
+
# βββ Salesforce tool (premium) βββββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
@mcp.tool()
|
| 52 |
+
def query_salesforce(sql: str) -> str:
|
| 53 |
+
"""Run SOQL-as-SQL against Salesforce objects (requires Pro plan)."""
|
| 54 |
+
return sf_query(sql)
|
| 55 |
+
|
| 56 |
if __name__ == "__main__":
|
| 57 |
mcp.run(transport="stdio")
|