This is a sample to show how to use the @microsoft/agents-copilotstudio-client package to talk to an Agent hosted in CopilotStudio using WebChat.
- An Agent Created in Microsoft Copilot Studio or access to an existing Agent.
- Ability to configure a local web server to serve the files in the
webfolder. If you are using VSCode you can install the extension Live Server - Ability to Create an Application Identity in Azure for a Public Client/Native App Registration Or access to an existing Public Client/Native App registration with the
CopilotStudio.Copilots.InvokeAPI Permission assigned.
The Copilot Studio Client requires a User Token to operate. For this sample, we are using a user interactive flow to get the user token for the application ID created above. Other flows are allowed.
- Create an Agent in Copilot Studio
- Publish your newly created Copilot
- Goto Settings => Advanced => Metadata and copy the following values, You will need them later:
- Schema name
- Environment Id
This step will require permissions to create application identities in your Azure tenant. For this sample, you will create a Client Application Identity, which does not have secrets.
- Open https://portal.azure.com
- Navigate to Entra Id
- Create a new App Registration in Entra ID
- Provide a Name
- Choose "Accounts in this organization directory only"
- In the "Select a Platform" list, Choose "Single-page Application"
- In the Redirect URI url box, type in the URL you will use to serve this application, for local development use
http://localhost:5500(note: use the port used by your local web server) - Then click register.
- In your newly created application
- On the Overview page, Note down for use later when configuring the example application:
- The Application (client) ID
- The Directory (tenant) ID
- Go to API Permissions in
Managesection - Click Add Permission
- In the side panel 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
- In the Delegated permissions list, choose
CopilotStudioand CheckCopilotStudio.Copilots.Invoke - Click
Add Permissions
- In the side panel that appears, Click the tab
- (Optional) Click
Grant Admin consent for copilotsdk
- On the Overview page, Note down for use later when configuring the example application:
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
With the above information, you can now configure the web client in the web folder.
- Open the
settings.TEMPLATE.jsfile and rename it tosettings.js. - Configure the values based on what was recorded during the setup phase.
environmentId="" # Environment ID of environment with the CopilotStudio App.
schemaName="" # Schema Name of the Copilot to use
tenantId="" # Tenant ID of the App Registration used to login, this should be in the same tenant as the Copilot.
appClientId="" # App ID of the App Registration used to login, this should be in the same tenant as the CopilotStudio environment.
# Alternatively, you can provide a direct URL to connect to Copilot Studio instead of specifying the `environmentId` and `schemaName` values:
directConnectUrl="" # The URL to connect to the Copilot Studio service. If set, overrides `environmentId` and `schemaName`.This sample lets you configure the following settings in the .env file:
authorityEndpoint="" # The login authority to use for the connection. Default: "https://login.microsoftonline.com".
cloud="" # The cloud hosting the Power Platform Services. Default: "Prod".
customPowerPlatformCloud="" # The Power Platform API endpoint when cloud is set to "Other".
copilotAgentType="" # The type of Copilot Studio Agent (Published or Prebuilt). Default: "Published".
useExperimentalEndpoint="" # The flag to use the URL provided via the "x-ms-d2e-experimental" header for subsequent calls to the Copilot Studio service.-
Use a web server to serve the files in the
webfolder. If you are using Live Server, right clickindex.htmland selectOpen with Live Server -
Open a browser and navigate to the URL exposed by Live Server, by default it should be
http://localhost:5500. Make sure it's using the same port as the one used in your App Registration. -
You might need to allow pop-up window in your browser
-
In the pop-up window, authenticate with your user credentials.
-
You should see a Web Page with WebChat component to talk with your agent.