modelcontextprotocol / modelcontextprotocol/python-sdk

ClientSession: add public API for updating callbacks after initialization

Offen Anfängerfreundlich
#2,379 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

enhancement needs decision P3
Vorherrschende Sprache
Python
Sterne
24.3k
Forks
4k
Ø Merge
1 T. 1 Std.
Gemergte PRs (30 T.)
31

Beschreibung

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#326Client.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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lokalisieren Sie ClientSession und untersuchen Sie die vorhandenen Attribute _list_roots_callback, _sampling_callback und _elicitation_callback sowie deren Verwendungen. Sehen Sie sich die Tests für die nahegelegene ClientSession an und fügen Sie anschließend eine öffentliche Möglichkeit hinzu und testen Sie diese, um jeden Callback zu aktualisieren, sodass Änderungen eine bereits initialisierte Sitzung beeinflussen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
api
Issue-Typ
Feature
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
70/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.