Skip to content

kinfey/AzureMCPDemo

Repository files navigation

Azure MCP Demo - ZavaShop AI Assistant

Overview

This repository demonstrates an advanced implementation of Azure Functions with Model Context Protocol (MCP) integrated with Azure AI Agents for building intelligent shopping assistants. The project showcases how to create remote MCP servers and AI-powered conversational agents that can handle image generation, file search, and interactive customer service scenarios.

Project Architecture

The project consists of three main components:

1. Remote MCP Server with Azure Functions (Python)

Located in MCP/remote-mcp-functions-python/, this component provides:

  • MCP Tool Endpoints: Custom tools exposed via Azure Functions that can be consumed by AI agents
  • Image Generation: Integration with Azure OpenAI DALL-E for AI-powered image creation
  • Blob Storage Integration: Snippet storage and retrieval using Azure Blob Storage
  • Custom Shop Tools: Specialized tools for the ZavaShop use case (custom shirt design, login, product info)

Key Features:

  • Secure HTTPS endpoints with function key authentication
  • Server-Sent Events (SSE) for real-time MCP communication
  • Extensible tool framework using MCP protocol
  • Local development support with Azurite storage emulator
  • One-command deployment to Azure using azd

2. AI Agent with MCP Integration

File: zavashop_voicelive_with_mcp_file_agent.py

This implementation demonstrates:

  • Prompt Agent using Azure AI Projects SDK
  • MCP Tool Integration: Connects to the remote MCP server for image generation
  • File Search Capability: Vector store-based product information retrieval
  • Approval Workflow: Demonstrates MCP approval request handling for controlled tool execution

3. AI Agent with Function Tools

File: zavashop_voicelive_with_tool_genai_file_agent.py

This alternative implementation showcases:

  • Function Tools: Direct function call handling within the agent
  • Azure OpenAI Image Generation: Inline image generation without external MCP server
  • Hybrid Tool Approach: Combines file search and custom function tools

Use Case: ZavaShop Intelligent Assistant

The ZavaShop assistant is an AI-powered customer service agent that can:

  1. Answer Product Queries: Uses file search to retrieve information from product catalogs
  2. Generate Custom Designs: Creates visual representations of clothing items based on customer descriptions
  3. Handle Customer Requests: Assists with login, customization, and personalized shopping experiences

Example Workflows

Scenario 1: Product Search

User: "Can you list all SKUs in ZavaShop?"
Agent: Uses file search tool → Returns product information from vector store

Scenario 2: Custom Design Generation

User: "Create a blue Uniqlo-style shirt with the Strava logo"
Agent: Triggers image generation → Returns generated image URL

Scenario 3: Custom Shirt with Running Stats

User: "I want a custom shirt with my marathon stats"
Agent: Prompts for login → Retrieves stats → Generates custom design

Technology Stack

Azure Services

  • Azure Functions: Serverless compute for MCP server hosting
  • Azure AI Foundry: Agent orchestration and conversation management
  • Azure OpenAI Service: Image generation (DALL-E) and language models
  • Azure Blob Storage: Snippet and image storage
  • Azure Vector Store: Product information indexing and search

Development Tools

  • Python 3.11+: Primary programming language
  • Azure Developer CLI (azd): Simplified deployment
  • Azure Functions Core Tools: Local development and debugging
  • Model Context Protocol: Standardized AI tool integration
  • OpenAI SDK: Agent and response handling

Key Dependencies

  • azure-ai-projects: AI agent framework
  • azure-functions: Function app framework
  • azure-storage-blob: Blob storage integration
  • azure-identity: Azure authentication
  • openai: OpenAI client for agent operations
  • requests: HTTP client for API calls
  • Pillow: Image processing

Project Structure

AzureMCPDemo/
├── MCP/
│   └── remote-mcp-functions-python/     # Remote MCP Server
│       ├── src/
│       │   ├── function_app.py          # MCP tool definitions
│       │   ├── requirements.txt         # Python dependencies
│       │   └── imgs/                    # Generated images directory
│       ├── infra/                       # Azure infrastructure (Bicep)
│       │   ├── main.bicep              # Main infrastructure template
│       │   └── app/                    # Application-specific resources
│       └── azure.yaml                   # Azure Developer CLI configuration
├── zavashop_voicelive_with_mcp_file_agent.py      # MCP-based agent
├── zavashop_voicelive_with_tool_genai_file_agent.py  # Function tool-based agent
└── README.md                            # This file

Getting Started

Prerequisites

  1. Azure Subscription: An active Azure subscription

  2. Development Tools:

    • Python 3.11 or higher
    • Azure Functions Core Tools (>=4.0.7030)
    • Azure Developer CLI (azd)
    • Docker (for Azurite storage emulator)
  3. Environment Variables: Create a .env file with the following:

    AZURE_AI_PROJECT_ENDPOINT=<your-ai-project-endpoint>
    AZURE_AI_MODEL_DEPLOYMENT_NAME=<your-model-deployment>
    AZURE_OPENAI_ENDPOINT=<your-openai-endpoint>
    AZURE_OPENAI_DEPLOYMENT_NAME=<your-image-model-deployment>
    AZURE_OPENAI_API_KEY=<your-api-key>
    AZURE_OPENAI_API_VERSION=2025-04-01-preview
    MCP_ENDPOINT=<your-mcp-function-endpoint>
    ASSET_FILE_PATH=<path-to-product-catalog>

Local Development

1. Start Azure Storage Emulator

docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 \
    mcr.microsoft.com/azure-storage/azurite

2. Run MCP Server Locally

cd MCP/remote-mcp-functions-python/src
pip install -r requirements.txt
func start

