Skip to content

feat: OAuth 2.1 resource-server support (MCP authorization)#20

Open
VinciGit00 wants to merge 1 commit into
mainfrom
feat/oauth-resource-server
Open

feat: OAuth 2.1 resource-server support (MCP authorization)#20
VinciGit00 wants to merge 1 commit into
mainfrom
feat/oauth-resource-server

Conversation

@VinciGit00

@VinciGit00 VinciGit00 commented Jun 24, 2026

Copy link
Copy Markdown
Member

Summary

Adds optional OAuth 2.1 (MCP authorization) so remote MCP clients can sign in through the ScrapeGraphAI web app instead of passing an API key. The server becomes an OAuth resource server; the API-key path remains as a fallback.

Backend counterpart (authorization server + API bearer validation): ScrapeGraphAI/sgai-stack#796

How it works

  • When MCP_OAUTH_AUTH_SERVER is set, the server registers a FastMCP RemoteAuthProvider and exposes protected-resource metadata at /.well-known/oauth-protected-resource.
  • BetterAuthTokenVerifier validates the incoming Bearer token by calling the authorization server's session endpoint (/api/auth/mcp/get-session) — better-auth issues opaque tokens (not JWTs), so validity is confirmed server-to-server.
  • The verified raw token is forwarded to the ScrapeGraphAI API as Authorization: Bearer …, which resolves the user.
  • get_credentials() / make_client() prefer the OAuth bearer and fall back to the legacy SGAI-APIKEY (from X-API-Key header, env, or --config) for stdio / Smithery / existing remote users.

Changes

  • BetterAuthTokenVerifier + _build_auth_provider()
  • get_credentials() / make_client(); all tool handlers now build the client via make_client(ctx)
  • New env vars: MCP_OAUTH_AUTH_SERVER, MCP_PUBLIC_URL, MCP_OAUTH_VERIFY_URL
  • .env.example, render.yaml, and .agent/ docs updated

Testing

Verified end-to-end locally against the better-auth authorization server (apps/web) and the ScrapeGraphAI API (apps/api): login flow (discovery → DCR → authorize → token), token verification, authenticated MCP session, and 16/17 tools working through the full OAuth chain (schema 404s only because that route isn't implemented in the backend). With OAuth disabled the server keeps the existing API-key behaviour.

🤖 Generated with Claude Code

Add optional OAuth 2.1 (MCP authorization) so remote clients can sign in via
the ScrapeGraphAI web app instead of passing an API key. When
MCP_OAUTH_AUTH_SERVER is set, the server runs as an OAuth resource server
(FastMCP RemoteAuthProvider): it verifies opaque better-auth access tokens
against the AS session endpoint and forwards the bearer to the ScrapeGraphAI
API, which resolves the user. Falls back to the existing API-key auth when
OAuth is disabled (stdio / Smithery / legacy remote).

- BetterAuthTokenVerifier + _build_auth_provider() (RemoteAuthProvider)
- get_credentials()/make_client(): bearer-first with API-key fallback
- all tool handlers build their client via make_client(ctx)
- env: MCP_OAUTH_AUTH_SERVER, MCP_PUBLIC_URL, MCP_OAUTH_VERIFY_URL
- update .agent docs, render.yaml and .env.example

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant