Title: Don't block the event loop on sync resource and prompt functions by maxisbey · Pull Request #2380 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Don't block the event loop on sync resource and prompt functions by maxisbey · Pull Request #2380 · modelcontextprotocol/python-sdk
X Title: Don't block the event loop on sync resource and prompt functions by maxisbey · Pull Request #2380 · modelcontextprotocol/python-sdk
Description: Extends the #1909 fix to resources and prompts — sync @mcp.resource and @mcp.prompt handlers now run in worker threads instead of blocking the event loop. Motivation and Context #1909 fixed tools by routing sync functions through anyio.to_thread.run_sync, but the same blocking pattern existed in three other places: FunctionResource.read() ResourceTemplate.create_resource() Prompt.render() All three called self.fn(...) directly and checked inspect.iscoroutine(result) afterward. A blocking sync handler (file I/O, HTTP request, CPU-bound work) would freeze the entire event loop. This applies the same fix: check inspect.iscoroutinefunction(self.fn) up front and dispatch sync functions to a worker thread. Related: #1646, #1839 How Has This Been Tested? Added thread-identity regression tests for each of the three call sites. Each test captures threading.get_ident() inside a sync handler and asserts it differs from the event loop's thread. Verified that pydantic.validate_call (which wraps the stored self.fn in templates and prompts) preserves async-ness — inspect.iscoroutinefunction(validate_call(async_fn)) returns True, so the dispatch check works correctly on the wrapped function. All 39 tests in the affected test files pass; pyright, ruff, and strict-no-cover are clean. Breaking Changes None. Sync handlers that were previously starving the event loop now run concurrently. Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context The previous implementation happened to support callable objects with async __call__ (by checking iscoroutine(result) after calling). That edge case is not preserved here — matching the approach taken in #1909, which relies on _is_async_callable being evaluated at registration time for tools. Resources and prompts have no equivalent pre-computed field; if that edge case matters, it's worth a separate discussion. AI Disclaimer
Open Graph Description: Extends the #1909 fix to resources and prompts — sync @mcp.resource and @mcp.prompt handlers now run in worker threads instead of blocking the event loop. Motivation and Context #1909 fixed tools b...
X Description: Extends the #1909 fix to resources and prompts — sync @mcp.resource and @mcp.prompt handlers now run in worker threads instead of blocking the event loop. Motivation and Context #1909 fixed tools b...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2380
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:8978667e-7778-5015-1f76-69fd92c42560 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C25E:FAAB5:11733B1:174ACDF:6A59B7D8 |
| html-safe-nonce | 15dd87fb8e7119c566fbee6b6b484d53fc77f9861f93f85ffe1e7ef306b5203f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjVFOkZBQUI1OjExNzMzQjE6MTc0QUNERjo2QTU5QjdEOCIsInZpc2l0b3JfaWQiOiI3MTQ0MjI1MTIxMzY0NDU3NDMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e2f70239c4ec0b32b1a07a0bd036c9f7b50e29fdfe38ce0f9dbc26d53331b6ab |
| hovercard-subject-tag | pull_request:3473125399 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/modelcontextprotocol/python-sdk/pull/2380/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | Extends the #1909 fix to resources and prompts — sync @mcp.resource and @mcp.prompt handlers now run in worker threads instead of blocking the event loop. Motivation and Context #1909 fixed tools b... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ba3976babb66479b1c943a8edc0777d96157da48fadc0161f9ddb219deee8353 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ab680789ae4a316cdaf0d5a292a1760140931cc4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width