Skip to content

kevinpbuckley/SC_MCP_With_MAF_External_Auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sitecore MCP External Authentication Examples

This repository demonstrates how to integrate with Sitecore's Model Context Protocol (MCP) server using OAuth 2.1 authentication with PKCE, showcasing both Python and .NET implementations.

Overview

These examples show how to build AI agents that can interact with Sitecore content management through MCP tools, using the Microsoft Agent Framework with Azure OpenAI.

πŸ“ Project Structure

SCMP_AI_Foundry_App/
β”œβ”€β”€ python_example/          # Python implementation (fully automated)
β”‚   β”œβ”€β”€ agent_with_mcp.py   # Main chat app with MCP tools
β”‚   β”œβ”€β”€ mcp_compliant_auth.py # OAuth 2.1 + PKCE authentication
β”‚   β”œβ”€β”€ instructions.md      # Agent behavior configuration
β”‚   β”œβ”€β”€ mcp.json            # MCP server configuration
β”‚   └── README.md           # Python-specific documentation
β”‚
β”œβ”€β”€ dotnet_example/          # .NET implementation (using MCP SDK)
β”‚   └── SitecoreMcpChat/
β”‚       β”œβ”€β”€ Program.cs      # Main chat application
β”‚       β”œβ”€β”€ McpOAuthClient.cs    # OAuth 2.1 + PKCE
β”‚       β”œβ”€β”€ instructions.md      # Agent behavior configuration
β”‚       β”œβ”€β”€ mcp.json            # MCP server configuration
β”‚       └── README.md           # .NET-specific documentation
β”‚
└── README.md               # This file

πŸš€ Quick Start

Python (Recommended)

cd python_example
pip install -r requirements.txt
cp .env.example .env        # Configure Azure OpenAI credentials
python agent_with_mcp.py

Features:

  • βœ… Streaming responses (word-by-word output)
  • βœ… Automatic MCP tool integration
  • βœ… 39+ Sitecore tools automatically available
  • βœ… Tool call indicators (πŸ”§ emoji)
  • βœ… OAuth 2.1 with PKCE and token caching

.NET

cd dotnet_example/SitecoreMcpChat
cp appsettings.example.json appsettings.json  # Configure Azure OpenAI
dotnet run

Features:

  • βœ… Official ModelContextProtocol C# SDK (v0.4.0-preview.3)
  • βœ… HttpClientTransport for HTTP/SSE connections
  • βœ… OAuth 2.1 with PKCE and token caching
  • βœ… MCP tool discovery via SDK (39+ tools)
  • βœ… Native MCP tool integration via Agent Framework
  • βœ… Automatic function calling - agent calls tools as needed
  • βœ… Tool call visibility - shows which tools are being called
  • ⚠️ No streaming (preview limitation)

πŸ” Authentication

Both implementations use OAuth 2.1 with PKCE for secure authentication:

  1. Metadata Discovery: Reads OAuth endpoints from .well-known/oauth-authorization-server
  2. Dynamic Client Registration: Registers OAuth client with the MCP server
  3. PKCE Flow: Uses code challenge/verifier for enhanced security
  4. Browser Authentication: Opens browser for user login
  5. Local Callback: Runs HTTP server on localhost:3000 to receive auth code
  6. Token Exchange: Exchanges authorization code for access token
  7. Token Caching: Caches tokens for 24 hours (development only)

⚠️ Security Warning - Token Caching

Current Implementation (Development Only):

  • Tokens cached in .mcp_token_cache.json
  • 24-hour expiry with 5-minute safety buffer
  • Plain text storage
  • NO encryption
  • NO audit logging

Production Requirements:

  • βœ… Use Azure Key Vault or similar secure credential storage
  • βœ… Implement managed identities where possible
  • βœ… Enable token rotation and refresh mechanisms
  • βœ… Set shorter token lifetimes (1 hour maximum)
  • βœ… Enable comprehensive audit logging
  • βœ… Use encrypted storage for cached credentials
  • ❌ NEVER commit token cache files to source control
  • ❌ NEVER use plain text file storage in production

πŸ› οΈ Available MCP Tools

Both implementations provide access to 39+ Sitecore MCP tools:

Site Management

  • List sites, get site information, manage site configuration

