Update mcp_server.py
Browse files- 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 |
-
#
|
| 24 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 25 |
-
cur
|
| 26 |
cur.execute("""
|
| 27 |
CREATE TABLE Customers (
|
| 28 |
-
CustomerID
|
| 29 |
-
Name
|
| 30 |
-
Region
|
| 31 |
-
LastOrderDate
|
| 32 |
)
|
| 33 |
""")
|
| 34 |
cur.executemany(
|
| 35 |
-
"INSERT INTO Customers (Name, Region, LastOrderDate) VALUES (
|
| 36 |
[
|
| 37 |
-
("Acme Corp",
|
| 38 |
-
("Beta Inc",
|
| 39 |
-
("Gamma Co",
|
| 40 |
-
("Delta LLC",
|
| 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
|
| 49 |
try:
|
| 50 |
cur.execute(sql)
|
| 51 |
-
cols = [
|
| 52 |
-
rows = [dict(zip(cols,
|
| 53 |
-
return json.dumps(rows)
|
| 54 |
except Exception as e:
|
| 55 |
return json.dumps({"error": str(e)})
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
@mcp.tool()
|
| 61 |
-
def
|
| 62 |
"""
|
| 63 |
-
Fetch up to `limit`
|
| 64 |
-
Example:
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return json.dumps({"error": he.body})
|
| 73 |
except Exception as e:
|
| 74 |
return json.dumps({"error": str(e)})
|
| 75 |
|
| 76 |
-
#
|
| 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")
|