How to Connect an AI Agent to Microsoft Teams
Updated 2026-11-17
This guide connects an AI agent to Microsoft Teams with the Bot Framework SDK for Python (v4). Teams sends activities to your bot’s /api/messages endpoint; your code runs an LLM and replies in the chat. Registration uses an Azure Bot (single-tenant app) per Microsoft’s current guidance. For Slack or Discord bots, see Connect an AI agent to Slack and Connect an AI agent to Discord. Last tested 2026-11-17.
Docs used:
- Bots in Teams — build a bot
- Register a Bot Framework bot with Azure
- Add authentication to your Teams bot
Prerequisites
- A Microsoft 365 tenant where you can upload a custom Teams app (or use Teams Toolkit dev tunneling for local test)
- Python 3.10+
- An Azure subscription to create an Azure Bot resource
OPENAI_API_KEY(or another LLM provider you wire in code)
Steps
1. Register the bot in Azure
- In the Azure portal, create an Azure Bot resource.
- Choose Single-tenant app type when prompted (multi-tenant creation is deprecated per Microsoft’s bot registration docs).
- Copy the Microsoft App ID, create a client secret (app password), and note the tenant ID.
Set environment variables locally (never commit these):
export MicrosoftAppType=SingleTenant
export MicrosoftAppId="<app-id>"
export MicrosoftAppPassword="<client-secret>"
export MicrosoftAppTenantId="<tenant-id>"
export OPENAI_API_KEY="sk-..."
See Bot identity configuration.
2. Install Python dependencies
pip install botbuilder-core botbuilder-integration-aiohttp aiohttp openai
3. Implement the messaging endpoint
The Bot Framework delivers JSON activities to POST /api/messages. The sample below uses CloudAdapter and ConfigurationBotFrameworkAuthentication, which read the MicrosoftApp* environment variables.
# teams_agent.py
import os
import traceback
from collections import defaultdict
from aiohttp import web
from aiohttp.web import Request, Response
from botbuilder.core import ActivityHandler, MessageFactory, TurnContext
from botbuilder.integration.aiohttp import (
CloudAdapter,
ConfigurationBotFrameworkAuthentication,
)
from openai import OpenAI
BOT_AUTH = ConfigurationBotFrameworkAuthentication()
ADAPTER = CloudAdapter(BOT_AUTH)
LLM = OpenAI()
HISTORY: dict[str, list[dict]] = defaultdict(list)
class TeamsAgentBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
text = (turn_context.activity.text or "").strip()
conv_id = turn_context.activity.conversation.id
HISTORY[conv_id].append({"role": "user", "content": text})
messages = [
{"role": "system", "content": "You are a helpful Teams assistant. Be concise."},
*HISTORY[conv_id][-12:],
]
reply = LLM.chat.completions.create(
model=os.environ.get("MODEL", "gpt-4.1-mini"),
messages=messages,
).choices[0].message.content
HISTORY[conv_id].append({"role": "assistant", "content": reply})
await turn_context.send_activity(MessageFactory.text(reply))
BOT = TeamsAgentBot()
async def on_error(context: TurnContext, error: Exception):
traceback.print_exc()
await context.send_activity("The bot hit an error. Check server logs.")
ADAPTER.on_turn_error = on_error
async def messages(req: Request) -> Response:
return await ADAPTER.process(req, BOT)
APP = web.Application()
APP.router.add_post("/api/messages", messages)
if __name__ == "__main__":
web.run_app(APP, host="0.0.0.0", port=int(os.environ.get("PORT", "3978")))
For Teams-specific activities, subclass TeamsActivityHandler instead of ActivityHandler (see Teams activity handlers).
4. Expose HTTPS for Teams
Teams must reach your bot over HTTPS.
- Local dev: use Microsoft Dev Tunnels or Teams Toolkit to tunnel port
3978. - Production: deploy behind a public URL and set the bot’s Messaging endpoint in the Azure Bot resource to
https://your-host/api/messages.
Package long-running bots with How to deploy an AI agent with Docker.
5. Sideload the Teams app
Use the Teams Developer Portal or Teams Toolkit to create an app manifest that references your bot’s app ID. Install the app in a team or personal chat and send a message to the bot.
Microsoft’s Python quickstart sample lives under Teams SDK bot-quickstart (Python).
6. Add tools (optional)
Replace the single chat.completions call with the tool loop from Build your first AI agent in Python, or attach MCP tools via Connect an AI agent to an MCP server.
Common errors
401 or 403 on /api/messages: Wrong MicrosoftAppId, expired password, or tenant mismatch. Regenerate the client secret and confirm MicrosoftAppType=SingleTenant with the correct MicrosoftAppTenantId.
Bot does not respond in channel: @mention the bot in channels unless you enable channel messaging without mentions in the manifest. In personal chat, a direct message is enough.
Endpoint not reachable: Azure Bot configuration must match your live HTTPS URL and certificate. Tunnel URLs change when you restart dev tunnels—update Azure.
Import errors (botbuilder vs microsoft_agents): This guide uses Bot Framework v4 packages shown in OfficeDev samples. Newer Microsoft Agents for Python use different imports—follow one stack per deployment.
FAQ
Teams SDK versus Bot Framework SDK?
Microsoft’s Bots in Teams doc describes Teams SDK for new AI agents and Bot Framework SDK for conventional bots. This guide follows the Bot Framework Python path because it is documented across Azure registration and OfficeDev samples.
Do I need Azure OpenAI?
No. The sample uses the OpenAI Python client with OPENAI_API_KEY. Swap in Azure OpenAI or another host by changing the client construction only.
Can the bot call Linear or Jira?
Yes. Give the LLM tools or run MCP servers your agent code invokes—see Linear MCP server and MCP server for Jira.