Update mcp_server.py
Browse files- mcp_server.py +31 -12
mcp_server.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
"""
|
| 2 |
mcp_server.py β MCP tool server exposing:
|
| 3 |
-
β’ query_database β SQLite
|
| 4 |
-
β’
|
| 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
|
| 11 |
|
|
|
|
| 12 |
mcp = FastMCP("EnterpriseData")
|
| 13 |
|
| 14 |
-
# βββ
|
| 15 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 16 |
cur = conn.cursor()
|
| 17 |
|
|
@@ -35,10 +35,10 @@ cur.executemany(
|
|
| 35 |
)
|
| 36 |
conn.commit()
|
| 37 |
|
| 38 |
-
# βββ SQLite tool
|
| 39 |
@mcp.tool()
|
| 40 |
def query_database(sql: str) -> str:
|
| 41 |
-
"""Run
|
| 42 |
try:
|
| 43 |
cur.execute(sql)
|
| 44 |
cols = [d[0] for d in cur.description or []]
|
|
@@ -47,11 +47,30 @@ def query_database(sql: str) -> str:
|
|
| 47 |
except Exception as exc:
|
| 48 |
return json.dumps({"error": str(exc)})
|
| 49 |
|
| 50 |
-
# βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
@mcp.tool()
|
| 52 |
-
def
|
| 53 |
-
"""
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
if __name__ == "__main__":
|
| 57 |
mcp.run(transport="stdio")
|
|
|
|
| 1 |
"""
|
| 2 |
mcp_server.py β MCP tool server exposing:
|
| 3 |
+
β’ query_database β built-in SQLite demo
|
| 4 |
+
β’ query_hubspot β HubSpot CRM via CData Python connector
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
from mcp.server.fastmcp import FastMCP
|
| 8 |
+
import sqlite3, json, os
|
| 9 |
+
from cdata.hubspot import connect # pip install cdata-hubspot
|
| 10 |
|
| 11 |
+
# βββ 1. FastMCP instance ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
mcp = FastMCP("EnterpriseData")
|
| 13 |
|
| 14 |
+
# βββ 2. In-memory SQLite sample βββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 16 |
cur = conn.cursor()
|
| 17 |
|
|
|
|
| 35 |
)
|
| 36 |
conn.commit()
|
| 37 |
|
| 38 |
+
# βββ 3. SQLite tool βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
@mcp.tool()
|
| 40 |
def query_database(sql: str) -> str:
|
| 41 |
+
"""Run SQL against the in-memory Customers table and return JSON rows."""
|
| 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 |
+
# βββ 4. HubSpot connector setup βββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
# Uses CDataβs HubSpot Python connector :contentReference[oaicite:4]{index=4}
|
| 52 |
+
HUBSPOT_TOKEN = os.getenv("HUBSPOT_TOKEN")
|
| 53 |
+
hs_conn = connect(PrivateAppToken=HUBSPOT_TOKEN)
|
| 54 |
+
|
| 55 |
+
# βββ 5. HubSpot tool ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
@mcp.tool()
|
| 57 |
+
def query_hubspot(sql: str) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Execute SQL/ODBC-style queries on HubSpot objects (Contacts, Deals, etc.).
|
| 60 |
+
Example:
|
| 61 |
+
SELECT firstname, lastname, email
|
| 62 |
+
FROM contacts
|
| 63 |
+
WHERE lifecyclestage = 'lead';
|
| 64 |
+
"""
|
| 65 |
+
try:
|
| 66 |
+
cur_hs = hs_conn.cursor()
|
| 67 |
+
cur_hs.execute(sql)
|
| 68 |
+
cols = [d[0] for d in cur_hs.description or []]
|
| 69 |
+
rows = [dict(zip(cols, r)) for r in cur_hs.fetchall()]
|
| 70 |
+
return json.dumps(rows)
|
| 71 |
+
except Exception as exc:
|
| 72 |
+
return json.dumps({"error": str(exc)})
|
| 73 |
|
| 74 |
+
# βββ 6. Run the server βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
if __name__ == "__main__":
|
| 76 |
mcp.run(transport="stdio")
|