|
| 1 | +"""Inventory resources for Katana MCP Server. |
| 2 | +
|
| 3 | +Provides read-only access to inventory data including items, stock movements, |
| 4 | +and stock adjustments. |
| 5 | +""" |
| 6 | + |
| 7 | +# NOTE: Do not use 'from __future__ import annotations' in this module |
| 8 | +# FastMCP requires Context to be the actual class, not a string annotation |
| 9 | + |
| 10 | +import time |
| 11 | +from datetime import UTC, datetime |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +from fastmcp import Context, FastMCP |
| 15 | +from pydantic import BaseModel, Field |
| 16 | + |
| 17 | +from katana_mcp.logging import get_logger |
| 18 | +from katana_mcp.services import get_services |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + pass |
| 22 | + |
| 23 | +logger = get_logger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +# ============================================================================ |
| 27 | +# Resource 1: katana://inventory/items |
| 28 | +# ============================================================================ |
| 29 | + |
| 30 | + |
| 31 | +class InventoryItemsSummary(BaseModel): |
| 32 | + """Summary statistics for inventory items.""" |
| 33 | + |
| 34 | + total_items: int = Field(..., description="Total number of items across all types") |
| 35 | + products: int = Field(..., description="Number of finished products") |
| 36 | + materials: int = Field(..., description="Number of raw materials/components") |
| 37 | + services: int = Field(..., description="Number of services") |
| 38 | + items_in_response: int = Field(..., description="Number of items in this response") |
| 39 | + low_stock_count: int | None = Field( |
| 40 | + None, description="Number of items below reorder threshold (if available)" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class InventoryItemsResource(BaseModel): |
| 45 | + """Response structure for inventory items resource.""" |
| 46 | + |
| 47 | + generated_at: str = Field( |
| 48 | + ..., description="ISO timestamp when resource was generated" |
| 49 | + ) |
| 50 | + summary: InventoryItemsSummary = Field(..., description="Summary statistics") |
| 51 | + items: list[dict] = Field(..., description="List of inventory items with details") |
| 52 | + next_actions: list[str] = Field( |
| 53 | + default_factory=list, description="Suggested next actions" |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +async def _get_inventory_items_impl(context: Context) -> InventoryItemsResource: |
| 58 | + """Implementation of inventory items resource. |
| 59 | +
|
| 60 | + Fetches all products, materials, and services from Katana and aggregates |
| 61 | + them into a unified inventory view with stock levels and type information. |
| 62 | +
|
| 63 | + Args: |
| 64 | + context: FastMCP context for accessing the Katana client |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Structured inventory data with summary and items list |
| 68 | +
|
| 69 | + Raises: |
| 70 | + Exception: If API calls fail |
| 71 | + """ |
| 72 | + logger.info("inventory_items_resource_started") |
| 73 | + start_time = time.monotonic() |
| 74 | + |
| 75 | + try: |
| 76 | + services = get_services(context) |
| 77 | + |
| 78 | + # Fetch all item types |
| 79 | + # TODO: Consider parallelizing with asyncio.gather() for better performance |
| 80 | + products_response = await services.client.products.list(limit=100) |
| 81 | + materials_response = await services.client.materials.list(limit=100) |
| 82 | + services_response = await services.client.services.list(limit=100) |
| 83 | + |
| 84 | + # Parse responses - handle both list and paginated response objects |
| 85 | + products = ( |
| 86 | + products_response |
| 87 | + if isinstance(products_response, list) |
| 88 | + else getattr(products_response, "items", []) |
| 89 | + ) |
| 90 | + materials = ( |
| 91 | + materials_response |
| 92 | + if isinstance(materials_response, list) |
| 93 | + else getattr(materials_response, "items", []) |
| 94 | + ) |
| 95 | + services_items = ( |
| 96 | + services_response |
| 97 | + if isinstance(services_response, list) |
| 98 | + else getattr(services_response, "items", []) |
| 99 | + ) |
| 100 | + |
| 101 | + # Aggregate into unified item list |
| 102 | + items = [] |
| 103 | + |
| 104 | + # Add products |
| 105 | + for product in products: |
| 106 | + items.append( |
| 107 | + { |
| 108 | + "id": product.id if hasattr(product, "id") else None, |
| 109 | + "name": product.name if hasattr(product, "name") else "Unknown", |
| 110 | + "type": "product", |
| 111 | + "is_sellable": getattr(product, "is_sellable", False), |
| 112 | + "is_producible": getattr(product, "is_producible", False), |
| 113 | + "is_purchasable": getattr(product, "is_purchasable", False), |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + # Add materials |
| 118 | + for material in materials: |
| 119 | + items.append( |
| 120 | + { |
| 121 | + "id": material.id if hasattr(material, "id") else None, |
| 122 | + "name": material.name if hasattr(material, "name") else "Unknown", |
| 123 | + "type": "material", |
| 124 | + "is_sellable": False, |
| 125 | + "is_producible": False, |
| 126 | + "is_purchasable": True, |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + # Add services |
| 131 | + for service in services_items: |
| 132 | + items.append( |
| 133 | + { |
| 134 | + "id": service.id if hasattr(service, "id") else None, |
| 135 | + "name": service.name if hasattr(service, "name") else "Unknown", |
| 136 | + "type": "service", |
| 137 | + "is_sellable": getattr(service, "is_sellable", True), |
| 138 | + "is_producible": False, |
| 139 | + "is_purchasable": False, |
| 140 | + } |
| 141 | + ) |
| 142 | + |
| 143 | + # Build summary |
| 144 | + summary = InventoryItemsSummary( |
| 145 | + total_items=len(products) + len(materials) + len(services_items), |
| 146 | + products=len(products), |
| 147 | + materials=len(materials), |
| 148 | + services=len(services_items), |
| 149 | + items_in_response=len(items), |
| 150 | + ) |
| 151 | + |
| 152 | + duration_ms = round((time.monotonic() - start_time) * 1000, 2) |
| 153 | + logger.info( |
| 154 | + "inventory_items_resource_completed", |
| 155 | + total_items=summary.total_items, |
| 156 | + duration_ms=duration_ms, |
| 157 | + ) |
| 158 | + |
| 159 | + return InventoryItemsResource( |
| 160 | + generated_at=datetime.now(UTC).isoformat(), |
| 161 | + summary=summary, |
| 162 | + items=items, |
| 163 | + next_actions=[ |
| 164 | + "Use search_items tool to find specific items by name or SKU", |
| 165 | + "Use check_inventory tool to get detailed stock levels for a specific SKU", |
| 166 | + "Use list_low_stock_items tool to identify items needing reorder", |
| 167 | + ], |
| 168 | + ) |
| 169 | + |
| 170 | + except Exception as e: |
| 171 | + duration_ms = round((time.monotonic() - start_time) * 1000, 2) |
| 172 | + logger.error( |
| 173 | + "inventory_items_resource_failed", |
| 174 | + error=str(e), |
| 175 | + error_type=type(e).__name__, |
| 176 | + duration_ms=duration_ms, |
| 177 | + exc_info=True, |
| 178 | + ) |
| 179 | + raise |
| 180 | + |
| 181 | + |
| 182 | +async def get_inventory_items(context: Context) -> dict: |
| 183 | + """Get inventory items resource. |
| 184 | +
|
| 185 | + Provides complete catalog view with current inventory levels for all products, |
| 186 | + materials, and services in the Katana system. |
| 187 | +
|
| 188 | + **Resource URI:** `katana://inventory/items` |
| 189 | +
|
| 190 | + **Purpose:** Complete catalog view for searching and accessing items |
| 191 | +
|
| 192 | + **Refresh Rate:** On-demand (no caching in v0.1.0) |
| 193 | +
|
| 194 | + **Data Includes:** |
| 195 | + - All products, materials, and services |
| 196 | + - Item type and capabilities (sellable, producible, purchasable) |
| 197 | + - Summary statistics by type |
| 198 | + - Total item counts |
| 199 | +
|
| 200 | + **Use Cases:** |
| 201 | + - Browse complete catalog |
| 202 | + - Find items by type |
| 203 | + - Get overview of inventory |
| 204 | + - Identify total item counts |
| 205 | +
|
| 206 | + **Related Tools:** |
| 207 | + - `search_items` - Search for specific items by name or SKU |
| 208 | + - `check_inventory` - Get detailed stock info for a specific SKU |
| 209 | + - `list_low_stock_items` - Find items needing reorder |
| 210 | +
|
| 211 | + **Example Response:** |
| 212 | + ```json |
| 213 | + { |
| 214 | + "generated_at": "2024-01-15T10:30:00Z", |
| 215 | + "summary": { |
| 216 | + "total_items": 150, |
| 217 | + "products": 50, |
| 218 | + "materials": 95, |
| 219 | + "services": 5, |
| 220 | + "items_in_response": 150 |
| 221 | + }, |
| 222 | + "items": [ |
| 223 | + { |
| 224 | + "id": 123, |
| 225 | + "name": "Widget Pro", |
| 226 | + "type": "product", |
| 227 | + "is_sellable": true, |
| 228 | + "is_producible": true, |
| 229 | + "is_purchasable": false |
| 230 | + } |
| 231 | + ], |
| 232 | + "next_actions": [...] |
| 233 | + } |
| 234 | + ``` |
| 235 | +
|
| 236 | + Args: |
| 237 | + context: FastMCP context providing access to Katana client |
| 238 | +
|
| 239 | + Returns: |
| 240 | + Dictionary containing inventory items data with summary and items list |
| 241 | + """ |
| 242 | + response = await _get_inventory_items_impl(context) |
| 243 | + return response.model_dump() |
| 244 | + |
| 245 | + |
| 246 | +def register_resources(mcp: FastMCP) -> None: |
| 247 | + """Register all inventory resources with the FastMCP instance. |
| 248 | +
|
| 249 | + Args: |
| 250 | + mcp: FastMCP server instance to register resources with |
| 251 | + """ |
| 252 | + # Register katana://inventory/items resource |
| 253 | + mcp.resource( |
| 254 | + uri="katana://inventory/items", |
| 255 | + name="Inventory Items", |
| 256 | + description="Complete catalog of all products, materials, and services", |
| 257 | + mime_type="application/json", |
| 258 | + )(get_inventory_items) |
| 259 | + |
| 260 | + |
| 261 | +__all__ = ["register_resources"] |
0 commit comments