Class: Anthropic::Helpers::Tools::Mcp::Tool Private

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/anthropic/helpers/tools/mcp.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Runnable tool backing tool. Each call to Tool.build produces a fresh anonymous subclass — that subclass owns a unique inner “parsed input” class so the runner can dispatch tool calls via ‘class.model === tool_use.parsed`, the same path used by hand-written BaseTool subclasses.

Constant Summary

Constants included from InputSchema::JsonSchemaConverter

InputSchema::JsonSchemaConverter::NO_REF, InputSchema::JsonSchemaConverter::POINTERS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTool

description, #dump, input_schema, inspect, #inspect, #parse

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, dump, #inspect, inspect, 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

.mcp_clientObject

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.



48
49
50
# File 'lib/anthropic/helpers/tools/mcp.rb', line 48

def mcp_client
  @mcp_client
end

.mcp_input_schemaObject

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.



48
49
50
# File 'lib/anthropic/helpers/tools/mcp.rb', line 48

def mcp_input_schema
  @mcp_input_schema
end

Class Method Details

.build(mcp_tool:, mcp_client:, cache_control: nil, defer_loading: nil, allowed_callers: nil, eager_input_streaming: nil, input_examples: nil, strict: nil) ⇒ 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.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/anthropic/helpers/tools/mcp.rb', line 51

def self.build(
  mcp_tool:,
  mcp_client:,
  cache_control: nil,
  defer_loading: nil,
  allowed_callers: nil,
  eager_input_streaming: nil,
  input_examples: nil,
  strict: nil
)
  api_name, description, raw_schema = Mcp.send(:extract_tool_fields, mcp_tool)
  raise ArgumentError, "MCP tool is missing a `name`" if api_name.to_s.empty?

  extras = {
    cache_control: cache_control,
    defer_loading: defer_loading,
    allowed_callers: allowed_callers,
    eager_input_streaming: eager_input_streaming,
    input_examples: input_examples,
    strict: strict
  }.compact

  input_class = Class.new(Hash)

  klass = Class.new(self)
  klass.description(description) if description
  klass.tool_name = api_name
  klass.tool_extra_props = extras
  klass.mcp_input_schema = Mcp.send(:normalize_schema, raw_schema)
  klass.mcp_client = mcp_client
  klass.instance_variable_set(:@model, input_class)
  klass.new
end

Instance Method Details

#call(parsed) ⇒ 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.



113
114
115
116
117
# File 'lib/anthropic/helpers/tools/mcp.rb', line 113

def call(parsed)
  args = parsed.is_a?(Hash) ? parsed.to_h : parsed
  response = self.class.mcp_client.call_tool(name: self.class.tool_name, arguments: args)
  Mcp.send(:convert_tool_result, response)
end

#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.

MCP arguments are opaque JSON objects — pass through unchanged. The returned object is an instance of this subclass’s per-tool ‘model`, which is both a Hash (for downstream serialization) and uniquely typed so the runner can identify which MCP tool the parsed input belongs to.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/anthropic/helpers/tools/mcp.rb', line 101

def coerce(value, state:)
  state.fetch(:exactness)[:yes] += 1
  wrapper = self.class.model.new
  input = if value.is_a?(Hash)
    value
  else
    (value.respond_to?(:to_h) ? value.to_h : {})
  end
  input.each { |k, v| wrapper[k] = v }
  wrapper
end

#to_json_schemaObject

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.



92
93
94
# File 'lib/anthropic/helpers/tools/mcp.rb', line 92

def to_json_schema
  Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_json_schema(self)
end

#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.

Return a deep copy of the raw schema; ‘SupportedSchemas.transform_schema!` mutates the hash it receives.



87
88
89
90
# File 'lib/anthropic/helpers/tools/mcp.rb', line 87

def to_json_schema_inner(state:)
  _ = state
  Marshal.load(Marshal.dump(self.class.mcp_input_schema))
end