modelcontextprotocol / modelcontextprotocol/python-sdk

ClientSession: add public API for updating callbacks after initialization

未關閉 適合新手
#2,379 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

enhancement needs decision P3
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

找到 ClientSession,檢查現有的 _list_roots_callback、_sampling_callback 和 _elicitation_callback 屬性及其使用方式。檢視 ClientSession 附近的測試,然後新增並測試一種公開方式來更新每個 callback,使變更影響已初始化的 session。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
功能
難度
2/5
預估耗時
1-3 小時
活躍度
活躍
描述清晰度
基本清楚
新手友好度
70/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。