Skip to content

Allow Agent instance to supply custom OAuth provider. #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-states-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

Allow agents to customize OAuth client behavior
18 changes: 11 additions & 7 deletions packages/agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
private async _scheduleNextAlarm() {
// Find the next schedule that needs to be executed
const result = this.sql`
SELECT time FROM cf_agents_schedules
SELECT time FROM cf_agents_schedules
WHERE time > ${Math.floor(Date.now() / 1000)}
ORDER BY time ASC
ORDER BY time ASC
LIMIT 1
`;
if (!result) return;
Expand Down Expand Up @@ -986,11 +986,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
oauthClientId?: string;
}
): Promise<{ id: string; authUrl: string | undefined }> {
const authProvider = new DurableObjectOAuthClientProvider(
this.ctx.storage,
this.name,
callbackUrl
);
const authProvider = this.createOAuthProvider(callbackUrl);

if (reconnect) {
authProvider.serverId = reconnect.id;
Expand Down Expand Up @@ -1084,6 +1080,14 @@ export class Agent<Env, State = unknown> extends Server<Env> {

return mcpState;
}

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

/**
Expand Down