Class: MCP::Client
- Inherits:
-
Object
- Object
- MCP::Client
- 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
-
#transport ⇒ Object
readonly
The user may want to access additional transport-specific methods/attributes So keeping it public.
Instance Method Summary collapse
-
#call_tool(tool:, arguments: nil) ⇒ Hash
Calls a tool via the transport layer and returns the full response from the server.
-
#initialize(transport:) ⇒ Client
constructor
Initializes a new MCP::Client instance.
-
#read_resource(uri:) ⇒ Array<Hash>
Reads a resource from the server by URI and returns the contents.
-
#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.
13 14 15 |
# File 'lib/mcp/client.rb', line 13 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
19 20 21 |
# File 'lib/mcp/client.rb', line 19 def transport @transport end |
Instance Method Details
#call_tool(tool:, arguments: 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.
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 |
#resources ⇒ Array<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 |
#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.
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 |