Update mcp_server.py
Browse files- mcp_server.py +46 -28
mcp_server.py
CHANGED
|
@@ -1,58 +1,76 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
from mcp.server.fastmcp import FastMCP
|
| 7 |
-
from connectors import hubspot_connector
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
mcp = FastMCP("EnterpriseData")
|
| 10 |
|
| 11 |
-
# βββ
|
| 12 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 13 |
-
cur
|
|
|
|
| 14 |
cur.execute("""
|
| 15 |
CREATE TABLE Customers (
|
| 16 |
-
CustomerID
|
| 17 |
-
Name
|
| 18 |
-
Region
|
| 19 |
-
LastOrderDate
|
| 20 |
)
|
| 21 |
""")
|
|
|
|
| 22 |
cur.executemany(
|
| 23 |
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
|
| 24 |
[
|
| 25 |
-
("Acme Corp",
|
| 26 |
-
("Beta Inc",
|
| 27 |
-
("Gamma Co",
|
| 28 |
-
("Delta LLC",
|
| 29 |
-
("Epsilon Ltd",
|
| 30 |
],
|
| 31 |
)
|
| 32 |
conn.commit()
|
| 33 |
|
| 34 |
@mcp.tool()
|
| 35 |
def query_database(sql: str) -> str:
|
| 36 |
-
"""Run SQL
|
| 37 |
try:
|
| 38 |
cur.execute(sql)
|
| 39 |
cols = [d[0] for d in cur.description or []]
|
| 40 |
rows = [dict(zip(cols, r)) for r in cur.fetchall()]
|
| 41 |
-
return json.dumps(rows)
|
| 42 |
except Exception as e:
|
| 43 |
return json.dumps({"error": str(e)})
|
| 44 |
|
| 45 |
-
# βββ 2. HubSpot tool ββββββββββββββββ
|
| 46 |
@mcp.tool()
|
| 47 |
-
def
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# βββ
|
| 57 |
if __name__ == "__main__":
|
| 58 |
mcp.run(transport="stdio")
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
mcp_server.py β Minimal MCP tool server for:
|
| 3 |
+
β’ query_database β sample SQLite table
|
| 4 |
+
β’ query_hubspot β contacts, companies, deals from HubSpot
|
| 5 |
|
| 6 |
+
Env requirements:
|
| 7 |
+
HUBSPOT_TOKEN
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os, json, sqlite3
|
| 11 |
from mcp.server.fastmcp import FastMCP
|
|
|
|
| 12 |
|
| 13 |
+
# βββ HubSpot βββββββββββββββββββββββββββββ
|
| 14 |
+
from hubspot import HubSpot
|
| 15 |
+
from hubspot.crm.contacts import ApiException as HSContactsError
|
| 16 |
+
|
| 17 |
+
hs_client = HubSpot(access_token=os.getenv("HUBSPOT_TOKEN"))
|
| 18 |
+
|
| 19 |
+
# βββ Setup MCP server ββββββββββββββββββββ
|
| 20 |
mcp = FastMCP("EnterpriseData")
|
| 21 |
|
| 22 |
+
# βββ In-memory SQLite setup ββββββββββββββ
|
| 23 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 24 |
+
cur = conn.cursor()
|
| 25 |
+
|
| 26 |
cur.execute("""
|
| 27 |
CREATE TABLE Customers (
|
| 28 |
+
CustomerID INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 29 |
+
Name TEXT,
|
| 30 |
+
Region TEXT,
|
| 31 |
+
LastOrderDate TEXT
|
| 32 |
)
|
| 33 |
""")
|
| 34 |
+
|
| 35 |
cur.executemany(
|
| 36 |
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (?, ?, ?)",
|
| 37 |
[
|
| 38 |
+
("Acme Corp", "Northeast", "2024-12-01"),
|
| 39 |
+
("Beta Inc", "West", "2025-06-01"),
|
| 40 |
+
("Gamma Co", "Northeast", "2023-09-15"),
|
| 41 |
+
("Delta LLC", "South", "2025-03-20"),
|
| 42 |
+
("Epsilon Ltd","Northeast", "2025-07-10"),
|
| 43 |
],
|
| 44 |
)
|
| 45 |
conn.commit()
|
| 46 |
|
| 47 |
@mcp.tool()
|
| 48 |
def query_database(sql: str) -> str:
|
| 49 |
+
"""Run SQL against the Customers table."""
|
| 50 |
try:
|
| 51 |
cur.execute(sql)
|
| 52 |
cols = [d[0] for d in cur.description or []]
|
| 53 |
rows = [dict(zip(cols, r)) for r in cur.fetchall()]
|
| 54 |
+
return json.dumps(rows, indent=2)
|
| 55 |
except Exception as e:
|
| 56 |
return json.dumps({"error": str(e)})
|
| 57 |
|
|
|
|
| 58 |
@mcp.tool()
|
| 59 |
+
def query_hubspot(object_type: str = "contacts", limit: int = 100) -> str:
|
| 60 |
+
"""
|
| 61 |
+
Fetch objects from HubSpot CRM (contacts, companies, deals).
|
| 62 |
+
Example: object_type="contacts"
|
| 63 |
+
"""
|
| 64 |
+
try:
|
| 65 |
+
api = getattr(hs_client.crm, object_type)
|
| 66 |
+
page = api.basic_api.get_page(limit=limit)
|
| 67 |
+
items = [r.to_dict() for r in page.results]
|
| 68 |
+
return json.dumps(items, indent=2)
|
| 69 |
+
except HSContactsError as he:
|
| 70 |
+
return json.dumps({"error": he.body})
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return json.dumps({"error": str(e)})
|
| 73 |
|
| 74 |
+
# βββ Launch MCP server βββββββββββββββββββ
|
| 75 |
if __name__ == "__main__":
|
| 76 |
mcp.run(transport="stdio")
|