modelcontextprotocol / modelcontextprotocol/python-sdk

Extract OAuth flow logic into reusable components for proxy use cases

Aperta
#1,743 9 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

auth enhancement P1 v2
Lingua principale
Python
Stelle
24.3k
Fork
4k
Merge medio
1g 1h
PR unite (30g)
31

Descrizione

Summary

Refactor OAuth implementation so the flow logic and state machine are usable by server-side proxy services, not just client-side browser flows.

Problem

The SDK's OAuth implementation is designed for local client-side flows (opening a browser locally). The business logic is embedded inside an httpx auth module, making it hard to reuse for other scenarios.

While individual helper functions have been extracted (PKCE utilities, token exchange, discovery), the core state machine that orchestrates the OAuth flow is not reusable. Proxy services that need to perform OAuth on behalf of users currently have to reimplement significant portions of the flow themselves — and when the SDK updates its OAuth logic, those reimplementations can fall out of sync.

Goal

  • Make the OAuth portions of the SDK compatible with proxy/gateway services that currently use custom workarounds
  • When an issue is fixed in the SDK, updating the SDK version should fix it everywhere — no custom OAuth reimplementations needed
  • Keep existing client-side flows working

Design Requirements (from maintainer discussion, Feb 2026)

Modularization into zones: Break the monolithic OAuth flow into modular, overridable pieces:

  • Discovery — obtaining and potentially customizing discovery URLs
  • Client Registration — dynamic client registration
  • Interactive Flow — authorization URL generation, redirect handling
  • Token Fetching — code exchange, refresh, new token extensions (XA, WIF)
  • Token Storage — pluggable storage (already exists)

Key requirements:

  • Each zone should operate as a pure function requiring minimal state
  • Every HTTP request in the flow must be interceptable — allow injection of a custom HTTP client/fetch interface (httpx client in Python, fetch in TypeScript) for custom headers, metrics, response handling
  • Support an "Auth Required" state as an SDK primitive — when a server responds with 401/403 mid-flow, the SDK should capture discovery metadata, scope, and WWW-Authenticate info and surface it so the calling application can handle it (rather than assuming auth happens upfront)
  • The flow must be resumable — a caller should be able to pick up an auth flow at any point (e.g., after a redirect returns on a different machine/request)
  • Support bypassing discovery when configuration is provided directly (important for enterprise environments with broken discovery)
  • Support new token-getting extensions (XA, WIF) that don't require interactive flows

Next steps:

  • Draft code sketches (potentially TypeScript first) to validate the modular function approach
  • Cross-SDK coordination — this applies to both Python and TypeScript SDKs

Related

  • #1240 - Implement OAuth relying on Authlib
  • #2053 - Replace Field(description=...) with docstrings in auth models

AI Disclaimer

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

L’issue non indica file né test. Inizia individuando l’implementazione OAuth esistente e i relativi helper per discovery, registrazione, flusso interattivo, recupero dei token e storage, quindi esamina #1240 e i requisiti dei maintainer. Convalida un design modulare e riprendibile con bozze TypeScript prima di coordinare le modifiche equivalenti in Python; il lavoro sarà completato quando i flussi client esistenti continueranno a funzionare e i casi d’uso con proxy potranno intercettare le richieste e riprendere l’autenticazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python, typescript
Ambito
authentication, backend-api-design
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.