From d952236369a6c03848041f323e0ee0b8983b624c Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Fri, 5 Sep 2025 20:50:04 +0530 Subject: [PATCH 1/2] fix(examples): Prevent resource leak in simple-auth-client CallbackServer --- .../mcp_simple_auth_client/main.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py index 19d6dcef8..142b7ab1f 100644 --- a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py +++ b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py @@ -130,6 +130,8 @@ def stop(self): self.server.server_close() if self.thread: self.thread.join(timeout=1) + print("🛑 Stopped callback server.") + def wait_for_callback(self, timeout=300): """Wait for OAuth callback with timeout.""" @@ -158,19 +160,15 @@ def __init__(self, server_url: str, transport_type: str = "streamable_http"): async def connect(self): """Connect to the MCP server.""" print(f"🔗 Attempting to connect to {self.server_url}...") - + callback_server = CallbackServer(port=3030) try: - callback_server = CallbackServer(port=3030) callback_server.start() async def callback_handler() -> tuple[str, str | None]: """Wait for OAuth callback and return auth code and state.""" print("⏳ Waiting for authorization callback...") - try: - auth_code = callback_server.wait_for_callback(timeout=300) - return auth_code, callback_server.get_state() - finally: - callback_server.stop() + auth_code = callback_server.wait_for_callback(timeout=300) + return auth_code, callback_server.get_state() client_metadata_dict = { "client_name": "Simple Auth Client", @@ -217,6 +215,8 @@ async def _default_redirect_handler(authorization_url: str) -> None: import traceback traceback.print_exc() + finally: + callback_server.stop() async def _run_session(self, read_stream, write_stream, get_session_id): """Run the MCP session with the given streams.""" From 4b5338c2c5b8b4968c6c828a8562cdebc72b21b9 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind Date: Fri, 5 Sep 2025 20:59:53 +0530 Subject: [PATCH 2/2] fix pre-commit issue --- .../clients/simple-auth-client/mcp_simple_auth_client/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py index 142b7ab1f..3b5566a2a 100644 --- a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py +++ b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py @@ -132,7 +132,6 @@ def stop(self): self.thread.join(timeout=1) print("🛑 Stopped callback server.") - def wait_for_callback(self, timeout=300): """Wait for OAuth callback with timeout.""" start_time = time.time()