get_product
Retrieve full product details by entering a product ID. Solves the need to access specific product information quickly from the product catalog.
Instructions
Retrieve full details for a single product by its ID (e.g. "P001").
Args: product_id: The product's unique identifier.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| product_id | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:66-77 (handler)The MCP tool handler for 'get_product'. It searches the CATALOG list for a product by its ID (case-insensitive) and returns the product details as JSON, or an error message if not found.
@mcp.tool() def get_product(product_id: str) -> str: """Retrieve full details for a single product by its ID (e.g. "P001"). Args: product_id: The product's unique identifier. """ for product in CATALOG: if product.id.upper() == product_id.upper(): return json.dumps(product.to_dict(), ensure_ascii=False, indent=2) return json.dumps({"error": f"Product '{product_id}' not found."}, indent=2) - server.py:66-67 (registration)Registration of 'get_product' as an MCP tool via the @mcp.tool() decorator on FastMCP instance.
@mcp.tool() def get_product(product_id: str) -> str: - products.py:6-20 (schema)Schema definition of the Product dataclass used by get_product. The handler calls product.to_dict() to serialize the matched product.
@dataclass class Product: id: str name: str category: str brand: str price: float currency: str stock: int rating: float description: str tags: list[str] def to_dict(self) -> dict[str, Any]: return asdict(self) - products.py:23-168 (helper)The CATALOG data store containing all products. The get_product handler iterates over this list to find a product by ID.
CATALOG: list[Product] = [ Product( id="P001", name="Apple MacBook Pro 14-inch M3", category="Laptops", brand="Apple", price=1999.00, currency="USD", stock=42, rating=4.8, description="14-inch Liquid Retina XDR display, M3 chip, 18GB RAM, 512GB SSD.", tags=["laptop", "apple", "macbook", "m3", "professional", "portable"], ), Product( id="P002", name="Dell XPS 15 9530", category="Laptops", brand="Dell", price=1749.00, currency="USD", stock=18, rating=4.5, description="15.6-inch OLED display, Intel Core i7-13700H, 32GB RAM, 1TB SSD, RTX 4060.", tags=["laptop", "dell", "xps", "oled", "gaming", "professional"], ), Product( id="P003", name="Sony WH-1000XM5", category="Headphones", brand="Sony", price=349.99, currency="USD", stock=120, rating=4.9, description="Industry-leading noise cancelling wireless headphones with 30-hour battery life.", tags=["headphones", "sony", "wireless", "noise-cancelling", "audio"], ), Product( id="P004", name="Apple AirPods Pro 2nd Gen", category="Headphones", brand="Apple", price=249.00, currency="USD", stock=85, rating=4.7, description="Active noise cancellation, Transparency mode, Adaptive Audio, USB-C charging.", tags=["earbuds", "apple", "airpods", "wireless", "noise-cancelling", "audio"], ), Product( id="P005", name="Samsung Galaxy S24 Ultra", category="Smartphones", brand="Samsung", price=1299.99, currency="USD", stock=60, rating=4.6, description="6.8-inch Dynamic AMOLED 2X, Snapdragon 8 Gen 3, 200MP camera, S Pen included.", tags=["smartphone", "samsung", "galaxy", "android", "camera", "s-pen"], ), Product( id="P006", name="iPhone 16 Pro Max", category="Smartphones", brand="Apple", price=1199.00, currency="USD", stock=75, rating=4.8, description="6.9-inch Super Retina XDR, A18 Pro chip, 48MP Fusion camera, titanium design.", tags=["smartphone", "apple", "iphone", "ios", "camera", "titanium"], ), Product( id="P007", name="LG C3 55-inch OLED TV", category="TVs", brand="LG", price=1296.99, currency="USD", stock=30, rating=4.7, description="55-inch OLED evo panel, 4K 120Hz, Dolby Vision, webOS, perfect blacks.", tags=["tv", "lg", "oled", "4k", "120hz", "smart-tv", "dolby"], ), Product( id="P008", name="Logitech MX Master 3S", category="Mice", brand="Logitech", price=99.99, currency="USD", stock=200, rating=4.8, description="High-precision 8000 DPI sensor, ultra-fast MagSpeed scroll wheel, Bluetooth.", tags=["mouse", "logitech", "wireless", "ergonomic", "productivity"], ), Product( id="P009", name="Keychron K2 Pro Mechanical Keyboard", category="Keyboards", brand="Keychron", price=89.99, currency="USD", stock=150, rating=4.6, description="75% layout, hot-swappable, Bluetooth 5.1, RGB backlight, Mac/Windows.", tags=["keyboard", "keychron", "mechanical", "wireless", "rgb", "hot-swap"], ), Product( id="P010", name="Samsung 49-inch Odyssey G9 OLED", category="Monitors", brand="Samsung", price=1299.99, currency="USD", stock=15, rating=4.5, description="49-inch dual QHD OLED, 240Hz, 0.03ms response, G-Sync & FreeSync Premium Pro.", tags=["monitor", "samsung", "oled", "ultrawide", "gaming", "240hz"], ), Product( id="P011", name="iPad Pro 13-inch M4", category="Tablets", brand="Apple", price=1299.00, currency="USD", stock=55, rating=4.8, description="13-inch Ultra Retina XDR OLED, M4 chip, Apple Pencil Pro support, 5G.", tags=["tablet", "apple", "ipad", "m4", "oled", "5g", "apple-pencil"], ), Product( id="P012", name="Bose QuietComfort Ultra Earbuds", category="Headphones", brand="Bose", price=299.00, currency="USD", stock=90, rating=4.6, description="World-class noise cancellation, Immersive Audio, 6-hour battery, IPX4.", tags=["earbuds", "bose", "wireless", "noise-cancelling", "audio", "ipx4"], ), ]