fix: support isolated workflow MCP calls#22087
Open
fengjikui wants to merge 1 commit into
Open
Conversation
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.
Fixes #22071.
Summary
workflow_factoryparameter toworkflow_as_mcpworkflow=...behavior compatible: a supplied workflow instance is still reusedselfRoot cause
workflow_as_mcpcloses over oneWorkflowinstance and callsworkflow.run(...)for every MCP request.Workflow.run()creates a fresh context per call, but user-defined mutable fields on the workflow object itself still persist across callers. A factory gives operators an explicit isolation path without trying to deepcopy arbitrary workflow objects that may hold non-copyable clients or resources.Validation
uv run --python 3.12 --frozen pytest tests/test_workflow_as_mcp.py -qenv -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy uv run --python 3.12 --frozen pytest tests -quv run --python 3.12 --frozen ruff format --check llama_index/tools/mcp/utils.py tests/test_workflow_as_mcp.pyuv run --python 3.12 --frozen ruff check .git diff --checkNote: running the full package tests without clearing local proxy environment variables failed before test execution for many existing tests because httpx picked up
socks5://127.0.0.1:7890and the environment lackedsocksio. Clearing those proxy variables made the package tests pass:52 passed, 2 warnings.Also tried
env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy uv run --python 3.12 --frozen mypy llama_index/tools/mcp/utils.py, but mypy stopped in dependency code because this package config setspython_version = 3.8whilemcp.client.sessionuses pattern matching syntax.AI assistance was used to inspect the issue/code path and draft the focused test and patch; I reviewed the diff and validated it locally with the commands above.