Class: Anthropic::Helpers::Tools::BaseTool
- Inherits:
-
Object
- Object
- Anthropic::Helpers::Tools::BaseTool
- Defined in:
- lib/anthropic/helpers/tools/base_tool.rb
Direct Known Subclasses
Constant Summary
Constants included from InputSchema::JsonSchemaConverter
InputSchema::JsonSchemaConverter::NO_REF, InputSchema::JsonSchemaConverter::POINTERS
Class Attribute Summary collapse
- .doc_string ⇒ String readonly
- .model ⇒ Class<Anthropic::Helpers::InputSchema::BaseModel> readonly
-
.tool_name ⇒ String?
private
When set, the runner uses this literal string as the API tool name instead of snake-casing the class name.
Class Method Summary collapse
- .description(description) ⇒ Object (also: doc)
- .input_schema(model) ⇒ Object
- .inspect(depth: 0) ⇒ Object private
- .tool_extra_props ⇒ Hash{Symbol=>Object} deprecated private Deprecated.
- .tool_extra_props=(props) ⇒ Object deprecated private Deprecated.
-
.tool_options(options = nil) ⇒ Hash{Symbol=>Object}
Declares additional properties for this tool's API definition, sent alongside the
name,descriptionandinput_schema— e.g.
Instance Method Summary collapse
- #call(parsed) ⇒ Object
- #coerce(value, state:) ⇒ Object private
- #dump(value, state:) ⇒ Object private
- #inspect ⇒ Object private
-
#parse(value) ⇒ Object
Override the
#parsemethod to customize the pre-processing of the tool call argument. - #to_json_schema ⇒ Object private
- #to_json_schema_inner(state:) ⇒ Object private
Methods included from InputSchema::JsonSchemaConverter
assoc_meta!, cache_def!, to_json_schema, to_json_schema_inner, to_nilable
Methods included from Internal::Type::Converter
coerce, dump, meta_info, new_coerce_state, type_info
Methods included from Internal::Util::SorbetRuntimeSupport
#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type
Class Attribute Details
.doc_string ⇒ String (readonly)
17 18 19 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 17 def doc_string @doc_string end |
.model ⇒ Class<Anthropic::Helpers::InputSchema::BaseModel> (readonly)
14 15 16 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 14 def model @model end |
.tool_name ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
When set, the runner uses this literal string as the API tool name instead of snake-casing the class name. Used by helpers (e.g. MCP) that build tools dynamically from an external definition.
26 27 28 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 26 def tool_name @tool_name end |
Class Method Details
.description(description) ⇒ Object Also known as: doc
31 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 31 def description(description) = (@doc_string = description) |
.input_schema(model) ⇒ Object
38 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 38 def input_schema(model) = (@model = model) |
.inspect(depth: 0) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
106 107 108 109 110 111 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 106 def inspect(depth: 0) # Only hand-written tools carry an input schema class here; helper-built tools (e.g. MCP) # hold a plain class and an undeclared schema is nil, neither of which takes `depth:`. schema = model.is_a?(Anthropic::Internal::Type::Converter) ? model.inspect(depth:) : model.inspect "#{name || tool_name}[#{schema}]" end |
.tool_extra_props ⇒ Hash{Symbol=>Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use tool_options.
88 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 88 def tool_extra_props = |
.tool_extra_props=(props) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use tool_options.
95 96 97 98 99 100 101 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 95 def tool_extra_props=(props) warn( "[DEPRECATION] `tool_extra_props=` is deprecated. Use `tool_options` instead.", category: :deprecated ) (props.to_h) end |
.tool_options(options = nil) ⇒ Hash{Symbol=>Object}
Declares additional properties for this tool's API definition, sent alongside the
name, description and input_schema — e.g. strict, cache_control,
defer_loading, allowed_callers, eager_input_streaming or input_examples.
Options accumulate: each call merges into what was already declared, and a subclass starts from its superclass's options. Called without an argument, returns the declared options as a frozen hash.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 60 def ( = nil) if .nil? return @tool_options if @tool_options # Nothing declared on this class: read through to the superclass (`BaseTool` has none). return superclass.respond_to?(:tool_options) ? superclass. : {}.freeze end = Anthropic::Internal::Util.coerce_hash!().to_h.transform_keys(&:to_sym) # These either have their own declaration or are derived from the class; letting them # through would desync the definition sent to the API from how responses are parsed. reserved = .keys & [:name, :description, :input_schema] unless reserved.empty? = "#{reserved.map { "`#{_1}`" }.join(', ')} cannot be set through `tool_options`; " \ "use `doc` and `input_schema` instead (the tool name is derived from the class name)." raise ArgumentError.new() end # Frozen so the stored declaration can't drift from what was validated above. @tool_options = Anthropic::Internal::Util.deep_frozen_copy(.merge()) end |
Instance Method Details
#call(parsed) ⇒ Object
149 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 149 def call(parsed) = raise NotImplementedError.new |
#coerce(value, state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
130 131 132 133 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 130 def coerce(value, state:) parsed = parse(value) Anthropic::Internal::Type::Converter.coerce(self.class.model, parsed, state:) end |
#dump(value, state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
124 125 126 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 124 def dump(value, state:) Anthropic::Internal::Type::Converter.dump(self.class.model, value, state:) end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 155 def inspect = "#<#{self.class.inspect(depth: 1)}:0x#{object_id.to_s(16)}>" |
#parse(value) ⇒ Object
Override the #parse method to customize the pre-processing of the tool call argument
144 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 144 def parse(value) = value |
#to_json_schema ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
120 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 120 def to_json_schema = self.class.model&.to_json_schema |
#to_json_schema_inner(state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
116 |
# File 'lib/anthropic/helpers/tools/base_tool.rb', line 116 def to_json_schema_inner(state:) = self.class.model&.to_json_schema_inner(state:) |