Title: ClientSession.call_tool issues a tools/list after every tools/call when the output-schema cache is empty — no opt-out; doubles round-trips on per-call sessions · Issue #3513 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: ClientSession.call_tool issues a tools/list after every tools/call when the output-schema cache is empty — no opt-out; doubles round-trips on per-call sessions · Issue #3513 · modelcontextprotocol/python-sdk
X Title: ClientSession.call_tool issues a tools/list after every tools/call when the output-schema cache is empty — no opt-out; doubles round-trips on per-call sessions · Issue #3513 · modelcontextprotocol/python-sdk
Description: Summary ClientSession.call_tool() issues a tools/list request after every successful tools/call whenever the called tool is not in the session's output-schema cache. On a short-lived session — one ClientSession per tools/call, the patter...
Open Graph Description: Summary ClientSession.call_tool() issues a tools/list request after every successful tools/call whenever the called tool is not in the session's output-schema cache. On a short-lived session — one ...
X Description: Summary ClientSession.call_tool() issues a tools/list request after every successful tools/call whenever the called tool is not in the session's output-schema cache. On a short-lived session — ...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3513
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ClientSession.call_tool issues a tools/list after every tools/call when the output-schema cache is empty — no opt-out; doubles round-trips on per-call sessions","articleBody":"### Summary\n\n`ClientSession.call_tool()` issues a **`tools/list` request after every successful `tools/call`** whenever the called tool is not in the session's output-schema cache. On a short-lived session — one `ClientSession` per `tools/call`, the pattern gateways and proxies use when the downstream caller is stateless — the cache is empty on every call, so every `call_tool` costs two round-trips instead of one (`initialize` + `notifications/initialized` + `tools/call` + **`tools/list`** = 4 POSTs on Streamable HTTP instead of 3).\n\nWhen the server behind the session is itself an aggregator whose `tools/list` fans out to N backends, the extra request is N upstream calls, and the slowest backend's `tools/list` latency is added to *every* `call_tool` — including calls to tools that declare no `outputSchema` and have nothing to validate.\n\nThere is no way to opt out short of subclassing `ClientSession` or patching the method.\n\n### Where\n\n`mcp` 1.29.1, `src/mcp/client/session.py`:\n\n```python\n# :386-415\nasync def call_tool(self, name, arguments=None, read_timeout_seconds=None, progress_callback=None, *, meta=None):\n ...\n result = await self.send_request(...)\n if not result.isError:\n await self._validate_tool_result(name, result)\n return result\n\n# :417-421\nasync def _validate_tool_result(self, name: str, result: types.CallToolResult) -> None:\n \"\"\"Validate the structured content of a tool result against its output schema.\"\"\"\n if name not in self._tool_output_schemas:\n # refresh output schema cache\n await self.list_tools()\n ...\n```\n\nStill present on `main` @ `6affe5c0` (2026-09-16) as the public `validate_tool_result`, `:1118-1127` — the cache is populated only by `list_tools()` (`_absorb_tool_listing`), and `_tool_output_schemas` is per-`ClientSession`, so a fresh session always pays the refresh.\n\n### Reproduction\n\n```python\nimport anyio\nfrom mcp import ClientSession\nfrom mcp.client.streamable_http import streamablehttp_client\n\nasync def main():\n async with streamablehttp_client(\"http://127.0.0.1:8000/mcp\") as (r, w, _):\n async with ClientSession(r, w) as s:\n await s.initialize()\n await s.call_tool(\"echo\", {\"text\": \"hi\"}) # server access log: POST initialize, POST initialized, POST tools/call, POST tools/list\n\nanyio.run(main)\n```\n\nAny FastMCP server with a tool that returns unstructured content shows the fourth POST. Measured against a proxy whose `tools/list` fans out to six backends: `call_tool` wall time p50 ≈ 3 s / p99 ≈ 36 s through the proxy vs p99 0.24 s calling the backend directly — the gap is entirely the post-result `tools/list` waiting on the slowest backend.\n\n### Proposed change (any of these would do)\n\n1. **A constructor opt-out**, e.g. `ClientSession(..., validate_tool_results: bool = True)`; when `False`, `call_tool` returns the result without calling `validate_tool_result`. Callers that already validate structured output elsewhere (a gateway with its own schema plugin, a server that validates before responding) can turn the client-side re-validation off.\n2. **Do not refresh on an empty cache.** If the session has never listed tools, `validate_tool_result` cannot know whether the tool has an `outputSchema`; today it spends a round-trip to find out. Skipping the refresh when `not self._tool_output_schemas` (and keeping it when the cache is populated but lacks the tool — a tool added since the last listing) removes the cost on per-call sessions while leaving long-lived sessions unchanged. A DEBUG log line on the skip keeps it observable.\n3. **Validate only when the result carries `structuredContent`.** A result with no `structuredContent` from a tool with no cached schema has nothing to check; the refresh then only serves to raise `RuntimeError(\"… has an output schema but did not return structured content\")` for a tool the client never listed — a stricter contract than the server side enforces.\n\nOption 2 is what we are running as a build-time patch on a vendored 1.29.1 (one three-line hunk in `_validate_tool_result`); happy to open a PR for whichever shape the maintainers prefer.\n\n### Environment\n\n- `mcp` 1.29.1 (Python 3.12); also reproduces on `main` @ `6affe5c0`\n- Transport: Streamable HTTP, stateless server (no `Mcp-Session-Id`), one `ClientSession` per call\n","author":{"url":"https://github.com/godwinbw-egm","@type":"Person","name":"godwinbw-egm"},"datePublished":"2026-09-16T21:03:09Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3513"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:469ca47f-405c-33a3-5552-62144c0dddfd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8CA8:65478:136D6F9:19D3759:6B0123A4 |
| html-safe-nonce | 17f9c3558bf27c3a45220d1896f86ab9f3dfcf84af8a4f197ab3760dec7e0d2f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Q0E4OjY1NDc4OjEzNkQ2Rjk6MTlEMzc1OTo2QjAxMjNBNCIsInZpc2l0b3JfaWQiOiIzMzcwNzQ5Mzc0MzE2NDg3NTg4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c897e2fa25e0d574d8920cb41247c3711c55a300c14eaaf80c6bb43e5eaa9960 |
| hovercard-subject-tag | issue:5479896193 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3513/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a963533547cec64570ee39e7d23c65a65b9538fad1de05fd2f7c252509934901/modelcontextprotocol/python-sdk/issues/3513 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a963533547cec64570ee39e7d23c65a65b9538fad1de05fd2f7c252509934901/modelcontextprotocol/python-sdk/issues/3513 |
| og:image:alt | Summary ClientSession.call_tool() issues a tools/list request after every successful tools/call whenever the called tool is not in the session's output-schema cache. On a short-lived session — one ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | godwinbw-egm |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width