From 3d6526479bd84a6b8cb091953c13fbcf84a1004b Mon Sep 17 00:00:00 2001 From: "open-swe[bot]" Date: Fri, 14 Nov 2025 14:47:12 +0000 Subject: [PATCH] Apply patch [skip ci] --- libs/langchain_v1/langchain/agents/factory.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.py index 8fe7279dcbf..998da6eb10b 100644 --- a/libs/langchain_v1/langchain/agents/factory.py +++ b/libs/langchain_v1/langchain/agents/factory.py @@ -641,6 +641,41 @@ def create_agent( # noqa: PLR0915 another graph as a subgraph node - particularly useful for building multi-agent systems. cache: An optional `BaseCache` instance to enable caching of graph execution. + mcp_session_config: Optional configuration for MCP session management. + + When provided, enables stateful session management for MCP tools, + maintaining persistent connections across multiple tool invocations. + This is essential for tools that require session state, such as + browser automation (Playwright) or database connections. + + The configuration should include: + - `client`: The MCP client instance (e.g., MultiServerMCPClient) + - `server_name`: The name of the MCP server (e.g., 'playwright') + - `auto_cleanup`: Whether to auto-cleanup the session (default: True) + + Example: + ```python + from langchain_mcp_adapters import MultiServerMCPClient + + mcp_client = MultiServerMCPClient() + + # Create agent with stateful MCP session + agent = create_agent( + model="gpt-4", + tools=mcp_tools, + mcp_session_config={ + "client": mcp_client, + "server_name": "playwright", + "auto_cleanup": True, + } + ) + ``` + + !!! note + Without this configuration, MCP tools will use stateless sessions, + creating a new session for each tool call. This can cause issues + with tools that maintain state (e.g., browser sessions closing + between navigation and interaction). Returns: A compiled `StateGraph` that can be used for chat interactions. @@ -1663,3 +1698,4 @@ __all__ = [ +