Skip to content

fix: support isolated workflow MCP calls#22087

Open
fengjikui wants to merge 1 commit into
run-llama:mainfrom
fengjikui:codex/workflow-as-mcp-factory
Open

fix: support isolated workflow MCP calls#22087
fengjikui wants to merge 1 commit into
run-llama:mainfrom
fengjikui:codex/workflow-as-mcp-factory

Conversation

@fengjikui

@fengjikui fengjikui commented Jun 22, 2026

Copy link
Copy Markdown

Fixes #22071.

Summary

  • add an optional workflow_factory parameter to workflow_as_mcp
  • keep the existing workflow=... behavior compatible: a supplied workflow instance is still reused
  • use the factory path to create a fresh workflow instance for every MCP tool call
  • document the factory form for workflows that keep request-specific mutable state on self
  • add regression coverage showing two MCP calls do not share workflow instance state when a factory is used

Root cause

workflow_as_mcp closes over one Workflow instance and calls workflow.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 -q
  • 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 pytest tests -q
  • uv run --python 3.12 --frozen ruff format --check llama_index/tools/mcp/utils.py tests/test_workflow_as_mcp.py
  • uv run --python 3.12 --frozen ruff check .
  • git diff --check

Note: 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:7890 and the environment lacked socksio. 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 sets python_version = 3.8 while mcp.client.session uses 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.

@fengjikui fengjikui marked this pull request as ready for review June 22, 2026 15:22
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: workflow_as_mcp captures a single Workflow instance, sharing self.* state across every MCP client

1 participant