Library for creating authentication tokens for agents using Microsoft Agent SDK
$ dotnet add package Microsoft.Agents.CopilotStudio.ClientProvides a client to interact with agents built in Copilot Studio. This Library is intended to provide access to a given agent's conversational channel.
| Version | Date | Changelog |
|---|---|---|
| 1.2.0 | 2025-08-19 | Detailed Changelog |
| 1.3.0 | 2025-10-22 | Detailed Changelog |
To use this library, you will need the following:
[!IMPORTANT] If you are using this client from a service, you will need to exchange the user token used to login to your service for a token for your agent hosted in copilot studio. This is called a On Behalf Of (OBO) authentication token. You can find more information about this authentication flow in .
When using this method, you will need to add the CopilotStudio.Copilots.Invoke delegated API permision to your application registration's API privilages
This step will require permissions to edit application identities in your Azure tenant.
API's my organization usesPower Platform API.
Power Platform API see the note at the bottom of this section.Delegated Permissions
CopilotStudio and Check CopilotStudio.Copilots.InvokeAdd PermissionsApplication Permissions
CopilotStudio and Check CopilotStudio.Copilots.InvokeAdd PermissionsGrant Admin consent for copilotsdk before the permissions will be available to the application.[!TIP] If you do not see
Power Platform APIin the list of API's your organization uses, you need to add the Power Platform API to your tenant. To do that, goto Power Platform API Authentication and follow the instructions on Step 2 to add the Power Platform Admin API to your Tenant
The Copilot Client is configured using the CopilotClientSettings class.
The ConnectionSettings class can be configured using either the default constructor or a parameterized constructor that accepts an IConfigurationSection. Below are the steps to configure an instance of the ConnectionSettings class.
You can create an instance of the ConnectionSettings class with default values using the default constructor. You can create the settings object using the the default constructor or via an IConfiguraition entry.
There are a few options for configuring the ConnectionSettings class. The following are the most common options:
Using Envrionment ID and Copilot Studio Agent Schema Name:
var connectionSettings = new ConnectionSettings
{
EnvironmentId = "your-environment-id",
SchemaName = "your-agent-schema-name",
};
Using the DirectConnectUrl:
var connectionSettings = new ConnectionSettings
{
DirectConnectUrl = "https://direct.connect.url",
};
[!NOTE] By default, its asumed your agent is in the Microsoft Public Cloud. If you are using a different cloud, you will need to set the
Cloudproperty to the appropriate value. See thePowerPlatformCloudenum for the supported values
You can create an instance of the ConnectionSettings class using an IConfigurationSection object.
The following are the most common options:
Using Envrionment ID and Copilot Studio Agent Schema Name:
{
"ConnectionSettings": {
"EnvironmentId": "your-environment-id",
"SchemaName": "your-agent-schema-name",
}
}
Using the DirectConnectUrl:
{
"ConnectionSettings": {
"DirectConnectUrl": "https://direct.connect.url",
}
}
[!NOTE] By default, its asumed your agent is in the Microsoft Public Cloud. If you are using a different cloud, you will need to set the
Cloudproperty to the appropriate value. See thePowerPlatformCloudenum for the supported values
[!Important] User based authentication flows are currently supported for this client, Service Prinipal Flows are in private prieview and not documented in this release.
Your code will need to create a User Token ( via MSAL Public Client or an OBO flow for a User access token) to call this service, the application you use to do this must have the CopilotStudio.Copilots.Invoke permission assigned to it.
There are currently two ways to pass auth to the CopilotClient.
In this case, you would create an HttpMessageRequestHandler that creates the bearer header on the http request message, In the example below, AddTokenHandler is responsible for adding the bearer header to the http request prior to the send:
// Create an http client for use by the DirectToEngine Client and add the token handler to the client.
builder.Services.AddHttpClient("mcs").ConfigurePrimaryHttpMessageHandler(
() => new AddTokenHandler(settings));
Then create an instance the client and request to start a conversation:
var copilotClient = new CopilotClient(settings, s.GetRequiredService<IHttpClientFactory>(), logger, "mcs");
await foreach (Activity act in copilotClient.StartConversationAsync(emitStartConversationEvent:true, cancellationToken:cancellationToken))
{
}
In this case, the TokenService is a class is responsible for managing the token acquire flow and returning the access token via the GetToken API call on the TokenService Class.
[!NOTE] We do not cover how to create the TokenService Class in this document. The Copilot client is looking for a function that matches the signature
Func<string, Task<string>> tokenProviderFunction
var tokenService = new TokenService(settings);
var copilotClient = new CopilotClient(settings, s.GetRequiredService<IHttpClientFactory>(), tokenService.GetToken, logger, "mcs");
await foreach (Activity act in copilotClient.StartConversationAsync(emitStartConversationEvent:true, cancellationToken:cancellationToken))
{
}