DEV Community

Anton Staykov
Anton Staykov

Posted on

Deploying assistive and autonomous agents on Cloudflare Workers with Microsoft Entra Agent ID

An AI agent running on Cloudflare Workers can use Microsoft Entra Agent ID whether it assists a signed-in person or runs an autonomous job. Give both agents the same task, counting users in a directory, and the important difference is not where the code runs. It is whose permissions the directory should honor.

The Cloudflare deployment sample implements both paths. The assistive agent calls a tool on behalf of the person who asked the question. The autonomous agent can call a tool as its own Entra user, without borrowing the session of whoever started the job. A delegated token identifies a user. That user does not have to be a person at a keyboard.

Cloudflare runs both agents; Entra identifies the caller

A Cloudflare Agent is application code running on Workers with a Durable Object behind the agent instance. The interactive Worker validates the caller's Entra token and routes the conversation to an object named for that user's directory ID. The autonomous Worker routes an operator's POST /run or a scheduled trigger to a named AutonomousAgent. The object keeps recent run records, but the operator's identity does not become the identity of the job.

The sample job asks how many users and groups are in a tenant. Workers AI can select a tool exposed by a Model Context Protocol (MCP) server to answer the question; you can also configure a fixed tool call and leave the model out of the execution path. In either case it is the agent code, not the model, that obtains the access token and attaches it to the request. That order is visible in the job implementation: acquire the token, open the client, discover the tools, then run the job.

Which account should the server authorize? For an employee asking an assistive agent a question, use the employee's delegated access. For an autonomous inventory job, use application permissions if the resource supports them. If the job needs delegated access instead, an agent user gives it a directory user linked to its own agent identity. That last option is the more demanding deployment: both the client and the user in the authorization decision belong to the agent.

The target explains why that distinction matters. Microsoft MCP Server for Enterprise documents delegated permissions and explicitly excludes app-only workflows. Asking for an application token with broader permissions does not turn it into a delegated token. Microsoft describes user-interactive scenarios in the server guide; it does not promise agent-user support for every tool. Read the result here as a test of the autonomous deployment, not as a support statement on behalf of every downstream service.

The deployable unit is the Worker. The accountable caller is a directory principal. Deleting a Worker and withdrawing a principal's permissions are therefore different operations, owned by potentially different teams. The Agent ID object model lets you keep those teams' responsibilities separate without making either one guess who the agent is.

Both Workers authenticate their blueprints with certificates

Each Worker in the sample has its own agent identity blueprint, with one agent identity per blueprint. BLUEPRINT_CLIENT_ID identifies the blueprint that holds the credential; AGENT_IDENTITY_ID identifies the agent whose permissions apply.

As of November 2026, certificate authentication is the most secure option available for an Entra blueprint using Cloudflare Workers alone. Instead of sending a shared client secret, each Worker signs a short-lived assertion with its private key. Entra verifies the signature against the public certificate registered on that blueprint.

Workload identity federation would be more secure here: the Worker would no longer need to hold a long-lived signing key. Entra could trust a short-lived JSON Web Token (JWT) issued by Cloudflare for the running Worker, either a standard workload JWT or a SPIFFE verifiable identity document in JWT format (JWT-SVID). Cloudflare Workers does not currently issue either for its workloads.

The assistive Worker calls the tool on behalf of the signed-in person

An employee asks, “How many users are in this directory?” The token arriving at the interactive Worker has the blueprint as its audience and access_agent as its scope. It establishes that the person may call the agent. It says nothing about whether the agent may read users from the directory. Forwarding that token to the MCP server would ask the server to accept a token meant for someone else.

Instead, the agent uses the person's token in the agent on-behalf-of flow. Entra issues a new token for the MCP server. The agent remains identifiable as the client, the employee remains identifiable as the user, and the token carries the scopes granted for that resource. The diagram shows what crosses each boundary in the sample's assistive flow.

sequenceDiagram
    participant P as Signed-in person
    participant W as Assistive Worker
    participant E as Microsoft Entra
    participant R as MCP server
    P->>W: User token for the blueprint
    W->>E: Signed blueprint assertion for this agent
    E-->>W: Blueprint token for the agent
    W->>E: Agent requests tool access on behalf of the person
    E-->>W: Delegated token for the MCP server
    W->>R: Tool call as agent, for person

The sample's second tool server makes the distinction visible. The same agent can call Microsoft's MCP server and a Cloudflare-hosted echo server within one conversation. The user and agent stay the same; the audience and scopes change. Even a private Cloudflare service binding changes only how the call reaches the echo Worker. A private route is not permission to act for the user.

An autonomous agent can supply its own user context

Move the directory count to an overnight job. Now there is no incoming user token to exchange. If a tool accepts application permissions, the agent can call it as its own application identity. If the tool requires delegated access, running on a schedule does not change that requirement. The autonomous Worker supports both modes; the one that demonstrates the full Entra model uses the agent's own user account.

An agent user is a directory object linked to the agent identity through identityParentId, not a staff account with a password stored in a Worker secret. An agent identity can have at most one such account; at the time of writing, this feature requires a tenant in the Frontier preview. The agent user flow below shows how Entra issues a delegated token for that linked account. Only the last token goes to the MCP server.

sequenceDiagram
    participant W as Autonomous Worker
    participant E as Microsoft Entra
    participant R as MCP server
    W->>E: Signed blueprint assertion, target child agent
    E-->>W: T1, blueprint token for that child
    W->>E: Agent client ID, T1 as client assertion
    E-->>W: T2, agent identity exchange token
    W->>E: T1, T2, agent user ID, resource scope
    E-->>W: Delegated resource token for the agent user
    W->>R: Tool request with delegated token

The two intermediate tokens answer different questions. T1 lets the blueprint act for the agent named in fmi_path. T2 identifies that agent in the user_fic exchange for its linked user. Neither token authorizes a directory read. Only when the agent requests the tool's audience does Entra issue the delegated resource token. The sample's token flows show why no employee token is missing from this sequence: the agent supplies evidence of its own identity, not evidence of an employee's session.

That distinction survives a change of trigger. The autonomous Worker entry point sends both an authorized operator request and a scheduled event to the same job. Replacing the schedule with a button changes who can initiate the work, not whose access the tool evaluates. Permission to start an agent is not permission for that agent to read the directory.

The directory grant tells you what to review

Suppose the employee who requested the directory count changes roles. Their new access should limit what the assistive agent can read for them. It should not decide whether the overnight inventory job still works. In the two delegated tokens, the distinction is visible in oid: the employee in one, the agent user in the other. The token claims also distinguish the client (azp in a version 2 token), the target (aud), and the delegated scopes (scp). A resource can tell which agent called without mistaking it for the user it represents.

For the overnight job, there is a specific delegated permission grant to review. Its clientId identifies the agent's service principal, its resourceId identifies the MCP server's service principal, and its principalId identifies the agent user. That grant allows the client to request particular scopes for this account. It does not expand what the account itself may read. The sample's Entra guide makes the distinction concrete: the MCP scopes and the optional Directory Readers assignment are separate decisions.

The same division helps when someone asks what happened. Entra sign-in and audit logs identify the directory principals and changes to their access. The Cloudflare job record identifies the tool the agent called and its outcome. Neither record substitutes for the other: a token issuance is not a tool invocation, and a successful tool call does not explain who granted access.

The working sample includes both Workers and a deployment guide. The design choice comes before deployment: if access should follow the person asking, use that person's delegated identity. If access belongs to the job, govern the agent's own principal. Cloudflare can run either workload without making the hosting platform the authority over directory access.

References

Top comments (0)