Class: MCPClient::Tool

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

Overview

Representation of an MCP tool

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, schema:, title: nil, output_schema: nil, annotations: nil, server: nil, task_support: nil, icons: nil, meta: nil) ⇒ Tool

Initialize a new Tool rubocop:disable Metrics/ParameterLists

Parameters:

  • name (String)

    the name of the tool

  • description (String)

    the description of the tool

  • schema (Hash)

    the JSON schema for the tool inputs

  • title (String, nil) (defaults to: nil)

    optional human-readable name of the tool for display purposes

  • output_schema (Hash, nil) (defaults to: nil)

    optional JSON schema for structured tool outputs (MCP 2025-06-18)

  • annotations (Hash, nil) (defaults to: nil)

    optional annotations describing tool behavior

  • server (MCPClient::ServerBase, nil) (defaults to: nil)

    the server this tool belongs to

  • task_support (String, nil) (defaults to: nil)

    execution.taskSupport value (MCP 2025-11-25)

  • icons (Array<Hash>, nil) (defaults to: nil)

    optional icons for display in user interfaces (MCP 2025-11-25)

  • meta (Hash, nil) (defaults to: nil)

    optional _meta metadata attached to the tool (MCP 2025-11-25)



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mcp_client/tool.rb', line 42

def initialize(name:, description:, schema:, title: nil, output_schema: nil, annotations: nil, server: nil,
               task_support: nil, icons: nil, meta: nil)
  @name = name
  @title = title
  @description = description
  @schema = schema
  @output_schema = output_schema
  @annotations = annotations
  @server = server
  @task_support = task_support
  @icons = icons
  @meta = meta
end

Instance Attribute Details

#annotationsHash? (readonly)

Returns optional annotations describing tool behavior (e.g., readOnly, destructive).

Returns:

  • (Hash, nil)

    optional annotations describing tool behavior (e.g., readOnly, destructive)



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#descriptionString (readonly)

Returns the description of the tool.

Returns:

  • (String)

    the description of the tool



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#iconsArray<Hash>? (readonly)

Returns optional icons for display in user interfaces (MCP 2025-11-25, SEP-973).

Returns:

  • (Array<Hash>, nil)

    optional icons for display in user interfaces (MCP 2025-11-25, SEP-973)



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#metaObject (readonly)

Returns the value of attribute meta.



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool



27
28
29
# File 'lib/mcp_client/tool.rb', line 27

def name
  @name
end

#output_schemaHash? (readonly)

Returns optional JSON schema for structured tool outputs (MCP 2025-06-18).

Returns:

  • (Hash, nil)

    optional JSON schema for structured tool outputs (MCP 2025-06-18)



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#schemaHash (readonly)

Returns the JSON schema for the tool inputs.

Returns:

  • (Hash)

    the JSON schema for the tool inputs



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#serverMCPClient::ServerBase? (readonly)

Returns the server this tool belongs to.

Returns:



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#task_supportString? (readonly)

Returns tool-level task negotiation (MCP 2025-11-25): 'forbidden' (default), 'optional', or 'required'; nil when not advertised.

Returns:

  • (String, nil)

    tool-level task negotiation (MCP 2025-11-25): 'forbidden' (default), 'optional', or 'required'; nil when not advertised



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

#titleString? (readonly)

Returns optional human-readable name of the tool for display purposes.

Returns:

  • (String, nil)

    optional human-readable name of the tool for display purposes



27
28
# File 'lib/mcp_client/tool.rb', line 27

attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server, :task_support,
:icons, :meta

Class Method Details

.from_json(data, server: nil) ⇒ MCPClient::Tool

Create a Tool instance from JSON data

Parameters:

  • data (Hash)

    JSON data from MCP server

  • server (MCPClient::ServerBase, nil) (defaults to: nil)

    the server this tool belongs to

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mcp_client/tool.rb', line 61

def self.from_json(data, server: nil)
  # Some servers (Playwright MCP CLI) use 'inputSchema' instead of 'schema'
  # Handle both string and symbol keys
  schema = data['inputSchema'] || data[:inputSchema] || data['schema'] || data[:schema]
  output_schema = data['outputSchema'] || data[:outputSchema]
  annotations = data['annotations'] || data[:annotations]
  title = data['title'] || data[:title]
  execution = data['execution'] || data[:execution]
  task_support = execution && (execution['taskSupport'] || execution[:taskSupport])
  icons = data['icons'] || data[:icons]
  meta = data['_meta'] || data[:_meta]
  new(
    name: data['name'] || data[:name],
    description: data['description'] || data[:description],
    schema: schema,
    title: title,
    output_schema: output_schema,
    annotations: annotations,
    server: server,
    task_support: task_support,
    icons: icons,
    meta: meta
  )
end

Instance Method Details

#destructive?Boolean

Check if the tool is marked as destructive (legacy annotation field)

Returns:

  • (Boolean)

    true if the tool is destructive

See Also:



129
130
131
# File 'lib/mcp_client/tool.rb', line 129

def destructive?
  !!(@annotations && @annotations['destructive'] == true)
end

#destructive_hint?Boolean

