Zoho_mcp_client / zoho_client_mcp.py
vachaspathi's picture
Create zoho_client_mcp.py
7c02217 verified
raw
history blame
4.31 kB
from mcp.server.fastmcp import FastMCP
from typing import dict, list, Optional
import os
# Placeholder for configuration constants and dependencies (e.g., Tesseract, pdf2image)
from config import CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, API_BASE
# --- Initialize the FastMCP Server ---
# The server is focused on Zoho CRM actions [12, 13].
mcp = FastMCP("ZohoCRMAgent")
# --- Conceptual Zoho API Handling Functions (Internal Logic) ---
# NOTE: These functions simulate the logic that would use the existing CLIENT_ID,
# CLIENT_SECRET, and REFRESH_TOKEN to make authenticated API calls to the
# new API_BASE (Zoho CRM /crm/v2) [5, 6].
def _get_valid_token_headers() -> dict:
"""Internal function to ensure a valid Zoho access token is available."""
# This logic reuses the pattern of handling OAuth and token refresh
# from the original zoho_client.py [1, 5, 6].
# In a real implementation, it would use the stored REFRESH_TOKEN to get a new Access Token.
return {"Authorization": "Zoho-oauthtoken <ACCESS_TOKEN_HERE>"}
# --- MCP Tools for Zoho CRM CRUD Operations (Required actions) ---
@mcp.tool()
def authenticate_zoho() -> str:
"""
Performs the OAuth 2.0 flow to ensure the agent has a valid Zoho CRM
access token for subsequent API calls [7, 14].
"""
# Placeholder for the actual OAuth flow [6].
return "Zoho CRM access token successfully refreshed."
@mcp.tool()
def create_record(module_name: str, record_data: dict) -> str:
"""
Creates a new record (e.g., Contact, Lead, Account, Deal) in the
specified Zoho CRM module [6, 12].
Example: create_record("Contacts", {"Last_Name": "Alice", "Email": "[email protected]"})
"""
# This tool fulfills the 'create user/contact' requirement [12, 13].
headers = _get_valid_token_headers()
# Logic: POST to f"{API_BASE}/{module_name}" with record_data
if module_name in ["Contacts", "Leads", "Accounts", "Deals"]:
return f"Successfully created a new record in {module_name}."
else:
return f"Error: Module {module_name} is not supported or recognized."
@mcp.tool()
def get_records(module_name: str, page: int=1, per_page: int=200) -> list:
"""
Fetches records from a CRM module based on module name, used for searches
or getting lists [6, 15].
"""
headers = _get_valid_token_headers()
# Logic: GET from f"{API_BASE}/{module_name}"
return [f"List of records retrieved from {module_name} module on page {page}."]
@mcp.tool()
def update_record(module_name: str, record_id: str, data: dict) -> str:
"""
Updates fields on an existing record identified by module and ID [6, 10, 15].
"""
headers = _get_valid_token_headers()
# Logic: PUT to f"{API_BASE}/{module_name}/{record_id}"
return f"Record {record_id} in {module_name} updated successfully."
@mcp.tool()
def delete_record(module_name: str, record_id: str) -> str:
"""
Deletes a record from the specified CRM module [6, 10, 15].
"""
headers = _get_valid_token_headers()
# Logic: DELETE to f"{API_BASE}/{module_name}/{record_id}"
return f"Record {record_id} in {module_name} deleted."
# --- MCP Tool for File Processing (Incorporating old ai_engine logic) ---
# This tool handles the requirement for accepting uploaded images or PDFs [13, 16, 17].
# NOTE: The FastMCP framework handles file references automatically, passing
# a path or URL to the tool function [10, 18].
@mcp.tool()
def process_document(file_path: str, target_module: Optional[str] = "Contacts") -> dict:
"""
Analyzes an attached PDF or image using OCR and AI, extracts structured data,
and returns fields ready for Zoho entry [8-10].
The internal process reuses perform_ocr and extract_intelligent_json logic [1, 9].
"""
# 1. OCR Step (Uses Tesseract/pdf2image from existing pipeline) [1, 9].
# raw_text = perform_ocr(file_path)
# 2. AI Parsing Step (Uses Gemini to parse text into JSON) [1, 11].
# extracted_data = gemini_parse_json(raw_text)
# 3. If successful, the LLM will decide whether to call 'create_record' next.
return {
"status": "success",
"file": os.path.basename(file_path),
"extracted_data": f"Structured data extracted from {target_module} document."
}