Skip to content
Kward Search API index

Class: Kward::Tools::MCPTool

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/mcp_tool.rb

Overview

Adapts an MCP server tool to Kward's model-callable tool interface.

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name:, client:, tool:) ⇒ MCPTool

Returns a new instance of MCPTool.



11
12
13
14
15
16
17
# File 'lib/kward/tools/mcp_tool.rb', line 11

def initialize(server_name:, client:, tool:)
  @server_name = server_name.to_s
  @client = client
  @tool = tool
  @remote_name = value(tool, "name").to_s
  super(kward_name(@server_name, @remote_name), description, properties: {}, required: [])
end

Instance Attribute Details

#remote_nameObject (readonly)

Returns the value of attribute remote_name.



9
10
11
# File 'lib/kward/tools/mcp_tool.rb', line 9

def remote_name
  @remote_name
end

#server_nameObject (readonly)

Returns the value of attribute server_name.



9
10
11
# File 'lib/kward/tools/mcp_tool.rb', line 9

def server_name
  @server_name
end

Class Method Details

.kward_name(server_name, tool_name) ⇒ Object



38
39
40
# File 'lib/kward/tools/mcp_tool.rb', line 38

def self.kward_name(server_name, tool_name)
  "#{sanitize_name(server_name)}__#{sanitize_name(tool_name)}"
end

.sanitize_name(value) ⇒ Object



42
43
44
45
46
# File 'lib/kward/tools/mcp_tool.rb', line 42

def self.sanitize_name(value)
  text = value.to_s.gsub(/[^A-Za-z0-9_-]/, "_")
  text = "mcp" if text.empty?
  text[0, 60]
end

Instance Method Details

#call(args, _conversation, cancellation: nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/kward/tools/mcp_tool.rb', line 30

def call(args, _conversation, cancellation: nil)
  cancellation&.raise_if_cancelled!
  result = @client.call_tool(@remote_name, args || {})
  format_result(result)
rescue StandardError => e
  "MCP tool #{server_name}.#{remote_name} failed: #{e.message}"
end

#schemaObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/kward/tools/mcp_tool.rb', line 19

def schema
  {
    type: "function",
    function: {
      name: name,
      description: description,
      parameters: input_schema
    }
  }
end