The MCP server will be available at: http://0.0.0.0:7071/runtime/webhooks/mcp/sse

3. Test with MCP Inspector

npx @modelcontextprotocol/inspector

Connect to: http://0.0.0.0:7071/runtime/webhooks/mcp/sse

4. Run AI Agent

# MCP-based agent
python zavashop_voicelive_with_mcp_file_agent.py

# Function tool-based agent
python zavashop_voicelive_with_tool_genai_file_agent.py

Deploy to Azure

Deploy MCP Server

cd MCP/remote-mcp-functions-python
azd up

This command will:

  • Provision Azure Functions, Storage Account, and other required resources
  • Deploy the MCP server code
  • Configure security and networking

Optional: Enable VNet Isolation

azd env set VNET_ENABLED true
azd up

Retrieve Function Keys

After deployment, get the MCP system key:

az functionapp keys list \
  --resource-group <resource_group> \
  --name <function_app_name>

Update your .env file with:

MCP_ENDPOINT=https://<function-app-name>.azurewebsites.net/runtime/webhooks/mcp/sse?code=<mcp_extension_key>

MCP Tools Available

Built-in Tools

  1. hello_mcp: Test connectivity and basic functionality
  2. custom: Initiate custom shirt design process
  3. login: Authenticate and retrieve user running statistics
  4. get_snippet: Retrieve saved code snippets from blob storage
  5. save_snippet: Save code snippets to blob storage
  6. gen_image: Generate images using Azure OpenAI DALL-E

Tool Definitions

Each tool is defined using the MCP protocol with:

  • Tool Name: Unique identifier
  • Description: Natural language description for AI understanding
  • Parameters: JSON schema defining input requirements
  • Bindings: Azure Functions bindings for storage/compute integration

Agent Implementation Patterns

Pattern 1: MCP Remote Tools (Recommended for Production)

Advantages:

  • Centralized tool management
  • Reusable across multiple agents
  • Scalable and maintainable
  • Supports approval workflows

Use When:

  • Multiple agents need the same tools
  • Tools require complex infrastructure
  • Governance and approval is required

Pattern 2: Function Tools (Lightweight)

Advantages:

  • Simpler implementation
  • Direct function execution
  • Lower latency
  • No external dependencies

Use When:

  • Single agent application
  • Simple tool logic
  • Rapid prototyping

Security Considerations

MCP Server Security

  • Function Keys: All endpoints secured with system keys
  • HTTPS Only: Enforced SSL/TLS encryption
  • CORS Configuration: Restricted origins
  • Optional OAuth: Extensible with Azure AD authentication
  • VNet Integration: Network isolation support

AI Agent Security

  • Credential Management: Uses Azure DefaultAzureCredential
  • MCP Approval Flow: Manual approval for sensitive operations
  • Input Validation: Tool parameters validated before execution
  • Audit Logging: All tool calls logged for compliance

Advanced Features

Image Generation Pipeline

  1. User provides natural language prompt
  2. Agent processes prompt and calls gen_image tool
  3. Azure OpenAI DALL-E generates image (base64)
  4. Image uploaded to Azure Blob Storage
  5. Public URL returned to user
  6. Image can be displayed in UI or used for further processing

Vector Store Search

  1. Product catalog uploaded to Azure OpenAI Vector Store
  2. Agent uses FileSearchTool for semantic search
  3. Relevant product information retrieved and presented
  4. Context-aware responses based on inventory

Monitoring and Debugging

View Function App Logs

az webapp log tail \
  --name <function-app-name> \
  --resource-group <resource-group>

MCP Inspector for Tool Testing

The MCP Inspector provides:

  • Real-time tool discovery
  • Interactive tool testing
  • Response validation
  • Debug information

Application Insights

All deployed functions include Application Insights for:

  • Performance monitoring
  • Error tracking
  • Custom telemetry
  • Distributed tracing

Cost Optimization

Consumption Plan

  • Pay only for execution time
  • Automatic scaling
  • No idle costs

Storage Costs

  • Blob storage: Pay for capacity used
  • Consider lifecycle policies for old images

AI Services

  • Azure OpenAI: Token-based pricing
  • Choose appropriate model tiers for use case

Best Practices

  1. Environment Management: Use separate environments for dev/test/prod
  2. Secret Management: Store sensitive data in Azure Key Vault
  3. Error Handling: Implement comprehensive try-catch blocks
  4. Logging: Use structured logging for better observability
  5. Testing: Test tools individually before agent integration
  6. Documentation: Keep tool descriptions clear for AI understanding
  7. Versioning: Use agent versioning for safe updates

Troubleshooting

Common Issues

MCP Connection Fails

  • Verify function app is running
  • Check function key is correct
  • Ensure CORS is configured properly

Image Generation Fails

  • Validate Azure OpenAI endpoint and key
  • Check deployment name matches
  • Verify API version compatibility

File Search Returns No Results

  • Confirm vector store is created
  • Verify file upload succeeded
  • Check agent has correct vector_store_ids

Agent Doesn't Use Tools

  • Ensure tool descriptions are clear
  • Verify instructions reference tool capabilities
  • Check MCP approval wasn't rejected

Contributing

This is a demonstration project showcasing Azure AI capabilities. Feel free to:

  • Fork and experiment with custom tools
  • Add new agent capabilities
  • Improve error handling and logging
  • Share feedback and suggestions

Related Resources

License

This project is provided as-is for demonstration purposes. Refer to individual component licenses for specific terms.

Support

For issues and questions:


Built with Azure AI | Powered by Model Context Protocol | Enhanced by OpenAI

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages