Microsoft.Agents.CopilotStudio.Client 0.2.290-alpha

Prefix Reserved
This is a prerelease version of Microsoft.Agents.CopilotStudio.Client.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Microsoft.Agents.CopilotStudio.Client --version 0.2.290-alpha
                    
NuGet\Install-Package Microsoft.Agents.CopilotStudio.Client -Version 0.2.290-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Agents.CopilotStudio.Client" Version="0.2.290-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microsoft.Agents.CopilotStudio.Client" Version="0.2.290-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Microsoft.Agents.CopilotStudio.Client" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Microsoft.Agents.CopilotStudio.Client --version 0.2.290-alpha
                    
#r "nuget: Microsoft.Agents.CopilotStudio.Client, 0.2.290-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Microsoft.Agents.CopilotStudio.Client&version=0.2.290-alpha&prerelease
                    
Install Microsoft.Agents.CopilotStudio.Client as a Cake Addin
#tool nuget:?package=Microsoft.Agents.CopilotStudio.Client&version=0.2.290-alpha&prerelease
                    
Install Microsoft.Agents.CopilotStudio.Client as a Cake Tool

Microsoft.Agents.CopilotStudio.Client

Provides a client to interact with agents built in Copilot Studio. This Library is intended to provide access to a given agent's conversational channel.

Instructions - Required Setup to use this library

Prerequisite

To use this library, you will need the following:

  1. An Agent Created in Microsoft Copilot Studio.
  2. Ability to Create or Edit an Application Identity in Azure
    1. (Option 1) for a Public Client/Native App Registration or access to an existing registration (Public Client/Native App) that has the CopilotStudio.Copilot.Invoke API Delegated Permission assigned.
    2. (Option 2) for a Confidential Client/Service Principal App Registration or access to an existing App Registration (Confidential Client/Service Principal) with the CopilotStudio.Copilot.Invoke API Application Permission assigned.

Create a Agent in Copilot Studio

  1. Create or open an Agent in Copilot Studio
    1. Make sure that the Copilot is Published
    2. Goto Settings ⇒ Advanced ⇒ Metadata and copy the following values. You will need them later:
      1. Schema name - this is the 'unique name' of your agent inside this environment.
      2. Environment Id - this is the ID of the environment that contains the agent.

Create an Application Registration in Entra ID to support user authentication to Copilot Studio

[!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 Entra Documentation.

When using this method, you will need to add the CopilotStudio.Copilots.Invoke delegated API permision to your application registration's API privilages

Add the CopilotStudio.Copilots.Invoke permissions to your Application Registration in Entra ID to support User or Service Principal authentication to Copilot Studio

This step will require permissions to edit application identities in your Azure tenant.

  1. In your azure application
    1. Goto Manage
    2. Goto API Permissions
    3. Click Add Permission
      1. In the side pannel that appears, Click the tab API's my organization uses
      2. Search for Power Platform API.
        1. If you do not see Power Platform API see the note at the bottom of this section.
      3. For User Interactive Permissions, choose Delegated Permissions
        1. In the permissions list choose CopilotStudio and Check CopilotStudio.Copilots.Invoke
        2. Click Add Permissions
      4. For Service Principal/Confidential Client, choose Application Permissions
        1. In the permissions list choose CopilotStudio and Check CopilotStudio.Copilots.Invoke
        2. Click Add Permissions
        3. A appropiate administrator must then Grant Admin consent for copilotsdk before the permissions will be available to the application.
    4. Close Azure Portal

[!TIP] If you do not see Power Platform API in 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

How-to use

Setting up configuration for the CopilotStudio Client

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.

Using the Default Constructor

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 Cloud property to the appropriate value. See the PowerPlatformCloud enum for the supported values

Using an IConfigurationSection

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 Cloud property to the appropriate value. See the PowerPlatformCloud enum for the supported values

Getting an AccessToken for the Copilot Studio Client API

[!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.

1. Create an HttpMessageRequest Handler that will populate the bearer token and assign it to the httpClient you create

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))
{

}
2. Create a function that returns a user token and pass that to the constructor for the Copilot Client

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))
{

}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1-beta 30 4/24/2025
1.0.1 33 4/24/2025
0.2.361-alpha 40 4/23/2025
0.2.358-alpha 67 4/22/2025
0.2.346-alpha 80 4/19/2025
0.2.343-alpha 137 4/18/2025
0.2.339-alpha 174 4/17/2025
0.2.315-alpha 184 4/16/2025
0.2.314-alpha 87 4/12/2025
0.2.308-alpha 164 4/10/2025
0.2.295-alpha 179 4/9/2025
0.2.290-alpha 153 4/8/2025
0.2.287-alpha 89 4/5/2025
0.2.286-alpha 177 4/4/2025
0.2.282-alpha 156 4/3/2025
0.2.277-alpha 153 4/1/2025
0.2.271-alpha 100 3/29/2025
0.2.267-alpha 122 3/28/2025
0.2.257-alpha 123 3/27/2025
0.2.180-alpha 458 3/26/2025
0.2.171-alpha 113 3/21/2025
0.2.162-alpha 144 3/19/2025
0.2.154-alpha 181 3/18/2025
0.2.153-alpha 69 3/15/2025
0.2.151-alpha 107 3/14/2025
0.2.146-alpha 160 3/12/2025
0.2.141-alpha 174 3/8/2025
0.2.137-alpha 213 3/7/2025
0.1.26 250 11/18/2024