Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions IMPLEMENTATION_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ flowchart LR
github["GitHub MCP server"]

operator --> agent
agent -->|"model request and Api-Key"| gateway
agent -->|"model request (Entra ID or Api-Key)"| gateway
agent -->|"Entra identity"| toolbox
toolbox -->|"project connection and Api-Key"| gateway
toolbox -->|"project managed identity (AI Gateway User RBAC)"| gateway
gateway -->|"managed identity and Foundry User RBAC"| models
gateway -->|"repository-scoped read-only credential"| github
```

Model requests use
`<gateway>/default/models/openai/v1/chat/completions` or the corresponding
Responses route. Tool requests use
`<gateway>/default/mcp/github/mcp`.
`<gateway>/default/toolservers/github/mcp`.

The agent does not call a Foundry model endpoint directly. This keeps model
traffic behind AI Gateway policies. `FoundryChatClient` is intentionally not
Expand All @@ -73,11 +73,13 @@ Agent -> Foundry Toolbox -> AI Gateway -> GitHub MCP
```

`toolbox.yaml` declares one MCP source backed by the `aigw-github` project
connection. Bicep creates that `RemoteTool` / `CustomKeys` connection and stores
the generated AI Gateway `Api-Key` in it. The postprovision hook creates
`repo-digest-tools` through the Foundry data-plane API using that connection;
it no longer runs `azd ai connection create`. `FoundryToolbox` authenticates
the agent-to-Toolbox request with Entra identity.
connection. Bicep creates that `RemoteTool` / `ProjectManagedIdentity`
connection (`audience: https://azure-api.net/aigateway`) and assigns the
`AI Gateway User` (`caad7a21-134e-43f4-a550-c5ffc17e051c`) role on AI Gateway
to the Foundry project and account managed identities. The postprovision hook
creates `repo-digest-tools` through the Foundry data-plane API using that
connection; it no longer runs `azd ai connection create`. `FoundryToolbox`
authenticates the agent-to-Toolbox request with Entra identity.

The GitHub source sets `require_approval: "never"` because all three exposed
operations are read-only and the scheduled routine cannot pause for approval.
Expand Down Expand Up @@ -294,22 +296,26 @@ The predefined `workspaces/default` resource is the parent of every
workspace-scoped child. The postprovision hook waits for the workspace child
collection before configuring the GitHub ToolServer.

The gateway module receives the agents account and project names from the
Foundry module outputs, so it waits for project provisioning. Its nested
connection module targets the agents resource group and depends on the gateway
and `apiKeys/default`. The connection target is
`<gateway>/default/mcp/github/mcp`, with `isSharedToAll: true` and
`metadata.type: custom_MCP`. Stable resource names and incremental deployments
allow repeated `azd up` runs to update the same connection.

The preview key contract used by the deployment is
`runtimeApiKey.listSecrets().key` at `2025-09-01-preview`. Bicep compiles this
created-resource call to ARM `listSecrets(resourceId(...), apiVersion).key`
and adds the key resource dependency. The value crosses the module boundary
only through an `@secure()` parameter (`securestring` in ARM), never an output.
The deploying identity needs permission to list the gateway key and write
project connections in the agents resource group. Do not log resolved
deployment parameters or run the hooks with shell tracing/debug logging.
The gateway module receives the agents account and project names and managed
identity principal IDs from the Foundry module outputs, so it waits for project
provisioning. It assigns the `AI Gateway User`
(`caad7a21-134e-43f4-a550-c5ffc17e051c`) role on AI Gateway to the Foundry
project and account managed identities (and optional developer `principalId`),
and its nested connection module targets the agents resource group. The
connection target is `<gateway>/default/toolservers/github/mcp`, with
`authType: ProjectManagedIdentity` (`audience: https://azure-api.net/aigateway`),
`isSharedToAll: true`, and `metadata.type: custom_MCP`. Stable resource names
and incremental deployments allow repeated `azd up` runs to update the same
connection.

When `useManagedIdentityForFoundryConnections: false` is explicitly opted into,
the connection falls back to `CustomKeys` using
`runtimeApiKey.listSecrets().?primaryKey ?? runtimeApiKey.listSecrets().?key` at
`2025-09-01-preview` (the live ARM `listSecrets` response returns `primaryKey`
and `secondaryKey`). The value crosses the module boundary only through an
`@secure()` parameter (`securestring` in ARM), never an output. Do not log
resolved deployment parameters or run the hooks with shell tracing/debug
logging.

