Skip to main content

How to Add the Teams Extension to an Agent in the Microsoft 365 Agents SDK in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Some features exist only in Microsoft Teams: dialogs, message extensions, meeting events, team and channel events. The Microsoft 365 Agents SDK keeps them in its Teams extension. You turn it on for an agent with one package and one attribute.

Syntax​

C#
using Microsoft.Agents.Extensions.MSTeams;

[TeamsExtension]
public partial class <AgentName> : AgentApplication
{
// Teams features through the generated property:
// TeamsExtension.TaskModules, TeamsExtension.MessageExtensions, TeamsExtension.Meetings, ...
// or through Teams route attributes on methods, such as [TeamsMessageRoute].
}
PartWhat it does
Microsoft.Agents.Extensions.MSTeamsThe NuGet package with the Teams extension. Its readme: Provides Microsoft Teams-specific extensions for the Microsoft 365 Agents SDK.
[TeamsExtension]Asks the SDK's source generator to add the extension to this class
partialLets the generator write the other part of the class. Without it, the build fails
<AgentName>Your agent class, a subclass of AgentApplication
TeamsExtensionThe property the generator adds, of type TeamsAgentExtension

The property's name is TeamsExtension. The SDK's own documentation for the attribute says the property is called Teams, which doesn't build: Fixed: "The name 'Teams' does not exist in the current context" with [TeamsExtension] in the Microsoft 365 Agents SDK shows what the generator really writes.

Inside TeamsExtension, the SDK's source groups the Teams features as TaskModules (dialogs), MessageExtensions, Meetings, Channels, Teams (team events) and FileConsent.

The Example​

The agent for this post answers every message in Teams with the kind of conversation it's in: Hello from Teams! This is a personal conversation. Its one route is a Teams route attribute, [TeamsMessageRoute], whose handler gets the activity as a Teams activity.

The agent is one class in an ASP.NET Core project with the Agents SDK 1.9.205, set up to run in Teams: its own bot registration and identity, token validation, and an app package whose bot allows personal.

Prerequisites​

  • An agent that already runs in Microsoft Teams, in a personal chat: a bot registration, the agent's identity in its settings, and the app added in Teams.

Step 1: Add the Package​

In the project's folder:

Terminal
dotnet add package Microsoft.Agents.Extensions.MSTeams --version 1.9.205

dotnet add package adds the package to the project file. Use the same version as your other Microsoft.Agents.* packages.

Step 2: Mark the Agent Class​

WhereAmIAgent.cs
using Microsoft.Agents.Builder.App;
using Microsoft.Agents.Builder.State;
using Microsoft.Agents.Extensions.MSTeams;
using Microsoft.Agents.Extensions.MSTeams.App;

namespace HoExtension;

[TeamsExtension]
public partial class WhereAmIAgent(AgentApplicationOptions options) : AgentApplication(options)
{
[TeamsMessageRoute]
public async Task OnMessageAsync(ITeamsTurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
string conversationType = turnContext.Activity.Conversation.ConversationType;
await turnContext.SendActivityAsync($"Hello from Teams! This is a {conversationType} conversation.", cancellationToken: cancellationToken);
}
}

WhereAmIAgent.cs in a macOS-style editor window: the class marked [TeamsExtension] and partial, deriving from AgentApplication, with one method marked [TeamsMessageRoute] that reads the conversation type from the Teams activity and sends Hello from Teams! This is a ... conversation.

  • [TeamsExtension] and partial turn the extension on for this class.
  • [TeamsMessageRoute] is one of the extension's route attributes. With no text given, the SDK's description says it matches any message.
  • ITeamsTurnContext is the turn context as the Teams extension sees it. Its Activity is, in the SDK's words, a strongly-typed TeamsActivity, so Conversation.ConversationType is right there.

Register the class in Program.cs the usual way, with builder.AddAgent<WhereAmIAgent>().

Step 3: See What the Attribute Adds​

The attribute works through a source generator: code that writes more code while the project builds. To see that code, build once with the compiler's generated files turned on, and with --no-incremental, so the compiler runs even if nothing changed:

Terminal
dotnet build --no-incremental -p:EmitCompilerGeneratedFiles=true
Output
Determining projects to restore...
All projects are up-to-date for restore.
HoExtension -> /Users/you/HoExtension/bin/Debug/net10.0/HoExtension.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.63

The generator's file is under obj/Debug/net10.0/generated/Microsoft.Agents.Core.Analyzers/:

HoExtension.WhereAmIAgent.AgentExtensions.g.cs
// <auto-generated />
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

namespace HoExtension
{
partial class WhereAmIAgent
{
/// <summary>
/// Provides access to <see cref="Microsoft.Agents.Extensions.MSTeams.TeamsAgentExtension"/> agent features.
/// </summary>
public global::Microsoft.Agents.Extensions.MSTeams.TeamsAgentExtension TeamsExtension { get; private set; }

protected override void ConfigureExtensions()
{
base.ConfigureExtensions();
TeamsExtension = new global::Microsoft.Agents.Extensions.MSTeams.TeamsAgentExtension(this);
RegisterExtension(TeamsExtension, _ => { });
}
}
}

The other part of WhereAmIAgent gets the TeamsExtension property, and a ConfigureExtensions method that creates it and registers it when the agent is constructed. That's why the class must be partial.

Step 4: Try It in Teams​

Start the agent, and send it a message in its personal chat:

Leo's message Where am I? on the right, and the agent Dialog Demo's answer on the left: Hello from Teams! This is a personal conversation.

personal is the conversation type of a one-to-one chat with the agent. In a group chat, the same field says groupChat.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out partialerror MAA001: 'WhereAmIAgent' must be declared as 'partial' to use an AgentExtension attributeDeclare the class partial
Writing Teams.TaskModules, as the SDK's documentation sayserror CS0103: The name 'Teams' does not exist in the current contextWrite TeamsExtension.TaskModules
Using the old Microsoft.Agents.Extensions.Teams packageIts classes are marked obsolete in 1.9.205Use Microsoft.Agents.Extensions.MSTeams
Expecting the generated file after a build that changed nothingNo .g.cs fileAdd --no-incremental

Troubleshooting​

SymptomLikely causeFix
MAA001The class isn't partialAdd partial
CS0103 for TeamsThe property is called TeamsExtensionRename the uses
The agent doesn't answer in TeamsThe bot can't reach the agent, or the route isn't registeredCheck the agent's log for the incoming message

FAQ​

Do I need the generated property if I only use route attributes?​

No. Attribute routes such as [TeamsMessageRoute] register without touching it, as in this post. You need the property for the fluent methods, such as TeamsExtension.TaskModules.OnFetch.

What else does the extension give me?​

Besides the feature groups, TeamsAgentExtension has GetTeamsClient, for Teams' own API, and GetGraphClient and GetAppGraphClient, for Microsoft Graph.

Is the Teams extension the same as the Teams SDK?​

No. The Agents SDK's Teams extension builds on Microsoft's Teams SDK package, Microsoft.Teams.Apps, which 1.9.205 brings in at version 2.1.2, and exposes its features through Agents SDK routes.

Conclusion​

To use Teams-only features in an Agents SDK agent, add Microsoft.Agents.Extensions.MSTeams, mark your AgentApplication class [TeamsExtension] and partial, and use the generated TeamsExtension property or the Teams route attributes.

Additional Resources​