Class: Kreuzberg::MCPProxy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/kreuzberg/mcp_proxy.rb

Overview

MCP server instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport: 'stdio') ⇒ Server

Initialize MCP server

Parameters:

  • transport (String) (defaults to: 'stdio')

    Transport method (“stdio” or “sse”)



21
22
23
24
25
26
27
# File 'lib/kreuzberg/mcp_proxy.rb', line 21

def initialize(transport: 'stdio')
  @transport = transport
  @pid = nil
  @stdin = nil
  @stdout = nil
  @stderr = nil
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



15
16
17
# File 'lib/kreuzberg/mcp_proxy.rb', line 15

def pid
  @pid
end

#transportObject (readonly)

Returns the value of attribute transport.



15
16
17
# File 'lib/kreuzberg/mcp_proxy.rb', line 15

def transport
  @transport
end

Instance Method Details

#read_messageHash

Read a message from the server (stdio only)

Returns:

  • (Hash)

    JSON-RPC message

Raises:



78
79
80
81
82
83
84
# File 'lib/kreuzberg/mcp_proxy.rb', line 78

def read_message
  raise ServerError, 'Can only read messages in stdio mode' unless @transport == 'stdio'
  raise ServerError, 'Server not started' unless @stdout

  line = @stdout.gets
  JSON.parse(line) if line
end

#running?Boolean

Check if server is running

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/kreuzberg/mcp_proxy.rb', line 90

def running?
  return false unless @pid

  Process.kill(0, @pid)
  true
rescue Errno::ESRCH, Errno::EPERM
  false
end

#send_message(message) ⇒ void

This method returns an undefined value.

Send a message to the server (stdio only)

Parameters:

  • message (Hash)

    JSON-RPC message

Raises:



66
67
68
69
70
71
72
# File 'lib/kreuzberg/mcp_proxy.rb', line 66

def send_message(message)
  raise ServerError, 'Can only send messages in stdio mode' unless @transport == 'stdio'
  raise ServerError, 'Server not started' unless @stdin

  @stdin.puts(JSON.generate(message))
  @stdin.flush
end

#startInteger?

Start the MCP server

Returns:

  • (Integer, nil)

    Process ID (for SSE) or nil (for stdio)



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kreuzberg/mcp_proxy.rb', line 33

def start
  binary = MCPProxy.find_mcp_binary

  case @transport
  when 'stdio'
    start_stdio(binary)
  when 'sse'
    start_sse(binary)
  else
    raise ServerError, "Unknown transport: #{@transport}"
  end
end

#stopvoid

This method returns an undefined value.

Stop the server



50
51
52
53
54
55
56
57
58
59
# File 'lib/kreuzberg/mcp_proxy.rb', line 50

def stop
  return unless @pid

  Process.kill('TERM', @pid)
  Process.wait(@pid)
rescue Errno::ESRCH, Errno::ECHILD # rubocop:disable Lint/SuppressedException
ensure
  @pid = nil
  close_pipes
end