Both postprovision hooks still retrieve the key with `listSecrets` (preferring
`key`, with legacy response-field and `listValues` compatibility fallbacks) and
Expand Down
29 changes: 16 additions & 13 deletions infra/ai-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,25 @@ az deployment group create \
## Foundry GitHub remote-tool connection

When `foundryAgentsProjectName` is provided (as in the subscription-scoped
`../main.bicep` entry point), this module deploys
`../main.bicep` entry point), this module assigns `AI Gateway User`
(`caad7a21-134e-43f4-a550-c5ffc17e051c`) on AI Gateway to the Foundry project
and account managed identities and deploys
`../foundry-agents/github-connection.bicep` into the agents resource group.

The `aigw-github` project connection uses
`Microsoft.CognitiveServices/accounts/projects/connections@2025-04-01-preview`,
category `RemoteTool`, authentication `CustomKeys`, and target
`https://<gateway-host>/default/mcp/github/mcp`. It is shared with the project
and has `metadata.type: custom_MCP`.

The gateway's `2025-09-01-preview` key is read with
`runtimeApiKey.listSecrets().key` and passed only through the connection
module's `@secure()` `gatewayApiKey` parameter. ARM waits for key creation
before evaluating the nested deployment. No deployment output contains the
secret. The deployment identity needs key-listing permission in the gateway
resource group and connection-write permission in the agents resource group.
Stable names and incremental deployment keep repeat provisioning idempotent.
`Microsoft.CognitiveServices/accounts/projects/connections@2025-06-01`,
category `RemoteTool`, authentication `ProjectManagedIdentity`
(`audience: https://azure-api.net/aigateway`), and target
`https://<gateway-host>/default/toolservers/github/mcp`. It is shared with the
project and has `metadata.type: custom_MCP`.

If `useManagedIdentityForFoundryConnections` is set to `false`, the connection
falls back to `CustomKeys` using
`runtimeApiKey.listSecrets().?primaryKey ?? runtimeApiKey.listSecrets().?key`
(the live `2025-09-01-preview` `listSecrets` payload returns `primaryKey` and
`secondaryKey`) passed only through the connection module's `@secure()`
`gatewayApiKey` parameter. No deployment output contains the secret. Stable
names and incremental deployment keep repeat provisioning idempotent.

## AI Gateway monitoring

Expand Down
44 changes: 43 additions & 1 deletion infra/ai-gateway/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ param modelAliasesConfig array = []
param foundryAgentsResourceGroupName string = resourceGroup().name
param foundryAgentsAccountName string = ''
param foundryAgentsProjectName string = ''
param foundryAgentsAccountPrincipalId string = ''
param foundryAgentsProjectPrincipalId string = ''
param principalId string = ''
param useManagedIdentityForFoundryConnections bool = true
param aiGatewayAudience string = 'https://azure-api.net/aigateway'
@secure()
param toolServerConfigs object = {}

var aiGatewayApiVersion = '2025-09-01-preview'
var appInsightsApiVersion = '2020-02-02'
var logAnalyticsApiVersion = '2023-09-01'
var telemetryWorkspaceName = 'default'
var aiGatewayUserRoleDefinitionId = 'caad7a21-134e-43f4-a550-c5ffc17e051c'
var normalizedFoundryEndpoint = endsWith(foundryEndpoint, '/') ? foundryEndpoint : '${foundryEndpoint}/'
#disable-next-line BCP081
resource aiGateway 'Microsoft.ApiManagement/aigateways@2025-09-01-preview' = if (deployApimGatewayViaBicep) {
Expand Down Expand Up @@ -193,16 +199,52 @@ resource runtimeApiKey 'Microsoft.ApiManagement/aigateways/apiKeys@2025-09-01-pr
}
}

resource aiGatewayProjectUserRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployApimGatewayViaBicep && !empty(foundryAgentsProjectName) && !empty(foundryAgentsProjectPrincipalId)) {
scope: aiGateway
name: guid(aiGateway.id, aiGatewayUserRoleDefinitionId, foundryAgentsProjectName)
properties: {
principalId: foundryAgentsProjectPrincipalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', aiGatewayUserRoleDefinitionId)
}
}

