from mcp.server import MCPServer
server = MCPServer("customer-support")
@server.tool()
async def search_knowledge_base(
query: str,
category: Optional[str] = None
) -> dict:
"""Search support articles and documentation.
Use when:
- Customer has a question about product features
- Need troubleshooting steps
- Looking for how-to information
Do NOT use for:
- Customer-specific data (use get_customer_info)
- Order status (use get_order_status)
Args:
query: Search keywords (e.g., "reset password", "shipping costs")
category: Optional filter ("billing", "technical", "shipping")
Returns:
Top 3 relevant articles with titles, summaries, and links
"""
results = await knowledge_db.search(query, category=category, limit=3)
return {
"success": True,
"data": {
"articles": [{
"title": r.title,
"summary": r.summary,
"url": r.url,
"relevance_score": r.score
} for r in results]
},
"error": None,
"message": f"Found {len(results)} articles"
}
@server.tool()
async def get_customer_info(email: str) -> dict:
"""Get customer account details and history.
Use when:
- Need to look up customer account
- Checking customer status/tier
- Understanding customer context before helping
Args:
email: Customer email address
Returns:
Customer profile including name, account status, order history summary
"""
try:
customer = await customer_db.find_by_email(email)
if not customer:
return {
"success": False,
"data": None,
"error": "not_found",
"message": f"No account found for {email}"
}
return {
"success": True,
"data": {
"name": customer.name,
"email": customer.email,
"account_status": customer.status,
"total_orders": customer.order_count,
"lifetime_value": customer.lifetime_value,
"vip_status": customer.is_vip
},
"error": None,
"message": "Customer found"
}
except Exception as e:
return {
"success": False,
"data": None,
"error": str(e),
"message": "Database error - try again"
}
@server.tool()
async def create_support_ticket(
customer_email: str,
subject: str,
description: str,
priority: Literal["low", "medium", "high"] = "medium"
) -> dict:
"""Create a support ticket for issues requiring human follow-up.
Use when:
- Issue cannot be resolved immediately
- Customer requests human assistance
- Complex problem requiring investigation
Args:
customer_email: Customer's email address
subject: Brief ticket summary (under 100 chars)
description: Detailed problem description
priority: Ticket priority level
Returns:
Ticket ID and estimated response time
"""
try:
ticket = await ticketing_api.create({
"customer_email": customer_email,
"subject": subject,
"description": description,
"priority": priority,
"source": "ai_agent"
})
eta_hours = {"low": 48, "medium": 24, "high": 4}
return {
"success": True,
"data": {
"ticket_id": ticket.id,
"ticket_url": ticket.url,
"estimated_response_hours": eta_hours[priority]
},
"error": None,
"message": f"Ticket {ticket.id} created"
}
except Exception as e:
return {
"success": False,
"data": None,
"error": str(e),
"message": "Could not create ticket - escalating to supervisor"
}
@server.tool()
async def check_order_status(order_id: str) -> dict:
"""Get current status and tracking for an order.
Use when:
- Customer asks "where is my order"
- Checking delivery status
- Getting tracking information
Args:
order_id: Order ID in format ORD-##### (e.g., "ORD-12345")
Returns:
Order status, tracking number, estimated delivery
"""
try:
order = await order_api.get(order_id)
return {
"success": True,
"data": {
"order_id": order.id,
"status": order.status,
"tracking_number": order.tracking,
"carrier": order.carrier,
"estimated_delivery": order.estimated_delivery.isoformat(),
"items": [
{"name": item.name, "quantity": item.quantity}
for item in order.items
]
},
"error": None,
"message": f"Order status: {order.status}"
}
except Exception as e:
return {
"success": False,
"data": None,
"error": str(e),
"message": "Could not retrieve order - check order ID"
}