Check the destructiveHint annotation (MCP 2025-11-25) When true, the tool may perform destructive updates. Only meaningful when readOnlyHint is false. Per the MCP ToolAnnotations schema the default is true, i.e. a non-read-only tool without this hint is assumed to be potentially destructive.

Returns:

  • (Boolean)

    defaults to true when not specified



156
157
158
159
160
# File 'lib/mcp_client/tool.rb', line 156

def destructive_hint?
  return true unless @annotations

  fetch_annotation_hint('destructiveHint', :destructiveHint, true)
end

#idempotent_hint?Boolean

Check the idempotentHint annotation (MCP 2025-11-25) When true, calling the tool repeatedly with the same arguments has no additional effect. Only meaningful when readOnlyHint is false.

Returns:

  • (Boolean)

    defaults to false when not specified



166
167
168
169
170
# File 'lib/mcp_client/tool.rb', line 166

def idempotent_hint?
  return false unless @annotations

  fetch_annotation_hint('idempotentHint', :idempotentHint, false)
end

#open_world_hint?Boolean

Check the openWorldHint annotation (MCP 2025-11-25) When true, the tool may interact with the "open world" (external entities).

Returns:

  • (Boolean)

    defaults to true when not specified



175
176
177
178
179
# File 'lib/mcp_client/tool.rb', line 175

def open_world_hint?
  return true unless @annotations

  fetch_annotation_hint('openWorldHint', :openWorldHint, true)
end

#read_only?Boolean

Check if the tool is marked as read-only (legacy annotation field)

Returns:

  • (Boolean)

    true if the tool is read-only

See Also:



122
123
124
# File 'lib/mcp_client/tool.rb', line 122

def read_only?
  !!(@annotations && @annotations['readOnly'] == true)
end

#read_only_hint?Boolean

Check the readOnlyHint annotation (MCP 2025-11-25) When true, the tool does not modify its environment. Per the MCP ToolAnnotations schema the default is false, i.e. a tool without this hint is assumed to potentially modify its environment.

Returns:

  • (Boolean)

    defaults to false when not specified



144
145
146
147
148
# File 'lib/mcp_client/tool.rb', line 144

def read_only_hint?
  return false unless @annotations

  fetch_annotation_hint('readOnlyHint', :readOnlyHint, false)
end

#requires_confirmation?Boolean

Check if the tool requires confirmation before execution

Returns:

  • (Boolean)

    true if the tool requires confirmation



135
136
137
# File 'lib/mcp_client/tool.rb', line 135

def requires_confirmation?
  !!(@annotations && @annotations['requiresConfirmation'] == true)
end

#structured_output?Boolean

Check if the tool supports structured outputs (MCP 2025-06-18)

Returns:

  • (Boolean)

    true if the tool has an output schema defined



183
184
185
# File 'lib/mcp_client/tool.rb', line 183

def structured_output?
  !@output_schema.nil? && !@output_schema.empty?
end

#supports_task?Boolean

Whether task-augmented execution is allowed for this tool (MCP 2025-11-25). True when execution.taskSupport is 'optional' or 'required'.

Returns:

  • (Boolean)


190
191
192
# File 'lib/mcp_client/tool.rb', line 190

def supports_task?
  %w[optional required].include?(@task_support)
end

#task_forbidden?Boolean

Whether task-augmented execution is forbidden (the default when unset)

Returns:

  • (Boolean)


208
209
210
# File 'lib/mcp_client/tool.rb', line 208

def task_forbidden?
  @task_support.nil? || @task_support == 'forbidden'
end

#task_optional?Boolean

Whether task-augmented execution is optional for this tool

Returns:

  • (Boolean)


202
203
204
# File 'lib/mcp_client/tool.rb', line 202

def task_optional?
  @task_support == 'optional'
end

#task_required?Boolean

Whether task-augmented execution is required for this tool

Returns:

  • (Boolean)


196
197
198
# File 'lib/mcp_client/tool.rb', line 196

def task_required?
  @task_support == 'required'
end

#to_anthropic_toolHash

Convert tool to Anthropic Claude tool specification format

Returns:

  • (Hash)

    Anthropic Claude tool specification with cleaned schema



101
102
103
104
105
106
107
# File 'lib/mcp_client/tool.rb', line 101

def to_anthropic_tool
  {
    name: @name,
    description: @description,
    input_schema: cleaned_schema(@schema)
  }
end

#to_google_toolHash

Convert tool to Google Vertex AI tool specification format

Returns:

  • (Hash)

    Google Vertex AI tool specification with cleaned schema



111
112
113
114
115
116
117
# File 'lib/mcp_client/tool.rb', line 111

def to_google_tool
  {
    name: @name,
    description: @description,
    parameters: cleaned_schema(@schema)
  }
end

#to_openai_toolHash

Convert tool to OpenAI function specification format

Returns:

  • (Hash)

    OpenAI function specification



88
89
90
91
92
93
94
95
96
97
# File 'lib/mcp_client/tool.rb', line 88

def to_openai_tool
  {
    type: 'function',
    function: {
      name: @name,
      description: @description,
      parameters: @schema
    }
  }
end