Skip to content

Issue: 401 Unauthorized Error When Connecting Custom Engine Agent to Copilot Studio #15594

Description

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

  1. ✅ Verified clientId and tenantId are correctly set in Azure App Service Configuration
  2. ✅ Confirmed Managed Identity is assigned to the App Service
  3. ✅ Verified Bot Registration has correct MSI configuration
  4. ✅ Tested that the endpoint is accessible (/api/messages returns 401, which is expected without valid auth)
  5. ✅ Checked that Bot ID in manifest matches the Managed Identity Client ID
  6. ⏳ App Service restart pending to load new environment variables

Questions

  1. Is Managed Identity authentication supported when Copilot Studio calls Custom Engine Agents via Bot Framework?

  2. 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?
  3. 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?
  4. Is there special configuration needed in Copilot Studio when connecting to an agent using Managed Identity vs. App Password?

  5. Are there logs or diagnostics I can check to see exactly why Bot Framework is rejecting the authentication?

Expected Behavior

  1. User interacts with Copilot Studio agent
  2. Agent triggers handoff/escalation action
  3. Copilot Studio calls my Custom Engine Agent at /api/messages
  4. Bot Framework validates the JWT token from Copilot Studio
  5. 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:

  1. Correct configuration for Managed Identity + Copilot Studio integration
  2. Expected authentication flow and token format
  3. Debugging steps to identify the authentication failure
  4. Whether this scenario is supported or if App Password authentication is required

Steps to Reproduce

  1. 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
  2. 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
  3. Create Handoff Topic:

    • Create a topic in Copilot Studio with handoff/escalation action
    • Configure it to transfer conversation to your Custom Engine Agent
  4. 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

Metadata

Metadata

Labels

upstreamThe issue is tracked external dependency

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions