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, 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.
-
#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.
17 18 19 |
# File 'lib/mcp/client.rb', line 17 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
23 24 25 |
# File 'lib/mcp/client.rb', line 23 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.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mcp/client.rb', line 113 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 transport.send_request(request: { jsonrpc: JsonRpcHandler::Version::V2_0, id: request_id, method: "tools/call", params: params, }) end |
#get_prompt(name:) ⇒ Hash
Gets a prompt from the server by name and returns its details.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mcp/client.rb', line 149 def get_prompt(name:) response = transport.send_request(request: { jsonrpc: JsonRpcHandler::Version::V2_0, id: request_id, 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.
83 84 85 86 87 88 89 90 91 |
# File 'lib/mcp/client.rb', line 83 def prompts response = transport.send_request(request: { jsonrpc: JsonRpcHandler::Version::V2_0, id: request_id, 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 139 140 141 142 143 |
# File 'lib/mcp/client.rb', line 134 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 |
#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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/mcp/client.rb', line 69 def resource_templates response = transport.send_request(request: { jsonrpc: JsonRpcHandler::Version::V2_0, id: request_id, 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.
55 56 57 58 59 60 61 62 63 |
# File 'lib/mcp/client.rb', line 55 def resources response = transport.send_request(request: { jsonrpc: JsonRpcHandler::Version::V2_0, id: request_id, 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mcp/client.rb', line 35 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 |