Skip to content

Use ProjectManagedIdentity for Foundry connections and fix AI Gateway MCP wire route - #33

Merged
Paul Yuknewicz (paulyuk) merged 1 commit into
mainfrom
copilot/bicep-managed-identity-and-wire-fixes
Nov 2, 2026
Merged

Paul Yuknewicz (paulyuk) merged 1 commit into
mainfrom
copilot/bicep-managed-identity-and-wire-fixes

Conversation

@paulyuk

Copy link
Copy Markdown
Member

Problem

  1. Unnecessary static secrets in Foundry connections and agent runtime:
    • appInsightsConnection (Microsoft.CognitiveServices/accounts/connections) authenticated to Application Insights using authType: 'ApiKey' and appInsights.properties.InstrumentationKey instead of ProjectManagedIdentity and ConnectionString.
    • aigw-github (Microsoft.CognitiveServices/accounts/projects/connections) authenticated from Foundry Toolbox to AI Gateway using authType: 'CustomKeys' (Api-Key) instead of ProjectManagedIdentity with AI Gateway User RBAC.
    • repo_digest_agent.py required AZURE_AI_GATEWAY_API_KEY even when running under an identity with AI Gateway User RBAC.
  2. Two live wire bugs in the Bicep aigw-github connection (#27):
    • aigw-github targeted <gatewayUrl>/default/mcp/github/mcp, which returns HTTP 404 Resource not found on the live AI Gateway data plane; the live route for toolServers/github is <gatewayUrl>/default/toolservers/github/mcp.
    • runtimeApiKey!.listSecrets().key fails when evaluated on live ARM (2025-09-01-preview) because listSecrets() returns {"primaryKey": "...", "secondaryKey": "..."} rather than key.

Solution

  • Foundry Application Insights connection (infra/foundry-agents/main.bicep):
    • Switch appInsightsConnection to authType: 'ProjectManagedIdentity' (@2025-06-01) with metadata.ApplicationInsightsConnectionString: appInsights.properties.ConnectionString.
    • Assign Monitoring Metrics Publisher (3913510d-42f4-4e42-8a64-420c390055eb) on Application Insights to the Foundry account and project system-assigned managed identities.
  • Foundry Toolbox -> AI Gateway MCP connection (infra/foundry-agents/github-connection.bicep, infra/ai-gateway/main.bicep, infra/main.bicep):
    • Fix the aigw-github target URL to ${normalizedGatewayUrl}/default/toolservers/github/mcp.
    • Default aigw-github (@2025-06-01) to authType: 'ProjectManagedIdentity' with audience: 'https://azure-api.net/aigateway'.
    • Assign AI Gateway User (caad7a21-134e-43f4-a550-c5ffc17e051c) on Microsoft.ApiManagement/aigateways to the Foundry project identity, Foundry account identity, and optional developer principalId.
    • Fix the CustomKeys fallback (useManagedIdentityForFoundryConnections: false) to read runtimeApiKey!.listSecrets().?primaryKey ?? runtimeApiKey!.listSecrets().?key ?? ''.
    • Support declarative toolServers/github header registration when @secure() param githubMcpAuthorization is supplied at deployment time.
  • Keyless agent runtime (repo_digest_agent.py):
    • Authenticate to AI Gateway with Entra ID (https://azure-api.net/aigateway/.default) via DefaultAzureCredential / ManagedIdentityCredential when AZURE_AI_GATEWAY_API_KEY is unset, while preserving Api-Key header support when AZURE_AI_GATEWAY_API_KEY is configured.

Canonical DX

Keyless Bicep connection (infra/foundry-agents/github-connection.bicep)

resource githubConnection 'Microsoft.CognitiveServices/accounts/projects/connections@2025-06-01' = {
  parent: aiProject
  name: connectionName
  properties: useManagedIdentity
    ? {
        authType: 'ProjectManagedIdentity'
        category: 'RemoteTool'
        target: '${normalizedGatewayUrl}/default/toolservers/github/mcp'
        isSharedToAll: true
        audience: 'https://azure-api.net/aigateway'
        metadata: {
          type: 'custom_MCP'
        }
      }
    : {
        authType: 'CustomKeys'
        category: 'RemoteTool'
        target: '${normalizedGatewayUrl}/default/toolservers/github/mcp'
        isSharedToAll: true
        credentials: {
          keys: {
            'Api-Key': gatewayApiKey
          }
        }
        metadata: {
          type: 'custom_MCP'
        }
      }
}

Keyless local or hosted agent execution

# Run without AZURE_AI_GATEWAY_API_KEY using Entra ID / Managed Identity (AI Gateway User role)
uv run python -c '
import asyncio, repo_digest_agent
async def main():
    agent = repo_digest_agent.build_agent()
    resp = await agent.run("Give me a concise digest of Azure-Samples/simple-foundry-hosted-agent-python-aigateway")
    print(resp.text)
asyncio.run(main())
'

Validation

  • Deployed infra/foundry-agents/main.bicep and infra/ai-gateway/main.bicep to live Azure (rg-aigw3-7vezhdb5-foundryagents and rg-aigateway-quickstart) and verified all created ARM resources and role assignments.
  • Verified end-to-end over the live wire with AZURE_AI_GATEWAY_API_KEY unset:
    1. Entra ID bearer token (https://azure-api.net/aigateway/.default) -> AI Gateway /default/models/openai/v1/chat/completions (gpt-latest -> gpt-6.1-sol-2026-09-29, HTTP 200).
    2. Entra ID bearer token -> AI Gateway /default/toolservers/github/mcp (tools/list, HTTP 200).
    3. Foundry Toolbox (/toolboxes/repo-digest-tools/versions/1/mcp?api-version=v1) -> aigw-github (ProjectManagedIdentity) -> AI Gateway (/default/toolservers/github/mcp) -> GitHub MCP (aigw-github___github_list_pull_requests, HTTP 200).
    4. Full repo_digest_agent.build_agent().run(...) executing parallel tool calls (list_pull_requests, list_issues, actions_list) through Foundry Toolbox and returning the live repository digest with zero API keys.
  • Passed bash tests/test-ai-gateway-model-registration.sh and all 18 unit tests (python -m unittest discover -s tests -p "test_*.py").

… MCP wire route

- Switch Foundry appInsightsConnection to ProjectManagedIdentity with Monitoring Metrics Publisher RBAC on Application Insights
- Switch Foundry aigw-github connection to ProjectManagedIdentity (audience https://azure-api.net/aigateway) with AI Gateway User RBAC (caad7a21-134e-43f4-a550-c5ffc17e051c) on AI Gateway
- Fix aigw-github target route from /default/mcp/github/mcp (HTTP 404) to /default/toolservers/github/mcp (HTTP 200)
- Fix runtimeApiKey.listSecrets() fallback to read primaryKey (live ARM response property) before key
- Add declarative toolServers/github support when githubMcpAuthorization is supplied
- Support keyless Entra ID bearer token auth (https://azure-api.net/aigateway/.default) in repo_digest_agent.py when AZURE_AI_GATEWAY_API_KEY is unset

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4448b1aa-1ea5-4c5f-af63-7ec98b7acb9e
@paulyuk
Paul Yuknewicz (paulyuk) merged commit 663d34f into main Nov 2, 2026
2 checks passed
@paulyuk
Paul Yuknewicz (paulyuk) deleted the copilot/bicep-managed-identity-and-wire-fixes branch November 2, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant