Skip to content

Latest commit

Β 

History

History
287 lines (236 loc) Β· 9.01 KB

File metadata and controls

287 lines (236 loc) Β· 9.01 KB

Nike Search Widget - System Architecture

πŸ—οΈ System Overview

The Nike Search Widget is a comprehensive system that enables intelligent Nike product search and comparison through multiple interfaces. It's built as a Model Context Protocol (MCP) server with custom UI components, designed to integrate seamlessly with ChatGPT's Apps SDK.

graph TB
    subgraph "Frontend Layer"
        UI[React UI Widget]
        HTML[Test Interface]
    end
    
    subgraph "API Layer"
        MCP[MCP Server]
        REST[REST API Server]
        SSE[SSE Endpoint]
    end
    
    subgraph "Data Layer"
        NIKE[Nike.com API]
        CACHE[Response Cache]
    end
    
    subgraph "Integration Layer"
        CHATGPT[ChatGPT Apps SDK]
        CLIENT[MCP Client]
    end
    
    UI --> MCP
    HTML --> REST
    MCP --> NIKE
    REST --> MCP
    SSE --> MCP
    CHATGPT --> UI
    CLIENT --> SSE
    MCP --> CACHE
Loading

πŸ”§ Core Components

1. Nike Search MCP Server (nikesearch.py)

Primary component - Full-featured MCP server with advanced Nike API integration.

Key Features:

  • πŸ” Product Search - Multi-parameter search with filters
  • πŸ†š Product Comparison - Side-by-side product analysis
  • πŸ–ΌοΈ Image Processing - Product image extraction and optimization
  • πŸ“Š Smart Caching - Response caching for performance
  • πŸ›‘οΈ Error Handling - Robust error recovery and fallbacks

Architecture:

class NikeSearchMCPServer:
    β”œβ”€β”€ NikeSearchClient          # Core API client
    β”œβ”€β”€ search_products()         # Main search functionality
    β”œβ”€β”€ get_product_details()     # Detailed product info
    β”œβ”€β”€ compare_products()        # Product comparison
    └── get_product_images()      # Image extraction

2. REST API Server (nikesearch_rest.py)

Bridge component - FastAPI wrapper providing REST and SSE endpoints.

Endpoints:

  • GET /search - Product search via REST
  • GET /compare - Product comparison via REST
  • POST /sse - Server-Sent Events for MCP communication
  • GET /health - Health check endpoint

Integration Pattern:

FastAPI App
β”œβ”€β”€ CORS Support           # Cross-origin requests
β”œβ”€β”€ MCP Server Instance    # Embedded MCP server
β”œβ”€β”€ SSE Transport Layer    # Real-time communication
└── Request Validation     # Pydantic models

3. React UI Components (web/)

Presentation layer - Modern React widgets following OpenAI Apps SDK patterns.

Component Hierarchy:

App.tsx (Main Container)
β”œβ”€β”€ ProductCard.tsx       # Individual product display
β”œβ”€β”€ hooks.ts             # OpenAI SDK integration hooks
β”œβ”€β”€ types.ts             # TypeScript definitions
└── component.tsx        # Entry point and mounting

Key Features:

  • πŸŒ“ Theme Support - Light/dark mode switching
  • πŸ“± Responsive Design - Mobile-first responsive layout
  • ⚑ Real-time Updates - Live data synchronization
  • 🎨 Nike Branding - Official Nike design system
  • πŸ”— External Links - Direct Nike.com integration

4. Test Infrastructure (test_mcp_client.py)

Development tool - Comprehensive testing client for MCP endpoints.

πŸ“Š Data Flow Architecture

Search Flow

sequenceDiagram
    participant U as User
    participant UI as React Widget
    participant MCP as MCP Server
    participant N as Nike API
    
    U->>UI: Enter search query
    UI->>MCP: search_products(query, filters)
    MCP->>N: HTTP request with headers
    N->>MCP: Product JSON response
    MCP->>MCP: Parse & validate data
    MCP->>UI: Structured product list
    UI->>U: Rendered product cards
Loading

Comparison Flow

sequenceDiagram
    participant U as User
    participant UI as React Widget
    participant MCP as MCP Server
    participant N as Nike API
    
    U->>UI: Select products to compare
    UI->>MCP: compare_products(product_ids)
    par Parallel API Calls
        MCP->>N: Get product 1 details
        MCP->>N: Get product 2 details
    end
    MCP->>MCP: Compare specifications
    MCP->>UI: Comparison matrix
    UI->>U: Side-by-side comparison
Loading

πŸ› οΈ Technology Stack

Backend

  • Python 3.9+ - Core runtime
  • MCP Protocol - Model Context Protocol for AI integration
  • FastAPI - Modern async web framework
  • aiohttp - Async HTTP client
  • Pydantic - Data validation and serialization

