langchain-ai/deepagents · error · RuntimeError

Cannot configure a closed MCP session manager

Error message

Cannot configure a closed MCP session manager

What it means

MCPSessionManager.configure() raises RuntimeError if the manager has been closed (cleanup ran). After close, the manager no longer accepts new connection configs.

Source

Thrown at libs/code/deepagents_code/mcp_tools.py:670

    def configure(self, connections: dict[str, Connection]) -> None:
        """Set or validate the connection configs used by this manager.

        When no sessions exist yet, `connections` overwrites the stored
        configs unconditionally. Once any session has been created, the
        new `connections` must produce the same signature as the stored
        ones — otherwise this raises to prevent rebinding live sessions
        to different transports or auth providers.

        Args:
            connections: Connection configs keyed by server name.

        Raises:
            RuntimeError: If the manager is closed or reconfigured
                incompatibly after sessions already exist.
        """
        if self._closed:
            msg = "Cannot configure a closed MCP session manager"
            raise RuntimeError(msg)

        if not self._entries:
            self._connections = dict(connections)
            return

        if _connections_signature(self._connections) != _connections_signature(
            connections,
        ):
            msg = "Cannot reconfigure MCP session manager after sessions are active"
            raise RuntimeError(msg)
        self._connections = dict(connections)

    async def get_session(self, server_name: str) -> ClientSession:
        """Return a cached session for `server_name`, creating it lazily."""
        entry = self._entries.get(server_name)
        if entry is not None:
            return entry.session

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Reorder code so configure() is called before close()/cleanup
  2. Create a fresh MCPSessionManager instance if reconfiguration is needed after close
  3. Guard the configure call with a check that the manager is not closed

Example fix

// before
await manager.close()
await manager.configure(connections)  # raises
// after
await manager.configure(connections)
await manager.close()
Defensive patterns

Strategy: try-catch

Validate before calling

if manager.is_closed():  # or track _closed yourself
    manager = MCPSessionManager()

Try / catch

try:
    await manager.configure(connections)
except RuntimeError as e:
    if 'closed' in str(e):
        manager = MCPSessionManager()
        await manager.configure(connections)
    else:
        raise

Prevention

When it happens

Trigger: Calling configure() on a manager after close()/cleanup; reconfiguring inside shutdown paths such as _fail_closed_disable_tracing after cleanup already ran.

Common situations: Long-lived app where an atexit/finally handler closed the manager and later code (e.g. LangSmith secret-redaction config) tries to reconfigure; tests reusing a closed fixture.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/55a077450357b84c. Report an issue: GitHub.