mgbam commited on
Commit
defb2ee
Β·
verified Β·
1 Parent(s): 5c279ec

Update connectors/hubspot_connector.py

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