Class: MCPClient::Tool
- Inherits:
-
Object
- Object
- MCPClient::Tool
- Defined in:
- lib/mcp_client/tool.rb
Overview
Representation of an MCP tool
Instance Attribute Summary collapse
-
#annotations ⇒ Hash?
readonly
Optional annotations describing tool behavior (e.g., readOnly, destructive).
-
#description ⇒ String
readonly
The description of the tool.
-
#name ⇒ String
readonly
The name of the tool.
-
#output_schema ⇒ Hash?
readonly
Optional JSON schema for structured tool outputs (MCP 2025-06-18).
-
#schema ⇒ Hash
readonly
The JSON schema for the tool inputs.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#title ⇒ String?
readonly
Optional human-readable name of the tool for display purposes.
Class Method Summary collapse
-
.from_json(data, server: nil) ⇒ MCPClient::Tool
Create a Tool instance from JSON data.
Instance Method Summary collapse
-
#destructive? ⇒ Boolean
Check if the tool is marked as destructive (legacy annotation field).
-
#destructive_hint? ⇒ Boolean
Check the destructiveHint annotation (MCP 2025-11-25) When true, the tool may perform destructive updates.
-
#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.
-
#initialize(name:, description:, schema:, title: nil, output_schema: nil, annotations: nil, server: nil) ⇒ Tool
constructor
Initialize a new Tool.
-
#open_world_hint? ⇒ Boolean
Check the openWorldHint annotation (MCP 2025-11-25) When true, the tool may interact with the “open world” (external entities).
-
#read_only? ⇒ Boolean
Check if the tool is marked as read-only (legacy annotation field).
-
#read_only_hint? ⇒ Boolean
Check the readOnlyHint annotation (MCP 2025-11-25) When true, the tool does not modify its environment.
-
#requires_confirmation? ⇒ Boolean
Check if the tool requires confirmation before execution.
-
#structured_output? ⇒ Boolean
Check if the tool supports structured outputs (MCP 2025-06-18).
-
#to_anthropic_tool ⇒ Hash
Convert tool to Anthropic Claude tool specification format.
-
#to_google_tool ⇒ Hash
Convert tool to Google Vertex AI tool specification format.
-
#to_openai_tool ⇒ Hash
Convert tool to OpenAI function specification format.
Constructor Details
#initialize(name:, description:, schema:, title: nil, output_schema: nil, annotations: nil, server: nil) ⇒ Tool
Initialize a new Tool
30 31 32 33 34 35 36 37 38 |
# File 'lib/mcp_client/tool.rb', line 30 def initialize(name:, description:, schema:, title: nil, output_schema: nil, annotations: nil, server: nil) @name = name @title = title @description = description @schema = schema @output_schema = output_schema @annotations = annotations @server = server end |
Instance Attribute Details
#annotations ⇒ Hash? (readonly)
Returns optional annotations describing tool behavior (e.g., readOnly, destructive).
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
#description ⇒ String (readonly)
Returns the description of the tool.
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
#name ⇒ String (readonly)
Returns the name of the tool.
20 21 22 |
# File 'lib/mcp_client/tool.rb', line 20 def name @name end |
#output_schema ⇒ Hash? (readonly)
Returns optional JSON schema for structured tool outputs (MCP 2025-06-18).
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
#schema ⇒ Hash (readonly)
Returns the JSON schema for the tool inputs.
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
#server ⇒ Object (readonly)
Returns the value of attribute server.
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
#title ⇒ String? (readonly)
Returns optional human-readable name of the tool for display purposes.
20 |
# File 'lib/mcp_client/tool.rb', line 20 attr_reader :name, :title, :description, :schema, :output_schema, :annotations, :server |
Class Method Details
.from_json(data, server: nil) ⇒ MCPClient::Tool
Create a Tool instance from JSON data
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mcp_client/tool.rb', line 44 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] new( name: data['name'] || data[:name], description: data['description'] || data[:description], schema: schema, title: title, output_schema: output_schema, annotations: annotations, server: server ) end |
Instance Method Details
#destructive? ⇒ Boolean
Check if the tool is marked as destructive (legacy annotation field)
105 106 107 |
# File 'lib/mcp_client/tool.rb', line 105 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.
128 129 130 131 132 |
# File 'lib/mcp_client/tool.rb', line 128 def destructive_hint? return false unless @annotations fetch_annotation_hint('destructiveHint', :destructiveHint, false) 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.
138 139 140 141 142 |
# File 'lib/mcp_client/tool.rb', line 138 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).
147 148 149 150 151 |
# File 'lib/mcp_client/tool.rb', line 147 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)
98 99 100 |
# File 'lib/mcp_client/tool.rb', line 98 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.
118 119 120 121 122 |
# File 'lib/mcp_client/tool.rb', line 118 def read_only_hint? return true unless @annotations fetch_annotation_hint('readOnlyHint', :readOnlyHint, true) end |
#requires_confirmation? ⇒ Boolean
Check if the tool requires confirmation before execution
111 112 113 |
# File 'lib/mcp_client/tool.rb', line 111 def requires_confirmation? !!(@annotations && @annotations['requiresConfirmation'] == true) end |
#structured_output? ⇒ Boolean
Check if the tool supports structured outputs (MCP 2025-06-18)
155 156 157 |
# File 'lib/mcp_client/tool.rb', line 155 def structured_output? !@output_schema.nil? && !@output_schema.empty? end |
#to_anthropic_tool ⇒ Hash
Convert tool to Anthropic Claude tool specification format
77 78 79 80 81 82 83 |
# File 'lib/mcp_client/tool.rb', line 77 def to_anthropic_tool { name: @name, description: @description, input_schema: cleaned_schema(@schema) } end |
#to_google_tool ⇒ Hash
Convert tool to Google Vertex AI tool specification format
87 88 89 90 91 92 93 |
# File 'lib/mcp_client/tool.rb', line 87 def to_google_tool { name: @name, description: @description, parameters: cleaned_schema(@schema) } end |
#to_openai_tool ⇒ Hash
Convert tool to OpenAI function specification format
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mcp_client/tool.rb', line 64 def to_openai_tool { type: 'function', function: { name: @name, description: @description, parameters: @schema } } end |