---
title: Build on your gateway with coding agents
summary: Install the AI Gateway plugin in GitHub Copilot CLI, Claude Code, Cursor, or Gemini CLI, let your coding agent discover the gateway's models and MCP tools and generate working client code, then migrate AI assets from Azure API Management.
category: agents
level: Intermediate
duration: 45
tracks: [code, cli, iac, terraform]
order: 17
illustration: diagrams/architecture.svg
tags: [plugin, skills, aigateway, GitHub Copilot CLI, Claude Code, Cursor, Gemini CLI, MCP, tool projection, OpenAPI, AI_GATEWAY_API_KEY, migration, API Management, migrate-apim, Bicep, Terraform]
objectives:
  - Install the aigateway plugin in your coding agent
  - Discover a gateway's model aliases and MCP tool servers from its Azure resource ID
  - Generate and run app code that calls the gateway with a runtime key
  - Review generated code for the right endpoint, alias, and secret handling
  - Migrate model APIs and both kinds of MCP server from Azure API Management, directly or as Bicep or Terraform
prereqs:
  - 02-first-model-call
---

# Build on your gateway with coding agents

Your platform team publishes models and MCP tools behind AI Gateway, and product developers want to use them
without reading gateway settings or passing keys around in chat. The AI Gateway plugin teaches coding agents
such as GitHub Copilot CLI, Claude Code, Cursor, and Gemini CLI what your gateway offers. It then generates
code that uses the right endpoint, alias, and auth header. Its developer commands only read from the gateway, so
developers build on top of your governance instead of around it. Administrators use the same plugin to create
and manage gateways, and to migrate the model APIs and MCP servers that they run on Azure API Management.

![AI Gateway architecture with coding agents among the clients that call one gateway](../assets/diagrams/architecture.svg "Coding agents call the same gateway as apps and agents. The code they generate goes through the gateway's access checks, policies, and telemetry like any other client.")

## Before you begin

<!-- setup: tools sign-in extension clone -->

- Complete [Publish a Foundry model and make your first call](../02-first-model-call/README.md). You need the
  gateway, a model that answers, and its alias.
- Optional: complete [Federate MCP tools behind one endpoint](../04-federate-mcp-tools/README.md) so the gateway
  also has MCP tools to discover.
