# ============================================================================
# Azure APIM MCP Demo - Deployment Script
# ============================================================================
# This script deploys the complete infrastructure for the APIM MCP demo
# ============================================================================
param(
[Parameter(Mandatory=$false)]
[string]$ResourceGroupName = "apim-mcp-demo-rg",
[Parameter(Mandatory=$false)]
[string]$Location = "eastus",
[Parameter(Mandatory=$false)]
[string]$Environment = "dev",
[Parameter(Mandatory=$true)]
[string]$PublisherEmail,
[Parameter(Mandatory=$false)]
[string]$NamePrefix = "apimmcp"
)
$ErrorActionPreference = "Stop"
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Azure APIM MCP Demo - Deployment" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
# Check if logged in to Azure
Write-Host "Checking Azure login status..." -ForegroundColor Yellow
$account = az account show 2>$null | ConvertFrom-Json
if (-not $account) {
Write-Host "Not logged in to Azure. Please run 'az login' first." -ForegroundColor Red
exit 1
}
Write-Host "Logged in as: $($account.user.name)" -ForegroundColor Green
Write-Host "Subscription: $($account.name)" -ForegroundColor Green
Write-Host ""
# Create Resource Group if it doesn't exist
Write-Host "Creating resource group '$ResourceGroupName' in '$Location'..." -ForegroundColor Yellow
az group create --name $ResourceGroupName --location $Location --output none
Write-Host "Resource group ready." -ForegroundColor Green
Write-Host ""
# Deploy Infrastructure
Write-Host "Deploying infrastructure (this may take 15-30 minutes for APIM)..." -ForegroundColor Yellow
$deploymentName = "apim-mcp-deploy-$(Get-Date -Format 'yyyyMMddHHmmss')"
$deployment = az deployment group create `
--resource-group $ResourceGroupName `
--template-file ".\main.bicep" `
--parameters `
location=$Location `
namePrefix=$NamePrefix `
environment=$Environment `
apimPublisherEmail=$PublisherEmail `
--name $deploymentName `
--output json | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) {
Write-Host "Deployment failed!" -ForegroundColor Red
exit 1
}
Write-Host "Infrastructure deployed successfully!" -ForegroundColor Green
Write-Host ""
# Get deployment outputs
$outputs = $deployment.properties.outputs
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Deployment Outputs" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Function App URL: $($outputs.functionAppUrl.value)" -ForegroundColor White
Write-Host "APIM Gateway URL: $($outputs.apimGatewayUrl.value)" -ForegroundColor White
Write-Host "MCP Endpoint: $($outputs.mcpEndpoint.value)" -ForegroundColor White
Write-Host "Developer Portal: $($outputs.apimPortalUrl.value)" -ForegroundColor White
Write-Host ""
# Apply MCP Policy
Write-Host "Applying MCP Server policy to APIM..." -ForegroundColor Yellow
$apimName = "$NamePrefix-apim-$Environment"
$policyPath = ".\apim-policies\mcp-server-policy.xml"
if (Test-Path $policyPath) {
$policyContent = Get-Content $policyPath -Raw
# Deploy policy module
az deployment group create `
--resource-group $ResourceGroupName `
--template-file ".\modules\apim-policy.bicep" `
--parameters `
apimName=$apimName `
policyContent="$policyContent" `
--output none
if ($LASTEXITCODE -eq 0) {
Write-Host "MCP policy applied successfully!" -ForegroundColor Green
} else {
Write-Host "Warning: Failed to apply policy. You may need to apply it manually." -ForegroundColor Yellow
}
} else {
Write-Host "Warning: Policy file not found at $policyPath" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Next Steps" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "1. Deploy Azure Functions code:" -ForegroundColor White
Write-Host " cd ..\backend-functions" -ForegroundColor Gray
Write-Host " npm install" -ForegroundColor Gray
Write-Host " npm run build" -ForegroundColor Gray
Write-Host " func azure functionapp publish $NamePrefix-func-$Environment" -ForegroundColor Gray
Write-Host ""
Write-Host "2. Get APIM subscription key:" -ForegroundColor White
Write-Host " az apim subscription list --resource-group $ResourceGroupName --service-name $apimName --query '[0].primaryKey' -o tsv" -ForegroundColor Gray
Write-Host ""
Write-Host "3. Test MCP endpoint:" -ForegroundColor White
Write-Host " curl -X POST $($outputs.mcpEndpoint.value) -H 'Ocp-Apim-Subscription-Key: <your-key>' -H 'Content-Type: application/json' -d '{""jsonrpc"":""2.0"",""method"":""tools/list"",""id"":""1""}'" -ForegroundColor Gray
Write-Host ""