This sample demonstrates how to use the Copilot Studio Chat API to communicate with a Copilot Studio Agent using Single Sign-On (SSO) authentication.
Caution
This sample uses an experimental API that is not officially supported for production use.
This sample showcases:
- Single sign-on using Microsoft Authentication Library (MSAL)
- Support for streaming responses (ChatGPT style)
- A "Retrieving" indicator when Knowledge is invoked by the agent
- A Copilot Studio agent with "Authenticate with Microsoft" enabled
- Microsoft Entra ID app registration with the appropriate permissions
- Create an App Registration in the Microsoft Entra admin center
- Configure authentication:
- Click on Add a platform
- Select Single-page application (SPA)
- Enter the redirect URI where your index.html will be hosted (e.g.,
https://yourdomain.com/index.htmlorhttp://localhost:8000/index.htmlfor local testing) - Click Configure
- Configure API permissions:
- Navigate to API permission > Add permissions
- Select APIs my organization uses, and search for Power Platform API
- Select Delegated permissions > Copilot Studio > Copilot Studio.Copilots.Invoke permission
- Click Add Permissions
- Grant admin consent for your directory
- Navigate to Overview and record your app registration's client ID and tenant ID
- In Copilot Studio, select your agent
- Navigate to Settings > Advanced
- Under Metadata, locate the Schema name
- Record this values for configuration
- In Copilot Studio, go to your agent's Channels page
- Select either Web app or Native app
- Copy the connection string (next to Microsoft 365 Agents SDK)
- Extract the environment URL from the connection string
For example, if your connection string is:
https://08300adc6f65e2abb02298e1cd5c44.08.environment.api.powerplatform.com/copilotstudio/dataverse-backed/authenticated/bots/cr981_myAgent/conversations?api-version=2022-03-01-preview
Then your environment URL is:
https://08300adc6f65e2abb02298e1cd5c44.08.environment.api.powerplatform.com/
Open index.html and update the following values:
-
In the MSAL configuration section:
- Update
clientIdwith your Microsoft Entra ID app registration client ID - Update
authoritywith your tenant ID (https://login.microsoftonline.com/YOUR_TENANT_ID) - Verify
redirectUrimatches your app's URL
- Update
-
In the bot configuration section:
- Update
botSchemawith your agent's schema name - Update
environmentEndpointURLwith your environment URL
- Update
const msalInstance = new msal.PublicClientApplication({
auth: {
clientId: "YOUR_CLIENT_ID",
authority: "https://login.microsoftonline.com/YOUR_TENANT_ID",
redirectUri: window.location.origin
},
// ...
});
// ...
const strategy = new window.CopilotStudioDirectToEngineChatAdapter.ThirdPartyPublishedBotStrategy({
botSchema: 'YOUR_SCHEMA_NAME',
environmentEndpointURL: new URL('YOUR_ENVIRONMENT_URL'),
getToken: () => token,
transport: 'auto'
});- Host the sample files on a web server or use a local development server
- Open the application in a web browser
- You will be prompted to sign in with Microsoft credentials
- After successful authentication, the chat interface will connect to your Copilot Studio agent
- Start chatting with your agent!