Successfully implemented the Azure AI Foundry infrastructure following the pattern from foundry-samples/45-basic-agent-bing.
- Resource:
Microsoft.Bing/accounts@2020-06-10 - SKU: G1
- Kind: Bing.Grounding
- Purpose: Provides web search capabilities for the AI agent
- Resource:
Microsoft.CognitiveServices/accounts@2025-04-01-preview - Kind: AIServices
- Name:
aifoundry-{resourceToken} - Features:
- System-assigned managed identity
- Supports project management (
allowProjectManagement: true) - Model deployment:
gpt-4o-realtime-preview - Public network access enabled
- Resource:
Microsoft.CognitiveServices/accounts/projects@2025-04-01-preview - Name:
project-{resourceToken} - Parent: AI Foundry Account
- Features:
- System-assigned managed identity
- Project description and display name
- Resource:
Microsoft.CognitiveServices/accounts/connections@2025-04-01-preview - Category: ApiKey
- Auth Type: ApiKey (using Bing Search keys)
- Purpose: Connects AI Foundry account to Bing Search for grounding
- Role: Azure AI Developer (
64702f94-c441-49e6-a78b-ef80e0188fee) - Assignees:
- Backend Container App Managed Identity
- User Principal (for testing/development)
The following environment variables are now available in the backend Container App:
AZURE_AI_FOUNDRY_ENDPOINT=<AI Foundry Account Endpoint>
AZURE_AI_FOUNDRY_PROJECT_ID=<Project Resource ID>
AZURE_AI_FOUNDRY_BING_CONNECTION_ID=<Bing Connection Resource ID>✅ Bicep Compilation: Successfully validated with az bicep build
- No errors
- Expected warnings for Bing resource type (no schema available)
# src/backend/routes/mcp_agents.py
from fastapi import APIRouter, HTTPException
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
mcp_router = APIRouter()
class AIFoundryAgentService:
def __init__(self):
self.client = AIProjectClient(
credential=DefaultAzureCredential(),
endpoint=os.getenv("AZURE_AI_FOUNDRY_ENDPOINT")
)
self.agent_id = None
async def initialize(self):
# Create agent with Bing Search tool
agent = await self.client.agents.create_agent(
model=os.getenv("AZURE_OPENAI_GPT_REALTIME_DEPLOYMENT"),
name="Web Search Agent",
instructions="...",
tools=[{"type": "bing_grounding"}]
)
self.agent_id = agent.id
async def search_web(self, query: str) -> str:
# Create ephemeral thread
thread = await self.client.agents.create_thread()
try:
# Execute search via AI Foundry agent
# ... (implementation as previously described)
finally:
await self.client.agents.delete_thread(thread.id)# src/backend/main.py
from routes.mcp_agents import mcp_router
app.include_router(mcp_router, prefix="/api/mcp", tags=["mcp"])# src/backend/services/assistant_service.py
async def handle_tool_call(self, tool_name: str, parameters: dict, call_id: str):
if tool_name == "search_web_ai_foundry":
# Call local MCP endpoint
response = await self.http_client.post(
f"{self.backend_base_url}/api/mcp/ai-foundry",
json={
"jsonrpc": "2.0",
"id": call_id,
"method": "tools/call",
"params": {
"name": tool_name,
"arguments": parameters
}
}
)
# ... handle response
else:
# Existing tool handling
pass# Deploy infrastructure
azd provision
# Deploy backend with new route
azd deploy backend
# Test MCP endpoint
curl -X POST https://<backend-url>/api/mcp/ai-foundry \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'Browser WebSocket
↓
FastAPI Backend (Container App)
├─ /api/mcp/ai-foundry (NEW MCP endpoint)
│ ↓
│ AI Foundry Agent Service
│ ↓
│ Azure AI Foundry Project
│ ├─ AI Foundry Account
│ └─ Bing Search Connection
│ └─ Bing Grounding Service
│
└─ Existing routes (/api/*, /api/realtime/*)
- ✅ Minimal Infrastructure Changes: Added to existing backend, no new Container App needed
- ✅ Zero Additional Cost: Uses existing backend Container App
- ✅ Low Latency: In-process HTTP call (~1-5ms vs 20-50ms network hop)
- ✅ Simple Deployment: Single
azd updeploys everything - ✅ Easier Debugging: All logs in one place
- ✅ Gradual Migration: Can extract to separate service later if needed
infra/modules/ai/account.bicepinfra/modules/ai/project.bicepinfra/modules/ai/bing-connection.bicepinfra/modules/ai/rbac.bicep
infra/main.bicep(added AI Foundry resources, RBAC, env vars, outputs)
infra/modules/bing/grounding-bing-search.bicep(already present)
# Provision all infrastructure (including AI Foundry)
azd provision
# The following resources will be created:
# - Bing Search for Grounding
# - AI Foundry Account
# - AI Foundry Project
# - Bing Search Connection
# - RBAC role assignments