From df4e2f9f151803decbb1555b5278e31a558814b0 Mon Sep 17 00:00:00 2001 From: Laith Weinberger <70768382+laithrw@users.noreply.github.com> Date: Sun, 12 Apr 2026 11:34:39 -0400 Subject: [PATCH] fix: handle BrokenPipeError gracefully when MCP client disconnects --- browser_use/mcp/server.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/browser_use/mcp/server.py b/browser_use/mcp/server.py index bb4e9be7b..d27d1f40c 100644 --- a/browser_use/mcp/server.py +++ b/browser_use/mcp/server.py @@ -1227,18 +1227,21 @@ class BrowserUseServer: raise RuntimeError('MCP stdio transport requires stdin, but this process was launched without one.') async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): - await self.server.run( - read_stream, - write_stream, - InitializationOptions( - server_name='browser-use', - server_version='0.1.0', - capabilities=self.server.get_capabilities( - notification_options=NotificationOptions(), - experimental_capabilities={}, + try: + await self.server.run( + read_stream, + write_stream, + InitializationOptions( + server_name='browser-use', + server_version='0.1.0', + capabilities=self.server.get_capabilities( + notification_options=NotificationOptions(), + experimental_capabilities={}, + ), ), - ), - ) + ) + except BrokenPipeError: + logger.warning('MCP client disconnected while writing to stdio; shutting down server cleanly.') async def main(session_timeout_minutes: int = 10):