Class: Riffer::Mcp::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers::Dependencies
Defined in:
lib/riffer/mcp/client.rb

Overview

Thin wrapper around the MCP Ruby SDK client (mcp gem v0.8+).

Resolves headers (if a Proc) once at initialization, then provides tools_list and tools_call. Used for discovery (+Manifest#discovery_headers+) and for tools/call when no credentials proc is configured.

MCP gem API used:

MCP::Client::HTTP.new(url:, headers:)  — HTTP transport (requires faraday)
MCP::Client.new(transport:)            — client
client.tools                           — Array<MCP::Client::Tool>
client.call_tool(tool:, arguments:)    — raw JSON-RPC response Hash

Instance Method Summary collapse

Methods included from Helpers::Dependencies

#depends_on

Constructor Details

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

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/riffer/mcp/client.rb', line 21

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

  @client = client || begin
    resolved_headers = headers.is_a?(Proc) ? headers.call : headers
    transport = MCP::Client::HTTP.new(url: endpoint, headers: resolved_headers)
    MCP::Client.new(transport: transport)
  end
end

Instance Method Details

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

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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/riffer/mcp/client.rb', line 51

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_listObject

Returns an array of tool definition hashes, each with :name, :description, and :input_schema keys.

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



37
38
39
40
41
42
43
44
45
# File 'lib/riffer/mcp/client.rb', line 37

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