Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ 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."""
Expand Down Expand Up @@ -158,19 +159,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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also want to stop it here though, so we're not holding on to the port. Agree it's better to have it in the finally as well, but I think we want it both places since we don't need the callback server after the callback is complete.

auth_code = callback_server.wait_for_callback(timeout=300)
return auth_code, callback_server.get_state()

client_metadata_dict = {
"client_name": "Simple Auth Client",
Expand Down Expand Up @@ -217,6 +214,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."""
Expand Down
Loading