[PR #28621] feat: MCP session reuse per workflow run #32116

Open
opened 2026-02-21 20:50:46 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/28621

State: open
Merged: No


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes: https://github.com/langgenius/dify/issues/24224

Persist MCP client sessions per workflow_execution_id so multi-step flows (navigate → screenshot) reuse the same browser/context, fixing the blank screenshot issue caused by recreating a fresh context on every call.

Background

  • Each MCP tool call previously opened/closed a new session, losing browser state, causing blank screenshots after navigation, and incurring high init latency. McpSessionManager.acquire also had a race that could leak orphaned clients under concurrent acquires.

Fix

  • McpSessionManager/McpSessionRegistry cache clients per workflow run with idle timeout and cleanup.
  • MCPClient/MCPClientWithAuthRetry expose reusable connect().

Tests

  • make lint (pass)
  • make type-check (pass)
  • uv run --project api --dev dev/pytest/pytest_unit_tests.sh (pass; only existing DeprecationWarnings)
  • Manual E2E: Playwright MCP browser_install → browser_navigate → browser_take_screenshot stays in a single session and returns correct page captures.

Note

  • Agent App calls still use the ephemeral MCP path because they don’t carry a workflow_execution_id.
  • To avoid changing that flow in this PR, I left it as-is. If we want session reuse for Agent App as well, I’ll file a follow-up issue/PR.

Screenshots

MCP tool chains keep browser state and avoid re-init cost; Playwright install → navigate → screenshot runs in one mcp-session-id, producing correct captures instead of blank images.

Before After
image image

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/28621 **State:** open **Merged:** No --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary Fixes: https://github.com/langgenius/dify/issues/24224 Persist MCP client sessions per workflow_execution_id so multi-step flows (navigate → screenshot) reuse the same browser/context, fixing the blank screenshot issue caused by recreating a fresh context on every call. ### Background - Each MCP tool call previously opened/closed a new session, losing browser state, causing blank screenshots after navigation, and incurring high init latency. McpSessionManager.acquire also had a race that could leak orphaned clients under concurrent acquires. ### Fix - McpSessionManager/McpSessionRegistry cache clients per workflow run with idle timeout and cleanup. - MCPClient/MCPClientWithAuthRetry expose reusable connect(). ### Tests - make lint (pass) - make type-check (pass) - uv run --project api --dev dev/pytest/pytest_unit_tests.sh (pass; only existing DeprecationWarnings) - Manual E2E: Playwright MCP browser_install → browser_navigate → browser_take_screenshot stays in a single session and returns correct page captures. ### Note - Agent App calls still use the ephemeral MCP path because they don’t carry a workflow_execution_id. - To avoid changing that flow in this PR, I left it as-is. If we want session reuse for Agent App as well, I’ll file a follow-up issue/PR. <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> ## Screenshots MCP tool chains keep browser state and avoid re-init cost; Playwright install → navigate → screenshot runs in one mcp-session-id, producing correct captures instead of blank images. | Before | After | |--------|-------| | <img width="346" height="347" alt="image" src="https://github.com/user-attachments/assets/68ad159c-4530-4ec1-b044-13949453577b" /> | <img width="344" height="355" alt="image" src="https://github.com/user-attachments/assets/5b7c78f7-5cf2-4796-ace2-b8b9ee767af0" />| ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:50:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32116