Skip to content

Python: Fix M365 agent sample startup and anonymous mode - #9598

Draft
Naveen Chatlapalli (1aifanatic) wants to merge 1 commit into
microsoft:mainfrom
1aifanatic:contrib/python-m365-sample-aiohttp-middlewares
Draft

Naveen Chatlapalli (1aifanatic) wants to merge 1 commit into
microsoft:mainfrom
1aifanatic:contrib/python-m365-sample-aiohttp-middlewares

Conversation

@1aifanatic

Copy link
Copy Markdown
Contributor

Motivation & Context

The M365 Agents SDK sample (python/samples/05-end-to-end/m365-agent) does not start, and once startup is fixed, anonymous mode still cannot answer a message. Details and reproduction in #9595:

  1. web.Application(middleware=...) raises TypeError, because aiohttp's keyword is middlewares. The middlewares → middleware rename (Python: [BREAKING]: removed display_name, renamed context_providers, middleware and AggregateContextProvider #3139) appears to have caught it.
  2. FoundryChatClient() is created without a credential, and the README still documents OpenAI variables.
  3. The current M365 Agents SDK (1.9.0) requires a connection manager for CloudAdapter and AgentApplication. ClaimsIdentity now takes (claims, authentication_type, ...), so the sample's "anonymous" identity was treated as authenticated and the adapter tried to get an MSAL token (TENANT_ID is not set).

Description & Review Guide

  • What are the major changes?
    • web.Application(middlewares=[middleware_fn]).
    • FoundryChatClient(credential=AzureCliCredential()). The README now lists FOUNDRY_PROJECT_ENDPOINT / FOUNDRY_MODEL and az login, and the docstring no longer points to a nonexistent .env.example.
    • In anonymous mode, build_connection_manager returns an MsalConnectionManager with an empty SERVICE_CONNECTION, and build_authorization always builds Authorization. Anonymous requests never request tokens, so the placeholder connection is never used.
    • The anonymous middleware injects ClaimsIdentity(authentication_type="Anonymous") with no claims, which ClaimsIdentity.allow_anonymous recognizes.
    • The run-command comment points to samples/05-end-to-end/... instead of samples/demos/....
  • What is the impact of these changes? Sample only. Authenticated mode keeps its existing path: MsalConnectionManager(**agents_sdk_config) plus Authorization.
  • What do you want reviewers to focus on? Whether an empty SERVICE_CONNECTION is an acceptable way to satisfy the SDK in anonymous mode, or whether you would rather the sample require connection settings even for local runs.

Verified with aiohttp's test client: create_app(load_app_config()) in anonymous mode, with the agent replaced by an echo stub and a local endpoint standing in for the channel service:

result
main TypeError at web.Application(middleware=...)
this PR GET /api/health 200, GET /api/messages 200, POST /api/messages 202, and the reply activity reaches the channel

I also checked that the real build_agent() constructs with only FOUNDRY_* set. I could not test authenticated mode, which needs an Azure Bot registration. ruff format --check passes. ruff check reports one pre-existing E501 (the long approval_mode NOTE comment), which is also on main. Pyright's two remaining errors are pre-existing typing nits (an Optional return type and the sync lambda route handler), and both routes respond correctly at runtime.

Related Issue

Fixes #9595

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Claude Code helped trace the breakages through aiohttp and the M365 Agents SDK, implement the fix, build the test-client verification, and draft this description and the linked issue.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

🤖 Generated with Claude Code

- aiohttp's Application takes middlewares=, not middleware= (TypeError at startup).
- FoundryChatClient needs a credential; pass AzureCliCredential and document the
  FOUNDRY_* settings the sample actually reads instead of OpenAI ones.
- The M365 Agents SDK now requires a connection manager for CloudAdapter and
  AgentApplication; give anonymous mode an empty service connection.
- ClaimsIdentity's signature is now (claims, authentication_type, ...); build a
  claim-less anonymous identity so the adapter skips token acquisition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 12:54
@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Oct 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The anonymous identity and empty MSAL connection depend on external SDK behavior that still needs independent verification before approval.

No open issues 🎉

What changed in this PR

This PR fixes startup and anonymous messaging failures in the Python M365 agent sample reported in #9595.

Changes:

  • Corrects aiohttp middleware registration and supplies an Azure CLI credential to FoundryChatClient.
  • Adds a placeholder connection manager and claim-less identity for anonymous mode while preserving the authenticated configuration path.
  • Updates Foundry setup instructions and corrects the run-command and configuration comments.
File Description
python/​samples/​05-end-to-end/​m365-agent/​README.md Replaces OpenAI settings with Foundry configuration and Azure CLI prerequisites.
python/​samples/​05-end-to-end/​m365-agent/​m365_agent_demo/​app.py Fixes startup, configures anonymous SDK components, and corrects usage comments.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — df18abea Deployed Oct 22, 2026 by 1aifanatic via team_check #7245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: M365 agent sample fails at startup and in anonymous mode

2 participants