- One coding agent, installed and signed in: GitHub Copilot CLI, Claude Code, Cursor, or Gemini CLI. Each agent
  documents its plugin support: [GitHub Copilot CLI](https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/plugins-finding-installing),
  [Claude Code](https://code.claude.com/docs/en/plugins), [Cursor](https://cursor.com/docs/plugins), and
  [Gemini CLI](https://geminicli.com/docs/extensions/).
- Read access to the gateway: the plugin uses your Azure sign-in to read it. Read access doesn't let you call the
  gateway's models or tools, so the app uses the key that you create below.
- Python 3.10 or later to run the generated app.
- For [Migrate from API Management](#migrate-from-api-management): the **Contributor** and **User Access
  Administrator** roles, or **Owner**, on a subscription where you can create API Management, Foundry, and AI
  Gateway resources, because the migrated gateway gets a role on the Foundry account. The Terraform track also
  needs [Terraform](https://developer.hashicorp.com/terraform/install) 1.9 or later, as in
  [Deploy a lab with Terraform](../SETUP.md#deploy-a-lab-with-terraform).

Set these variables in the shell that you'll start your coding agent from. Create a key for this app so you can
revoke it on its own later.

```bash title="Bash"
# Name your gateway and the alias that the generated app calls
export RG="<resource-group>"
export GATEWAY="<gateway>"
export AI_GATEWAY_MODEL="<model-name>"   # the alias from lab 02
echo "📝 Gateway $GATEWAY in $RG, alias $AI_GATEWAY_MODEL"

# Read the gateway's resource ID, which you pass to the plugin, and its runtime URL, which the app calls
export GATEWAY_ID="$(az aigateway show -g "$RG" -n "$GATEWAY" --query id -o tsv)"
export AI_GATEWAY_URL="https://$(az aigateway show -g "$RG" -n "$GATEWAY" --query properties.frontend.defaultHostname -o tsv)"
echo "🌐 Runtime URL: $AI_GATEWAY_URL"

# Create an API key for this app, so that you can revoke it on its own later
az aigateway api-key create -g "$RG" --gateway-name "$GATEWAY" -n app-coding-agent -o none &&
  echo "🔑 API key app-coding-agent is ready"

# Read the key into an environment variable, so that it never shows on screen
AI_GATEWAY_API_KEY="$(az aigateway api-key list-secrets -g "$RG" --gateway-name "$GATEWAY" -n app-coding-agent \
  --query primaryKey -o tsv)" && export AI_GATEWAY_API_KEY
echo "🔑 Key: in AI_GATEWAY_API_KEY (${#AI_GATEWAY_API_KEY} characters, not shown)"
```

```powershell title="PowerShell"
# Name your gateway and the alias that the generated app calls
$RG      = "<resource-group>"
$GATEWAY = "<gateway>"
$env:AI_GATEWAY_MODEL = "<model-name>"   # the alias from lab 02
Write-Host "📝 Gateway $GATEWAY in $RG, alias $env:AI_GATEWAY_MODEL"

# Read the gateway's resource ID, which you pass to the plugin, and its runtime URL, which the app calls
$GATEWAY_ID = az aigateway show -g $RG -n $GATEWAY --query id -o tsv
$env:AI_GATEWAY_URL = "https://$(az aigateway show -g $RG -n $GATEWAY --query properties.frontend.defaultHostname -o tsv)"
Write-Host "🌐 Runtime URL: $env:AI_GATEWAY_URL"

# Create an API key for this app, so that you can revoke it on its own later
az aigateway api-key create -g $RG --gateway-name $GATEWAY -n app-coding-agent -o none
if ($?) { Write-Host "🔑 API key app-coding-agent is ready" }

# Read the key into an environment variable, so that it never shows on screen
$env:AI_GATEWAY_API_KEY = az aigateway api-key list-secrets -g $RG --gateway-name $GATEWAY -n app-coding-agent `
  --query primaryKey -o tsv
Write-Host "🔑 Key: in AI_GATEWAY_API_KEY ($($env:AI_GATEWAY_API_KEY.Length) characters, not shown)"
```

Then print the gateway's Azure resource ID. You pass it to the plugin.

```bash title="Bash"
# Print the gateway's resource ID, to copy into the plugin's commands
echo "🆔 Gateway resource ID: $GATEWAY_ID"
```

```powershell title="PowerShell"
# Print the gateway's resource ID, to copy into the plugin's commands
Write-Host "🆔 Gateway resource ID: $GATEWAY_ID"
```

The ID looks like
`/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.ApiManagement/aigateways/<gateway>`.

## How it works

The `aigateway` plugin bundles commands and skills for two personas. Developers use `discover` and `build` to use
a gateway's models and tools in their apps. Administrators use `create` and `manage` to run the gateway itself.
The skills hold the know-how: which endpoints to call, which model name the gateway accepts, and how to send the
credential. The commands run a skill for one task. This lab uses the developer commands, then `migrate`.

Discovery goes through Azure Resource Manager, which is why the plugin needs the resource ID and your Azure
sign-in. Your app then calls the gateway's runtime URL with a runtime key or a Microsoft Entra token, like any
other client. The developer commands are read-only against the gateway: they never create, update, or delete
gateways, models, tools, or keys. The administrator commands change the gateway through the `az aigateway` CLI,
and ask you to confirm before they delete anything, change access, or read a secret.

| Piece | Invoked as | What it does |
|---|---|---|
| Skill `use-ai-gateway` | `/aigateway:use-ai-gateway` | The full discover, select, connect, and verify workflow. `discover` and `build` use it. |
| Command `discover` | `/aigateway:discover <resource ID>` | Lists the gateway's model aliases and MCP tool servers, with each endpoint, operation, and auth requirement, and key metadata when you ask. Stops there. |
| Command `build` | `/aigateway:build <resource ID> <description>` | Discovers what's missing, integrates the assets into your project with a credential you already have, then verifies one request. |
| Commands `create` and `manage` | `/aigateway:create`, `/aigateway:manage` | Create a gateway, then configure and operate its models, tools, policies, and keys with the skill `manage-ai-gateway`. Both take the gateway name and resource group, then a region or the operation. |
| Command `migrate` | `/aigateway:migrate <apim-name> <resource-group> [target-gateway]` | Moves model APIs, MCP servers, and A2A agents from an API Management instance to AI Gateway in four steps: inventory, plan, adapt the plan with you, and provision it phase by phase. It runs the `migrate-apim` tool that the plugin bundles, with the skill `migrate-apim`. |
| Command `feedback` | `/aigateway:feedback` | Anonymizes a session summary into a local file that you can share as product feedback. |

What the generated code relies on:

| Item | Value |
|---|---|
| Model base URL (OpenAI-compatible) | `<gateway URL>/default/models/openai/v1` |
| Model operation | One that the alias publishes: Chat Completions, Responses, Anthropic Messages, or embeddings |
| MCP endpoint | `<gateway URL>/default/toolservers/<server-name>/mcp` |
| Auth header | `Authorization: Bearer <credential>`, a runtime key or a Microsoft Entra token, for both models and MCP tools |
| Credential | A Microsoft Entra token or a key that you already have. A key comes from the `AI_GATEWAY_API_KEY` environment variable |
| Model name | The alias as the gateway publishes it, for example `gpt-5-mini` |
| Project | Your existing project, in its language and with its dependencies. A standalone agent only if you ask for one |

## Install the plugin

<!-- steps -->

### Create a project folder

Start in an empty folder with Git, so you can check later that no secret gets committed.

```bash title="Bash"
# Create the project folder, move into it, and start a Git repository quietly
mkdir ticket-summarizer && cd ticket-summarizer && git init -q &&
  echo "📂 Project folder with Git: $(pwd)"
```

```powershell title="PowerShell"
# Create the project folder, move into it, and start a Git repository quietly
New-Item -ItemType Directory ticket-summarizer | Out-Null; Set-Location ticket-summarizer; git init -q
if ($?) { Write-Host "📂 Project folder with Git: $(Get-Location)" }
```

### Add the plugin to your agent

The plugin lives in the [Azure/aigateway](https://github.com/Azure/aigateway) repository and follows each
agent's plugin format. In GitHub Copilot CLI and Claude Code, start a session in the folder (`copilot` or
`claude`) and run the slash commands. In Gemini CLI, run the command in your shell, then start `gemini` in the
folder. In Cursor, use the dashboard, then open the folder.

```text title="GitHub Copilot CLI"
/plugin marketplace add Azure/aigateway
/plugin install aigateway@azure
```

```text title="Claude Code"
/plugin marketplace add Azure/aigateway
/plugin install aigateway@azure
```

```text title="Cursor"
In the Cursor dashboard, open Plugins and select Add Marketplace.
Import https://github.com/Azure/aigateway.
Install aigateway from Customize.
```

```bash title="Gemini CLI"
# Install the plugin as a Gemini CLI extension, straight from the GitHub repository
gemini extensions install https://github.com/Azure/aigateway &&
  echo "📦 The aigateway extension is installed in Gemini CLI"
```

The marketplace is named `azure` and the plugin `aigateway`. GitHub Copilot CLI reports
`Plugin "aigateway" installed successfully. Installed 4 skills.` Earlier versions of the plugin were named
`ai-gateway`. If a guide still shows `/plugin install ai-gateway@azure`, use `aigateway@azure` instead.

### Check that the commands are available

In your agent, run the discover command without a resource ID:

```text
/aigateway:discover
```

The plugin lists the gateways that your Azure sign-in can read in the current subscription and asks which one to
use. It only reads. That's how you know it's loaded. Answer with your gateway's name or the ID you printed, or
continue with the next step.

<!-- /steps -->

> **Checkpoint**
> `/aigateway:discover` is recognized and asks which gateway to use.

## Discover what the gateway offers

<!-- steps -->

### Run discover with your resource ID

Paste the gateway ID that you printed in [Before you begin](#before-you-begin). Discover lists a tool server's
tools only with a credential that you already have, so tell it to use the key you created for this app:

```text
/aigateway:discover /subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.ApiManagement/aigateways/<gateway> For the MCP handshake, use the key in the AI_GATEWAY_API_KEY environment variable.
```

When your agent asks to run `az`, `curl`, or `python3` commands, approve the ones that read: `az aigateway` and
`az rest` `GET` requests to `management.azure.com`, and MCP calls to your gateway URL, which the plugin may send
from a short Python script. Deny any `PUT`, `PATCH`, or `DELETE`. Discover never needs them.

### Read the summary

The plugin reads the gateway's runtime URL and lists each model alias with the operations it publishes, such as
Chat Completions at `/default/models/openai/v1/chat/completions` and Responses at
`/default/models/openai/v1/responses`. It lists the MCP tool servers with their endpoints. With your key, it runs
an MCP handshake against each tool server and lists its tools, such as `learn_microsoft_docs_search`, without
calling them. Ask follow-up questions in plain language, for example:

```text
Which model fits short summaries? Do any tool servers search Microsoft Learn? Which API keys does the gateway have?
```

For the last question, it lists the gateway's API keys from their metadata, without their values.

Discover doesn't write code, read key secrets, call models or tools, or change the gateway. If you don't name a
credential, it still lists the aliases and tool servers, but not the tools. Stop here, or move on to `build`.

<!-- /steps -->

> **Checkpoint**
> The summary lists the alias you published in lab 02 and, if you did lab 04, the `learn` tool server and its
> tools.

## Build an app with the gateway

<!-- steps -->

### Describe what to build

Run `build` with the same resource ID and a short description. `build` uses a credential that you already have, so
name the environment variable that holds the key you created for this app. Otherwise it asks which credential to
use:

```text
/aigateway:build /subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.ApiManagement/aigateways/<gateway> a Python command-line script that summarizes a support ticket from a text file. Use the <model-name> model and read the key from the AI_GATEWAY_API_KEY environment variable.
```

Other descriptions to try:

| You want | Describe it as |
|---|---|
| A model call in an app you already have | `add a /summarize endpoint to my FastAPI app that calls the gateway model` |
| Tools in your own MCP client | `connect my app to the learn MCP tools` |
| A ready-to-run agent | `a standalone agent that answers Azure questions with the learn tools` |

### Answer the plugin's questions

`build` reuses what you gave it, discovers only what's missing, and asks what you're building, if your
description didn't say. It works in the project that's in the folder and follows its language, framework, and
dependencies:

- **Call a model**: code for an operation that the alias publishes, such as Chat Completions or Responses.
- **Connect MCP tools**: it lists the server's tools first, then configures the ones you choose by their exact
  names in the MCP client that your project uses. It asks before it calls a tool.
- **Build a standalone agent**: only when you ask for one, as in the last example above.

`build` sends the credential as an `Authorization: Bearer` header and keeps it where it is, such as in
`AI_GATEWAY_API_KEY`. It never reads, creates, or rotates keys, so it writes no key to a file: an `.env.example`
holds only an empty placeholder. Then it verifies the code with one request. If that request costs tokens and
your description didn't approve it, `build` asks you first. If you decline, it tells you that the request wasn't
verified.

### Review the generated code

Read the code before you run it again or commit it. Check that:

- The base URL is your gateway URL followed by `/default/models/openai/v1`, or `/default/toolservers/<name>/mcp`
  for tools. Without `/default/`, calls return `404`.
- The `model` value is the alias that discover listed, so you can retarget models later without a code change,
  as explained in [Publish models from any provider behind stable aliases](../03-multi-provider-aliases/README.md).
- The key comes from `AI_GATEWAY_API_KEY` and goes in the `Authorization: Bearer` header. No key value appears in code,
  `README.md`, or config files, and `.env.example` holds only an empty placeholder.
- If the project has a `.env` file, `.gitignore` lists it.
- The app calls only the gateway's runtime URL, never `management.azure.com`.
- For MCP tools: the configuration lists the selected tools by name instead of `"*"`, and tool calls wait for your
  decision instead of approving all.

### Run the app

`build` reported how it checked the code and the command that runs it. Run the app yourself from the shell where
you set `AI_GATEWAY_API_KEY`, using that command or the generated `README.md`. If the project has a
`requirements.txt`, install its packages in a virtual environment first:

```bash title="Bash"
# Create a virtual environment in .venv and activate it, so that the packages stay out of your global Python
python3 -m venv .venv && source .venv/bin/activate &&
  echo "📦 Virtual environment .venv is active"

# Install the packages that the generated app lists in requirements.txt
pip install -r requirements.txt &&
  echo "📦 The packages in requirements.txt are installed"
```

```powershell title="PowerShell"
# Create a virtual environment in .venv and activate it, so that the packages stay out of your global Python
python -m venv .venv; .\.venv\Scripts\Activate.ps1
if ($?) { Write-Host "📦 Virtual environment .venv is active" }

# Install the packages that the generated app lists in requirements.txt
pip install -r requirements.txt
if ($?) { Write-Host "📦 The packages in requirements.txt are installed" }
```

Then start the entry point, for example `python summarize_ticket.py ticket.txt`. A script that uses only the
Python standard library has no `requirements.txt`, so run it directly.

<!-- /steps -->

> **Checkpoint**
> The generated app prints a model response that came through your gateway.

## Migrate from API Management

If you already serve models and MCP servers through Azure API Management, `/aigateway:migrate` moves them to AI
Gateway. It runs the `migrate-apim` tool that the plugin bundles, in four steps. Each step writes a file in the
folder that you work in, so you can review it, stop, and pick up where you left off.

| Step | What the agent runs | What it writes |
|---|---|---|
| Inventory | `migrate-apim collect`, `discover-tools`, and `summary` read the API Management instance: facts only, no verdicts | `inventory.json` |
| Plan | `migrate-apim plan` matches each asset against what the installed `az aigateway` can create | `plan.json` |
| Adapt | You settle each item that doesn't map one to one, and the agent records your decisions in the plan | `plan.json` |
| Provision | `migrate-apim apply` builds the gateway one phase at a time, or the agent writes Bicep or Terraform from the plan | `journal.json`, or a template |

The migration is additive: it builds a new gateway and changes nothing in API Management. Callers keep using API
Management until you move them, and retiring anything there stays your decision.

<!-- steps -->

### Deploy a sample API Management instance

Skip this step if you migrate your own instance. Set `MIG_RG` to its resource group, `APIM` to its name, and
`FOUNDRY_ID` to the resource ID of the Foundry account that it calls.

Otherwise, deploy a sample that serves AI the way many API Management instances do: an Azure OpenAI API in front of
a new Foundry account with `gpt-5-mini`, a token limit for each subscription, a header that names the calling app,
a product with one subscription for a ticket app, and both kinds of MCP server that API Management serves:

- `learn-mcp`, a **passthrough** that proxies to the Microsoft Learn MCP server, which already speaks MCP.
- `orders-mcp`, a **tool projection**: nothing upstream speaks MCP, so API Management synthesizes one tool from
  each operation of `orders-rest`, a plain REST API that the sample also deploys and never calls.

They migrate by two different mappings, which is why the sample has both. The plan works out which one each server
takes. It takes about 10 minutes. API Management BasicV2 bills by the hour until you delete it.

```bash title="Bash"
# Name the resource group, the region, and the sample's resources, with a random suffix
export MIG_RG="rg-aigw-lab17mig"
export MIG_LOCATION="swedencentral"
export SUFFIX="$(openssl rand -hex 3)"
export APIM="apim-contoso-$SUFFIX"
export FOUNDRY="aigwsrc$SUFFIX"
echo "📝 API Management $APIM and Foundry account $FOUNDRY, in $MIG_RG"

# Create the resource group
az group create -n "$MIG_RG" -l "$MIG_LOCATION" -o none && echo "📂 Resource group $MIG_RG is ready"

# Deploy API Management, the Foundry account, the model API, and the two MCP servers. It takes about 10 minutes
az deployment group create -g "$MIG_RG" -n apim-source \
  -f ~/aigateway/labs/17-coding-agent-plugin/infra/apim-source.bicep \
  -p apimName="$APIM" foundryName="$FOUNDRY" -o none &&
  echo "🚀 API Management $APIM is deployed"

# Read the Foundry account's resource ID, which the migrated gateway calls
export FOUNDRY_ID="$(az cognitiveservices account show -g "$MIG_RG" -n "$FOUNDRY" --query id -o tsv)"
echo "🆔 Foundry account: $FOUNDRY_ID"
```

```powershell title="PowerShell"
# Name the resource group, the region, and the sample's resources, with a random suffix
$MIG_RG       = "rg-aigw-lab17mig"
$MIG_LOCATION = "swedencentral"
$SUFFIX       = [guid]::NewGuid().ToString("N").Substring(0, 6)
$APIM         = "apim-contoso-$SUFFIX"
$FOUNDRY      = "aigwsrc$SUFFIX"
Write-Host "📝 API Management $APIM and Foundry account $FOUNDRY, in $MIG_RG"

# Create the resource group
az group create -n $MIG_RG -l $MIG_LOCATION -o none
if ($?) { Write-Host "📂 Resource group $MIG_RG is ready" }

# Deploy API Management, the Foundry account, the model API, and the two MCP servers. It takes about 10 minutes
az deployment group create -g $MIG_RG -n apim-source `
  -f ~/aigateway/labs/17-coding-agent-plugin/infra/apim-source.bicep `
  -p apimName=$APIM foundryName=$FOUNDRY -o none
if ($?) { Write-Host "🚀 API Management $APIM is deployed" }

# Read the Foundry account's resource ID, which the migrated gateway calls
$FOUNDRY_ID = az cognitiveservices account show -g $MIG_RG -n $FOUNDRY --query id -o tsv
Write-Host "🆔 Foundry account: $FOUNDRY_ID"
```

To use another region, pick one where API Management BasicV2, the `gpt-5-mini` model, and AI Gateway are all
available, as in [Find the available regions](../SETUP.md#find-the-available-regions). On GitHub, open
[infra/apim-source.bicep](infra/apim-source.bicep) to see the policies that the migration has to carry over.

### Start the migration in your agent

Create a folder for the migration files, then start your coding agent in it.

```bash title="Bash"
# Create the folder that holds inventory.json, plan.json, and journal.json, and move into it
mkdir -p ~/apim-migration && cd ~/apim-migration && echo "📂 Migration folder: $(pwd)"
```

```powershell title="PowerShell"
# Create the folder that holds inventory.json, plan.json, and journal.json, and move into it
New-Item -ItemType Directory -Force ~/apim-migration | Out-Null
Set-Location ~/apim-migration
if ($?) { Write-Host "📂 Migration folder: $(Get-Location)" }
```

Ask your agent for the first two steps only. Replace `<apim-name>` and `<resource-group>` with the values of
`APIM` and `MIG_RG`.

```text
/aigateway:migrate <apim-name> <resource-group>
Work in the current folder. Do only the inventory and the review: collect the inventory, show the summary,
discover the MCP tools, and propose the plan. Then present every item that needs a decision, with its options
and your recommendation, and list the items that are ready to migrate and the prerequisites. Don't record
decisions and don't run apply yet.
```

The agent says which copy of the tool it runs, usually the plugin's own copy, and asks you before it calls an MCP
server. It writes `inventory.json` and `plan.json` in the folder, then presents the items to decide.

The next steps show each command that the agent runs and what it prints, so you can check its work or run the
steps yourself. To run them, define a shortcut to the same tool in your clone of the repo. It's a Python package
with no dependencies.

```bash title="Bash"
# Run the migrate-apim tool in place from your clone of the repo, as the plugin runs its own copy
migrate-apim() { PYTHONPATH=~/aigateway/tools/migrate-apim python3 -m migrate_apim.cli "$@"; }
migrate-apim --version && echo "🔧 migrate-apim is ready"
```

```powershell title="PowerShell"
# Run the migrate-apim tool in place from your clone of the repo, as the plugin runs its own copy
function migrate-apim { $env:PYTHONPATH = "$HOME/aigateway/tools/migrate-apim"; python -m migrate_apim.cli @args }
migrate-apim --version
if ($LASTEXITCODE -eq 0) { Write-Host "🔧 migrate-apim is ready" }
```

### Inventory the AI assets

`collect` reads the instance through Azure Resource Manager and writes `inventory.json`: the backends, the APIs and
their operations, the policies, the products and subscriptions, and the Foundry deployments behind them. It records
how to retrieve each secret, never its value. `--operation-policies` also reads each operation's policy, at one
read per operation.

```bash title="Bash"
# Inventory the AI assets of the API Management instance, with every operation's policy
migrate-apim collect --apim "$APIM" -g "$MIG_RG" --operation-policies -o inventory.json
```

```powershell title="PowerShell"
# Inventory the AI assets of the API Management instance, with every operation's policy
migrate-apim collect --apim $APIM -g $MIG_RG --operation-policies -o inventory.json
```

It prints a line for each read, a summary, and `Wrote inventory.json`.

The inventory can't know which tools a passthrough MCP server exposes until something asks the server. Asking calls
the server, maybe with a subscription key, so `discover-tools` calls nothing until you authorize the server. A
projected server takes no call at all: its tools are API Management operations, so `collect` already holds the
authoritative list, and only `learn-mcp` shows up below.

```bash title="Bash"
# See which servers need your authorization: nothing is called yet
migrate-apim discover-tools --inventory inventory.json

# Authorize the call to learn-mcp, and record its tools in inventory.json
migrate-apim discover-tools --inventory inventory.json --allow-secret-use mcp:learn-mcp
```

```powershell title="PowerShell"
# See which servers need your authorization: nothing is called yet
migrate-apim discover-tools --inventory inventory.json

# Authorize the call to learn-mcp, and record its tools in inventory.json
migrate-apim discover-tools --inventory inventory.json --allow-secret-use mcp:learn-mcp
```

The first run calls nothing:

```text
  needsAuthorization mcp:learn-mcp via none
                     Listing tools calls the server; not authorized.

Nothing was called. Listing tools calls the server and may use a subscription key, so it needs explicit authorization:
  --allow-secret-use <assetId>   (or 'all')
```

The second run lists the tools through the API Management gateway, which is what callers see:

```text
  discovered         mcp:learn-mcp via gateway
                     microsoft_docs_search, microsoft_code_sample_search, microsoft_docs_fetch
                     3 tools

Updated inventory.json (1 server(s) discovered)
```

Print the summary of the inventory:

```bash title="Bash"
# Print a readable summary of inventory.json
migrate-apim summary inventory.json
```

```powershell title="PowerShell"
# Print a readable summary of inventory.json
migrate-apim summary inventory.json
```

```text
APIM AI inventory: apim-contoso-<suffix>
  /subscriptions/<subscription-id>/resourceGroups/rg-aigw-lab17mig/providers/Microsoft.ApiManagement/service/apim-contoso-<suffix>
  sku BasicV2, region Sweden Central, vnet None, identity SystemAssigned
  collected <time> in 36 ARM reads

Assets
  1 model providers
      modelProvider:foundry-ai-endpoint [foundry] endpoint=https://aigwsrc<suffix>.cognitiveservices.azure.com/openai auth=managedIdentity models=1 refs=0 apis/1 policies
  1 model APIs
      azure-openai [inferenceProxy, confidence=high] provider=foundry auth=managedIdentity models=1
  2 MCP servers
      orders-mcp [toolProjection, confidence=high] tools=2 upstream=1
      learn-mcp [passthrough, confidence=high] tools=3 upstream=1
  0 A2A agents
  4 APIs on the service, 3 inventoried, 1 not
      notAnAiAssetButReferencedByOne: 1

Secrets
  3 references, 3 retrievable automatically, 0 needing manual re-provisioning
      apimSubscriptionKey: 2
      azureManagedIdentity: 1

Policy surface
  2 documents, 9 elements, 2 distinct policy expressions
  top families: composition (4), routing (2), other (1), tokenLimit (1), transform (1)
```

The inventory found the four assets to migrate: the Foundry backend as a model provider, the Azure OpenAI API as
a model API that signs in to Foundry with API Management's managed identity, and the two MCP servers, told apart by
what they carry. `learn-mcp` has a backend that speaks MCP, so it reads as a `passthrough`. `orders-mcp` has no
`backendId` and no `serviceUrl`, only tools that name the operations they project, so it reads as a
`toolProjection`. The line that counts the APIs says one of the four wasn't inventoried: `orders-rest` is no AI
asset of its own, but the inventory keeps it as `notAnAiAssetButReferencedByOne`, because `orders-mcp` projects
from it. The inventory also found the secrets that callers use, here two subscription keys, and the policies that
the plan must translate.

### Review the migration strategy for each asset

`plan` matches every asset against what the installed `az aigateway` can create, and writes `plan.json`. Each
asset becomes one or more items: an item is **ready** when it maps one to one, and **needs a decision** when it
doesn't.

```bash title="Bash"
# Propose a migration plan from the inventory
migrate-apim plan --inventory inventory.json -o plan.json
```

```powershell title="PowerShell"
# Propose a migration plan from the inventory
migrate-apim plan --inventory inventory.json -o plan.json
```

```text
note: this plan says nothing about where it deploys, and `apply` refuses without it.
      Re-run with --target /subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.ApiManagement/aigateways/<gateway>
Migration plan: apim-contoso-<suffix>
  11 items against catalog 1.0.0b31 (declared, installed CLI not probed)
  7 needs-decision, 4 ready

Needs a decision (7, 3 blocking)
  choose: 6
  supply: 1
    ! [choose] model:azure-openai/publishedSurface -> proposal=split of split/inference-only/retain-in-apim/block
      [supply] model:azure-openai/aliasNames -> proposal=use-model-names of use-model-names/override
      [choose] mcp:orders-mcp/registration -> proposal=accept of accept/retain-in-apim/block
    ! [choose] policy/set-header -> proposal=accept of accept/compensate/retain-in-apim/block
      [choose] policy/llm-token-limit -> proposal=identity of identity/ipAddress/identity+ipAddress/pooled
    ! [choose] access/subscriptions -> proposal=entra-rbac of gateway-key-per-consumer/entra-rbac/retain-in-apim
      [choose] telemetry/destination -> proposal=gateway-metrics-only of new-appinsights/gateway-metrics-only/retain-in-apim

Ready to execute (4)
    identity.assign: identity [gatewayIdentity]
    provider.create: modelProvider:foundry-ai-endpoint [registration]
    model.create: model:azure-openai [model:gpt-5-mini]
    mcp.create: mcp:learn-mcp [registration]

Manual prerequisites (4)
  [azure-administrator] Grant the gateway identity data-plane access on each Foundry account
  [azure-administrator] Create an Application Insights resource with the OTLP endpoint enabled
  [azure-administrator] Assign callers AI Gateway Models User / Tools User at the right scope
  [migrate-apim] Preflight the target name against Microsoft.ApiManagement/service

Wrote plan.json (33,728 bytes)
```

The two MCP servers land on opposite sides of that list, because they map to different things:

| In API Management | On AI Gateway | In the plan |
|---|---|---|
| A **passthrough**, proxying to an upstream that already speaks MCP (`learn-mcp`) | A federated tool server with one `kind: mcp` backend, pointed at the same upstream | **Ready**, verdict `native`: it maps one to one |
| A **tool projection**, whose tools API Management synthesizes from a REST API's operations (`orders-mcp`) | A tool server with a `kind: openApi` source: the REST API's OpenAPI contract, exported from API Management and sent inline | **Needs a decision**, verdict `partial` |
| A projection whose source API has no OpenAPI contract to export, because it isn't `http`-typed or isn't in the inventory | Nothing: with no contract, there's no source to register | The item says so in its own words, and preflight blocks the run rather than building a thinner server |

The projection needs a decision because API Management's per-tool policies and tool-name overrides have no
equivalent on the gateway, and the plan never carries a loss silently. On this sample the loss it names is empty:
the inventory records `hasToolPolicy: false` for both tools, and the names that API Management gives them turn out
to be the ones the gateway derives from the contract anyway, as **Verify** shows. That's why `accept` is
reasonable *here*. On your own instance, read the item's `evidence` first: a server with per-tool policies, or
with tool names that don't follow from its contract, really does lose them.

A `!` marks a blocking item: nothing gets built until you settle it. Each item in `plan.json` has a `rationale`,
its `options` with their consequences, and a `proposal`. Your agent presents them with its recommendation.
`summary` reads a plan as well as an inventory: `migrate-apim summary plan.json --unsettled` prints every open
item in full, and `migrate-apim summary plan.json --item publishedSurface` prints one, named the way the short
list above prints it. This lab settles them as follows:

| Item | Why it needs a decision | Options | This lab's choice |
|---|---|---|---|
| `model:azure-openai/publishedSurface` | 1 of the 3 operations moves: image generation has no route on the gateway, and no model behind the API serves embeddings | `split`, `inference-only`, `retain-in-apim`, `block` | `split`: chat completions moves, image generation and embeddings stay on API Management |
| `model:azure-openai/aliasNames` | Apps send the alias as `model`, and API Management has none | `use-model-names`, `override` | `use-model-names`: apps keep sending `gpt-5-mini` |
| `mcp:orders-mcp/registration` | The projected tools migrate as the `orders-rest` contract, an `openApi` source, but per-tool policies and tool-name overrides don't come with it | `accept`, `retain-in-apim`, `block` | `accept`: both tools are `hasToolPolicy: false`, so neither loses anything here |
| `policy/set-header` | The gateway doesn't rewrite requests, so the `x-caller-app` header is lost | `accept`, `compensate`, `retain-in-apim`, `block` | `accept`: the sample's backend doesn't read the header |
| `policy/llm-token-limit` | API Management counts tokens by subscription, and the gateway by identity, IP address, both, or one pool. `estimate-prompt-tokens` and `remaining-tokens-header-name` have no equivalent | `identity`, `ipAddress`, `identity+ipAddress`, `pooled` | `identity`: each key or Entra identity gets 5,000 tokens a minute |
| `access/subscriptions` | Gateway keys are gateway-wide, and no key value can be imported, so every caller gets a new credential | `gateway-key-per-consumer`, `entra-rbac`, `retain-in-apim` | `gateway-key-per-consumer`: a key named after the `ticket-app` subscription |
| `telemetry/destination` | Something reports telemetry, but nothing names an Application Insights resource: the `azureMonitor` logger reports through diagnostic settings, so there's no component to enable an OTLP endpoint on | `new-appinsights`, `gateway-metrics-only`, `retain-in-apim` | `gateway-metrics-only`: the gateway's built-in metrics, with nothing to set up |

Your agent may recommend otherwise, and for production it should. When this lab was tested, the agent recommended
`entra-rbac`, so that callers sign in instead of holding keys, `compensate` or `retain-in-apim` for the header,
and `new-appinsights`, which is the only option that gets gateway telemetry into Application Insights at all. The
lab picks keys and built-in metrics to stay self-contained:
[Go keyless with Microsoft Entra ID and RBAC](../11-entra-rbac/README.md) and
[Attribute tokens and cost with OpenTelemetry](../14-otel-chargeback/README.md) cover the other options.

Tell your agent your decisions, and it records them. It sets only each item's `status` to `adapted` and its
`decision` to the option you chose. This snippet records the same decisions for the sample:

```bash title="Bash"
# Record a decision for each item that needs one, keyed by the aspect it covers, or by its asset and aspect
python3 - <<'EOF'
import json
decisions = {
    "publishedSurface": "split",
    "aliasNames": "use-model-names",
    "mcp:orders-mcp/registration": "accept",
    "llm-token-limit": "identity",
    "set-header": "accept",
    "subscriptions": "gateway-key-per-consumer",
    "destination": "gateway-metrics-only",
}
with open("plan.json", encoding="utf-8") as f:
    plan = json.load(f)
for item in plan["items"]:
    source = item.get("source", {})
    choice = decisions.get(f"{source.get('assetId')}/{source.get('aspect')}") or decisions.get(source.get("aspect"))
    if item.get("status") == "needs-decision" and choice:
        item["status"], item["decision"] = "adapted", choice
        print(f"📝 {item['id']}: {choice}")
with open("plan.json", "w", encoding="utf-8") as f:
    json.dump(plan, f, indent=2)
EOF
```

```powershell title="PowerShell"
# Record a decision for each item that needs one, keyed by the aspect it covers, or by its asset and aspect
@'
import json
decisions = {
    "publishedSurface": "split",
    "aliasNames": "use-model-names",
    "mcp:orders-mcp/registration": "accept",
    "llm-token-limit": "identity",
    "set-header": "accept",
    "subscriptions": "gateway-key-per-consumer",
    "destination": "gateway-metrics-only",
}
with open("plan.json", encoding="utf-8") as f:
    plan = json.load(f)
for item in plan["items"]:
    source = item.get("source", {})
    choice = decisions.get(f"{source.get('assetId')}/{source.get('aspect')}") or decisions.get(source.get("aspect"))
    if item.get("status") == "needs-decision" and choice:
        item["status"], item["decision"] = "adapted", choice
        print(f"📝 {item['id']}: {choice}")
with open("plan.json", "w", encoding="utf-8") as f:
    json.dump(plan, f, indent=2)
'@ | python -
```

It prints the seven items that it settled:

```text
📝 item:model/azure-openai/surface: split
📝 item:model/azure-openai/aliasNames: use-model-names
📝 item:mcp/orders-mcp/registration: accept
📝 item:policy/da79943af481: accept
📝 item:policy/ea1ffb3ae788: identity
📝 item:access/subscriptions: gateway-key-per-consumer
📝 item:telemetry/destination: gateway-metrics-only
```

The two policy items are named after a hash of the policy, so their IDs differ in your plan. The MCP decision is
keyed by its asset as well as its aspect, because every server's registration item has the aspect `registration`.

### Identify the assets that are ready to migrate

Run `plan` again on the same file. It keeps your decisions, turns them into steps, and recomputes what's ready.

```bash title="Bash"
# Regenerate the plan in place, with your decisions
migrate-apim plan --inventory inventory.json -o plan.json
```

```powershell title="PowerShell"
# Regenerate the plan in place, with your decisions
migrate-apim plan --inventory inventory.json -o plan.json
```

```text
note: this plan says nothing about where it deploys, and `apply` refuses without it.
      Re-run with --target /subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.ApiManagement/aigateways/<gateway>
Migration plan: apim-contoso-<suffix>
  11 items against catalog 1.0.0b31 (declared, installed CLI not probed)
  7 adapted, 4 ready

Needs a decision (0, 0 blocking)

Ready to execute (4)
    identity.assign: identity [gatewayIdentity]
    provider.create: modelProvider:foundry-ai-endpoint [registration]
    model.create: model:azure-openai [model:gpt-5-mini]
    mcp.create: mcp:learn-mcp [registration]

Manual prerequisites (3)
  [azure-administrator] Grant the gateway identity data-plane access on each Foundry account
  [azure-administrator] Assign callers AI Gateway Models User / Tools User at the right scope
  [migrate-apim] Preflight the target name against Microsoft.ApiManagement/service

Wrote plan.json (34,714 bytes)
```

Nothing needs a decision anymore, so every asset is ready to migrate:

- **Ready** items map one to one: the gateway's identity, the Foundry provider, the `gpt-5-mini` model, and the
  `learn-mcp` MCP server.
- **Adapted** items, which `plan.json` holds but the summary doesn't list, carry your decisions: the chat
  completions route under the alias `gpt-5-mini`, the `orders-mcp` tool server with the `orders-rest` contract as
  its source, the token limit for each identity, and the `ticket-app` key. The header, the image generation
  operation, and the embeddings operation stay behind, as decided.
- **Manual prerequisites** are what happens outside the gateway: two for an administrator, one for the tool
  itself. The telemetry prerequisite is gone, because no exporter needs it. The gateway's identity needs a role on
  the Foundry account, and the callers need a role on the gateway if they sign in with Entra ID.

<!-- /steps -->

> **Checkpoint**
> `plan.json` shows `7 adapted, 4 ready` and `Needs a decision (0, 0 blocking)`.

## Build the migrated gateway

Choose how to build the gateway that the plan describes. **Migrate directly** runs `migrate-apim apply`, which
creates it with `az aigateway`, one phase at a time, and keeps a journal so that you can resume. **Bicep** and
**Terraform** turn the plan into infrastructure as code that you review, commit, and deploy with your pipelines.

<!-- track: cli | Migrate directly -->

### Point the plan at a new gateway

`apply` refuses a plan that doesn't say where it deploys. Record the new gateway's resource ID in the plan. Later
runs of `plan` keep it, so a resumed session doesn't have to repeat it.

```bash title="Bash"
# Name the new gateway, and build its resource ID in the migration's resource group
export MIG_GATEWAY="aigw-contoso-$SUFFIX"
export TARGET_ID="/subscriptions/$(az account show --query id -o tsv)/resourceGroups/$MIG_RG/providers/Microsoft.ApiManagement/aigateways/$MIG_GATEWAY"
echo "🆔 Target gateway: $TARGET_ID"

# Record the target in the plan, which adds the step that creates the gateway
migrate-apim plan --inventory inventory.json -o plan.json --target "$TARGET_ID"
```

```powershell title="PowerShell"
# Name the new gateway, and build its resource ID in the migration's resource group
$MIG_GATEWAY = "aigw-contoso-$SUFFIX"
$TARGET_ID   = "/subscriptions/$(az account show --query id -o tsv)/resourceGroups/$MIG_RG/providers/Microsoft.ApiManagement/aigateways/$MIG_GATEWAY"
Write-Host "🆔 Target gateway: $TARGET_ID"

# Record the target in the plan, which adds the step that creates the gateway
migrate-apim plan --inventory inventory.json -o plan.json --target $TARGET_ID
```

```text
🆔 Target gateway: /subscriptions/<subscription-id>/resourceGroups/rg-aigw-lab17mig/providers/Microsoft.ApiManagement/aigateways/aigw-contoso-<suffix>
Migration plan: apim-contoso-<suffix>
  12 items against catalog 1.0.0b31 (declared, installed CLI not probed)
  7 adapted, 5 ready

Needs a decision (0, 0 blocking)

Ready to execute (5)
    gateway.create: gateway [create]
    identity.assign: identity [gatewayIdentity]
    provider.create: modelProvider:foundry-ai-endpoint [registration]
    model.create: model:azure-openai [model:gpt-5-mini]
    mcp.create: mcp:learn-mcp [registration]

Manual prerequisites (3)
  [azure-administrator] Grant the gateway identity data-plane access on each Foundry account
  [azure-administrator] Assign callers AI Gateway Models User / Tools User at the right scope
  [migrate-apim] Preflight the target name against Microsoft.ApiManagement/service

Wrote plan.json (36,448 bytes)
```

The plan now has 12 items: `gateway.create` comes first.

### Preflight the migration

The preflight checks the plan and the target, lists the prerequisites, and prints the phases, without writing
anything. Define a second shortcut for the phases too.

```bash title="Bash"
# Run one phase of the plan: a rehearsal that prints the commands, or a write with --execute
migrate-phase() { migrate-apim apply --plan plan.json --inventory inventory.json --phase "$@"; }

# Check the plan, the target, and the prerequisites, and list the phases
migrate-apim apply --plan plan.json --inventory inventory.json --preflight
```

```powershell title="PowerShell"
# Run one phase of the plan: a rehearsal that prints the commands, or a write with --execute
function migrate-phase { migrate-apim apply --plan plan.json --inventory inventory.json --phase @args }

# Check the plan, the target, and the prerequisites, and list the phases
migrate-apim apply --plan plan.json --inventory inventory.json --preflight
```

```text
Preflight:
  [ note] capabilitiesNotProbed: The plan was built against catalog 1.0.0b31 as declared; the installed `az aigateway` was not asked what it exposes. A command this catalog declares and your extension lacks will fail when its step runs.
          Re-run plan with --probe-cli to reconcile the two before writing anything.
  [ note] prerequisites: 3 step(s) outside this tool, none acknowledged yet:
      outstanding   [azure-administrator] prereq:foundry-rbac: Grant the gateway identity data-plane access on each Foundry account
      outstanding   [azure-administrator] prereq:caller-rbac: Assign callers AI Gateway Models User / Tools User at the right scope
      outstanding   [migrate-apim] prereq:name-collision: Preflight the target name against Microsoft.ApiManagement/service -- checked here: no Microsoft.ApiManagement/service has that name
          Acknowledge each with --acknowledge <id> once it is done, on its own or alongside any other apply flag. Acknowledgement is recorded and does not gate writing: a gateway builds cleanly without any of them while failing to serve traffic. Where the grant is readable it is read as well -- see foundryRoleMissing -- and a line that disagrees with its acknowledgement is marked CONTRADICTED.

Phases to run:
  gateway      pending    (1 steps)
  identity     pending    (1 steps)
  providers    pending    (1 steps)
  models       pending    (1 steps)
  aliases      pending    (1 steps)
  toolServers  pending    (2 steps)
  policies     pending    (1 steps)
  keys         pending    (1 steps)

Put these in your task list now, one entry per phase, and tick them from what a run
prints rather than from memory -- this checklist is rebuilt from the journal every
time, and a list kept alongside it drifts.
```

`apply` runs a phase as a rehearsal unless you add `--execute`, and refuses to execute a phase that you haven't
rehearsed. Your agent rehearses each phase, shows you the commands, and asks before it executes, one phase at a
time. Every run ends by naming the phase that comes next.

### Create the gateway and its identity

```bash title="Bash"
# Rehearse the gateway phase: it prints the az command that it would run
migrate-phase gateway

# Create the gateway. It takes about two minutes
migrate-phase gateway --execute

# Rehearse, then give the gateway a system-assigned identity
migrate-phase identity && migrate-phase identity --execute
```

```powershell title="PowerShell"
# Rehearse the gateway phase: it prints the az command that it would run
migrate-phase gateway

# Create the gateway. It takes about two minutes
migrate-phase gateway --execute

# Rehearse, then give the gateway a system-assigned identity
migrate-phase identity
if ($LASTEXITCODE -eq 0) { migrate-phase identity --execute }
```

Each run first prints the preflight note and the phases, with `>` on the phase that runs. Then a rehearsal prints
the command, and an execution reads the result back. This excerpt shows the rest of the output:

```text
Phase gateway -- rehearsal (pass --execute to write)
  planned          item:gateway/create
                   aigw-contoso-<suffix>: dry run; not executed
                   $ az aigateway create --subscription <subscription-id> -g rg-aigw-lab17mig -n aigw-contoso-<suffix> --location 'Sweden Central' -o json

Rehearsed gateway. Nothing was written. Run the same phase with --execute to apply it.

Phase gateway -- EXECUTING
  verified         item:gateway/create
                   aigw-contoso-<suffix>: created and read back

Phase gateway complete. Next: identity.

Phase identity -- EXECUTING
  verified         item:identity/assign
                   created and read back

Phase identity complete. Next: providers.
```

The gateway is created in the region of API Management.

### Grant the gateway access to the Foundry account

The provider that the next phase creates authenticates with the gateway's identity, and role assignments live
outside the gateway's resource provider, so no phase grants one: that's what `prereq:foundry-rbac` is for, and the
principal it needs exists only now that the `identity` phase has run. Grant the role, then acknowledge the
prerequisite. `--acknowledge` records on its own, alongside any other `apply` flag or none at all, and prints what
it recorded and which prerequisites are still outstanding. Your agent never acknowledges one for you.

```bash title="Bash"
# Read the principal ID of the identity that the identity phase assigned
PRINCIPAL_ID="$(az aigateway show -g "$MIG_RG" -n "$MIG_GATEWAY" --query identity.principalId -o tsv)"
echo "👤 Gateway identity: $PRINCIPAL_ID"

# Give it the Foundry User role on the Foundry account, the command that prereq:foundry-rbac carries
az role assignment create --assignee-object-id "$PRINCIPAL_ID" --assignee-principal-type ServicePrincipal \
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "$FOUNDRY_ID" -o none &&
  echo "🔐 The gateway's identity has the Foundry User role on the Foundry account"

# Record in journal.json that the role is granted
migrate-apim apply --plan plan.json --inventory inventory.json --acknowledge prereq:foundry-rbac
```

```powershell title="PowerShell"
# Read the principal ID of the identity that the identity phase assigned
$PRINCIPAL_ID = az aigateway show -g $MIG_RG -n $MIG_GATEWAY --query identity.principalId -o tsv
Write-Host "👤 Gateway identity: $PRINCIPAL_ID"

# Give it the Foundry User role on the Foundry account, the command that prereq:foundry-rbac carries
az role assignment create --assignee-object-id $PRINCIPAL_ID --assignee-principal-type ServicePrincipal `
  --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope $FOUNDRY_ID -o none
if ($?) { Write-Host "🔐 The gateway's identity has the Foundry User role on the Foundry account" }

# Record in journal.json that the role is granted
migrate-apim apply --plan plan.json --inventory inventory.json --acknowledge prereq:foundry-rbac
```

```text
👤 Gateway identity: <principal-id>
🔐 The gateway's identity has the Foundry User role on the Foundry account

Acknowledged prereq:foundry-rbac: Grant the gateway identity data-plane access on each Foundry account
  2 prerequisite(s) still outstanding: prereq:caller-rbac, prereq:name-collision
```

Skip this step and nothing stops you: the phases still read back as verified and the gateway reads as configured,
but no call through it reaches the account.

### Publish the model

Three phases publish the model: `providers` registers the Foundry account as a model provider, `models` registers
the deployment that the model API served, and `aliases` publishes the name that apps call. Rehearse each one, then
execute it.

```bash title="Bash"
# Rehearse, then create the Foundry provider
migrate-phase providers && migrate-phase providers --execute

# Rehearse, then register the deployment that the model API served
migrate-phase models && migrate-phase models --execute

# Rehearse, then publish the alias that apps call
migrate-phase aliases && migrate-phase aliases --execute
```

```powershell title="PowerShell"
# Rehearse, then create the Foundry provider
migrate-phase providers
if ($LASTEXITCODE -eq 0) { migrate-phase providers --execute }

# Rehearse, then register the deployment that the model API served
migrate-phase models
if ($LASTEXITCODE -eq 0) { migrate-phase models --execute }

# Rehearse, then publish the alias that apps call
migrate-phase aliases
if ($LASTEXITCODE -eq 0) { migrate-phase aliases --execute }
```

A rehearsal prints the command as it will run, quoted so that you can paste it, and the file it passes to
`--foundry`. Read the payload before you execute: the provider calls the Foundry account's root endpoint, not the
backend URL that API Management called, which ends in `/openai`, and it authenticates with the gateway's identity.
An excerpt of the output:

```text
Phase providers -- rehearsal (pass --execute to write)
  planned          item:modelProvider/modelProvider-foundry-ai-endpoint
                   foundry-ai-endpoint: dry run; not executed
                   $ az aigateway model-provider create --subscription <subscription-id> -g rg-aigw-lab17mig --gateway-name aigw-contoso-<suffix> -n foundry-ai-endpoint --display-name foundry-ai-endpoint --kind Foundry --foundry '@<tempfile>' -o json
                     --foundry @<tempfile> is:
                       {
                         "endpoint": "https://aigwsrc<suffix>.cognitiveservices.azure.com/",
                         "resourceIds": [
                           "/subscriptions/<subscription-id>/resourceGroups/rg-aigw-lab17mig/providers/Microsoft.CognitiveServices/accounts/aigwsrc<suffix>"
                         ],
                         "authentication": {
                           "kind": "ManagedIdentity",
                           "managedIdentity": {
                             "resource": "https://cognitiveservices.azure.com/"
                           }
                         }
                       }

Rehearsed providers. Nothing was written. Run the same phase with --execute to apply it.

Phase providers -- EXECUTING
  verified         item:modelProvider/modelProvider-foundry-ai-endpoint
                   foundry-ai-endpoint: created and read back

Phase providers complete. Next: models.

Phase models -- EXECUTING
  verified         item:model/azure-openai/model/gpt-5-mini
                   gpt-5-mini: created and read back; apiFormat is not returned by a read and was not compared

Phase models complete. Next: aliases.

Phase aliases -- EXECUTING
  verified         item:model/azure-openai/aliasNames
                   gpt-5-mini: created and read back

Phase aliases complete. Next: toolServers.
```

Each execution reads the object back and says what it couldn't compare: a read doesn't return `apiFormat`, so the
`models` phase reports that it didn't check that field.

### Register the two MCP servers

One phase, two steps: the passthrough and the projection, each by its own mapping.

```bash title="Bash"
# Rehearse, then create both MCP servers
migrate-phase toolServers && migrate-phase toolServers --execute

# Read back the projected server: the contract it holds is not part of the answer
az aigateway mcp show -g "$MIG_RG" --gateway-name "$MIG_GATEWAY" -n orders-mcp --query properties.endpoints
```

```powershell title="PowerShell"
# Rehearse, then create both MCP servers
migrate-phase toolServers
if ($LASTEXITCODE -eq 0) { migrate-phase toolServers --execute }

# Read back the projected server: the contract it holds is not part of the answer
az aigateway mcp show -g $MIG_RG --gateway-name $MIG_GATEWAY -n orders-mcp --query properties.endpoints
```

```text
Phase toolServers -- EXECUTING
  verified         item:mcp/orders-mcp/registration
                   orders-mcp: created and read back; its OpenAPI contract is write-only and was not read back
  verified         item:mcp/learn-mcp/registration
                   learn-mcp: created and read back

Phase toolServers complete. Next: policies.
```

`learn-mcp` passes through to the same Microsoft Learn server that API Management did, as a `kind: mcp` endpoint.
For `orders-mcp` the step first asks API Management to export the OpenAPI contract of `orders-rest`, then sends
that document inline as a `kind: openApi` source. The read-back shows the endpoint that holds it:

```json
[
  {
    "id": "fb07ce1fd8e74090bc3a374f495105dd",
    "kind": "openApi",
    "mode": "failClosed",
    "namespace": "orders-mcp",
    "openApi": {
      "specSource": {
        "type": "inline"
      }
    }
  }
]
```

The document itself isn't in the answer: the gateway stores it write-only. So the step says what it couldn't
compare instead of implying that it checked the contract, and no read-back can tell you that the right document
arrived. The tool list can, and **Verify** lists the tools of both servers through the gateway.

### Carry over the token limit and the keys

```bash title="Bash"
# Rehearse, then set the token limit on the model
migrate-phase policies && migrate-phase policies --execute

# Rehearse, then create a key for each API Management subscription
migrate-phase keys && migrate-phase keys --execute
```

```powershell title="PowerShell"
# Rehearse, then set the token limit on the model
migrate-phase policies
if ($LASTEXITCODE -eq 0) { migrate-phase policies --execute }

# Rehearse, then create a key for each API Management subscription
migrate-phase keys
if ($LASTEXITCODE -eq 0) { migrate-phase keys --execute }
```

An excerpt of the output:

```text
Phase policies -- EXECUTING
  verified         item:policy/ea1ffb3ae788
                   gpt-5-mini: created and read back; the source also set estimate-prompt-tokens=false, remaining-tokens-header-name=x-remaining-tokens, which the target has no equivalent for

Phase policies complete. Next: keys.

Phase keys -- EXECUTING
  verified         item:access/subscriptions
                   2/2 objects: master: already present with the desired state; ticket-app: created and read back

Phase keys complete. No phases remain.
```

The token limit is 5,000 tokens a minute for each identity, as decided. API Management's built-in all-access
subscription maps to the `master` key that every gateway has, and the `ticket-app` subscription gets its own key.

### Check the migration status

Every phase has run. Print where the migration stands, and read the values that **Verify** uses.

```bash title="Bash"
# Print the state of each phase, from journal.json
migrate-apim apply --plan plan.json --inventory inventory.json --status

# Read the gateway's resource ID and runtime URL, which Verify uses
export MIG_GATEWAY_ID="$TARGET_ID"
export MIG_URL="https://$(az aigateway show -g "$MIG_RG" -n "$MIG_GATEWAY" --query properties.frontend.defaultHostname -o tsv)"
echo "🌐 Migrated gateway: $MIG_URL"
```

```powershell title="PowerShell"
# Print the state of each phase, from journal.json
migrate-apim apply --plan plan.json --inventory inventory.json --status

# Read the gateway's resource ID and runtime URL, which Verify uses
$MIG_GATEWAY_ID = $TARGET_ID
$MIG_URL = "https://$(az aigateway show -g $MIG_RG -n $MIG_GATEWAY --query properties.frontend.defaultHostname -o tsv)"
Write-Host "🌐 Migrated gateway: $MIG_URL"
```

The `--status` run prints the preflight note, then:

```text
Migration run
  target: /subscriptions/<subscription-id>/resourceGroups/rg-aigw-lab17mig/providers/Microsoft.ApiManagement/aigateways/aigw-contoso-<suffix>
  steps: 9 in the plan, 9 verified
    gateway      complete   (1 steps)
    identity     complete   (1 steps)
    providers    complete   (1 steps)
    models       complete   (1 steps)
    aliases      complete   (1 steps)
    toolServers  complete   (2 steps)
    policies     complete   (1 steps)
    keys         complete   (1 steps)
  acknowledged: prereq:foundry-rbac

2 prerequisite(s) outside this tool were never acknowledged:
  [azure-administrator] prereq:caller-rbac: Assign callers AI Gateway Models User / Tools User at the right scope
  [migrate-apim] prereq:name-collision: Preflight the target name against Microsoft.ApiManagement/service
Until they are done the gateway reads as configured and does not serve traffic.

These objects exist. Whether the gateway serves traffic is a separate question:
every check above is a management read-back, not a call through the gateway.
Open it and see what the run provisioned:
  https://ai.gateway.azure.com/subscriptions/<subscription-id>/resourceGroups/rg-aigw-lab17mig/providers/Microsoft.ApiManagement/aigateways/aigw-contoso-<suffix>/overview
🌐 Migrated gateway: https://aigw-conto-<hash>.swedencentral-01.ai.gateway.azure.com
```

Every phase is complete, every step read back, and `acknowledged` lists the Foundry role. The other two
prerequisites stay outstanding, and neither applies to the sample: its callers use a gateway key rather than an
Entra role, and the name check runs in every preflight. It stops the run if an API Management instance in the
resource group has the gateway's name. **Verify** makes the call that no read-back can stand in for.

<!-- track: iac | Bicep -->

### Ask your agent for Bicep

In the migration folder, ask your agent to turn the plan into a template:

```text
From plan.json and inventory.json in this folder, write migrated-gateway.bicep: a new AI Gateway with the
2026-11-01 API version that creates every item that the plan marks ready or adapted, as decided, and grants the
gateway's identity the Foundry User role on the Foundry account. Take the gateway name, the region, and the
Foundry account ID as parameters. Don't deploy it.
```

When this lab was tested, the agent's template passed `az bicep build` and `az deployment group validate`. It
differed in details from the reference template below, so review it the same way: every ready and adapted item is
there, and nothing that you decided to leave behind.

### Review the reference template

This is the template that the settled plan describes for the sample. It creates the gateway and its identity, the
Foundry User role, the provider on the account's root endpoint, the model with its token limit, the alias, the
`learn-mcp` tool server, and the `ticket-app` key.

It stops at the passthrough server. `orders-mcp` has no upstream URL to write down: its source is the OpenAPI
contract that API Management exports, which the gateway takes inline, so a template can only carry it as a literal
document or a parameter. The **Migrate directly** track exports it and sends it for you. If your agent's template
declares `orders-mcp`, check where its contract comes from before you deploy it.

<!-- include: infra/migrated-gateway.bicep -->

### Deploy the template

Deploy the reference template, or your agent's template with its own parameters, to the migration's resource
group. It takes about two minutes.

```bash title="Bash"
# Deploy the migrated gateway
az deployment group create -g "$MIG_RG" -n migrated-gateway \
  -f ~/aigateway/labs/17-coding-agent-plugin/infra/migrated-gateway.bicep \
  -p gatewayName="aigw-contoso-$SUFFIX" foundryAccountId="$FOUNDRY_ID" -o none &&
  echo "🚀 Gateway aigw-contoso-$SUFFIX is deployed"

# Read the gateway's resource ID and runtime URL, which Verify uses
export MIG_GATEWAY_ID="$(az deployment group show -g "$MIG_RG" -n migrated-gateway --query properties.outputs.gatewayId.value -o tsv)"
export MIG_URL="$(az deployment group show -g "$MIG_RG" -n migrated-gateway --query properties.outputs.gatewayUrl.value -o tsv)"
echo "🌐 Migrated gateway: $MIG_URL"
```

```powershell title="PowerShell"
# Deploy the migrated gateway
az deployment group create -g $MIG_RG -n migrated-gateway `
  -f ~/aigateway/labs/17-coding-agent-plugin/infra/migrated-gateway.bicep `
  -p gatewayName="aigw-contoso-$SUFFIX" foundryAccountId=$FOUNDRY_ID -o none
if ($?) { Write-Host "🚀 Gateway aigw-contoso-$SUFFIX is deployed" }

# Read the gateway's resource ID and runtime URL, which Verify uses
$MIG_GATEWAY_ID = az deployment group show -g $MIG_RG -n migrated-gateway --query properties.outputs.gatewayId.value -o tsv
$MIG_URL        = az deployment group show -g $MIG_RG -n migrated-gateway --query properties.outputs.gatewayUrl.value -o tsv
Write-Host "🌐 Migrated gateway: $MIG_URL"
```

<!-- track: terraform | Terraform -->

### Ask your agent for Terraform

In the migration folder, ask your agent to turn the plan into a configuration:

```text
From plan.json and inventory.json in this folder, write a Terraform configuration in a terraform folder: a new
AI Gateway with azapi resources at the 2026-11-01 API version that creates every item that the plan marks ready
or adapted, as decided, and grants the gateway's identity the Foundry User role on the Foundry account. Take the
region and the Foundry account ID as variables. Don't apply it.
```

When this lab was tested, the agent's configuration passed `terraform validate`. It differed in details from the
reference configuration below, so review it the same way: every ready and adapted item is there, and nothing that
you decided to leave behind.

### Review the configuration

The reference configuration creates the resource group `rg-aigw-lab17-tf` with [lab 01's gateway
module](../01-provision-gateway/terraform/gateway), then what the settled plan maps. Like the Bicep template, it
covers the passthrough server only: `orders-mcp` takes its OpenAPI contract inline, so a configuration has to
carry that document itself. The **Migrate directly** track exports it from API Management and sends it.

<!-- include: terraform/main.tf -->

<!-- include: terraform/migrated-gateway.tf -->

### Deploy with terraform apply

Move into the lab's Terraform folder, pass the Foundry account and the source's region, then plan and apply. It
takes about three minutes.

```bash title="Bash"
# Move into the lab's Terraform folder
cd ~/aigateway/labs/17-coding-agent-plugin/terraform &&
  echo "📂 Terraform folder: $(pwd)"

# Deploy to the Azure CLI's current subscription, in the source's region, with the source's Foundry account
export ARM_SUBSCRIPTION_ID="$(az account show --query id --output tsv)"
export TF_VAR_location="$MIG_LOCATION"
export TF_VAR_foundry_account_id="$FOUNDRY_ID"
echo "📍 Subscription $ARM_SUBSCRIPTION_ID, region $TF_VAR_location"

# Download the providers, then plan and apply the gateway, the role, the provider, the model, the alias, the MCP
# server, and the key
terraform init &&
  terraform plan -out tfplan &&
  terraform apply tfplan &&
  echo "🚀 Gateway $(terraform output -raw ai_gateway_name) is deployed"
```

```powershell title="PowerShell"
# Move into the lab's Terraform folder
Set-Location ~/aigateway/labs/17-coding-agent-plugin/terraform
if ($?) { Write-Host "📂 Terraform folder: $(Get-Location)" }

# Deploy to the Azure CLI's current subscription, in the source's region, with the source's Foundry account
$env:ARM_SUBSCRIPTION_ID       = az account show --query id --output tsv
$env:TF_VAR_location           = $MIG_LOCATION
$env:TF_VAR_foundry_account_id = $FOUNDRY_ID
Write-Host "📍 Subscription $env:ARM_SUBSCRIPTION_ID, region $env:TF_VAR_location"

# Download the providers, then plan and apply the gateway, the role, the provider, the model, the alias, the MCP
# server, and the key
terraform init
if ($?) { terraform plan -out tfplan }
if ($?) { terraform apply tfplan }
if ($?) { Write-Host "🚀 Gateway $(terraform output -raw ai_gateway_name) is deployed" }
```

### Read the outputs

```bash title="Bash"
# Read the gateway's resource ID and runtime URL, which Verify uses
export MIG_GATEWAY_ID="$(terraform output -raw ai_gateway_id)"
export MIG_URL="$(terraform output -raw ai_gateway_url)"
echo "🌐 Migrated gateway: $MIG_URL"

# Move back to the migration folder
cd ~/apim-migration && echo "📂 Migration folder: $(pwd)"
```

```powershell title="PowerShell"
# Read the gateway's resource ID and runtime URL, which Verify uses
$MIG_GATEWAY_ID = terraform output -raw ai_gateway_id
$MIG_URL        = terraform output -raw ai_gateway_url
Write-Host "🌐 Migrated gateway: $MIG_URL"

# Move back to the migration folder
Set-Location ~/apim-migration
if ($?) { Write-Host "📂 Migration folder: $(Get-Location)" }
```

`terraform apply` ends with `Apply complete! Resources: 8 added, 0 changed, 0 destroyed.`

<!-- /tracks -->

> **Checkpoint**
> The new gateway has the model `gpt-5-mini` with its alias, the tool server `learn-mcp`, and the key
> `ticket-app`. On the **Migrate directly** track it also has `orders-mcp`. API Management is unchanged and still
> serves the same calls.

Before you move callers from API Management to the gateway, tell their owners what changes:

- **The endpoint.** Model calls move from `https://<apim-name>.azure-api.net/openai/deployments/gpt-5-mini/...` to
  `<gateway-url>/default/models/openai/v1/...`, with `"model": "gpt-5-mini"` in the body. MCP clients move to
  `<gateway-url>/default/toolservers/<server>/mcp`.
- **The tool names.** The gateway publishes a federated tool as `<namespace>_<tool>`, so `get_order` becomes
  `orders-mcp_get_order` and `microsoft_docs_search` becomes `learn-mcp_microsoft_docs_search`. Every client that
  names a tool has to use the new name. The base names survive, including the ones the gateway rebuilds from the
  contract of a projected server.
- **The credential.** No subscription key carries over: each caller gets its gateway key, or an Entra role.
- **What stays behind.** Image generation stays on API Management. The token limit counts for each identity
  instead of each subscription, the `x-remaining-tokens` header is gone, and so is `x-caller-app`.

## Verify

<!-- steps -->

### Confirm the key and model outside the generated code

If the app fails, this call tells you whether the problem is in the gateway setup or in the code.

```bash title="Bash"
# Call the model through the gateway with your key and alias, and print only the HTTP status code
echo "🤖 HTTP status of a model call through the gateway:"
curl -sS -o /dev/null -w "HTTP %{http_code}\n" "${AI_GATEWAY_URL%/}/default/models/openai/v1/chat/completions" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
  -d "{\"model\":\"$AI_GATEWAY_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}"
```

```powershell title="PowerShell"
# Call the model through the gateway with your key and alias, and print only the HTTP status code
Write-Host "🤖 HTTP status of a model call through the gateway:"
$body = @{ model = $env:AI_GATEWAY_MODEL; messages = @(@{ role = "user"; content = "ping" }) } | ConvertTo-Json -Depth 5
(Invoke-WebRequest -Method Post -Uri "$($env:AI_GATEWAY_URL.TrimEnd('/'))/default/models/openai/v1/chat/completions" `
  -Headers @{ Authorization = "Bearer $env:AI_GATEWAY_API_KEY" } -ContentType "application/json" -Body $body -SkipHttpErrorCheck).StatusCode
```

Expect `HTTP 200` (`200` in PowerShell).

### Check that no secret can be committed

Run this in the project folder. It checks that Git ignores `.env`, if the project has one, and that your key
appears in no other file.

```bash title="Bash"
# If the project has a .env file, which holds a key, check that Git ignores it
if [ ! -e .env ]; then echo "✅ No .env file"
elif git check-ignore -q .env; then echo "✅ .env is ignored"
else echo "🚨 .env is not ignored"; fi

# Search every other file for your key, skipping .git, .venv, and node_modules, and list any file that holds it
if grep -rIlF --exclude=.env --exclude-dir=.git --exclude-dir=.venv --exclude-dir=node_modules -- "$AI_GATEWAY_API_KEY" .; then
  echo "🚨 The key appears in the files above"
else
  echo "✅ No key in project files"
fi
```

```powershell title="PowerShell"
# If the project has a .env file, which holds a key, check that Git ignores it
if (-not (Test-Path .env)) { Write-Host "✅ No .env file" } else {
  git check-ignore -q .env; if ($LASTEXITCODE -eq 0) { Write-Host "✅ .env is ignored" } else { Write-Host "🚨 .env is not ignored" } }

# Search every other file for your key, skipping .git, .venv, and node_modules, and list any file that holds it
$hits = Get-ChildItem -Recurse -File -Force |
  Where-Object { $_.Name -ne ".env" -and $_.FullName -notmatch '[\\/](\.git|\.venv|node_modules)[\\/]' } |
  Select-String -SimpleMatch -Pattern $env:AI_GATEWAY_API_KEY -List
if ($hits) { Write-Host "🚨 The key appears in:"; $hits.Path } else { Write-Host "✅ No key in project files" }
```

Expect `✅ No .env file` when the code reads the key only from your environment, or `✅ .env is ignored`, and
`✅ No key in project files`. If `.env` holds a different key, run the check again with that key in
`AI_GATEWAY_API_KEY`.

### Call the migrated model

If you migrated the sample, call `gpt-5-mini` through the new gateway with the `ticket-app` key, by the same model
name that API Management served.

```bash title="Bash"
# Read the ticket-app key into a variable, so that it never shows on screen
MIG_KEY="$(az aigateway api-key list-secrets --ids "$MIG_GATEWAY_ID/apiKeys/ticket-app" --query primaryKey -o tsv)"
echo "🔑 Key: in MIG_KEY (${#MIG_KEY} characters, not shown)"

# Call the model through the migrated gateway, and print only the HTTP status code
echo "🤖 HTTP status of a model call through the migrated gateway:"
curl -sS -o /dev/null -w "HTTP %{http_code}\n" "$MIG_URL/default/models/openai/v1/chat/completions" \
  -H "Authorization: Bearer $MIG_KEY" -H "Content-Type: application/json" \
  -d '{"model":"gpt-5-mini","messages":[{"role":"user","content":"ping"}]}'
```

```powershell title="PowerShell"
# Read the ticket-app key into a variable, so that it never shows on screen
$MIG_KEY = az aigateway api-key list-secrets --ids "$MIG_GATEWAY_ID/apiKeys/ticket-app" --query primaryKey -o tsv
Write-Host "🔑 Key: in MIG_KEY ($($MIG_KEY.Length) characters, not shown)"

# Call the model through the migrated gateway, and print only the HTTP status code
Write-Host "🤖 HTTP status of a model call through the migrated gateway:"
$body = @{ model = "gpt-5-mini"; messages = @(@{ role = "user"; content = "ping" }) } | ConvertTo-Json -Depth 5
(Invoke-WebRequest -Method Post -Uri "$MIG_URL/default/models/openai/v1/chat/completions" `
  -Headers @{ Authorization = "Bearer $MIG_KEY" } -ContentType "application/json" -Body $body -SkipHttpErrorCheck).StatusCode
```

Expect `HTTP 200` (`200` in PowerShell).

### List the tools of both migrated MCP servers

One call for each server, which is how you check what no read-back can tell you: that the passthrough reaches its
upstream, and that the projected server got the right contract. The second call needs `orders-mcp`, so run it only
if you migrated directly: the reference templates of the other tracks create `learn-mcp` alone.

```bash title="Bash"
# List the tools of the passthrough server, and print their names
echo "🔌 Tools of learn-mcp on the migrated gateway:"
curl -sS "$MIG_URL/default/toolservers/learn-mcp/mcp" -H "Authorization: Bearer $MIG_KEY" \
  -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | grep -o '"name":"[^"]*"'

# List the tools of the projected server, which the gateway builds from the OpenAPI contract
echo "🔌 Tools of orders-mcp on the migrated gateway:"
curl -sS "$MIG_URL/default/toolservers/orders-mcp/mcp" -H "Authorization: Bearer $MIG_KEY" \
  -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | grep -o '"name":"[^"]*"'
```

```powershell title="PowerShell"
# List the tools of the passthrough server, and print their names
Write-Host "🔌 Tools of learn-mcp on the migrated gateway:"
$body = '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
$response = Invoke-WebRequest -Method Post -Uri "$MIG_URL/default/toolservers/learn-mcp/mcp" `
  -Headers @{ Authorization = "Bearer $MIG_KEY"; Accept = "application/json, text/event-stream" } `
  -ContentType "application/json" -Body $body
[regex]::Matches($response.Content, '"name":"[^"]*"').Value

# List the tools of the projected server, which the gateway builds from the OpenAPI contract
Write-Host "🔌 Tools of orders-mcp on the migrated gateway:"
$response = Invoke-WebRequest -Method Post -Uri "$MIG_URL/default/toolservers/orders-mcp/mcp" `
  -Headers @{ Authorization = "Bearer $MIG_KEY"; Accept = "application/json, text/event-stream" } `
  -ContentType "application/json" -Body $body
[regex]::Matches($response.Content, '"name":"[^"]*"').Value
```

Expect the three tools that `discover-tools` found in API Management, each with the prefix `learn-mcp_`:

```text
"name":"learn-mcp_microsoft_docs_search"
"name":"learn-mcp_microsoft_code_sample_search"
"name":"learn-mcp_microsoft_docs_fetch"
```

And the two tools that API Management projected from `orders-rest`:

```text
"name":"orders-mcp_get_order"
"name":"orders-mcp_list_orders"
```

This is the proof that the contract arrived: the gateway read the OpenAPI document and built a tool for each
operation. The names round-tripped, too. API Management called them `get_order` and `list_orders`, and the gateway
derived the same base names from the contract rather than from the operation ids `get-order` and `list-orders`.

<!-- /steps -->

> **Checkpoint**
> The direct call returns `200`, the app works, and no key can reach Git. If you migrated the sample, the migrated
> gateway answers `200` and lists the three Microsoft Learn tools, plus the two order tools on the
> **Migrate directly** track.

## Go further

### Use the gateway's tools in VS Code agent mode

VS Code reads MCP servers from `.vscode/mcp.json`. To give agent mode the tools that you federated in
[Federate MCP tools behind one endpoint](../04-federate-mcp-tools/README.md), save this file as
`.vscode/mcp.json` in your project. Replace `<gateway-url>` with your gateway
URL (`echo $AI_GATEWAY_URL`) and `learn` with any tool server name that discover listed. To copy a tool server's
exact MCP URL, run
`az aigateway mcp show -g "$RG" --gateway-name "$GATEWAY" -n learn --query "properties.gatewayEndpoints[0].url" -o tsv`.

<!-- include: infra/mcp.json -->

On GitHub, open [infra/mcp.json](infra/mcp.json).

Start the server from the `mcp.json` editor and paste your runtime key when VS Code prompts for it. The `inputs`
entry keeps the key out of the file.

### Go keyless

For production apps, ask `build` to use Microsoft Entra ID instead of a key. Your identity then needs the
**AI Gateway Models User** role to call models, or **AI Gateway Tools User** to call tools: read access to the
gateway grants neither. Signed in to the Azure CLI, you get tokens for the scope
`https://ai.gateway.azure.com/Data.All`. Managed identities and service principals use
`https://ai.gateway.azure.com/.default`. [Go keyless with Microsoft Entra ID and RBAC](../11-entra-rbac/README.md)
shows both, and how to grant the roles.

### Manage the gateway from your agent

If you administer the gateway, the same plugin runs the `az aigateway` CLI for you. `manage` takes the gateway
name, its resource group, and the task in plain language. Start with a request that only reads:

```text
/aigateway:manage <gateway> <resource-group> list the API keys and the model aliases
```

It replies with the keys, their state, and their expiration, without their values, and with each alias, its
backend model, and the operations it publishes. Ask for changes the same way. `manage` runs the creates and
updates that you ask for, and asks you to confirm before it deletes anything, changes access, or reads a secret.
Foundry accounts and Connector Namespaces must already exist. The only thing outside the gateway that `manage`
creates is a role assignment that the gateway needs to work, and only after you confirm. To create a gateway, run
`/aigateway:create <gateway> <resource-group> <region>`.

## Clean up

Keep the gateway, model, and alias. Other labs use them.

Delete the key you created for this lab. Then delete the `ticket-summarizer` folder when you're done. If you added
a `.env` file, it holds a live key.

```bash title="Bash"
# Delete the API key that you created for this lab, so that it stops working everywhere
az aigateway api-key delete -g "$RG" --gateway-name "$GATEWAY" -n app-coding-agent --yes &&
  echo "🚮 API key app-coding-agent is deleted"
```

```powershell title="PowerShell"
# Delete the API key that you created for this lab, so that it stops working everywhere
az aigateway api-key delete -g $RG --gateway-name $GATEWAY -n app-coding-agent --yes
if ($?) { Write-Host "🚮 API key app-coding-agent is deleted" }
```

If a `.env` file with another gateway key was shared or committed, regenerate that key as
shown in [Issue and rotate runtime API keys](../10-api-keys/README.md). Remove the `ai-gateway-learn` entry from
`.vscode/mcp.json` if you added it.

If you did the migration, delete what it created. On the Terraform track, destroy its resource group first:

```bash title="Bash"
# Delete the migrated gateway and the resource group that terraform apply created
cd ~/aigateway/labs/17-coding-agent-plugin/terraform && terraform destroy -auto-approve &&
  echo "🚮 Everything that terraform apply created is deleted"
```

```powershell title="PowerShell"
# Delete the migrated gateway and the resource group that terraform apply created
Set-Location ~/aigateway/labs/17-coding-agent-plugin/terraform
if ($?) { terraform destroy -auto-approve }
if ($?) { Write-Host "🚮 Everything that terraform apply created is deleted" }
```

Then delete the sample's resource group, with API Management, the Foundry account, and the gateway of the other
tracks, and purge the soft-deleted Foundry account and API Management instance. It takes about 5 minutes.

```bash title="Bash"
# Delete the sample's resource group: API Management, the Foundry account, and the migrated gateway
az group delete -n "$MIG_RG" --yes &&
  echo "🚮 Resource group $MIG_RG is deleted"

# Purge the soft-deleted Foundry account, so that its name and quota are free again
az cognitiveservices account purge -g "$MIG_RG" -n "$FOUNDRY" -l "$MIG_LOCATION" &&
  echo "🚮 Foundry account $FOUNDRY is purged"

# Purge the soft-deleted API Management instance
az apim deletedservice purge --service-name "$APIM" --location "$MIG_LOCATION" &&
  echo "🚮 API Management instance $APIM is purged"
```

```powershell title="PowerShell"
# Delete the sample's resource group: API Management, the Foundry account, and the migrated gateway
az group delete -n $MIG_RG --yes
if ($?) { Write-Host "🚮 Resource group $MIG_RG is deleted" }

# Purge the soft-deleted Foundry account, so that its name and quota are free again
az cognitiveservices account purge -g $MIG_RG -n $FOUNDRY -l $MIG_LOCATION
if ($?) { Write-Host "🚮 Foundry account $FOUNDRY is purged" }

# Purge the soft-deleted API Management instance
az apim deletedservice purge --service-name $APIM --location $MIG_LOCATION
if ($?) { Write-Host "🚮 API Management instance $APIM is purged" }
```

If you migrated your own API Management instance, delete only the new gateway, with
`az aigateway delete --ids "$MIG_GATEWAY_ID" --yes`. Then delete the `~/apim-migration` folder. Its files
hold no secret values, but they describe your instance.

Optionally, remove the plugin. In Cursor, remove it where you installed it.

```text title="GitHub Copilot CLI"
/plugin uninstall aigateway
/plugin marketplace remove azure
```

```text title="Claude Code"
/plugin uninstall aigateway@azure
/plugin marketplace remove azure
```

```bash title="Gemini CLI"
# Remove the aigateway extension from Gemini CLI
gemini extensions uninstall aigateway &&
  echo "🚮 The aigateway extension is removed from Gemini CLI"
```

## Next steps

- [Use your gateway's models and tools in Claude Code](../25-claude-code/README.md)
- [Connect agent frameworks to models and tools](../15-agent-frameworks/README.md)
- [Go keyless with Microsoft Entra ID and RBAC](../11-entra-rbac/README.md)
- [Deploy a Foundry hosted agent behind AI Gateway](../16-foundry-hosted-agent/README.md)
- [Use the AI Gateway plugin](https://ai.gateway.azure.com/docs/quickstarts/plugin) in the AI Gateway docs
- [migrate-apim](https://github.com/Azure/aigateway/tree/main/tools/migrate-apim), the tool behind
  `/aigateway:migrate`, with every command and the rules that it applies
