Skip to content
Draft
Show file tree
Hide file tree
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
32 changes: 28 additions & 4 deletions connector_builder_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"""

import asyncio
import signal
import sys

import anyio
from fastmcp import FastMCP

from connector_builder_mcp._util import initialize_logging
Expand All @@ -22,13 +24,35 @@
def main() -> None:
"""Main entry point for the Builder MCP server."""
print("Starting Builder MCP server.", file=sys.stderr)

async def run_server():
"""Run the server with proper signal handling."""
shutdown_event = asyncio.Event()

def signal_handler():
print("Builder MCP server interrupted by user.", file=sys.stderr)
shutdown_event.set()

if sys.platform != "win32":
loop = asyncio.get_running_loop()
for sig in (signal.SIGTERM, signal.SIGINT):
loop.add_signal_handler(sig, signal_handler)
Comment on lines +36 to +39
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

Signal handlers are registered but never cleaned up. If the function is called multiple times or the event loop is reused, this could lead to duplicate signal handlers. Consider adding signal handler cleanup in a finally block or using a context manager approach.

Copilot uses AI. Check for mistakes.


try:
async with anyio.create_task_group() as tg:
tg.start_soon(app.run_stdio_async)
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

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

There's a potential race condition where shutdown_event.wait() could complete before app.run_stdio_async() is fully started by the task group. Consider using tg.start_task() instead of tg.start_soon() to ensure the task is properly started before waiting for shutdown.

Suggested change
tg.start_soon(app.run_stdio_async)
tg.start_task(app.run_stdio_async)

Copilot uses AI. Check for mistakes.

await shutdown_event.wait()
tg.cancel_scope.cancel()
except anyio.get_cancelled_exc_class():
pass
except Exception as ex:
print(f"Error running Builder MCP server: {ex}", file=sys.stderr)
sys.exit(1)

try:
asyncio.run(app.run_stdio_async())
asyncio.run(run_server())
except KeyboardInterrupt:
print("Builder MCP server interrupted by user.", file=sys.stderr)
except Exception as ex:
print(f"Error running Builder MCP server: {ex}", file=sys.stderr)
sys.exit(1)

print("Builder MCP server stopped.", file=sys.stderr)
sys.exit(0)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"airbyte-cdk>=6.61.6,<7.0",
"anyio>=4.0.0,<5.0",
"fastmcp>=2.12.1,<3.0",
"jsonschema>=4.0.0,<5.0",
"privatebin>=0.3.0,<1.0",
Expand Down
Loading
Loading