Class: RubyMCP::Transport::Stdio

Inherits:
RubyMCP::Transport show all
Defined in:
lib/ruby_mcp/transport/stdio.rb

Instance Method Summary collapse

Methods inherited from RubyMCP::Transport

#close, #on_message

Instance Method Details

#on_close(&block) ⇒ Object



28
29
30
# File 'lib/ruby_mcp/transport/stdio.rb', line 28

def on_close(&block)
  @on_close = block
end

#send(message) ⇒ Object



22
23
24
25
# File 'lib/ruby_mcp/transport/stdio.rb', line 22

def send(message)
  $stdout.puts(JSON.generate(message))
  $stdout.flush
end

#startObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_mcp/transport/stdio.rb', line 4

def start
  @running = true

  while @running
    begin
      line = $stdin.gets

      break if line.nil?

      @on_message.call(line.strip)
    rescue StandardError => e
      RubyMCP.logger.error("Exception: #{e}")
    end
  end

  @on_close.call
end