Frontend

  • React 18 - UI framework with hooks
  • TypeScript - Type-safe JavaScript
  • ESBuild - Fast bundling and compilation
  • CSS-in-JS - Component-scoped styling

Integration

  • OpenAI Apps SDK - ChatGPT custom app integration
  • Server-Sent Events - Real-time communication
  • CORS - Cross-origin resource sharing

πŸ“ File Structure

nikesearch/
β”œβ”€β”€ 🐍 Backend Services
β”‚   β”œβ”€β”€ nikesearch.py          # Main MCP server (1554 lines)
β”‚   β”œβ”€β”€ nikesearch_rest.py     # REST API wrapper (328 lines)
β”‚   β”œβ”€β”€ test_mcp_client.py     # Testing utilities (151 lines)
β”‚   └── main.py                # Entry point
β”‚
β”œβ”€β”€ βš›οΈ Frontend Components  
β”‚   └── web/
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ App.tsx            # Main container (224 lines)
β”‚       β”‚   β”œβ”€β”€ ProductCard.tsx    # Product display (158 lines) 
β”‚       β”‚   β”œβ”€β”€ component.tsx      # Entry point (26 lines)
β”‚       β”‚   β”œβ”€β”€ hooks.ts           # SDK integration (100 lines)
β”‚       β”‚   └── types.ts           # Type definitions (57 lines)
β”‚       β”œβ”€β”€ dist/
β”‚       β”‚   └── component.js       # Compiled bundle
β”‚       β”œβ”€β”€ test-with-server.html  # Local testing interface
β”‚       └── package.json           # Node.js dependencies
β”‚
β”œβ”€β”€ πŸ“¦ Configuration
β”‚   β”œβ”€β”€ pyproject.toml        # Python project config
β”‚   β”œβ”€β”€ requirements.txt      # Python dependencies  
β”‚   β”œβ”€β”€ uv.lock              # Lock file
β”‚   └── app-manifest.json     # OpenAI app manifest
β”‚
└── πŸ“š Documentation
    β”œβ”€β”€ README.md
    β”œβ”€β”€ ARCHITECTURE.md       # This file
    β”œβ”€β”€ QUICK_START.md
    └── *.md                  # Additional docs

πŸ”„ Integration Patterns

OpenAI Apps SDK Integration

// Hook-based state management
const toolOutput = useToolOutput();           // Get MCP results
const theme = useTheme();                     // UI theme sync
const displayMode = useDisplayMode();         // Layout mode
const [widgetState, setWidgetState] = useWidgetState({});

MCP Protocol Compliance

# Standard MCP server structure
server = Server("nike-search")

@server.call_tool()
async def search_products(query: str, **filters) -> CallToolResult:
    """Tool implementation following MCP standards"""
    return CallToolResult(content=[TextContent(type="text", text=result)])

Nike API Integration

# Robust API client with fallbacks
class NikeSearchClient:
    async def search_products(self, query: str) -> List[NikeProduct]:
        # Multiple endpoint strategies
        # Error handling and retries
        # Response caching
        # Data validation

πŸš€ Deployment Architecture

Development Environment

# Backend services
uv run python nikesearch.py          # MCP server (stdio)
uv run python nikesearch_rest.py     # REST API (port 8000)

# Frontend development  
cd web && npm run build              # Build React components
python3 -m http.server 8080          # Serve test interface

Production Considerations

  • Load Balancing - Multiple MCP server instances
  • Caching Layer - Redis for API response caching
  • Rate Limiting - Nike API usage optimization
  • Monitoring - Health checks and error tracking
  • Security - API key management and request validation

πŸ” Key Design Patterns

1. Async/Await Throughout

All I/O operations use async patterns for optimal performance.

2. Type Safety

Pydantic models and TypeScript ensure data consistency.

3. Error Boundaries

Graceful degradation at every layer.

4. Caching Strategy

Smart caching to minimize Nike API calls.

5. Responsive Design

Mobile-first UI with adaptive layouts.

6. SDK Integration

Native OpenAI Apps SDK patterns for seamless ChatGPT integration.

🎯 Performance Optimizations

  • Parallel API Calls - Concurrent product fetching
  • Image Optimization - Lazy loading and fallbacks
  • Bundle Splitting - Optimized JavaScript delivery
  • Request Deduplication - Avoid redundant API calls
  • Memory Management - Efficient data structures

πŸ” Security Considerations

  • Input Validation - All user inputs sanitized
  • Rate Limiting - Prevent API abuse
  • CORS Policy - Controlled cross-origin access
  • Error Sanitization - No sensitive data in error messages
  • API Key Management - Secure credential handling

This architecture supports both development testing and production deployment, providing a robust foundation for Nike product search and comparison capabilities.