Class: Whoosh::MCP::ClientManager

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/mcp/client_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeClientManager

Returns a new instance of ClientManager.



9
10
11
12
13
14
# File 'lib/whoosh/mcp/client_manager.rb', line 9

def initialize
  @configs = {}
  @clients = {}
  @pids = {}
  @mutex = Mutex.new
end

Instance Method Details

#configsObject



24
25
26
# File 'lib/whoosh/mcp/client_manager.rb', line 24

def configs
  @configs.dup
end

#get_client(name) ⇒ Object



32
33
34
# File 'lib/whoosh/mcp/client_manager.rb', line 32

def get_client(name)
  @mutex.synchronize { @clients[name] }
end

#pidsObject



52
53
54
# File 'lib/whoosh/mcp/client_manager.rb', line 52

def pids
  @mutex.synchronize { @pids.transform_values { |v| v[:pid] } }
end

#register(name, command:, **options) ⇒ Object



16
17
18
# File 'lib/whoosh/mcp/client_manager.rb', line 16

def register(name, command:, **options)
  @configs[name] = { command: command, **options }
end

#registered?(name) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/whoosh/mcp/client_manager.rb', line 20

def registered?(name)
  @configs.key?(name)
end

#set_client(name, client) ⇒ Object



28
29
30
# File 'lib/whoosh/mcp/client_manager.rb', line 28

def set_client(name, client)
  @mutex.synchronize { @clients[name] = client }
end

#shutdown_allObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/whoosh/mcp/client_manager.rb', line 56

def shutdown_all
  @mutex.synchronize do
    @clients.each_value { |c| c.close if c.respond_to?(:close) }
    @pids.each_value do |info|
      begin
        Process.kill("TERM", info[:pid])
        info[:thread]&.join(5)
      rescue Errno::ESRCH, Errno::ECHILD
      end
      info[:stderr]&.close rescue nil
    end
    @clients.clear
    @pids.clear
  end
end

#spawn_client(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/whoosh/mcp/client_manager.rb', line 36

def spawn_client(name)
  config = @configs[name]
  raise Whoosh::Errors::DependencyError, "Unknown MCP client: #{name}" unless config

  stdin, stdout, stderr, wait_thr = Open3.popen3(config[:command])

  client = Client.new(stdin: stdin, stdout: stdout)

  @mutex.synchronize do
    @clients[name] = client
    @pids[name] = { pid: wait_thr.pid, thread: wait_thr, stderr: stderr }
  end

  client
end