# Introduction to MCP Database Integration ## 🎯 What This Lab Covers This introduction lab provides a comprehensive overview of building Model Context Protocol (MCP) servers with database integration. You'll understand the business case, technical architecture, and real-world applications through the Zava Retail analytics use case at https://github.com/microsoft/MCP-Server-and-PostgreSQL-Sample-Retail. ## Overview **Model Context Protocol (MCP)** enables AI assistants to securely access and interact with external data sources in real-time. When combined with database integration, MCP unlocks powerful capabilities for data-driven AI applications. This learning path teaches you to build production-ready MCP servers that connect AI assistants to retail sales data through PostgreSQL, implementing enterprise patterns like Row Level Security, semantic search, and multi-tenant data access. ## Learning Objectives By the end of this lab, you will be able to: - **Define** Model Context Protocol and its core benefits for database integration - **Identify** key components of an MCP server architecture with databases - **Understand** the Zava Retail use case and its business requirements - **Recognize** enterprise patterns for secure, scalable database access - **List** the tools and technologies used throughout this learning path ## 🧭 The Challenge: AI Meets Real-World Data ### Traditional AI Limitations Modern AI assistants are incredibly powerful but face significant limitations when working with real-world business data: | **Challenge** | **Description** | **Business Impact** | |---------------|-----------------|-------------------| | **Static Knowledge** | AI models trained on fixed datasets can't access current business data | Outdated insights, missed opportunities | | **Data Silos** | Information locked in databases, APIs, and systems AI can't reach | Incomplete analysis, fragmented workflows | | **Security Constraints** | Direct database access raises security and compliance concerns | Limited deployment, manual data preparation | | **Complex Queries** | Business users need technical knowledge to extract data insights | Reduced adoption, inefficient processes | ### The MCP Solution Model Context Protocol addresses these challenges by providing: - **Real-time Data Access**: AI assistants query live databases and APIs - **Secure Integration**: Controlled access with authentication and permissions - **Natural Language Interface**: Business users ask questions in plain English - **Standardized Protocol**: Works across different AI platforms and tools ## πŸͺ Meet Zava Retail: Our Learning Case Study https://github.com/microsoft/MCP-Server-and-PostgreSQL-Sample-Retail Throughout this learning path, we'll build an MCP server for **Zava Retail**, a fictional DIY retail chain with multiple store locations. This realistic scenario demonstrates enterprise-grade MCP implementation. ### Business Context **Zava Retail** operates: - **8 physical stores** across Washington state (Seattle, Bellevue, Tacoma, Spokane, Everett, Redmond, Kirkland) - **1 online store** for e-commerce sales - **Diverse product catalog** including tools, hardware, garden supplies, and building materials - **Multi-level management** with store managers, regional managers, and executives ### Business Requirements Store managers and executives need AI-powered analytics to: 1. **Analyze sales performance** across stores and time periods 2. **Track inventory levels** and identify restocking needs 3. **Understand customer behavior** and purchasing patterns 4. **Discover product insights** through semantic search 5. **Generate reports** with natural language queries 6. **Maintain data security** with role-based access control ### Technical Requirements The MCP server must provide: - **Multi-tenant data access** where store managers see only their store's data - **Flexible querying** supporting complex SQL operations - **Semantic search** for product discovery and recommendations - **Real-time data** reflecting current business state - **Secure authentication** with row-level security - **Scalable architecture** supporting multiple concurrent users ## πŸ—οΈ MCP Server Architecture Overview Our MCP server implements a layered architecture optimized for database integration: ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ VS Code AI Client β”‚ β”‚ (Natural Language Queries) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ HTTP/SSE β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MCP Server β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Tool Layer β”‚ β”‚ Security Layer β”‚ β”‚ Config Layer β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Query Tools β”‚ β”‚ β€’ RLS Context β”‚ β”‚ β€’ Environment β”‚ β”‚ β”‚ β”‚ β€’ Schema Tools β”‚ β”‚ β€’ User Identity β”‚ β”‚ β€’ Connections β”‚ β”‚ β”‚ β”‚ β€’ Search Tools β”‚ β”‚ β€’ Access Controlβ”‚ β”‚ β€’ Validation β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ asyncpg β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ PostgreSQL Database β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ Retail Schema β”‚ β”‚ RLS Policies β”‚ β”‚ pgvector β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Stores β”‚ β”‚ β€’ Store-based β”‚ β”‚ β€’ Embeddings β”‚ β”‚ β”‚ β”‚ β€’ Customers β”‚ β”‚ Isolation β”‚ β”‚ β€’ Similarity β”‚ β”‚ β”‚ β”‚ β€’ Products β”‚ β”‚ β€’ Role Control β”‚ β”‚ Search β”‚ β”‚ β”‚ β”‚ β€’ Orders β”‚ β”‚ β€’ Audit Logs β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ REST API β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Azure OpenAI β”‚ β”‚ (Text Embeddings) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Key Components #### **1. MCP Server Layer** - **FastMCP Framework**: Modern Python MCP server implementation - **Tool Registration**: Declarative tool definitions with type safety - **Request Context**: User identity and session management - **Error Handling**: Robust error management and logging #### **2. Database Integration Layer** - **Connection Pooling**: Efficient asyncpg connection management - **Schema Provider**: Dynamic table schema discovery - **Query Executor**: Secure SQL execution with RLS context - **Transaction Management**: ACID compliance and rollback handling #### **3. Security Layer** - **Row Level Security**: PostgreSQL RLS for multi-tenant data isolation - **User Identity**: Store manager authentication and authorization - **Access Control**: Fine-grained permissions and audit trails - **Input Validation**: SQL injection prevention and query validation #### **4. AI Enhancement Layer** - **Semantic Search**: Vector embeddings for product discovery - **Azure OpenAI Integration**: Text embedding generation - **Similarity Algorithms**: pgvector cosine similarity search - **Search Optimization**: Indexing and performance tuning ## πŸ”§ Technology Stack ### Core Technologies | **Component** | **Technology** | **Purpose** | |---------------|----------------|-------------| | **MCP Framework** | FastMCP (Python) | Modern MCP server implementation | | **Database** | PostgreSQL 17 + pgvector | Relational data with vector search | | **AI Services** | Azure OpenAI | Text embeddings and language models | | **Containerization** | Docker + Docker Compose | Development environment | | **Cloud Platform** | Microsoft Azure | Production deployment | | **IDE Integration** | VS Code | AI Chat and development workflow | ### Development Tools | **Tool** | **Purpose** | |----------|-------------| | **asyncpg** | High-performance PostgreSQL driver | | **Pydantic** | Data validation and serialization | | **Azure SDK** | Cloud service integration | | **pytest** | Testing framework | | **Docker** | Containerization and deployment | ### Production Stack | **Service** | **Azure Resource** | **Purpose** | |-------------|-------------------|-------------| | **Database** | Azure Database for PostgreSQL | Managed database service | | **Container** | Azure Container Apps | Serverless container hosting | | **AI Services** | Microsoft Foundry | OpenAI models and endpoints | | **Monitoring** | Application Insights | Observability and diagnostics | | **Security** | Azure Key Vault | Secrets and configuration management | ## 🎬 Real-World Usage Scenarios Let's explore how different users interact with our MCP server: ### Scenario 1: Store Manager Performance Review **User**: Sarah, Seattle Store Manager **Goal**: Analyze last quarter's sales performance **Natural Language Query**: > "Show me the top 10 products by revenue for my store in Q4 2024" **What Happens**: 1. VS Code AI Chat sends query to MCP server 2. MCP server identifies Sarah's store context (Seattle) 3. RLS policies filter data to Seattle store only 4. SQL query generated and executed 5. Results formatted and returned to AI Chat 6. AI provides analysis and insights ### Scenario 2: Product Discovery with Semantic Search **User**: Mike, Inventory Manager **Goal**: Find products similar to a customer request **Natural Language Query**: > "What products do we sell that are similar to 'waterproof electrical connectors for outdoor use'?" **What Happens**: 1. Query processed by semantic search tool 2. Azure OpenAI generates embedding vector 3. pgvector performs similarity search 4. Related products ranked by relevance 5. Results include product details and availability 6. AI suggests alternatives and bundling opportunities ### Scenario 3: Cross-Store Analytics **User**: Jennifer, Regional Manager **Goal**: Compare performance across all stores **Natural Language Query**: > "Compare sales by category for all stores in the last 6 months" **What Happens**: 1. RLS context set for regional manager access 2. Complex multi-store query generated 3. Data aggregated across store locations 4. Results include trends and comparisons 5. AI identifies insights and recommendations ## πŸ”’ Security and Multi-Tenancy Deep Dive Our implementation prioritizes enterprise-grade security: ### Row Level Security (RLS) PostgreSQL RLS ensures data isolation: ```sql -- Store managers see only their store's data CREATE POLICY store_manager_policy ON retail.orders FOR ALL TO store_managers USING (store_id = get_current_user_store()); -- Regional managers see multiple stores CREATE POLICY regional_manager_policy ON retail.orders FOR ALL TO regional_managers USING (store_id = ANY(get_user_store_list())); ``` ### User Identity Management Each MCP connection includes: - **Store Manager ID**: Unique identifier for RLS context - **Role Assignment**: Permissions and access levels - **Session Management**: Secure authentication tokens - **Audit Logging**: Complete access history ### Data Protection Multiple layers of security: - **Connection Encryption**: TLS for all database connections - **SQL Injection Prevention**: Parameterized queries only - **Input Validation**: Comprehensive request validation - **Error Handling**: No sensitive data in error messages ## 🎯 Key Takeaways After completing this introduction, you should understand: βœ… **MCP Value Proposition**: How MCP bridges AI assistants and real-world data βœ… **Business Context**: Zava Retail's requirements and challenges βœ… **Architecture Overview**: Key components and their interactions βœ… **Technology Stack**: Tools and frameworks used throughout βœ… **Security Model**: Multi-tenant data access and protection βœ… **Usage Patterns**: Real-world query scenarios and workflows ## πŸš€ What's Next Ready to dive deeper? Continue with: **[Lab 01: Core Architecture Concepts](../01-Architecture/README.md)** Learn about MCP server architecture patterns, database design principles, and the detailed technical implementation that powers our retail analytics solution. ## πŸ“š Additional Resources ### MCP Documentation - [MCP Specification](https://modelcontextprotocol.io/docs/) - Official protocol documentation - [MCP for Beginners](https://aka.ms/mcp-for-beginners) - Comprehensive MCP learning guide - [FastMCP Documentation](https://github.com/modelcontextprotocol/python-sdk) - Python SDK documentation ### Database Integration - [PostgreSQL Documentation](https://www.postgresql.org/docs/) - Complete PostgreSQL reference - [pgvector Guide](https://github.com/pgvector/pgvector) - Vector extension documentation - [Row Level Security](https://www.postgresql.org/docs/current/ddl-rowsecurity.html) - PostgreSQL RLS guide ### Azure Services - [Azure OpenAI Documentation](https://docs.microsoft.com/azure/cognitive-services/openai/) - AI service integration - [Azure Database for PostgreSQL](https://docs.microsoft.com/azure/postgresql/) - Managed database service - [Azure Container Apps](https://docs.microsoft.com/azure/container-apps/) - Serverless containers --- **Disclaimer**: This is a learning exercise using fictional retail data. Always follow your organization's data governance and security policies when implementing similar solutions in production environments.