"""Customer management tools for MCP server"""
from typing import Optional
from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.types as types
def register_customer_tools(server: Server, customer_db):
"""Register customer-related MCP tools"""
async def search_customers_handler(arguments: dict) -> list[types.TextContent]:
"""Search for customers by name, email, phone, or customer ID"""
query = arguments.get("query", "")
search_by = arguments.get("search_by", "name")
if not query:
return [TextContent(type="text", text="Error: query parameter is required")]
if search_by not in ["name", "email", "phone", "customer_id"]:
return [TextContent(type="text", text="Error: search_by must be one of: name, email, phone, customer_id")]
try:
results = customer_db.search(query, search_by)
if not results:
return [TextContent(type="text", text=f"No customers found matching '{query}' in {search_by}")]
# Format results
output = f"Found {len(results)} customer(s):\n\n"
for customer in results:
output += f"Customer ID: {customer['customer_id']}\n"
output += f"Name: {customer['name']}\n"
output += f"Email: {customer['email']}\n"
output += f"Phone: {customer['phone']}\n"
output += f"Location: {customer['city']}, {customer['state']}\n"
output += f"Loyalty Tier: {customer['loyalty_tier']}\n"
output += f"Registered: {customer['registration_date']}\n"
output += "-" * 50 + "\n"
return [TextContent(type="text", text=output)]
except Exception as e:
return [TextContent(type="text", text=f"Error searching customers: {str(e)}")]
@server.call_tool()
async def get_customer_profile(name: str, arguments: dict) -> list[types.TextContent]:
"""
Get detailed customer profile including trip statistics.
Args:
customer_id (int): The customer ID to retrieve
"""
try:
customer_id = int(arguments.get("customer_id"))
except (TypeError, ValueError):
return [TextContent(type="text", text="Error: customer_id must be a valid integer")]
try:
profile = customer_db.get_profile(customer_id)
if not profile:
return [TextContent(type="text", text=f"Customer with ID {customer_id} not found")]
# Format profile
output = "CUSTOMER PROFILE\n"
output += "=" * 50 + "\n\n"
output += f"Customer ID: {profile['customer_id']}\n"
output += f"Name: {profile['name']}\n"
output += f"Email: {profile['email']}\n"
output += f"Phone: {profile['phone']}\n"
output += f"Address: {profile['address']}\n"
output += f"City: {profile['city']}, {profile['state']}, {profile['country']}\n"
output += f"Registration Date: {profile['registration_date']}\n"
output += f"Loyalty Tier: {profile['loyalty_tier']}\n\n"
output += "STATISTICS\n"
output += "-" * 50 + "\n"
stats = profile['statistics']
output += f"Total Trips: {stats['total_trips']}\n"
output += f"Lifetime Spending: ${stats['lifetime_spending']:,.2f}\n"
output += f"Last Trip: {stats['last_trip_date'] or 'N/A'}\n"
return [TextContent(type="text", text=output)]
except Exception as e:
return [TextContent(type="text", text=f"Error retrieving customer profile: {str(e)}")]