Skip to content

Commit 129d5af

Browse files
committed
Handle MCP server interrupts cleanly
1 parent 994af3b commit 129d5af

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

python/blacknode/cli.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,16 @@ def add(label: str, ok: bool, detail: str, *, required: bool = True) -> None:
199199
def _mcp(args: argparse.Namespace | None = None) -> int:
200200
from .mcp import main as run_mcp
201201

202-
run_mcp(
203-
transport=getattr(args, "transport", "stdio"),
204-
host=getattr(args, "host", None),
205-
port=getattr(args, "port", None),
206-
path=getattr(args, "path", None),
207-
allowed_hosts=getattr(args, "allowed_hosts", None),
208-
)
202+
try:
203+
run_mcp(
204+
transport=getattr(args, "transport", "stdio"),
205+
host=getattr(args, "host", None),
206+
port=getattr(args, "port", None),
207+
path=getattr(args, "path", None),
208+
allowed_hosts=getattr(args, "allowed_hosts", None),
209+
)
210+
except KeyboardInterrupt:
211+
return 130
209212
return 0
210213

211214

tests/test_cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ def test_mcp_forwards_streamable_http_options(self):
223223
allowed_hosts=["localhost:*", "blacknode-mcp:*"],
224224
)
225225

226+
def test_mcp_ctrl_c_exits_without_traceback(self):
227+
with patch("blacknode.mcp.main", side_effect=KeyboardInterrupt):
228+
code = main(["mcp", "--transport", "streamable-http"])
229+
230+
self.assertEqual(code, 130)
231+
226232

227233
def _valid_workflow() -> dict:
228234
return workflow(

0 commit comments

Comments
 (0)