Skip to content

Commit 85252e0

Browse files
committed
Suppress Interrupt exception for stdio server
This PR suppresses the following backtrace of `Interrupt` (Ctrl+c) exception for stdio server: ## Before ```console $ ./examples/stdio_server.rb ^C/Users/koic/src/github.com/modelcontextprotocol/ruby-sdk/lib/mcp/server/transports/stdio_transport.rb:20: in 'IO#gets': Interrupt from /Users/koic/src/github.com/modelcontextprotocol/ruby-sdk/lib/mcp/server/transports/stdio_transport.rb:20: in 'MCP::Server::Transports::StdioTransport#open' from ./examples/stdio_server.rb:95:in '<main>' $ echo $? 130 ``` ## After ```console $ ./examples/stdio_server.rb ^C Exiting... $ echo $? 130 ``` This change should prevent unexpected backtrace from appearing when running the example: https://github.com/modelcontextprotocol/ruby-sdk?tab=readme-ov-file#stdio-transport
1 parent fda5640 commit 85252e0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/mcp/server/transports/stdio_transport.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module MCP
77
class Server
88
module Transports
99
class StdioTransport < Transport
10+
STATUS_INTERRUPTED = Signal.list["INT"] + 128
11+
1012
def initialize(server)
1113
@server = server
1214
@open = false
@@ -20,6 +22,10 @@ def open
2022
while @open && (line = $stdin.gets)
2123
handle_json_request(line.strip)
2224
end
25+
rescue Interrupt
26+
warn("\nExiting...")
27+
28+
exit(STATUS_INTERRUPTED)
2329
end
2430

2531
def close

0 commit comments

Comments
 (0)