Skip to content

Add Graph client factory helpers to Teams turn context and app-only overloads - #928

Merged
tracyboehrer merged 3 commits into
mainfrom
users/tracyboehrer/teams-graphclient-for-userid
Jul 16, 2026
Merged

Add Graph client factory helpers to Teams turn context and app-only overloads#928
tracyboehrer merged 3 commits into
mainfrom
users/tracyboehrer/teams-graphclient-for-userid

Conversation

@tracyboehrer

Copy link
Copy Markdown
Member

Summary

Adds Microsoft Graph client factory helpers so agents can obtain a GraphServiceClient for both delegated (user) and app-only (application) tokens without manually wiring up token providers or scopes.

Changes

  • New GraphClientFactory — shared by TeamsAgentExtension and TeamsTurnContext, centralizing the token-provider and .default scope-derivation logic (no duplication). Scope/resource is derived from the Graph base URL so national clouds (e.g. graph.microsoft.us) are honored.
  • App-only Graph client overloads:
    • GetAppGraphClient(...) — connection resolved from the current turn (ITurnContext.Identity + Activity) via IConnections.GetTokenProvider.
    • GetAppGraphClientForConnection(connectionName, ...) — named token connection. No extra appsettings required beyond the existing token connection.
  • Turn-context surfaceGetGraphClient / GetAppGraphClient / GetAppGraphClientForConnection are now exposed on ITeamsTurnContext / TeamsTurnContext, resolving UserAuthorization and IConnections from ITurnContext.Services (clear InvalidOperationException if unconfigured).
  • AgentApplication.SetTurnContextServices now registers IConnections in turn services so the turn-context helpers can resolve it.

Notes

  • The named-connection variant is GetAppGraphClientForConnection (not an overload of GetAppGraphClient) to avoid a silent single-string overload collision with GetAppGraphClient(graphBaseUrl).
  • App-only clients use application permissions, so callers specify the target resource in the request path (e.g. client.Users["{userId}"]...).

Testing

  • dotnet build src\libraries\Extensions\Microsoft.Agents.Extensions.MSTeams\Microsoft.Agents.Extensions.MSTeams.csproj0 warnings / 0 errors.

…verloads

Introduce a shared GraphClientFactory used by both TeamsAgentExtension and
TeamsTurnContext to build GraphServiceClient instances, eliminating duplicated
token-provider and scope-derivation logic.

- Add app-only (application token) GraphServiceClient overloads:
  - GetAppGraphClient (connection resolved from the current turn)
  - GetAppGraphClientForConnection (named token connection)
- Expose GetGraphClient / GetAppGraphClient / GetAppGraphClientForConnection on
  ITeamsTurnContext / TeamsTurnContext, resolving UserAuthorization and
  IConnections from ITurnContext.Services.
- Register IConnections in turn services (AgentApplication.SetTurnContextServices)
  so the turn-context helpers can resolve it.
- Name the named-connection variant GetAppGraphClientForConnection to avoid a
  single-string overload collision with GetAppGraphClient(graphBaseUrl).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b49f943e-28fb-4905-b258-0b51ef3aa557
@tracyboehrer
tracyboehrer requested a review from sw-joelmut as a code owner July 16, 2026 20:45
Copilot AI review requested due to automatic review settings July 16, 2026 20:45
@tracyboehrer
tracyboehrer requested review from a team and ceciliaavila as code owners July 16, 2026 20:45
@github-actions github-actions Bot added the ML: Core Tags changes to core libraries label Jul 16, 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.

Pull request overview

Adds Microsoft Graph GraphServiceClient factory helpers to the MSTeams extension so agents can easily obtain delegated (user) and app-only Graph clients from either TeamsAgentExtension or ITeamsTurnContext, with shared scope/resource derivation via a central factory.

Changes:

  • Introduces GraphClientFactory to centralize Graph client creation for delegated and app-only authentication.
  • Exposes Graph client helpers on TeamsTurnContext / ITeamsTurnContext and adds app-only overloads (including named-connection).
  • Registers IConnections into per-turn services in AgentApplication.SetTurnContextServices so turn-context helpers can resolve it.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/libraries/Extensions/Microsoft.Agents.Extensions.MSTeams/TeamsTurnContext.cs Adds Graph client helper methods that resolve UserAuthorization / IConnections from turn services.
src/libraries/Extensions/Microsoft.Agents.Extensions.MSTeams/TeamsAgentExtension.cs Refactors delegated Graph client creation to factory; adds app-only Graph client helpers (turn-resolved + named-connection).
src/libraries/Extensions/Microsoft.Agents.Extensions.MSTeams/ITeamsTurnContext.cs Adds Graph client helper methods to the public Teams turn-context interface.
src/libraries/Extensions/Microsoft.Agents.Extensions.MSTeams/GraphClientFactory.cs New shared factory for delegated + app-only Graph client construction and scope derivation.
src/libraries/Builder/Microsoft.Agents.Builder/App/AgentApplication.cs Registers IConnections into turnContext.Services when configured, enabling turn-context resolution.

tracyboehrer and others added 2 commits July 16, 2026 15:52
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@tracyboehrer
tracyboehrer enabled auto-merge July 16, 2026 20:52
@tracyboehrer
tracyboehrer merged commit 064c515 into main Jul 16, 2026
12 checks passed
@tracyboehrer
tracyboehrer deleted the users/tracyboehrer/teams-graphclient-for-userid branch July 16, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ML: Core Tags changes to core libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants