ruby-sdk/lib/model_context_protocol/transports/stdio.rb
iberianpig 01460b0b4f
fix: force UTF-8 encoding for stdio streams
This change forces $stdin/$stdout to use UTF-8 encoding,
similar to PR 198 in the python-sdk.
https://github.com/modelcontextprotocol/python-sdk/pull/198

It addresses platform-dependent encoding issues.
2025-05-13 17:26:10 +09:00

35 lines
732 B
Ruby

# frozen_string_literal: true
require_relative "../transport"
require "json"
module ModelContextProtocol
module Transports
class StdioTransport < Transport
def initialize(server)
@server = server
@open = false
$stdin.set_encoding("UTF-8")
$stdout.set_encoding("UTF-8")
super
end
def open
@open = true
while @open && (line = $stdin.gets)
handle_json_request(line.strip)
end
end
def close
@open = false
end
def send_response(message)
json_message = message.is_a?(String) ? message : JSON.generate(message)
$stdout.puts(json_message)
$stdout.flush
end
end
end
end