Class: McpOnRuby::Tool

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

Overview

Base class for MCP tools - functions that AI can execute

Direct Known Subclasses

ApplicationTool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: '', input_schema: {}, metadata: {}, tags: []) ⇒ Tool

Create a new tool

Parameters:

  • name (String)

    The tool name

  • description (String) (defaults to: '')

    The tool description

  • input_schema (Hash) (defaults to: {})

    JSON Schema for input validation

  • metadata (Hash) (defaults to: {})

    Additional metadata

  • tags (Array<String>) (defaults to: [])

    Tags for categorization



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

def initialize(name:, description: '', input_schema: {}, metadata: {}, tags: [])
  @name = name.to_s
  @description = description
  @input_schema = normalize_schema(input_schema)
   = 
  @tags = Array(tags)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#input_schemaObject (readonly)

Returns the value of attribute input_schema.



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

def input_schema
  @input_schema
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

Instance Method Details

#authorized?(context = {}) ⇒ Boolean

Check if tool is authorized for the given context

Parameters:

  • context (Hash) (defaults to: {})

    Request context

Returns:

  • (Boolean)

    True if authorized



61
62
63
64
65
66
67
68
# File 'lib/mcp_on_ruby/tool.rb', line 61

def authorized?(context = {})
  return true unless respond_to?(:authorize, true)
  
  authorize(context)
rescue => error
  McpOnRuby.logger.warn("Authorization check failed for tool '#{name}': #{error.message}")
  false
end

#call(arguments = {}, context = {}) ⇒ Hash

Execute the tool with given arguments

Parameters:

  • arguments (Hash) (defaults to: {})

    The arguments to pass to the tool

  • context (Hash) (defaults to: {})

    Request context (headers, user info, etc.)

Returns:

  • (Hash)

    The tool execution result



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mcp_on_ruby/tool.rb', line 26

def call(arguments = {}, context = {})
  # Validate arguments against schema
  validate_arguments!(arguments)
  
  # Call the implementation
  execute(arguments, context)
rescue => error
  McpOnRuby.logger.error("Tool '#{name}' execution failed: #{error.message}")
  McpOnRuby.logger.error(error.backtrace.join("\n"))
  
  {
    error: {
      code: -32603,
      message: "Tool execution failed: #{error.message}",
      data: { tool: name, error_type: error.class.name }
    }
  }
end

#to_schemaHash

Get the tool’s schema for MCP protocol

Returns:

  • (Hash)

    The tool schema



47
48
49
50
51
52
53
54
55
56
# File 'lib/mcp_on_ruby/tool.rb', line 47

def to_schema
  {
    name: name,
    description: description,
    inputSchema: input_schema
  }.tap do |schema|
    schema[:metadata] =  unless .empty?
    schema[:tags] = tags unless tags.empty?
  end
end