Class: MCP::Client

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

Defined Under Namespace

Classes: HTTP, RequestHandlerError, Tool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport:) ⇒ Client

Initializes a new MCP::Client instance.

Examples:

transport = MCP::Client::HTTP.new(url: "http://localhost:3000")
client = MCP::Client.new(transport: transport)


13
14
15
# File 'lib/mcp/client.rb', line 13

def initialize(transport:)
  @transport = transport
end

Instance Attribute Details

#transportObject (readonly)

The user may want to access additional transport-specific methods/attributes So keeping it public



19
20
21
# File 'lib/mcp/client.rb', line 19

def transport
  @transport
end

Instance Method Details

#call_tool(tool:, arguments: nil) ⇒ Hash

Note:

The exact requirements for ‘arguments` are determined by the transport layer in use. Consult the documentation for your transport (e.g., MCP::Client::HTTP) for details.

Calls a tool via the transport layer and returns the full response from the server.

Examples:

tool = client.tools.first
response = client.call_tool(tool: tool, arguments: { foo: "bar" })
structured_content = response.dig("result", "structuredContent")


75
76
77
78
79
80
81
82
# File 'lib/mcp/client.rb', line 75

def call_tool(tool:, arguments: nil)
  transport.send_request(request: {
    jsonrpc: JsonRpcHandler::Version::V2_0,
    id: request_id,
    method: "tools/call",
    params: { name: tool.name, arguments: arguments },
  })
end

#read_resource(uri:) ⇒ Array<Hash>

Reads a resource from the server by URI and returns the contents.



88
89
90
91
92
93
94
95
96
97
# File 'lib/mcp/client.rb', line 88

def read_resource(uri:)
  response = transport.send_request(request: {
    jsonrpc: JsonRpcHandler::Version::V2_0,
    id: request_id,
    method: "resources/read",
    params: { uri: uri },
  })

  response.dig("result", "contents") || []
end

#resourcesArray<Hash>

Returns the list of resources available from the server. Each call will make a new request – the result is not cached.



51
52
53
54
55
56
57
58
59
# File 'lib/mcp/client.rb', line 51

def resources
  response = transport.send_request(request: {
    jsonrpc: JsonRpcHandler::Version::V2_0,
    id: request_id,
    method: "resources/list",
  })

  response.dig("result", "resources") || []
end

#toolsArray<MCP::Client::Tool>

Returns the list of tools available from the server. Each call will make a new request – the result is not cached.

Examples:

tools = client.tools
tools.each do |tool|
  puts tool.name
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mcp/client.rb', line 31

def tools
  response = transport.send_request(request: {
    jsonrpc: JsonRpcHandler::Version::V2_0,
    id: request_id,
    method: "tools/list",
  })

  response.dig("result", "tools")&.map do |tool|
    Tool.new(
      name: tool["name"],
      description: tool["description"],
      input_schema: tool["inputSchema"],
    )
  end || []
end