feat: add public setter methods for ClientSession callbacks#2407
Open
dgenio wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
feat: add public setter methods for ClientSession callbacks#2407dgenio wants to merge 1 commit intomodelcontextprotocol:mainfrom
dgenio wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Add set_sampling_callback(), set_elicitation_callback(), and set_list_roots_callback() methods to ClientSession, allowing callbacks to be updated at runtime after initialization without mutating private attributes directly. Also removes the # pragma: no cover from _default_elicitation_callback and adds coverage via the new test for set_elicitation_callback(None). Reported-by: dgenio Github-Issue: modelcontextprotocol#2379
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
src/mcp/client/session.py: added three public methods toClientSession:set_sampling_callback(callback)set_elicitation_callback(callback)set_list_roots_callback(callback)# pragma: no coverfrom_default_elicitation_callback(now covered by test)tests/client/test_list_roots_callback.py: addedtest_set_list_roots_callbacktests/client/test_sampling_callback.py: addedtest_set_sampling_callbacktests/client/test_elicitation_callback.py: new file withtest_set_elicitation_callbackWhy
ClientSessionaccepts callback parameters at initialization but provided no public API to update them at runtime. Any client needing to change callbacks (e.g., updating roots after a user changes working directory) was forced to mutate private attributes like_list_roots_callbackdirectly — coupling consumers to implementation details. This was the pattern used in PrefectHQ/fastmcp#3714.Each setter accepts
callback | None. PassingNonerestores the default behaviour (returning an error to the server), consistent with the__init__fallback pattern. The docstrings note that capabilities are advertised duringinitialize()and won't be re-negotiated by calling these setters.How verified
Tradeoffs / risks
initialize(), the server won't know about the capability until a new session is created. This is documented in each setter's docstring.logging_callback/message_handler: not included — the issue only names the three callbacks above. Can be added as a follow-up if desired.strict-no-covercannot run on Windows; CI will verify on Ubuntu.Scope notes
Changes are strictly limited to the three callbacks named in #2379. No refactoring of existing code.