Class: Ask::MCP::Adapters::ToolServer

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/mcp/adapters/tool_server.rb

Overview

Converts duck-typed tool objects into MCP tool definitions and dispatches calls. This is the server-direction adapter — it takes any objects that respond to name, description, params_schema, and call(args) and exposes them over MCP.

Tools can return:

- An object responding to #ok?/#ok and #output/#error_message (Ask::Result style)
- A plain String (treated as success)
- Any other value (treated as success, .to_s is used for the response)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tools = []) ⇒ ToolServer

Returns a new instance of ToolServer.



18
19
20
21
# File 'lib/ask/mcp/adapters/tool_server.rb', line 18

def initialize(tools = [])
  @tools = tools
  @tool_map = tools.each_with_object({}) { |t, h| h[t.name] = t }
end

Instance Attribute Details

#toolsObject (readonly)

Returns the value of attribute tools.



16
17
18
# File 'lib/ask/mcp/adapters/tool_server.rb', line 16

def tools
  @tools
end

Instance Method Details

#call(name, arguments = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ask/mcp/adapters/tool_server.rb', line 34

def call(name, arguments = {})
  tool = @tool_map[name]
  unless tool
    return error_result("Tool not found: #{name}")
  end

  normalized = deep_stringify_keys(arguments)
  result = tool.call(normalized)
  wrap_result(result)
rescue StandardError => e
  if defined?(Ask::Tool::Halt) && e.is_a?(Ask::Tool::Halt)
    return { content: [{ type: "text", text: e.content.to_s }], isError: false }
  end
  error_result("#{e.class}: #{e.message}")
end

#definitionsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/ask/mcp/adapters/tool_server.rb', line 23

def definitions
  @tools.map do |tool|
    schema = tool.params_schema || { type: "object", properties: {}, required: [] }
    {
      name: tool.name,
      description: tool.description || "",
      inputSchema: schema
    }
  end
end