modelcontextprotocol / modelcontextprotocol/python-sdk
ClientSession: add public API for updating callbacks after initialization
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 24.3k
- Fork
- 4k
- Merge medio
- 1g 1h
- PR unite (30g)
- 31
Descrizione
Problem
ClientSession accepts callback parameters (list_roots_callback, sampling_callback, elicitation_callback) at initialization, but provides no public API to update them after the session is created.
This means any client that needs to change callbacks at runtime (e.g., updating roots in response to user action) must mutate private attributes like _list_roots_callback directly — which is fragile and couples consumers to implementation details.
Use case
A client connects to a server with initial roots, then the user changes the working directory or project context. The client needs to update its roots callback so that the next roots/list request from the server reflects the new roots. Today this requires:
session._list_roots_callback = new_callback # private attribute
The same issue applies to _sampling_callback and _elicitation_callback.
Proposed solution
Add public setter methods on ClientSession for updating callbacks after initialization. For example:
session.set_list_roots_callback(callback)
session.set_sampling_callback(callback)
session.set_elicitation_callback(callback)
Or alternatively, make the callback attributes public (without the leading underscore).
Context
This came up while fixing PrefectHQ/fastmcp#326 — Client.set_roots() wasn't updating the live session because it only modified pending kwargs. The fix (PrefectHQ/fastmcp#3714) mutates _list_roots_callback directly with a comment noting the fragility. A public API would make this safe and stable.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Individua ClientSession e analizza gli attributi esistenti _list_roots_callback, _sampling_callback e _elicitation_callback e i relativi utilizzi. Esamina i test vicini di ClientSession, quindi aggiungi e testa un modo pubblico per aggiornare ciascun callback, in modo che le modifiche abbiano effetto su una sessione già inizializzata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- api
- Tipo di issue
- Funzionalità
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 70/100