Class: RCrewAI::MCP::Client
- Inherits:
-
Object
- Object
- RCrewAI::MCP::Client
- Defined in:
- lib/rcrewai/mcp/client.rb
Overview
Minimal MCP (Model Context Protocol) JSON-RPC client. Connects to a server via stdio or streamable HTTP, performs the initialize/initialized handshake, lists tools, and exposes them as RCrewAI::Tools::Base instances.
Constant Summary collapse
- PROTOCOL_VERSION =
'2024-11-05'
Instance Attribute Summary collapse
-
#server_name ⇒ Object
readonly
Returns the value of attribute server_name.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Class Method Summary collapse
Instance Method Summary collapse
- #call_tool(name, args) ⇒ Object
- #close ⇒ Object
-
#initialize(command: nil, args: [], env: {}, url: nil, headers: {}) ⇒ Client
constructor
A new instance of Client.
- #open ⇒ Object
Constructor Details
#initialize(command: nil, args: [], env: {}, url: nil, headers: {}) ⇒ Client
Returns a new instance of Client.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rcrewai/mcp/client.rb', line 33 def initialize(command: nil, args: [], env: {}, url: nil, headers: {}) @transport = if url Transport::Http.new(url: url, headers: headers) else Transport::Stdio.new(command: command, args: args, env: env) end @request_id = 0 @tools = [] @server_name = nil end |
Instance Attribute Details
#server_name ⇒ Object (readonly)
Returns the value of attribute server_name.
18 19 20 |
# File 'lib/rcrewai/mcp/client.rb', line 18 def server_name @server_name end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
18 19 20 |
# File 'lib/rcrewai/mcp/client.rb', line 18 def tools @tools end |
Class Method Details
.connect(**opts) ⇒ Object
20 21 22 |
# File 'lib/rcrewai/mcp/client.rb', line 20 def self.connect(**opts) new(**opts).tap(&:open) end |
.with_connection(**opts) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/rcrewai/mcp/client.rb', line 24 def self.with_connection(**opts) c = connect(**opts) begin yield c ensure c&.close end end |
Instance Method Details
#call_tool(name, args) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rcrewai/mcp/client.rb', line 54 def call_tool(name, args) result = request('tools/call', name: strip_prefix(name), arguments: args) text = result.dig('content', 0, 'text') text || result['content'] end |
#close ⇒ Object
50 51 52 |
# File 'lib/rcrewai/mcp/client.rb', line 50 def close @transport.close end |
#open ⇒ Object
44 45 46 47 48 |
# File 'lib/rcrewai/mcp/client.rb', line 44 def open @transport.open handshake load_tools end |