resource aiGatewayAccountUserRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployApimGatewayViaBicep && !empty(foundryAgentsAccountName) && !empty(foundryAgentsAccountPrincipalId)) {
scope: aiGateway
name: guid(aiGateway.id, aiGatewayUserRoleDefinitionId, foundryAgentsAccountName)
properties: {
principalId: foundryAgentsAccountPrincipalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', aiGatewayUserRoleDefinitionId)
}
}

resource aiGatewayDeveloperUserRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployApimGatewayViaBicep && !empty(principalId)) {
scope: aiGateway
name: guid(aiGateway.id, aiGatewayUserRoleDefinitionId, principalId)
properties: {
principalId: principalId
principalType: 'User'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', aiGatewayUserRoleDefinitionId)
}
}

var runtimeHostname = deployApimGatewayViaBicep ? (aiGateway!.properties.?frontend.?defaultHostname ?? replace(aiGateway!.properties.?gatewayUrl ?? '', 'https://', '')) : ''

module githubConnection '../foundry-agents/github-connection.bicep' = if (deployApimGatewayViaBicep && !empty(foundryAgentsProjectName)) {
name: 'github-connection-${uniqueString(gatewayName, foundryAgentsProjectName)}'
scope: resourceGroup(foundryAgentsResourceGroupName)
dependsOn: [
aiGatewayProjectUserRoleAssignment
aiGatewayAccountUserRoleAssignment
]
params: {
aiServicesName: foundryAgentsAccountName
aiProjectName: foundryAgentsProjectName
gatewayUrl: 'https://${runtimeHostname}'
gatewayApiKey: runtimeApiKey!.listSecrets().key
useManagedIdentity: useManagedIdentityForFoundryConnections
gatewayAudience: aiGatewayAudience
gatewayApiKey: useManagedIdentityForFoundryConnections ? '' : (runtimeApiKey!.listSecrets().?primaryKey ?? runtimeApiKey!.listSecrets().?key ?? '')
}
}

Expand Down
20 changes: 16 additions & 4 deletions infra/foundry-agents/github-connection.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ targetScope = 'resourceGroup'
param aiServicesName string
param aiProjectName string
param gatewayUrl string
param useManagedIdentity bool = true
param gatewayAudience string = 'https://azure-api.net/aigateway'
@secure()
param gatewayApiKey string
param gatewayApiKey string = ''

var normalizedGatewayUrl = endsWith(gatewayUrl, '/') ? take(gatewayUrl, length(gatewayUrl) - 1) : gatewayUrl

Expand All @@ -17,13 +19,23 @@ resource aiProject 'Microsoft.CognitiveServices/accounts/projects@2025-04-01-pre
name: aiProjectName
}

