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