Skip to content

feat(assistants): MCP OAuth flow on AuthRequired#2935

Merged
danielkov merged 4 commits into
mainfrom
daniel/age-2437-assistant-runtime-drives-mcp-oauth-flow-on-authrequired
May 20, 2026
Merged

feat(assistants): MCP OAuth flow on AuthRequired#2935
danielkov merged 4 commits into
mainfrom
daniel/age-2437-assistant-runtime-drives-mcp-oauth-flow-on-authrequired

Conversation

@danielkov
Copy link
Copy Markdown
Contributor

@danielkov danielkov commented May 19, 2026

Summary

  • When a configured MCP server returns AuthRequired, the assistant runtime drives RFC 8414 discovery + RFC 7591 dynamic client registration + PKCE authorize URL construction, then surfaces the auth URL to the model so it can relay to the user.
  • Authorization callback consumes the code at the issuer's token endpoint and enqueues an assistant_mcp_auth thread event; the agent reacts by calling mcp_force_reconnect and continuing.
  • OAuth state is a signed JWT carrying the AES-GCM encrypted PKCE verifier so a leaked redirect URL can't defeat PKCE (signing alone is not confidential).
  • System addendum spells out the exact event types and field names the model will observe (assistant_mcp_auth_required / assistant_mcp_auth, AuthURL, MCPServerID, MCPSlug, Status, ErrorDescription) so it doesn't have to infer the protocol.

Why consumeMCPAuthGrant discards the token response

The flow only fires for Gram-hosted MCP URLs (/mcp/{slug}), so metadata.TokenEndpoint resolves to Gram's own OAuth token endpoint, not the upstream MCP provider's. The POST exists to prime the Gram session issuer: by the time the token endpoint returns 2xx, the grant has been recorded and the owner's remote_session for that issuer has been persisted server-side. Capturing the returned bearer in this handler would be redundant — assistants never present a bearer to /mcp/{slug}; the public MCP path resolves the assistant token via the assistant-token special case in server/internal/mcp/serve_platform.go and looks up the owner's stored remote_session for the upstream call (see assistant_resolver_integration_test.go::TestServePublic_AssistantTokenResolvesOwnerRemoteSessionToUpstream).

Closes AGE-2437

✻ Clauded...

danielkov and others added 2 commits May 19, 2026 23:31
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
When a configured MCP server returns AuthRequired the assistant runtime
mints a per-flow OAuth state, drives RFC 8414 discovery + RFC 7591
dynamic client registration + PKCE authorize URL construction, surfaces
the URL to the model so it can relay to the user, and consumes the
authorization code on callback. The OAuth state is a signed JWT carrying
the AES-GCM encrypted PKCE verifier so a leaked redirect URL cannot
defeat PKCE.

Auth completion is delivered to the model as a follow-up turn event
(<message-context> envelope with EventType, MCPServerID, MCPSlug, Status,
Error/ErrorDescription) so the agent can mcp_force_reconnect and
continue. The system addendum names the exact event types and field
names instead of relying on the model to infer them.

Closes AGE-2437.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 19, 2026

AGE-2437

@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
gram-docs-redirect Ready Ready Preview, Comment May 20, 2026 3:16pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 19, 2026

🦋 Changeset detected

Latest commit: 9f6e7fe

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

This PR includes changesets to release 1 package
Name Type
server 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

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f121365d84

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread server/internal/assistants/mcp_auth_handler.go
@github-actions github-actions Bot added the preview Spawn a preview environment label May 19, 2026
@speakeasybot
Copy link
Copy Markdown
Collaborator

speakeasybot commented May 19, 2026

🚀 Preview Environment (PR #2935)

Preview URL: https://pr-2935.dev.getgram.ai

Component Status Details Updated (UTC)
✅ Database Ready Existing database reused 2026-05-20 15:20:53.
✅ Images Available Container images ready 2026-05-20 15:20:32.

Gram Preview Bot

Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
Comment thread agents/runner/src/gram_client.rs Outdated
Comment thread server/internal/assistants/impl.go Outdated
Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
Comment thread server/internal/assistants/mcp_auth_handler.go Outdated
- DCR registers with client_secret_basic; store encrypted secret in flow
  claims and send via HTTP Basic on token exchange.
- Capture full MCP endpoint URL in flow claims so customer-domain MCPs
  are reachable from the callback. Slug derived at use sites.
- Move thread correlation lookup to assistantrepo.ResolveThreadCorrelation.
- Body-close defers wrapped with o11y.NoLogDefer.
- Token-exchange failure now logs at error level.
- Route paths camelCased: /rpc/assistantMcpAuth.{create,/{id}/oauth/callback}.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae748bc7cc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread server/internal/assistants/mcp_auth_handler.go
Comment thread agents/runner/src/runtime.rs Outdated
Auth-required notices sat before persisted chat history, making them
the oldest items in the transcript and the first candidates for
context-trim — exactly the prompt the assistant needs to act on.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@danielkov danielkov requested a review from qstearns May 20, 2026 15:23
@danielkov danielkov added this pull request to the merge queue May 20, 2026
Merged via the queue into main with commit 9aa2fed May 20, 2026
31 checks passed
@danielkov danielkov deleted the daniel/age-2437-assistant-runtime-drives-mcp-oauth-flow-on-authrequired branch May 20, 2026 15:47
@github-actions github-actions Bot locked and limited conversation to collaborators May 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

preview Spawn a preview environment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants