Class: Encom::Server::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/encom/server/tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, schema:, proc:) ⇒ Tool

Initialize a new tool

Parameters:

  • name (Symbol, String)

    The name of the tool

  • description (String)

    A description of what the tool does

  • schema (Hash)

    A hash describing the input schema for the tool

  • proc (Proc)

    A proc that implements the tool’s functionality



14
15
16
17
18
19
# File 'lib/encom/server/tool.rb', line 14

def initialize(name:, description:, schema:, proc:)
  @name = name
  @description = description
  @schema = schema
  @proc = proc
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/encom/server/tool.rb', line 6

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/encom/server/tool.rb', line 6

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/encom/server/tool.rb', line 6

def schema
  @schema
end

Instance Method Details

#call(arguments) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/encom/server/tool.rb', line 29

def call(arguments)
  result = nil
  
  begin
    if @proc.parameters.first && @proc.parameters.first[0] == :keyreq
      # If the proc expects keyword arguments, pass the arguments hash
      result = @proc.call(**arguments)
    else
      # Otherwise, pass arguments as an array
      result = @proc.call(*arguments)
    end
    
    # Ensure the result is in the standard format
    standardize_tool_response(result)
  rescue StandardError => e
    # Return error in MCP-compliant format as per docs
    {
      isError: true,
      content: [
        {
          type: 'text',
          text: "Error: #{e.message}"
        }
      ]
    }
  end
end

#definitionObject



21
22
23
24
25
26
27
# File 'lib/encom/server/tool.rb', line 21

def definition
  {
    name: @name.to_s,
    description: @description,
    inputSchema: schema_definition
  }.compact
end