Class: Html2mdMcpClient::Client
- Inherits:
-
Object
- Object
- Html2mdMcpClient::Client
- Defined in:
- lib/html2md_mcp_client/client.rb
Constant Summary collapse
- JSONRPC_VERSION =
'2.0'.freeze
- PROTOCOL_VERSION =
'2025-03-26'.freeze
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#server_info ⇒ Object
readonly
Returns the value of attribute server_info.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
-
#call_tool(name, arguments = {}) ⇒ Object
Call a tool.
-
#connect! ⇒ Object
— Connection lifecycle —.
- #connected? ⇒ Boolean
- #disconnect! ⇒ Object
-
#find_tool(name) ⇒ Object
Find a tool definition by name.
- #get_prompt(name, arguments = {}) ⇒ Object
-
#initialize(transport, client_name: 'html2md_mcp_client', client_version: Html2mdMcpClient::VERSION) ⇒ Client
constructor
A new instance of Client.
-
#list_prompts ⇒ Object
— Prompts —.
-
#list_resources ⇒ Object
— Resources —.
-
#list_tools ⇒ Object
Returns array of tool definitions: [{ “name” => …, “description” => …, “inputSchema” => … }].
- #read_resource(uri) ⇒ Object
-
#tool_text(name, arguments = {}) ⇒ Object
Convenience: call a tool and return joined text content.
Constructor Details
#initialize(transport, client_name: 'html2md_mcp_client', client_version: Html2mdMcpClient::VERSION) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 |
# File 'lib/html2md_mcp_client/client.rb', line 11 def initialize(transport, client_name: 'html2md_mcp_client', client_version: Html2mdMcpClient::VERSION) @transport = transport @client_name = client_name @client_version = client_version @request_id = 0 @connected = false @tools_cache = nil end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
9 10 11 |
# File 'lib/html2md_mcp_client/client.rb', line 9 def capabilities @capabilities end |
#server_info ⇒ Object (readonly)
Returns the value of attribute server_info.
9 10 11 |
# File 'lib/html2md_mcp_client/client.rb', line 9 def server_info @server_info end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
9 10 11 |
# File 'lib/html2md_mcp_client/client.rb', line 9 def transport @transport end |
Instance Method Details
#call_tool(name, arguments = {}) ⇒ Object
Call a tool. Returns the content array. Raises ToolError if the server signals an error.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/html2md_mcp_client/client.rb', line 65 def call_tool(name, arguments = {}) ensure_connected! result = request('tools/call', { name: name, arguments: arguments }) if result['isError'] texts = Array(result['content']).select { |c| c['type'] == 'text' }.map { |c| c['text'] } raise ToolError, "Tool '#{name}' error: #{texts.join('; ')}" end result['content'] || [] end |
#connect! ⇒ Object
— Connection lifecycle —
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/html2md_mcp_client/client.rb', line 22 def connect! return self if @connected @transport.start if @transport.respond_to?(:start) result = request('initialize', { protocolVersion: PROTOCOL_VERSION, capabilities: {}, clientInfo: { name: @client_name, version: @client_version } }) @server_info = result['serverInfo'] @capabilities = result['capabilities'] || {} notify('notifications/initialized') @connected = true self end |
#connected? ⇒ Boolean
48 49 50 |
# File 'lib/html2md_mcp_client/client.rb', line 48 def connected? @connected end |
#disconnect! ⇒ Object
41 42 43 44 45 46 |
# File 'lib/html2md_mcp_client/client.rb', line 41 def disconnect! return unless @connected @transport.close @connected = false @tools_cache = nil end |
#find_tool(name) ⇒ Object
Find a tool definition by name. Returns nil if not found.
86 87 88 |
# File 'lib/html2md_mcp_client/client.rb', line 86 def find_tool(name) list_tools.find { |t| t['name'] == name } end |
#get_prompt(name, arguments = {}) ⇒ Object
112 113 114 115 |
# File 'lib/html2md_mcp_client/client.rb', line 112 def get_prompt(name, arguments = {}) ensure_connected! request('prompts/get', { name: name, arguments: arguments }) end |
#list_prompts ⇒ Object
— Prompts —
106 107 108 109 110 |
# File 'lib/html2md_mcp_client/client.rb', line 106 def list_prompts ensure_connected! result = request('prompts/list', {}) result['prompts'] || [] end |
#list_resources ⇒ Object
— Resources —
92 93 94 95 96 |
# File 'lib/html2md_mcp_client/client.rb', line 92 def list_resources ensure_connected! result = request('resources/list', {}) result['resources'] || [] end |
#list_tools ⇒ Object
Returns array of tool definitions: [{ “name” => …, “description” => …, “inputSchema” => … }]
55 56 57 58 59 60 61 |
# File 'lib/html2md_mcp_client/client.rb', line 55 def list_tools ensure_connected! @tools_cache ||= begin result = request('tools/list', {}) result['tools'] || [] end end |
#read_resource(uri) ⇒ Object
98 99 100 101 102 |
# File 'lib/html2md_mcp_client/client.rb', line 98 def read_resource(uri) ensure_connected! result = request('resources/read', { uri: uri }) result['contents'] || [] end |
#tool_text(name, arguments = {}) ⇒ Object
Convenience: call a tool and return joined text content.
78 79 80 81 82 83 |
# File 'lib/html2md_mcp_client/client.rb', line 78 def tool_text(name, arguments = {}) call_tool(name, arguments) .select { |c| c['type'] == 'text' } .map { |c| c['text'] } .join("\n") end |