@microsoft/agents-copilotstudio-client
v1.1.1
Published
Microsoft Copilot Studio Client for JavaScript. Copilot Studio Client.
Maintainers
Readme
@microsoft/agents-copilotstudio-client
Overview
The @microsoft/agents-copilotstudio-client package allows you to interact with Copilot Studio Agents using the Direct Engine Protocol. This client library is designed to facilitate communication with agents, enabling seamless integration and interaction within your JavaScript or TypeScript applications.
This package provides exports for CommonJS and ES6 modules, and also a bundle to be used in the browser.
[!NOTE] The Client needs to be initialized with a valid JWT Token.
Installation
To install the package, use npm or yarn:
npm install @microsoft/agents-copilotstudio-clientPrerequisite
To use this library, you will need the following:
- An Agent Created in Microsoft Copilot Studio.
- Ability to Create or Edit an Application Identity in Azure
- (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.
- (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
- Create or open an Agent in Copilot Studio
- Make sure that the Copilot is Published
- Goto Settings => Advanced => Metadata and copy the following values. You will need them later:
- Schema name - this is the 'unique name' of your agent inside this environment.
- 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.Invokedelegated 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.
- In your azure application
- Goto Manage
- Goto API Permissions
- Click Add Permission
- In the side pannel that appears, Click the tab
API's my organization uses - Search for
Power Platform API.- If you do not see
Power Platform APIsee the note at the bottom of this section.
- If you do not see
- For User Interactive Permissions, choose
Delegated Permissions- In the permissions list choose
CopilotStudioand CheckCopilotStudio.Copilots.Invoke - Click
Add Permissions
- In the permissions list choose
- For Service Principal/Confidential Client, choose
Application Permissions- In the permissions list choose
CopilotStudioand CheckCopilotStudio.Copilots.Invoke - Click
Add Permissions - An appropriate administrator must then
Grant Admin consent for copilotsdkbefore the permissions will be available to the application.
- In the permissions list choose
- In the side pannel that appears, Click the tab
- Close Azure Portal
[!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
How-to use
The Copilot Client is configured using the ConnectionSettings class and a jwt token to authenticate to the service.
The ConnectionSettings class can be configured using either instantiating the class or loading the settings from a .env.
Using the default class
There are a few options for configuring the ConnectionSettings class. The following are the most common options:
Using Environment ID and Copilot Studio Agent Schema Name:
const settings: ConnectionSettings = {
environmentId: "your-environment-id",
agentIdentifier: "your-agent-schema-name",
};Using the DirectConnectUrl:
const settings: ConnectionSettings = {
directConnectUrl: "https://direct.connect.url",
};[!NOTE] By default, it's assumed 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
Using the .env file
You can use the loadCopilotStudioConnectionSettingsFromEnv function to load the ConnectionSettings from a .env file.
The following are the most common options:
Using Environment ID and Copilot Studio Agent Schema Name:
environmentId=your-environment-id
agentIdentifier=your-agent-schema-nameUsing the DirectConnectUrl:
directConnectUrl=https://direct.connect.urlExample of how to create a Copilot client
const createClient = async (): Promise<CopilotStudioClient> => {
const settings = loadCopilotStudioConnectionSettingsFromEnv()
const token = await acquireToken(settings)
const copilotClient = new CopilotStudioClient(settings, token)
return copilotClient
}
const copilotClient = await createClient()
const replies = await copilotClient.startConversationAsync(true)
replies.forEach(r => console.log(r.text))