Class: Riffer::Mcp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/mcp/client.rb,
sig/generated/riffer/mcp/client.rbs

Overview

Thin wrapper around the MCP Ruby SDK client (mcp gem v0.8+). Resolves headers (if a Proc) once at init, then provides tools_list / tools_call — used for discovery and for tools/call when no credentials proc is configured.

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, headers: {}, client: nil) ⇒ Client

-- : (endpoint: String, ?headers: (Hash[String, String] | Proc), ?client: untyped?) -> void

Parameters:

  • endpoint: (String)
  • headers: (Hash[String, String], Proc) (defaults to: {})
  • client: (Object, nil) (defaults to: nil)


12
13
14
15
16
17
18
19
20
21
# File 'lib/riffer/mcp/client.rb', line 12

def initialize(endpoint:, headers: {}, client: nil)
  depends_on "mcp"
  depends_on "faraday"

  @client = client || begin
    resolved_headers = Riffer::Helpers::CallOrValue.resolve(headers)
    transport = MCP::Client::HTTP.new(url: endpoint, headers: resolved_headers)
    MCP::Client.new(transport: transport)
  end
end

Instance Method Details

#depends_on(gem_name) ⇒ true

: (String) -> true

Parameters:

  • (String)

Returns:

  • (true)


61
62
63
# File 'lib/riffer/mcp/client.rb', line 61

def depends_on(gem_name)
  Riffer::Helpers::Dependencies.depends_on(gem_name)
end

#tools_call(name, arguments = {}) ⇒ String

Calls a tool on the MCP server and returns joined text content from the response.

-- : (String, ?Hash[untyped, untyped]) -> String

Parameters:

  • (String)
  • (Hash[untyped, untyped])

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/riffer/mcp/client.rb', line 41

def tools_call(name, arguments = {})
  tool = MCP::Client::Tool.new(name: name, description: nil, input_schema: nil)
  response = @client.call_tool(tool: tool, arguments: arguments)

  if response["error"]
    raise Riffer::Error, response.dig("error", "message") || "MCP tool call failed"
  end

  if response.dig("result", "isError")
    message = (response.dig("result", "content") || []).filter_map { |item| item["text"] }.join
    raise Riffer::Error, message.empty? ? "MCP tool '#{name}' failed" : message
  end

  content = response.dig("result", "content") || []
  content.filter_map { |item| item["text"] }.join
end

#tools_listArray[Hash[Symbol, untyped]]

Returns tool definition hashes with :name, :description, and :input_schema keys.

: () -> Array[Hash[Symbol, untyped]]

Returns:

  • (Array[Hash[Symbol, untyped]])


27
28
29
30
31
32
33
34
35
# File 'lib/riffer/mcp/client.rb', line 27

def tools_list
  @client.tools.map do |tool|
    {
      name: tool.name,
      description: tool.description,
      input_schema: tool.input_schema
    }
  end
end