Content Management

  • Create/update/delete pages and content items
  • Search content, get page details, manage page templates

Component Management

  • List components, add/remove components from pages
  • Manage component datasources, set personalization

Language & Localization

  • Get available languages, add language versions to pages

Asset Management

  • Search assets, upload files, update asset metadata

Marketing Operations

  • Create personalization versions, manage targeting rules

πŸ“ Configuration

Agent Instructions

Both implementations use external instructions.md files for agent behavior:

Python: python_example/instructions.md

  • Simple guidelines for when to use MCP tools
  • Python Agent Framework handles tool integration automatically

dotnet: dotnet_example/SitecoreMcpChat/instructions.md

  • Simple guidelines for when to use MCP tools
  • .NET Agent Framework handles tool integration automatically
  • Same automatic approach as Python

MCP Server Configuration

Both use mcp.json for MCP server settings:

{
  "mcpServers": {
    "marketer": {
      "url": "https://edge-platform.sitecorecloud.io/mcp/marketer-mcp-prod",
      "auth": {
        "type": "external"
      }
    }
  }
}

Azure OpenAI Configuration

Python: .env file

AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_DEPLOYMENT=gpt-5.1-chat
AZURE_OPENAI_API_KEY=your-api-key
AZURE_OPENAI_API_VERSION=2025-01-01-preview

.NET: appsettings.json file

{
  "AZURE_OPENAI_ENDPOINT": "https://your-resource.openai.azure.com",
  "AZURE_OPENAI_DEPLOYMENT": "gpt-5.1-chat",
  "AZURE_OPENAI_API_KEY": "your-api-key"
}

πŸ”„ Implementation Comparison

Feature Python .NET
OAuth 2.1 + PKCE βœ… βœ…
Token Caching βœ… (24 hours) βœ… (24 hours)
MCP SDK Usage βœ… (MCPStreamableHTTPTool) βœ… (HttpClientTransport)
Tool Discovery βœ… (automatic) βœ… (automatic via SDK)
Tool Execution βœ… (automatic) βœ… (automatic via framework)
Tool Orchestration βœ… (automatic) βœ… (automatic)
Streaming Responses βœ… (word-level) ❌ (preview limitation)
Tool Call Visibility βœ… βœ…
Conversation History βœ… βœ…
Instructions File βœ… (instructions.md) βœ… (instructions.md)

Both implementations use native MCP integration:

  • Python: Uses MCPStreamableHTTPTool for automatic tool orchestration
  • .NET: Passes tools to agent via tools: [.. mcpTools.Cast<AITool>()] for automatic orchestration
  • Both follow official Microsoft Agent Framework documentation patterns
  • Both are production-ready with automatic function calling

Choose based on your stack: Both implementations provide the same automatic tool calling capabilities.

🎯 Use Cases

  • Content Management: Create, update, and manage Sitecore content items
  • Site Administration: Manage sites, pages, and templates
  • Marketing Operations: Set up personalization and targeting
  • Asset Management: Upload and organize media files
  • Multi-language Support: Manage content across multiple languages
  • Component Management: Configure page layouts and components

πŸ“š Documentation

πŸ”§ Prerequisites

Python

  • Python 3.13+
  • Azure OpenAI resource
  • Sitecore MCP server access

.NET

  • .NET 9.0 SDK+
  • Azure OpenAI resource
  • Sitecore MCP server access

🀝 Contributing

This is example code for learning purposes. Feel free to adapt and extend for your own use cases.

⚠️ Important Notes

  1. Token Caching: Current implementation is for DEVELOPMENT ONLY. Use secure credential storage for production.
  2. Error Handling: Examples include basic error handling. Production code should be more robust.
  3. Agent Framework Versions: Python uses agent-framework package, .NET uses Microsoft.Agents.AI preview packages.
  4. MCP SDK Integration:
    • Python: Native integration via MCPStreamableHTTPTool - fully automatic orchestration
    • .NET: Native integration via Agent Framework - passes tools directly to agent for automatic orchestration
    • Both use official SDKs with automatic function calling
  5. Production Use: Both Python and .NET are production-ready with automatic tool calling. Choose based on your preferred tech stack.

πŸ“„ License

This is example code for educational purposes.

About

Sitecore Marketer MCP Agent with External Auth examples in Python and .NET

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors