mgbam commited on
Commit
5c6707a
Β·
verified Β·
1 Parent(s): 40ab754

Update mcp_server.py

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