Description
I'm experiencing a 401 authentication error when trying to connect a Custom Engine Agent (M365 SDK Agent) to Copilot Studio for conversation handoff. The connector in Copilot Studio is successfully calling the agent endpoint, but Bot Framework authentication is failing.
Environment
- Agent Type: Custom Engine Agent (M365 SDK)
- Bot Framework SDK:
@microsoft/agents-hosting (latest)
- Deployment: Azure App Service
- Authentication: Managed Identity (User-assigned)
- Node.js Version: 24.x
Setup Details
Azure Configuration
Managed Identity:
- Type: User-assigned Managed Identity
- Client ID:
<Client ID>
- Tenant ID:
<Tenant ID>
- Object ID:
<Object ID>
Azure App Service Settings:
{
"clientId": "<Client ID>",
"tenantId": "<Tenant ID>"
}
Bot Registration:
- Bot Type: Azure Bot (registered via Bicep)
- MSA App Type:
UserAssignedMSI
- Endpoint:
https://<appservice-name>.azurewebsites.net/api/messages
Code Implementation
Backend (index.ts):
import { authorizeJWT, CloudAdapter, getAuthConfigWithDefaults, Request } from "@microsoft/agents-hosting";
const server = express();
const adapter = agentApp.adapter as CloudAdapter;
const authConfig = getAuthConfigWithDefaults();
server.post(
"/api/messages",
authorizeJWT(authConfig),
(req: Request, res: Response) => adapter.process(req, res, (context) => agentApp.run(context))
);
Manifest (manifest.json):
{
"copilotAgents": {
"customEngineAgents": [
{
"type": "bot",
"id": "${{BOT_ID}}"
}
]
},
"bots": [
{
"botId": "${{BOT_ID}}",
"scopes": ["copilot", "personal", "team"]
}
]
}
Problem
When Copilot Studio attempts to invoke the agent through the configured connector, the request fails with:
connectorRequestFailure
The connector returned an HTTP error with code 401.
What I've Tried
- ✅ Verified
clientId and tenantId are correctly set in Azure App Service Configuration
- ✅ Confirmed Managed Identity is assigned to the App Service
- ✅ Verified Bot Registration has correct MSI configuration
- ✅ Tested that the endpoint is accessible (
/api/messages returns 401, which is expected without valid auth)
- ✅ Checked that Bot ID in manifest matches the Managed Identity Client ID
- ⏳ App Service restart pending to load new environment variables
Questions
-
Is Managed Identity authentication supported when Copilot Studio calls Custom Engine Agents via Bot Framework?
-
What authentication mechanism does Copilot Studio use when calling the /api/messages endpoint?
- Does it send a Bot Framework JWT token?
- What should be in the
audience claim of the token?
- Should it match
clientId or something else?
-
How should getAuthConfigWithDefaults() be configured for Managed Identity authentication?
- Does it automatically read
clientId and tenantId from environment variables?
- Are there additional settings needed for Copilot Studio integration?
-
Is there special configuration needed in Copilot Studio when connecting to an agent using Managed Identity vs. App Password?
-
Are there logs or diagnostics I can check to see exactly why Bot Framework is rejecting the authentication?
Expected Behavior
- User interacts with Copilot Studio agent
- Agent triggers handoff/escalation action
- Copilot Studio calls my Custom Engine Agent at
/api/messages
- Bot Framework validates the JWT token from Copilot Studio
- My agent receives the message and processes handoff
Actual Behavior
Step 4 fails with 401 Unauthorized error from authorizeJWT(authConfig) middleware.
Documentation Gaps
I couldn't find clear documentation on:
- How to configure Managed Identity authentication for Custom Engine Agents called by Copilot Studio
- What the authentication flow looks like between Copilot Studio and Custom Engine Agents
- Whether Copilot Studio uses Bot Framework protocol or a different mechanism
- How to debug Bot Framework authentication issues in this scenario
Request
Please provide guidance on:
- Correct configuration for Managed Identity + Copilot Studio integration
- Expected authentication flow and token format
- Debugging steps to identify the authentication failure
- Whether this scenario is supported or if App Password authentication is required
Steps to Reproduce
-
Create a Custom Engine Agent:
- Install Microsoft 365 Agents Toolkit extension in VS Code
- Create a new Custom Engine Agent (M365 SDK Agent) project
- Deploy the agent to Azure App Service with Managed Identity authentication
- Ensure Bot registration is configured with
UserAssignedMSI type
-
Configure Copilot Studio:
- Open Copilot Studio and create/open an agent
- Go to the Agents tab
- Select Microsoft SDK option
- Configure the connection:
- Authentication: None
- Endpoint:
https://your-app.azurewebsites.net/api/messages
-
Create Handoff Topic:
- Create a topic in Copilot Studio with handoff/escalation action
- Configure it to transfer conversation to your Custom Engine Agent
-
Test the Handoff:
- Start a conversation in Copilot Studio
- Trigger the handoff action
- Observe the 401 authentication error
Related Links
Environment: Custom Engine Agent created using Microsoft 365 Agents Toolkit extension in VS Code
Description
I'm experiencing a 401 authentication error when trying to connect a Custom Engine Agent (M365 SDK Agent) to Copilot Studio for conversation handoff. The connector in Copilot Studio is successfully calling the agent endpoint, but Bot Framework authentication is failing.
Environment
@microsoft/agents-hosting(latest)Setup Details
Azure Configuration
Managed Identity:
<Client ID><Tenant ID><Object ID>Azure App Service Settings:
Bot Registration:
UserAssignedMSIhttps://<appservice-name>.azurewebsites.net/api/messagesCode Implementation
Backend (index.ts):
Manifest (manifest.json):
{ "copilotAgents": { "customEngineAgents": [ { "type": "bot", "id": "${{BOT_ID}}" } ] }, "bots": [ { "botId": "${{BOT_ID}}", "scopes": ["copilot", "personal", "team"] } ] }Problem
When Copilot Studio attempts to invoke the agent through the configured connector, the request fails with:
What I've Tried
clientIdandtenantIdare correctly set in Azure App Service Configuration/api/messagesreturns 401, which is expected without valid auth)Questions
Is Managed Identity authentication supported when Copilot Studio calls Custom Engine Agents via Bot Framework?
What authentication mechanism does Copilot Studio use when calling the
/api/messagesendpoint?audienceclaim of the token?clientIdor something else?How should
getAuthConfigWithDefaults()be configured for Managed Identity authentication?clientIdandtenantIdfrom environment variables?Is there special configuration needed in Copilot Studio when connecting to an agent using Managed Identity vs. App Password?
Are there logs or diagnostics I can check to see exactly why Bot Framework is rejecting the authentication?
Expected Behavior
/api/messagesActual Behavior
Step 4 fails with 401 Unauthorized error from
authorizeJWT(authConfig)middleware.Documentation Gaps
I couldn't find clear documentation on:
Request
Please provide guidance on:
Steps to Reproduce
Create a Custom Engine Agent:
UserAssignedMSItypeConfigure Copilot Studio:
https://your-app.azurewebsites.net/api/messagesCreate Handoff Topic:
Test the Handoff:
Related Links
Environment: Custom Engine Agent created using Microsoft 365 Agents Toolkit extension in VS Code