resource githubConnection 'Microsoft.CognitiveServices/accounts/projects/connections@2025-04-01-preview' = {
resource githubConnection 'Microsoft.CognitiveServices/accounts/projects/connections@2025-06-01' = {
parent: aiProject
name: 'aigw-github'
properties: {
#disable-next-line BCP036
properties: useManagedIdentity ? {
category: 'RemoteTool'
authType: 'ProjectManagedIdentity'
audience: gatewayAudience
target: '${normalizedGatewayUrl}/default/toolservers/github/mcp'
isSharedToAll: true
metadata: {
type: 'custom_MCP'
}
} : {
category: 'RemoteTool'
authType: 'CustomKeys'
target: '${normalizedGatewayUrl}/default/mcp/github/mcp'
target: '${normalizedGatewayUrl}/default/toolservers/github/mcp'
credentials: {
keys: {
'Api-Key': gatewayApiKey
Expand Down
29 changes: 25 additions & 4 deletions infra/foundry-agents/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ resource appInsightsConnection 'Microsoft.CognitiveServices/accounts/connections
name: appInsights.name
properties: {
category: 'AppInsights'
authType: 'ApiKey'
#disable-next-line BCP036
authType: 'ProjectManagedIdentity'
isSharedToAll: false
target: appInsights.id
credentials: {
key: appInsights.properties.ConnectionString
}
metadata: {
ApiType: 'Azure'
ApplicationInsightsConnectionString: appInsights.properties.ConnectionString
ResourceId: appInsights.id
displayName: appInsights.name
}
Expand Down Expand Up @@ -175,6 +174,27 @@ var cognitiveServicesOpenAIUserRoleDefinitionId = '5e0bd9bd-7b93-4f28-af87-19fc3
var cognitiveServicesUserRoleDefinitionId = 'a97b65f3-24c7-4388-baec-2e87135dc908'
var storageBlobDataContributorRoleDefinitionId = 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
var acrPullRoleDefinitionId = '7f951dda-4ed3-4680-a7ca-43fe172d538d'
var monitoringMetricsPublisherRoleDefinitionId = '3913510d-42f4-4e42-8a64-420c390055eb'

resource appInsightsMetricsPublisherAssignmentAccount 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: appInsights
name: guid(appInsights.id, monitoringMetricsPublisherRoleDefinitionId, aiServices.id)
properties: {
principalId: aiServices.identity.principalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', monitoringMetricsPublisherRoleDefinitionId)
}
}

resource appInsightsMetricsPublisherAssignmentProject 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: appInsights
name: guid(appInsights.id, monitoringMetricsPublisherRoleDefinitionId, aiProject.id)
properties: {
principalId: aiProject.identity.principalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', monitoringMetricsPublisherRoleDefinitionId)
}
}

resource cognitiveServicesContributorAssignmentProject 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: aiServices
Expand Down Expand Up @@ -268,6 +288,7 @@ resource storageBlobDataContributorAssignmentUser 'Microsoft.Authorization/roleA

output aiServicesName string = aiServices.name
output aiServicesId string = aiServices.id
output aiServicesPrincipalId string = aiServices.identity.principalId
output aiServicesEndpoint string = aiServices.properties.endpoint
output aiProjectName string = aiProject.name
output aiProjectResourceId string = aiProject.id
Expand Down
54 changes: 54 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ param aiGatewayName string = ''
@description('Default GitHub repository for the daily digest automation.')
param githubRepository string = 'microsoft/agent-framework'

@secure()
@description('Optional GitHub MCP Authorization header value (for example, Bearer <token>) for declarative Bicep toolServer provisioning.')
param githubMcpAuthorization string = ''

@description('GitHub MCP server URL.')
param githubMcpServerUrl string = 'https://api.githubcopilot.com/mcp/'

@description('Comma-separated read-only GitHub MCP tools exposed by the gateway.')
param githubMcpTools string = 'list_pull_requests,list_issues,actions_list'

@description('Backing Foundry model name for the efficient mini deployment.')
param miniModelName string = 'gpt-5.4-mini'

Expand Down Expand Up @@ -259,6 +269,9 @@ module aiGateway './ai-gateway/main.bicep' = {
foundryAgentsResourceGroupName: foundryAgentsRg.name
foundryAgentsAccountName: foundryAgents.outputs.aiServicesName
foundryAgentsProjectName: foundryAgents.outputs.aiProjectName
foundryAgentsAccountPrincipalId: foundryAgents.outputs.aiServicesPrincipalId
foundryAgentsProjectPrincipalId: foundryAgents.outputs.aiProjectPrincipalId
principalId: principalId
tags: tags
enableMonitoring: enableMonitoring
deployApimGatewayViaBicep: true
Expand Down Expand Up @@ -307,6 +320,47 @@ module aiGateway './ai-gateway/main.bicep' = {
]
}
]
toolServerConfigs: empty(githubMcpAuthorization) ? {} : {
github: {
displayName: 'GitHub repository digest'
description: 'Read-only GitHub tools for repository digests.'
type: 'mcp'
failureMode: 'failClosed'
policies: [
{
type: 'contentSafety'
hateSeverity: 'Medium'
selfHarmSeverity: 'Medium'
sexualSeverity: 'Medium'
violenceSeverity: 'Medium'
}
]
endpoints: [
{
namespace: 'github'
kind: 'mcp'
mcp: {
url: githubMcpServerUrl
transport: 'streamableHttp'
}
credentials: {
type: 'header'
headers: {
Authorization: [
githubMcpAuthorization
]
'X-MCP-Readonly': [
'true'
]
'X-MCP-Tools': [
githubMcpTools
]
}
}
}
]
}
}
}
}

Expand Down
Loading
Loading