Skip to content

Commit ac1ddda

Browse files
committed
⚡ Optimize simple-auth-client by making wait_for_callback async
What: Modified CallbackServer.wait_for_callback in mcp_simple_auth_client/main.py to be an async function, utilizing await asyncio.sleep(0.1) instead of the blocking time.sleep(0.1). The call in callback_handler was also updated to await this execution. Why: The previous implementation used the blocking time.sleep, which stalled the asyncio event loop and delayed other background coroutines running concurrently, making the application unresponsive during the waiting period. Measured Improvement: Baseline: Max event loop delay was measured at ~0.910s when simulating a 1s wait. After fix: Event loop remained fully responsive (Max delay 0.000s) during the wait period.
1 parent d2290ca commit ac1ddda

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

  • examples/clients/simple-auth-client/mcp_simple_auth_client

‎examples/clients/simple-auth-client/mcp_simple_auth_client/main.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def stop(self):
146146
if self.thread:
147147
self.thread.join(timeout=1)
148148

149-
def wait_for_callback(self, timeout: int = 300):
149+
async def wait_for_callback(self, timeout: int = 300):
150150
"""Wait for OAuth callback with timeout."""
151151
start_time = time.time()
152152
while time.time() - start_time < timeout:
153153
if self.callback_data["authorization_code"]:
154154
return self.callback_data["authorization_code"]
155155
elif self.callback_data["error"]:
156156
raise Exception(f"OAuth error: {self.callback_data['error']}")
157-
time.sleep(0.1)
157+
await asyncio.sleep(0.1)
158158
raise Exception("Timeout waiting for OAuth callback")
159159

160160
@property
@@ -194,7 +194,7 @@ async def callback_handler() -> AuthorizationCodeResult:
194194
"""Wait for OAuth callback and return auth code, state, and iss."""
195195
print("⏳ Waiting for authorization callback...")
196196
try:
197-
auth_code = callback_server.wait_for_callback(timeout=300)
197+
auth_code = await callback_server.wait_for_callback(timeout=300)
198198
return AuthorizationCodeResult(code=auth_code, state=callback_server.state, iss=callback_server.iss)
199199
finally:
200200
callback_server.stop()

0 commit comments

Comments
 (0)