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:, output_schema: nil, annotations: nil, server: nil) ⇒ Tool

Initialize a new Tool

Parameters:

  • name (String)

    the name of the tool

  • description (String)

    the description of the tool

  • schema (Hash)

    the JSON schema for the tool inputs

  • 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



27
28
29
30
31
32
33
34
# File 'lib/mcp_client/tool.rb', line 27

def initialize(name:, description:, schema:, output_schema: nil, annotations: nil, server: nil)
  @name = name
  @description = description
  @schema = schema
  @output_schema = output_schema
  @annotations = annotations
  @server = server
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)



18
# File 'lib/mcp_client/tool.rb', line 18

attr_reader :name, :description, :schema, :output_schema, :annotations, :server

#descriptionString (readonly)

Returns the description of the tool.

Returns:

  • (String)

    the description of the tool



18
# File 'lib/mcp_client/tool.rb', line 18

attr_reader :name, :description, :schema, :output_schema, :annotations, :server

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool



18
19
20
# File 'lib/mcp_client/tool.rb', line 18

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)



18
# File 'lib/mcp_client/tool.rb', line 18

attr_reader :name, :description, :schema, :output_schema, :annotations, :server

#schemaHash (readonly)

Returns the JSON schema for the tool inputs.

Returns:

  • (Hash)

    the JSON schema for the tool inputs



18
# File 'lib/mcp_client/tool.rb', line 18

attr_reader :name, :description, :schema, :output_schema, :annotations, :server

#serverObject (readonly)

Returns the value of attribute server.



18
# File 'lib/mcp_client/tool.rb', line 18

attr_reader :name, :description, :schema, :output_schema, :annotations, :server

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:



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

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]
  new(
    name: data['name'] || data[:name],
    description: data['description'] || data[:description],
    schema: schema,
    output_schema: output_schema,
    annotations: annotations,
    server: server
  )
end

Instance Method Details

#destructive?Boolean

Check if the tool is marked as destructive

Returns:

  • (Boolean)

    true if the tool is destructive



97
98
99
# File 'lib/mcp_client/tool.rb', line 97

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

#read_only?Boolean

Check if the tool is marked as read-only

Returns:

  • (Boolean)

    true if the tool is read-only



91
92
93
# File 'lib/mcp_client/tool.rb', line 91

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

#requires_confirmation?Boolean

Check if the tool requires confirmation before execution

Returns:

  • (Boolean)

    true if the tool requires confirmation



103
104
105
# File 'lib/mcp_client/tool.rb', line 103

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



109
110
111
# File 'lib/mcp_client/tool.rb', line 109

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

#to_anthropic_toolHash

Convert tool to Anthropic Claude tool specification format

Returns:

  • (Hash)

    Anthropic Claude tool specification



71
72
73
74
75
76
77
# File 'lib/mcp_client/tool.rb', line 71

def to_anthropic_tool
  {
    name: @name,
    description: @description,
    input_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



81
82
83
84
85
86
87
# File 'lib/mcp_client/tool.rb', line 81

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/mcp_client/tool.rb', line 58

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