Client serves cached tools after its context exits with an exception
Maintainer thường phản hồi trong vòng 1 ngày
@jonxsong đang làm issue này rồi.
Từ ngày 3/12/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 65/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Issue chỉ đến src/mcp/client/client.py, cụ thể là Client.__aexit__ ở các dòng 480–496. Đọc phương thức đó và kiểm tra kết nối hiện có, rồi tái hiện ví dụ với phản hồi tools/list được lưu vào bộ nhớ đệm và một ngoại lệ bên trong context. Công việc hoàn tất khi các lời gọi sau khi thoát bình thường hoặc do ngoại lệ đều từ chối client đã đóng, đồng thời ngoại lệ ban đầu vẫn được truyền tiếp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Initial Checks
- Reproduced on the latest 2.x release.
- Searched existing issues and pull requests for this behavior.
Release line
2.x (current stable)
Description
After an exception leaves an async with Client(...) block, client.list_tools() can still return a cached tool list even though the connection has closed. The same call after a normal exit raises RuntimeError: Client must be used within an async context manager. Both exits should make subsequent calls reject the closed client.
Client.aexit clears _session after awaiting the exit stack. If the stack propagates an exception, the assignment is skipped and the cache's connection check still passes.
The example prints ['echo'] on both versions below. Remove the line that raises ApplicationError and the final call raises RuntimeError as expected.
Example Code
import anyio
from mcp import Client
from mcp.server import CacheHint, MCPServer
class ApplicationError(Exception):
pass
async def main() -> None:
server = MCPServer("repro", cache_hints={"tools/list": CacheHint(ttl_ms=60_000)})
@server.tool()
def echo() -> str:
return "echo"
client = Client(server, mode="2026-07-28")
try:
async with client:
await client.list_tools()
raise ApplicationError
except* ApplicationError:
pass
print([tool.name for tool in (await client.list_tools()).tools])
anyio.run(main)
Python & MCP Python SDK
Windows, CPython 3.12.4, MCP 2.3.0 and main at 77001c96.
I'd like to work on the fix if maintainers are open to an outside PR. Clearing the session reference even when the exit stack raises would preserve the original exception and let the existing connection guard reject later calls.
Codex helped with implementation. Report written and reviewed by Jon Zhang.
- Ngôn ngữ chính
- Python
- Star
- 25k
- Fork
- 4.2k
- Merge trung bình
- 10 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/python-sdk
-
v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
modelcontextprotocol/python-sdk#3978 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
modelcontextprotocol/python-sdk#3957 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
modelcontextprotocol/python-sdk#3934 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug v1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
modelcontextprotocol/python-sdk#3929 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
DCR `RegistrationHandler` accepts `{"redirect_uris": null}` and returns `201 Created` for `authorization_code` clientsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởbug v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/python-sdk#3885 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/python-sdk
Issue tương tự
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwiseCó thể đã có người làm @abdulwaarith0 đã nhận hôm nay. Đang mởneeds-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
MakazhanAlpamys/Soup#3690 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug data-types native-format
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
ClickHouse/clickhouse-connect#1320 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Public benchmark import end-to-end test fails when the test stack's worker runs the parity jobĐang mởaccepted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
future-agi/future-agi#6392 ·
Maintainer thường phản hồi trong vòng 1 ngày