|
| 1 | +from typing import Any |
| 2 | +import asyncio |
| 3 | +from mcp.server.fastmcp import FastMCP |
| 4 | +from transactional_db import CUSTOMERS_TABLE, ORDERS_TABLE, PRODUCTS_TABLE |
| 5 | + |
| 6 | +mcp = FastMCP("ecommerce_tools") |
| 7 | + |
| 8 | + |
| 9 | +@mcp.tool() |
| 10 | +async def get_customer_info(customer_id: str) -> str: |
| 11 | + """Search for a customer using their unique identifier""" |
| 12 | + await asyncio.sleep(1) |
| 13 | + customer_info = CUSTOMERS_TABLE.get(customer_id) |
| 14 | + |
| 15 | + if not customer_info: |
| 16 | + return "Customer not found" |
| 17 | + |
| 18 | + return str(customer_info) |
| 19 | + |
| 20 | + |
| 21 | +@mcp.tool() |
| 22 | +async def get_order_details(order_id: str) -> str: |
| 23 | + """Get details about a specific order.""" |
| 24 | + await asyncio.sleep(1) |
| 25 | + order = ORDERS_TABLE.get(order_id) |
| 26 | + if not order: |
| 27 | + return f"No order found with ID {order_id}." |
| 28 | + |
| 29 | + items = [ |
| 30 | + PRODUCTS_TABLE[sku]["name"] |
| 31 | + for sku in order["items"] |
| 32 | + if sku in PRODUCTS_TABLE |
| 33 | + ] |
| 34 | + return ( |
| 35 | + f"Order ID: {order_id}\n" |
| 36 | + f"Customer ID: {order['customer_id']}\n" |
| 37 | + f"Date: {order['date']}\n" |
| 38 | + f"Status: {order['status']}\n" |
| 39 | + f"Total: ${order['total']:.2f}\n" |
| 40 | + f"Items: {', '.join(items)}" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@mcp.tool() |
| 45 | +async def check_inventory(product_name: str) -> str: |
| 46 | + """Search inventory for a product by product name.""" |
| 47 | + await asyncio.sleep(1) |
| 48 | + matches = [] |
| 49 | + for sku, product in PRODUCTS_TABLE.items(): |
| 50 | + if product_name.lower() in product["name"].lower(): |
| 51 | + matches.append( |
| 52 | + f"{product['name']} (SKU: {sku}) — Stock: {product['stock']}" |
| 53 | + ) |
| 54 | + return "\n".join(matches) if matches else "No matching products found." |
| 55 | + |
| 56 | + |
| 57 | +@mcp.tool() |
| 58 | +async def get_customer_ids_by_name(customer_name: str) -> list[str]: |
| 59 | + """Get customer IDs by using a customer's full name""" |
| 60 | + await asyncio.sleep(1) |
| 61 | + return [ |
| 62 | + cust_id |
| 63 | + for cust_id, info in CUSTOMERS_TABLE.items() |
| 64 | + if info.get("name") == customer_name |
| 65 | + ] |
| 66 | + |
| 67 | + |
| 68 | +@mcp.tool() |
| 69 | +async def get_orders_by_customer_id( |
| 70 | + customer_id: str, |
| 71 | +) -> dict[str, dict[str, str]]: |
| 72 | + """Get orders by customer ID""" |
| 73 | + await asyncio.sleep(1) |
| 74 | + return { |
| 75 | + order_id: order |
| 76 | + for order_id, order in ORDERS_TABLE.items() |
| 77 | + if order.get("customer_id") == customer_id |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + mcp.run(transport="stdio") |
0 commit comments