Skip to content

Allow Agent instance to supply custom OAuth provider.#298

Merged
threepointone merged 8 commits intocloudflare:mainfrom
jaredhanson:jaredhanson/custom-oauth-client-provider
Feb 9, 2026
Merged

Allow Agent instance to supply custom OAuth provider.#298
threepointone merged 8 commits intocloudflare:mainfrom
jaredhanson:jaredhanson/custom-oauth-client-provider

Conversation

@jaredhanson
Copy link
Contributor

I am implementing agents that use alternative ways to register and authenticate with OAuth authorization servers. These mechanisms are based on early draft OAuth specifications, and will hopefully see more widespread adoption as MCP's use of OAuth matures beyond dynamic client registration (which has many downsides).

To facilitate this, supplying a different AgentsOAuthProvider is necessary. This pull request proposes a createOAuthProvider function on the Agent class, which by default returns the current DurableObjectOAuthClientProvider instance. This function can be overridden by Agent subclasses that need to use an alternative implementation.

An example of a agent that has a custom OAuth provider is as follows:

export class MyChatAgent extends AIChatAgent<Env> {
   // ...

   createOAuthProvider(callbackUrl: string): AgentsOAuthProvider {
      return new MyCustomOAuthProvider(
        this.ctx.storage,
        this.name,
        callbackUrl
      );
   }
}

Let me know if I can supply any additional details. Any and all feedback is welcome! Thanks!

@changeset-bot
Copy link

changeset-bot bot commented May 21, 2025

🦋 Changeset detected

Latest commit: 6af2a3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jaredhanson
Copy link
Contributor Author

Kindly bumping to request review or feedback on this PR.

@jaredhanson
Copy link
Contributor Author

For context here, I'm working on making the underlying OAuthClientProvider interface from @modelcontextprotocol/sdk more extensible, allowing it to implement more secure forms of authentication that don't rely on shared secrets. A PR is in progress here: modelcontextprotocol/typescript-sdk#531

Instantiating such an implementation from within an Agent is driving the requirement for this PR.

@threepointone
Copy link
Contributor

threepointone commented May 29, 2025 via email

@jaredhanson
Copy link
Contributor Author

Support for implementing custom auth has landed in @modelcontextprotocol/sdk v1.15.1 (#720).

Now that it is in a stable release there, it'd be great to take advantage of better authentication mechanisms in Cloudflare Agents. Let me know if there's anything I can do to move this along. Thanks!

@threepointone
Copy link
Contributor

sorry for the delay, I'll have a look at this. We need to update the sdk version first, I'll do that.

threepointone and others added 2 commits February 9, 2026 02:16
Resolve conflicts:
- Take main's >= in schedule query
- Apply createOAuthProvider() to main's refactored addMcpServer
- Keep both createOAuthProvider and main's new OAuth callback methods

Co-authored-by: Cursor <cursoragent@cursor.com>
Tighten lint-staged pattern to target explicit source file extensions (js, mjs, cjs, jsx, ts, mts, cts, tsx, vue, astro, svelte, css) so oxfmt only runs on relevant files. package-lock.json was regenerated to reflect dependency changes after installing/updating packages.
@threepointone
Copy link
Contributor

Coming back to this after very long, sorry for the delay. Yeah we should add this, I'm working on it now, I'll take this PR over.

Introduce Agent.createOAuthProvider(callbackUrl) so subclasses can supply a custom OAuth provider (e.g. pre-registered clients, mTLS, or other auth strategies) instead of the default DurableObjectOAuthClientProvider. Update docs with usage details and the AgentsOAuthProvider contract. Add TestCustomOAuthAgent, expose it in test exports, update worker/wrangler test config, and add unit tests verifying default and overridden provider behavior. Bump changeset for "agents" to minor.
@threepointone
Copy link
Contributor

Added docs and tests and rewrote the changeset, landing this once CI passes.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@298

commit: 6af2a3e

@threepointone threepointone merged commit 27f4e3e into cloudflare:main Feb 9, 2026
4 of 5 checks passed
@github-actions github-actions bot mentioned this pull request Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants