# ============================================================================
# MCP Server Test Script (PowerShell)
# ============================================================================
# This script tests the MCP server endpoint exposed through Azure APIM
# Usage: .\test-mcp.ps1 -ApimUrl <url> -SubscriptionKey <key>
# ============================================================================
param(
[Parameter(Mandatory=$false)]
[string]$ApimUrl = $env:APIM_MCP_URL,
[Parameter(Mandatory=$false)]
[string]$SubscriptionKey = $env:APIM_SUBSCRIPTION_KEY
)
if (-not $ApimUrl -or -not $SubscriptionKey) {
Write-Host "Error: APIM URL and Subscription Key are required" -ForegroundColor Red
Write-Host "Usage: .\test-mcp.ps1 -ApimUrl <url> -SubscriptionKey <key>" -ForegroundColor Yellow
Write-Host " or set APIM_MCP_URL and APIM_SUBSCRIPTION_KEY environment variables" -ForegroundColor Yellow
exit 1
}
$headers = @{
"Content-Type" = "application/json"
"Ocp-Apim-Subscription-Key" = $SubscriptionKey
}
function Invoke-MCPRequest {
param(
[string]$Method,
[hashtable]$Params = @{},
[string]$Id = [guid]::NewGuid().ToString()
)
$body = @{
jsonrpc = "2.0"
method = $Method
params = $Params
id = $Id
} | ConvertTo-Json -Depth 10
try {
$response = Invoke-RestMethod -Uri $ApimUrl -Method Post -Headers $headers -Body $body -ErrorAction Stop
return @{
Success = $true
Response = $response
}
}
catch {
return @{
Success = $false
Error = $_.Exception.Message
}
}
}
# Test definitions
$tests = @(
@{
Name = "Initialize"
Method = "initialize"
Params = @{
protocolVersion = "2024-11-05"
capabilities = @{}
clientInfo = @{
name = "test-client"
version = "1.0.0"
}
}
},
@{
Name = "List Tools"
Method = "tools/list"
Params = @{}
},
@{
Name = "Get Products"
Method = "tools/call"
Params = @{
name = "get_products"
arguments = @{}
}
},
@{
Name = "Get Products (filtered)"
Method = "tools/call"
Params = @{
name = "get_products"
arguments = @{
category = "electronics"
}
}
},
@{
Name = "Search Products"
Method = "tools/call"
Params = @{
name = "search_products"
arguments = @{
query = "widget"
maxResults = 5
}
}
},
@{
Name = "Get Single Product"
Method = "tools/call"
Params = @{
name = "get_product"
arguments = @{
productId = "prod-001"
}
}
},
@{
Name = "Create Order"
Method = "tools/call"
Params = @{
name = "create_order"
arguments = @{
productId = "prod-001"
quantity = 2
}
}
},
@{
Name = "Get Orders"
Method = "tools/call"
Params = @{
name = "get_orders"
arguments = @{}
}
},
@{
Name = "Check Health"
Method = "tools/call"
Params = @{
name = "check_health"
arguments = @{}
}
}
)
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "MCP Server Tests" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "URL: $ApimUrl" -ForegroundColor White
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
$passed = 0
$failed = 0
foreach ($test in $tests) {
Write-Host "`nš Test: $($test.Name)" -ForegroundColor White
Write-Host " Method: $($test.Method)" -ForegroundColor Gray
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$result = Invoke-MCPRequest -Method $test.Method -Params $test.Params
$stopwatch.Stop()
if ($result.Success) {
if ($result.Response.error) {
Write-Host " ā FAILED ($($stopwatch.ElapsedMilliseconds)ms)" -ForegroundColor Red
Write-Host " Error: $($result.Response.error | ConvertTo-Json -Compress)" -ForegroundColor Red
$failed++
}
else {
Write-Host " ā
PASSED ($($stopwatch.ElapsedMilliseconds)ms)" -ForegroundColor Green
$preview = ($result.Response.result | ConvertTo-Json -Compress)
if ($preview.Length -gt 200) {
$preview = $preview.Substring(0, 200) + "..."
}
Write-Host " Response: $preview" -ForegroundColor Gray
$passed++
}
}
else {
Write-Host " ā FAILED" -ForegroundColor Red
Write-Host " Error: $($result.Error)" -ForegroundColor Red
$failed++
}
}
Write-Host "`n============================================" -ForegroundColor Cyan
Write-Host "Test Summary" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Total: $($tests.Count)" -ForegroundColor White
Write-Host "Passed: $passed" -ForegroundColor Green
Write-Host "Failed: $failed" -ForegroundColor $(if ($failed -gt 0) { "Red" } else { "Green" })
Write-Host "============================================" -ForegroundColor Cyan
exit $(if ($failed -gt 0) { 1 } else { 0 })