Class: RCrewAI::MCP::Transport::Stdio

Inherits:
Object
  • Object
show all
Defined in:
lib/rcrewai/mcp/transport/stdio.rb

Instance Method Summary collapse

Constructor Details

#initialize(command:, args: [], env: {}) ⇒ Stdio

Returns a new instance of Stdio.



10
11
12
13
14
15
16
17
18
# File 'lib/rcrewai/mcp/transport/stdio.rb', line 10

def initialize(command:, args: [], env: {})
  @command = command
  @args = args
  @env = env
  @stdin = nil
  @stdout = nil
  @stderr_thread = nil
  @wait_thr = nil
end

Instance Method Details

#closeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rcrewai/mcp/transport/stdio.rb', line 38

def close
  return unless @wait_thr&.alive?

  begin
    ::Process.kill('TERM', @wait_thr.pid)
  rescue Errno::ESRCH, Errno::EPERM
    # already dead
  end
  @stdin&.close
  @stdout&.close
  @stderr_thread&.kill
rescue IOError
  # streams already closed
end

#openObject



20
21
22
23
24
25
26
27
# File 'lib/rcrewai/mcp/transport/stdio.rb', line 20

def open
  @stdin, @stdout, stderr, @wait_thr = Open3.popen3(@env, @command, *@args)
  @stderr_thread = Thread.new do
    stderr.each_line { |l| Kernel.warn "[mcp-stderr] #{l}" }
  rescue IOError
    # stream closed
  end
end

#recv_lineObject



34
35
36
# File 'lib/rcrewai/mcp/transport/stdio.rb', line 34

def recv_line
  @stdout.gets
end

#send_line(json) ⇒ Object



29
30
31
32
# File 'lib/rcrewai/mcp/transport/stdio.rb', line 29

def send_line(json)
  @stdin.write("#{json}\n")
  @stdin.flush
end