fix(mcp): defer schema strip to lifespan (asyncio.run regression)#20
Merged
Conversation
v0.7.1 regressed by calling `_strip_internal_schema_fields()` at module import time via `asyncio.run()`. Stdio mode worked because the entry point hadn't started a loop yet, but Uvicorn imports the user app from inside `asyncio.run(self.serve(...))` so the same code path crashed with "asyncio.run() cannot be called from a running event loop" on every cluster pod. The mcp-server deployment in v0.13.1 of the monorepo restart-looped until rolled back. Fix: split `_strip_internal_schema_fields` into an async coroutine and call it from the right place per transport: - HTTP: wrap `mcp_app.lifespan` in a Starlette lifespan that awaits the strip before delegating to FastMCP's own lifespan. - Stdio: call `asyncio.run(_strip_internal_schema_fields())` inside `_run_stdio()` before `mcp.run()` boots its own loop. Pinned by `TestImportFromRunningLoopDoesNotCrash` which reloads the module from inside an async test and asserts `api_token` is gone after lifespan entry. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
v0.7.1 broke the cluster (
mono v0.13.1MCP Server deploy). Root cause:_strip_internal_schema_fields()calledasyncio.run()at module import time. Uvicorn imports the user app from inside its own running loop, so every pod crashed withRuntimeError: asyncio.run() cannot be called from a running event loop.This hotfix moves the strip into the right place per transport:
asyncio.run(_strip_internal_schema_fields())inside_run_stdio()beforemcp.run()boots its own loop.The strip itself is unchanged behaviorally —
api_tokenandmanagedByare still hidden from the schema, just at the right time now.Test plan
uv run pytest)TestImportFromRunningLoopDoesNotCrashreloads the module from inside anasyncio.runand assertsapi_tokenis gone after lifespan entry — this would have failed before the fixpython -c "asyncio.run(import devhelm_mcp.server; lifespan...)"succeedsCuts to v0.7.2 to supersede the broken v0.7.1.
Made with Cursor