Class: Rixie::MCP::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rixie/mcp/http/client.rb

Constant Summary collapse

MCP_PROTOCOL_VERSION =
"2024-11-05"

Instance Method Summary collapse

Constructor Details

#initialize(url:, headers: {}, client_info: nil, http_client: nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rixie/mcp/http/client.rb', line 12

def initialize(url:, headers: {}, client_info: nil, http_client: nil)
  uri = URI.parse(url)
  @url = url
  @path = uri.path.empty? ? "/" : uri.path
  @http_client = Rixie::Http::Client.new(
    headers: {
      "Content-Type" => "application/json",
      "Accept" => "application/json, text/event-stream"
    }.merge(headers),
    http_client: http_client,
    allow_private: true
  )
  @client_info = client_info || {name: "rixie", version: Rixie::VERSION}
  @request_id = 0
  @session_initialized = false
end

Instance Method Details

#call_tool(name, arguments = {}) ⇒ Object



47
48
49
50
51
# File 'lib/rixie/mcp/http/client.rb', line 47

def call_tool(name, arguments = {})
  initialize_session
  result = request("tools/call", {name: name, arguments: arguments})
  result.dig("result", "content").map { |c| c["text"] }.join
end

#list_toolsObject



41
42
43
44
45
# File 'lib/rixie/mcp/http/client.rb', line 41

def list_tools
  initialize_session
  result = request("tools/list", {})
  result.dig("result", "tools") || []
end

#toolsObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rixie/mcp/http/client.rb', line 29

def tools
  list_tools.map do |tool_def|
    name = tool_def["name"]
    Rixie::Tool.new(
      name: name,
      description: tool_def["description"],
      input_schema: tool_def["inputSchema"],
      call: ->(args) { call_tool(name, args) }
    )
  end
end