Update mcp_server.py
Browse files- mcp_server.py +4 -147
mcp_server.py
CHANGED
|
@@ -1,20 +1,10 @@
|
|
| 1 |
"""
|
| 2 |
mcp_server.py β MCP tool server exposing:
|
| 3 |
β’ query_database β SQLite sample
|
| 4 |
-
β’ query_salesforce β Salesforce via simple-salesforce
|
| 5 |
β’ query_hubspot β HubSpot via hubspot-api-client
|
| 6 |
-
β’ query_quickbooks β QuickBooks Online via python-quickbooks
|
| 7 |
-
β’ query_stripe β Stripe via stripe
|
| 8 |
-
β’ query_googleads β Google Ads via google-ads
|
| 9 |
-
β’ query_sharepoint β SharePoint via Office365-REST-Python-Client
|
| 10 |
|
| 11 |
Secrets expected in env:
|
| 12 |
-
SF_USER, SF_PASS, SF_TOKEN
|
| 13 |
HUBSPOT_TOKEN
|
| 14 |
-
QB_CLIENT_ID, QB_CLIENT_SECRET, QB_REFRESH_TOKEN, QB_REALM_ID
|
| 15 |
-
STRIPE_API_KEY
|
| 16 |
-
GOOGLE_ADS_DEVELOPER_TOKEN, GOOGLE_ADS_LOGIN_CUSTOMER_ID, GOOGLE_ADS_REFRESH_TOKEN, GOOGLE_ADS_CLIENT_ID, GOOGLE_ADS_CLIENT_SECRET
|
| 17 |
-
SHAREPOINT_SITE_URL, SHAREPOINT_CLIENT_ID, SHAREPOINT_CLIENT_SECRET, SHAREPOINT_TENANT_ID
|
| 18 |
"""
|
| 19 |
|
| 20 |
import os
|
|
@@ -23,32 +13,14 @@ import sqlite3
|
|
| 23 |
|
| 24 |
from mcp.server.fastmcp import FastMCP
|
| 25 |
|
| 26 |
-
# Salesforce
|
| 27 |
-
from simple_salesforce import Salesforce
|
| 28 |
-
|
| 29 |
# HubSpot
|
| 30 |
from hubspot import HubSpot
|
| 31 |
from hubspot.crm.contacts import ApiException as HSContactsError
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
from quickbooks import QuickBooks
|
| 35 |
-
from quickbooks.objects.customer import Customer # example object
|
| 36 |
-
|
| 37 |
-
# Stripe
|
| 38 |
-
import stripe
|
| 39 |
-
|
| 40 |
-
# Google Ads
|
| 41 |
-
from google.ads.googleads.client import GoogleAdsClient
|
| 42 |
-
from google.ads.googleads.errors import GoogleAdsException
|
| 43 |
-
|
| 44 |
-
# SharePoint
|
| 45 |
-
from office365.runtime.auth.client_credential import ClientCredential
|
| 46 |
-
from office365.sharepoint.client_context import ClientContext
|
| 47 |
-
|
| 48 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
mcp = FastMCP("EnterpriseData")
|
| 50 |
|
| 51 |
-
# βββ 1) In-memory SQLite sample
|
| 52 |
conn = sqlite3.connect(":memory:", check_same_thread=False)
|
| 53 |
cur = conn.cursor()
|
| 54 |
cur.execute("""
|
|
@@ -82,30 +54,7 @@ def query_database(sql: str) -> str:
|
|
| 82 |
except Exception as e:
|
| 83 |
return json.dumps({"error": str(e)})
|
| 84 |
|
| 85 |
-
# βββ 2)
|
| 86 |
-
sf = Salesforce(
|
| 87 |
-
username=os.getenv("SF_USER"),
|
| 88 |
-
password=os.getenv("SF_PASS"),
|
| 89 |
-
security_token=os.getenv("SF_TOKEN"),
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
@mcp.tool()
|
| 93 |
-
def query_salesforce(soql: str) -> str:
|
| 94 |
-
"""
|
| 95 |
-
Run SOQL query via simple-salesforce.
|
| 96 |
-
Example: SELECT Id, Name FROM Account WHERE Industry = 'Technology'
|
| 97 |
-
"""
|
| 98 |
-
try:
|
| 99 |
-
result = sf.query_all(soql)
|
| 100 |
-
records = result.get("records", [])
|
| 101 |
-
# remove attributes block
|
| 102 |
-
for r in records:
|
| 103 |
-
r.pop("attributes", None)
|
| 104 |
-
return json.dumps(records)
|
| 105 |
-
except Exception as e:
|
| 106 |
-
return json.dumps({"error": str(e)})
|
| 107 |
-
|
| 108 |
-
# βββ 3) HubSpot tool ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
hs_client = HubSpot(access_token=os.getenv("HUBSPOT_TOKEN"))
|
| 110 |
|
| 111 |
@mcp.tool()
|
|
@@ -124,98 +73,6 @@ def query_hubspot(object_type: str, limit: int = 100) -> str:
|
|
| 124 |
except Exception as e:
|
| 125 |
return json.dumps({"error": str(e)})
|
| 126 |
|
| 127 |
-
# βββ
|
| 128 |
-
qb = QuickBooks(
|
| 129 |
-
client_id=os.getenv("QB_CLIENT_ID"),
|
| 130 |
-
client_secret=os.getenv("QB_CLIENT_SECRET"),
|
| 131 |
-
refresh_token=os.getenv("QB_REFRESH_TOKEN"),
|
| 132 |
-
company_id=os.getenv("QB_REALM_ID"),
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
@mcp.tool()
|
| 136 |
-
def query_quickbooks(entity: str, max_results: int = 50) -> str:
|
| 137 |
-
"""
|
| 138 |
-
Query QuickBooks Online entity via python-quickbooks.
|
| 139 |
-
entity="Customer" or "Invoice", etc.
|
| 140 |
-
"""
|
| 141 |
-
try:
|
| 142 |
-
cls = globals().get(entity, None)
|
| 143 |
-
if cls is None:
|
| 144 |
-
raise ValueError(f"Unknown entity '{entity}'")
|
| 145 |
-
objs = cls.where("", qb=qb, max_results=max_results)
|
| 146 |
-
return json.dumps([o.to_dict() for o in objs])
|
| 147 |
-
except Exception as e:
|
| 148 |
-
return json.dumps({"error": str(e)})
|
| 149 |
-
|
| 150 |
-
# βββ 5) Stripe tool βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
-
stripe.api_key = os.getenv("STRIPE_API_KEY")
|
| 152 |
-
|
| 153 |
-
@mcp.tool()
|
| 154 |
-
def query_stripe(obj: str, limit: int = 10) -> str:
|
| 155 |
-
"""
|
| 156 |
-
List Stripe objects, e.g. obj="customers", "charges", "invoices"
|
| 157 |
-
"""
|
| 158 |
-
try:
|
| 159 |
-
method = getattr(stripe, obj)
|
| 160 |
-
items = method.list(limit=limit).data
|
| 161 |
-
return json.dumps([i.to_dict() for i in items])
|
| 162 |
-
except Exception as e:
|
| 163 |
-
return json.dumps({"error": str(e)})
|
| 164 |
-
|
| 165 |
-
# βββ 6) Google Ads tool ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
-
ga_config = {
|
| 167 |
-
"developer_token": os.getenv("GOOGLE_ADS_DEVELOPER_TOKEN"),
|
| 168 |
-
"login_customer_id": os.getenv("GOOGLE_ADS_LOGIN_CUSTOMER_ID"),
|
| 169 |
-
"refresh_token": os.getenv("GOOGLE_ADS_REFRESH_TOKEN"),
|
| 170 |
-
"client_id": os.getenv("GOOGLE_ADS_CLIENT_ID"),
|
| 171 |
-
"client_secret": os.getenv("GOOGLE_ADS_CLIENT_SECRET"),
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
ga_client = GoogleAdsClient.load_from_dict(ga_config)
|
| 175 |
-
|
| 176 |
-
@mcp.tool()
|
| 177 |
-
def query_googleads(customer_id: str, query: str) -> str:
|
| 178 |
-
"""
|
| 179 |
-
Run a GAQL query against a Google Ads customer.
|
| 180 |
-
Example: query="SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"
|
| 181 |
-
"""
|
| 182 |
-
try:
|
| 183 |
-
service = ga_client.get_service("GoogleAdsService")
|
| 184 |
-
response = service.search(customer_id=customer_id, query=query)
|
| 185 |
-
rows = []
|
| 186 |
-
for row in response:
|
| 187 |
-
# convert protobuf row to dict
|
| 188 |
-
rows.append({k: getattr(row, k) for k in row._pb.DESCRIPTOR.fields_by_name})
|
| 189 |
-
return json.dumps(rows)
|
| 190 |
-
except GoogleAdsException as ge:
|
| 191 |
-
return json.dumps({"error": ge.error.message})
|
| 192 |
-
except Exception as e:
|
| 193 |
-
return json.dumps({"error": str(e)})
|
| 194 |
-
|
| 195 |
-
# βββ 7) SharePoint tool ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 196 |
-
site_url = os.getenv("SHAREPOINT_SITE_URL")
|
| 197 |
-
client_id = os.getenv("SHAREPOINT_CLIENT_ID")
|
| 198 |
-
client_secret = os.getenv("SHAREPOINT_CLIENT_SECRET")
|
| 199 |
-
tenant_id = os.getenv("SHAREPOINT_TENANT_ID")
|
| 200 |
-
|
| 201 |
-
auth_ctx = ClientContext(site_url).with_credentials(
|
| 202 |
-
ClientCredential(client_id, client_secret)
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
@mcp.tool()
|
| 206 |
-
def query_sharepoint(list_title: str, top: int = 50) -> str:
|
| 207 |
-
"""
|
| 208 |
-
Fetch items from a SharePoint list.
|
| 209 |
-
Example: list_title="ContactsList"
|
| 210 |
-
"""
|
| 211 |
-
try:
|
| 212 |
-
sp_list = auth_ctx.web.lists.get_by_title(list_title)
|
| 213 |
-
items = sp_list.items.top(top).get().execute_query()
|
| 214 |
-
out = [dict(i.properties) for i in items]
|
| 215 |
-
return json.dumps(out, ensure_ascii=False)
|
| 216 |
-
except Exception as e:
|
| 217 |
-
return json.dumps({"error": str(e)})
|
| 218 |
-
|
| 219 |
-
# βββ 8) Start the MCP server βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 220 |
if __name__ == "__main__":
|
| 221 |
mcp.run(transport="stdio")
|
|
|
|
| 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
|
|
|
|
| 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("""
|
|
|
|
| 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()
|
|
|
|
| 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")
|