Skip to content
Merged
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
27 changes: 18 additions & 9 deletions test/mcp/client/stdio_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_send_request_starts_process_and_returns_response
}

# Simulate server responses: initialize response, then tools/list response
Thread.new do
server_thread = Thread.new do
# Read and respond to initialize request
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -62,6 +62,7 @@ def test_send_request_starts_process_and_returns_response
assert_equal(1, response.dig("result", "tools").size)
assert_equal("test_tool", response.dig("result", "tools", 0, "name"))
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand All @@ -85,7 +86,7 @@ def test_send_request_initializes_session_on_first_call

received_methods = []

Thread.new do
server_thread = Thread.new do
# Read initialize request
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -126,6 +127,7 @@ def test_send_request_initializes_session_on_first_call

assert_equal(["initialize", "notifications/initialized", "tools/list"], received_methods)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand All @@ -147,7 +149,7 @@ def test_send_request_skips_notifications
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Handle initialize handshake
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -187,6 +189,7 @@ def test_send_request_skips_notifications
assert_equal("test-id", response["id"])
assert_equal([], response.dig("result", "tools"))
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down Expand Up @@ -242,7 +245,7 @@ def test_send_request_raises_error_on_closed_stdout
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Handle initialize handshake
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -272,6 +275,7 @@ def test_send_request_raises_error_on_closed_stdout
assert_equal("Server process closed stdout unexpectedly", error.message)
assert_equal(:internal_error, error.error_type)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down Expand Up @@ -330,7 +334,7 @@ def test_send_request_skips_initialization_on_second_call

received_methods = []

Thread.new do
server_thread = Thread.new do
# First call: initialize handshake
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -384,6 +388,7 @@ def test_send_request_skips_initialization_on_second_call
received_methods,
)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down Expand Up @@ -415,7 +420,7 @@ def test_send_request_raises_error_on_invalid_json
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Handle initialize handshake
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -447,6 +452,7 @@ def test_send_request_raises_error_on_invalid_json
assert_equal(:internal_error, error.error_type)
assert_instance_of(JSON::ParserError, error.original_error)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand All @@ -468,7 +474,7 @@ def test_send_request_raises_error_when_initialization_fails
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Read initialize request and return an error
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand All @@ -487,6 +493,7 @@ def test_send_request_raises_error_when_initialization_fails
assert_equal("Server initialization failed: Invalid Request", error.message)
assert_equal(:internal_error, error.error_type)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down Expand Up @@ -534,7 +541,7 @@ def test_read_response_raises_error_on_timeout
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Handle initialize handshake
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand Down Expand Up @@ -563,6 +570,7 @@ def test_read_response_raises_error_on_timeout
assert_equal("Timed out waiting for server response", error.message)
assert_equal(:internal_error, error.error_type)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down Expand Up @@ -689,7 +697,7 @@ def test_send_request_raises_error_for_missing_result
method: "tools/list",
}

Thread.new do
server_thread = Thread.new do
# Read initialize request and return a response without result
init_line = stdin_read.gets
init_request = JSON.parse(init_line)
Expand All @@ -707,6 +715,7 @@ def test_send_request_raises_error_for_missing_result
assert_equal("Server initialization failed: missing result in response", error.message)
assert_equal(:internal_error, error.error_type)
ensure
server_thread.join
stdin_read.close
stdin_write.close
stdout_read.close
Expand Down