Class: MCP::Client

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

Defined Under Namespace

Classes: HTTP, RequestHandlerError, ServerError, Stdio, 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)

Parameters:

  • transport (Object)

    The transport object to use for communication with the server. The transport should be a duck type that responds to send_request. See the README for more details.



38
39
40
# File 'lib/mcp/client.rb', line 38

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



44
45
46
# File 'lib/mcp/client.rb', line 44

def transport
  @transport
end

Instance Method Details

#call_tool(name: nil, tool: nil, arguments: nil, progress_token: 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:

Call by name

response = client.call_tool(name: "my_tool", arguments: { foo: "bar" })
content = response.dig("result", "content")

Call with a tool object

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

Parameters:

  • name (String) (defaults to: nil)

    The name of the tool to call.

  • tool (MCP::Client::Tool) (defaults to: nil)

    The tool to be called.

  • arguments (Object, nil) (defaults to: nil)

    The arguments to pass to the tool.

  • progress_token (String, Integer, nil) (defaults to: nil)

    A token to request progress notifications from the server during tool execution.

Returns:

  • (Hash)

    The full JSON-RPC response from the transport.

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mcp/client.rb', line 118

def call_tool(name: nil, tool: nil, arguments: nil, progress_token: nil)
  tool_name = name || tool&.name
  raise ArgumentError, "Either `name:` or `tool:` must be provided." unless tool_name

  params = { name: tool_name, arguments: arguments }
  if progress_token
    params[:_meta] = { progressToken: progress_token }
  end

  request(method: "tools/call", params: params)
end

#complete(ref:, argument:, context: nil) ⇒ Hash

Requests completion suggestions from the server for a prompt argument or resource template URI.

Parameters:

  • ref (Hash)

    The reference, e.g. ‘{ type: “ref/prompt”, name: “my_prompt” }` or `{ type: “ref/resource”, uri: “file:///path” }`.

  • argument (Hash)

    The argument being completed, e.g. ‘{ name: “language”, value: “py” }`.

  • context (Hash, nil) (defaults to: nil)

    Optional context with previously resolved arguments.

Returns:

  • (Hash)

    The completion result with ‘“values”`, `“hasMore”`, and optionally `“total”`.



157
158
159
160
161
162
163
164
# File 'lib/mcp/client.rb', line 157

def complete(ref:, argument:, context: nil)
  params = { ref: ref, argument: argument }
  params[:context] = context if context

  response = request(method: "completion/complete", params: params)

  response.dig("result", "completion") || { "values" => [], "hasMore" => false }
end

#get_prompt(name:) ⇒ Hash

Gets a prompt from the server by name and returns its details.

Parameters:

  • name (String)

    The name of the prompt to get.

Returns:

  • (Hash)

    A hash containing the prompt details.



144
145
146
147
148
# File 'lib/mcp/client.rb', line 144

def get_prompt(name:)
  response = request(method: "prompts/get", params: { name: name })

  response.fetch("result", {})
end

#promptsArray<Hash>

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

Returns:

  • (Array<Hash>)

    An array of available prompts.



92
93
94
95
96
# File 'lib/mcp/client.rb', line 92

def prompts
  response = request(method: "prompts/list")

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

#read_resource(uri:) ⇒ Array<Hash>

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

Parameters:

  • uri (String)

    The URI of the resource to read.

Returns:

  • (Array<Hash>)

    An array of resource contents (text or blob).



134
135
136
137
138
# File 'lib/mcp/client.rb', line 134

def read_resource(uri:)
  response = request(method: "resources/read", params: { uri: uri })

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

#resource_templatesArray<Hash>

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

Returns:

  • (Array<Hash>)

    An array of available resource templates.



82
83
84
85
86
# File 'lib/mcp/client.rb', line 82

def resource_templates
  response = request(method: "resources/templates/list")

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

#resourcesArray<Hash>

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

Returns:

  • (Array<Hash>)

    An array of available resources.



72
73
74
75
76
# File 'lib/mcp/client.rb', line 72

def resources
  response = request(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

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mcp/client.rb', line 56

def tools
  response = request(method: "tools/list")

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