The Teams gateway¶
Teams is the one channel whose transport lives inside your own Azure tenant: the bot
identity, the consent and the public messaging endpoint are yours to create. This page is
that setup, once per fleet. After it, a Teams agent is an agents.yaml entry like any other.
A Teams bot is fronted by one shared gateway (agentctl teams-gateway) that routes by owner
identity to per-agent sessions. Replace the <...> placeholders; if IT owns your
Azure/Entra, the prerequisites list what to request. For Teams beside its peers rather than
on its own, read Channels first.
The only public surface is POST /api/messages (Bot Framework → gateway,
JWT-validated). /register, agent↔gateway traffic, and secrets stay private.
flowchart LR
subgraph OWNER["Owner laptop"]
TC["Teams client<br/>desktop / web"]
end
subgraph MSG["Microsoft 365 / Bot Framework — global, multi-tenant (Microsoft-operated)"]
TEAMS["Microsoft Teams<br/>service"]
CONN["Bot Framework Connector<br/>(activity serviceUrl relay)"]
end
subgraph TENANT["Your Azure tenant — single tenant + subscription"]
ENTRA["Entra ID / Azure AD<br/>app registration · validate inbound JWT · mint app token"]
subgraph VNET["Private network / VNet (NSG)"]
subgraph VMGW["Gateway VM — no agents"]
TLS["TLS reverse proxy :443<br/>exposes ONLY /api/messages"]
GW["Teams gateway<br/>/api/messages + /register<br/>route by owner AAD id"]
end
subgraph VMA["Agent VM A"]
AGA["Teams agent<br/>+ local secret store"]
end
subgraph VMB["Agent VM B"]
AGB["Teams agent<br/>+ local secret store"]
end
end
end
PROVIDER["LLM provider<br/>(model API)"]
AGA -. "0 · wss://gateway/register + reg token · PRIVATE" .-> GW
AGB -. "0 · wss + reg token · PRIVATE" .-> GW
TC -- "type message" --> TEAMS
TEAMS --> CONN
CONN == "1 · POST /api/messages · PUBLIC INTERNET<br/>HTTPS + signed JWT" ==> TLS
TLS -- "to gateway (loopback)" --> GW
ENTRA -. "validate inbound JWT" .-> GW
GW -- "2 · turn · WSS (private)" --> AGA
AGA -- "3 · inference · HTTPS egress" --> PROVIDER
PROVIDER -- "tokens" --> AGA
AGA -- "4 · stream chunks · WSS (private)" --> GW
ENTRA -. "mint outbound app token" .-> GW
GW == "5 · POST activities → serviceUrl<br/>HTTPS egress + app token" ==> CONN
CONN --> TEAMS
TEAMS -- "streamed reply" --> TC
classDef public fill:#ffe1e1,stroke:#d33,stroke-width:3px,color:#000;
classDef internal fill:#e6f2ff,stroke:#06c,color:#000;
classDef cloud fill:#eef7e6,stroke:#3a3,color:#000;
class TLS public;
class GW,AGA,AGB internal;
class TEAMS,CONN,ENTRA cloud;
Prerequisites¶
Request from IT if you lack them:
- Azure subscription + resource group, with Contributor (or Azure Bot Contributor) on it.
- App-registration rights in Entra (Application Administrator), or have IT register the app and hand you the appId + client secret.
- Teams Administrator (or an app-setup policy) to allow custom-app sideloading or org-catalog publish.
- Admin consent for Graph application permissions: only for proactive messaging / directory lookups; a 1:1 reply bot needs none.
- A public HTTPS route to the gateway's
/api/messages(real domain + trusted cert; a bare IP won't work). azCLI (≥ 2.x;az botis in the core CLI, no extension), signed in with the rights above.
Tenant setup¶
1. Register the provider (once per subscription, as Owner/Contributor):
az provider register -n Microsoft.BotService
2. Create the bot identity + Azure Bot. If IT registers the app, skip the az ad lines and start from their appId + secret.
az login
az ad app create --display-name "<BOT_DISPLAY_NAME>" --sign-in-audience AzureADMyOrg # → note appId
az ad sp create --id <APP_ID>
az ad app credential reset --id <APP_ID> --display-name teams-gateway --years 1 # → store the password (secret) in sops
az bot create -g <RG> -n <BOT_NAME> --app-type SingleTenant --appid <APP_ID> --tenant-id <TENANT_ID> --sku F0 --location global
az bot msteams create -g <RG> -n <BOT_NAME> # enable Teams
az bot update -g <RG> -n <BOT_NAME> --endpoint "https://<GATEWAY_HOST>/api/messages" # point at the gateway
3. Build the Teams app package. A template and the two icons ship on the host at
/usr/share/agentctl/runtime/teams/manifest/. Copy the directory, replace every {{…}}
placeholder, zip the three files at the top level, and upload the zip in Teams (sideload or
org catalog).
| placeholder | value |
|---|---|
{{BOT_APP_ID}} |
the Application (client) ID from step 2 — it appears twice, as id and as bots[0].botId |
{{BOT_NAME}} / {{BOT_NAME_FULL}} |
the short and full display names |
{{COMPANY_NAME}} / {{COMPANY_DOMAIN}} |
the developer block; the domain also builds the privacy and terms URLs |
{{GATEWAY_HOST}} |
the gateway's public FQDN, which is also validDomains |
The icons are color.png (192×192) and outline.png (32×32), referenced by those exact
filenames. Replace the shipped pair to brand the app; keep the names.
4. Wire the app into the gateway. With TEAMS_APP_ID set the gateway runs in Azure mode: validates the inbound Bot Framework JWT on /api/messages and mints an Entra token for outbound replies.
agentctl secret set TEAMS_APP_ID --value <APP_ID>
agentctl secret set TEAMS_APP_PASSWORD --value <CLIENT_SECRET>
agentctl secret set TEAMS_APP_TENANT_ID --value <TENANT_ID>
agentctl teams-gateway install
5. Add a Teams agent, routed by owner Entra object ID (a GUID), not the UPN; no bot token, no pairing. The provider key rides along on the add — a per-agent secret set is refused before the agent exists:
# get the object id: az ad user show --id <user@tenant> --query id -o tsv
agentctl agents add <name> --channel teams --owner-aad <owner-object-id> \
--runtime pi --provider openrouter --model <model-id> --api-key <KEY>
Hosting the gateway¶
It needs a long-lived process (a persistent /register WebSocket, so not an Azure
Function) and a public HTTPS /api/messages on a real domain with a trusted cert — a bare
IP will not do. The managed path is a VM running agentctl, the gateway and Caddy:
sudo agentctl host caddy domain set <domain>
sudo agentctl apply
apply renders the site itself, with /api/messages* scoped to 127.0.0.1:3978 and
everything else going to the login proxy, plus automatic Let's Encrypt, the structured
access log and the SSE compression exclusions. /register is never published: it is reached
only over the private network, firewall- or NSG-limited. See
TLS and Caddy.
Set the bot endpoint to https://<domain>/api/messages.
Running your own proxy: scope the Teams route
Front /api/messages alone. A proxy that sends the whole site to :3978 publishes
/register on public HTTPS, straight past the firewall rule that is supposed to keep
it fleet-internal.
The gateway also ships as a container image built from this repo, to run behind a reverse proxy you already operate.
Cross-host: joining the fleet¶
A cross-host Teams setup runs the public gateway on its own VM (only
teams-gateway, no agents) while the agents run on a separate private primary
host. Both hosts are writers on the same shared GitOps remote (multi-writer
fleet, per-agent shards commute). The gateway is just a fleet member that adopts
the shared ledger: it builds its Teams allow-map (TEAMS_AGENTS) from its own
copy of the ledger's Teams agents, so before it adopts the shared ledger it knows
zero agents and rejects every /register. teams-gateway status reports that
state as agents: 0 in the ledger.
Below, primary = the existing fleet writer running the agents; gateway = the freshly-provisioned second host. The steps are ordered and the order matters.
- Pin every agent to the primary FIRST —
run_on: <primary hostname>on each agent shard. An agent with norun_on:pin runs on every host in the fleet, so an unpinned agent is provisioned and started a second time the moment the gateway adopts the ledger and applies. Agents created on a single-host fleet carry no pin. Adding one to an existing agent is a direct ledger edit:agents setdoes not acceptrun_on, andagents addauto-pins only the agent it is creating. - Give the gateway credentials to reach the shared repo, e.g. copy the git credential / deploy key the primary uses to push the fleet remote.
- Point the gateway's
/opt/agentctlat the shared remote and adopt it.agentctl fleet adoptfetches + hard-resets to the remote's default branch and self-registers the host's age recipient. fleet adoptdetects the remote's default branch (main,master, or otherwise) on its own.- The adopt hard-reset overwrites the gateway's local ledger, so any gateway-local-only secrets (its Teams bot creds) must be re-seeded afterward (step 6).
- Gateway
apply: self-registers its age recipient intohost_keys, skips the primary-pinned agents (assigned to another host), and pushes. - Primary
fleet sync+apply: picks up the gateway's recipient and re-wraps the sops store to include it, so the gateway can decrypt/seed fleet secrets. (Only an existing recipient can re-wrap.) - Gateway
fleet sync+ re-seed the Teams bot creds the adopt wiped:TEAMS_APP_ID/TEAMS_APP_PASSWORD/TEAMS_APP_TENANT_ID. They're decryptable now because the gateway is a recipient (step 5). host.teams_gatewaymust be the gateway's HOSTNAME, not its IP. Add an/etc/hostsentry on the primary so agents resolve it. That marker is the agent-side dial address: an agent dials loopback when the marker is empty or equals its own hostname, andws://<marker>:3978/registerotherwise. With an IP there, agents on the gateway host itself stop using loopback and dial the IP.teams-gateway installwrites the marker from the host's own hostname — it is not settable withhost set, so the way to correct it is to run the install on the gateway host.- Gateway
apply+ restartagentctl-teams-gateway: rendersTEAMS_AGENTSfrom the now-shared ledger; agents that were retrying/registerconnect immediately.
Troubleshooting¶
| symptom | cause |
|---|---|
teams-gateway status → agents: 0 in the ledger |
the gateway has not adopted the shared ledger (step 3) — the allow-map is built from its own copy of it |
agents retry /register forever |
they are dialling the wrong address: check host.teams_gateway resolves from the primary (step 7) and that 3978 is open on the private network |
inbound messages 401 at /api/messages |
TEAMS_APP_ID / TEAMS_APP_PASSWORD / TEAMS_APP_TENANT_ID — the gateway validates the Bot Framework JWT against them |