Class: MCP::Client
- Inherits:
-
Object
- Object
- MCP::Client
- 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
-
#transport ⇒ Object
readonly
The user may want to access additional transport-specific methods/attributes So keeping it public.
Instance Method Summary collapse
-
#call_tool(name: nil, tool: nil, arguments: nil, progress_token: nil) ⇒ Hash
Calls a tool via the transport layer and returns the full response from the server.
-
#complete(ref:, argument:, context: nil) ⇒ Hash
Requests completion suggestions from the server for a prompt argument or resource template URI.
-
#get_prompt(name:) ⇒ Hash
Gets a prompt from the server by name and returns its details.
-
#initialize(transport:) ⇒ Client
constructor
Initializes a new MCP::Client instance.
-
#prompts ⇒ Array<Hash>
Returns the list of prompts available from the server.
-
#read_resource(uri:) ⇒ Array<Hash>
Reads a resource from the server by URI and returns the contents.
-
#resource_templates ⇒ Array<Hash>
Returns the list of resource templates available from the server.
-
#resources ⇒ Array<Hash>
Returns the list of resources available from the server.
-
#tools ⇒ Array<MCP::Client::Tool>
Returns the list of tools available from the server.
Constructor Details
#initialize(transport:) ⇒ Client
Initializes a new MCP::Client instance.
38 39 40 |
# File 'lib/mcp/client.rb', line 38 def initialize(transport:) @transport = transport end |
Instance Attribute Details
#transport ⇒ Object (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
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.
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.
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.
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 |
#prompts ⇒ Array<Hash>
Returns the list of prompts available from the server. Each call will make a new request – the result is not cached.
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.
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_templates ⇒ Array<Hash>
Returns the list of resource templates available from the server. Each call will make a new request – the result is not cached.
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 |
#resources ⇒ Array<Hash>
Returns the list of resources available from the server. Each call will make a new request – the result is not cached.
72 73 74 75 76 |
# File 'lib/mcp/client.rb', line 72 def resources response = request(method: "resources/list") response.dig("result", "resources") || [] end |
#tools ⇒ Array<MCP::Client::Tool>
Returns the list of tools available from the server. Each call will make a new request – the result is not cached.
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 |