Class: McpOnRuby::Tool
- Inherits:
-
Object
- Object
- McpOnRuby::Tool
- Defined in:
- lib/mcp_on_ruby/tool.rb
Overview
Base class for MCP tools - functions that AI can execute
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#input_schema ⇒ Object
readonly
Returns the value of attribute input_schema.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#authorized?(context = {}) ⇒ Boolean
Check if tool is authorized for the given context.
-
#call(arguments = {}, context = {}) ⇒ Hash
Execute the tool with given arguments.
-
#initialize(name:, description: '', input_schema: {}, metadata: {}, tags: []) ⇒ Tool
constructor
Create a new tool.
-
#to_schema ⇒ Hash
Get the tool’s schema for MCP protocol.
Constructor Details
#initialize(name:, description: '', input_schema: {}, metadata: {}, tags: []) ⇒ Tool
Create a new tool
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) = = Array() end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/mcp_on_ruby/tool.rb', line 6 def description @description end |
#input_schema ⇒ Object (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 |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/mcp_on_ruby/tool.rb', line 6 def end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/mcp_on_ruby/tool.rb', line 6 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
6 7 8 |
# File 'lib/mcp_on_ruby/tool.rb', line 6 def end |
Instance Method Details
#authorized?(context = {}) ⇒ Boolean
Check if tool is authorized for the given context
61 62 63 64 65 66 67 68 |
# File 'lib/mcp_on_ruby/tool.rb', line 61 def (context = {}) return true unless respond_to?(:authorize, true) (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
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_schema ⇒ Hash
Get the tool’s schema for MCP protocol
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] = unless .empty? end end |