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:, 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

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

    the server this tool belongs to



21
22
23
24
25
26
# File 'lib/mcp_client/tool.rb', line 21

def initialize(name:, description:, schema:, server: nil)
  @name = name
  @description = description
  @schema = schema
  @server = server
end

Instance Attribute Details

#descriptionString (readonly)

Returns the description of the tool.

Returns:

  • (String)

    the description of the tool



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

attr_reader :name, :description, :schema, :server

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool



14
15
16
# File 'lib/mcp_client/tool.rb', line 14

def name
  @name
end

#schemaHash (readonly)

Returns the JSON schema for the tool.

Returns:

  • (Hash)

    the JSON schema for the tool



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

attr_reader :name, :description, :schema, :server

#serverObject (readonly)

Returns the value of attribute server.



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

attr_reader :name, :description, :schema, :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:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mcp_client/tool.rb', line 32

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

Instance Method Details

#to_anthropic_toolHash

Convert tool to Anthropic Claude tool specification format

Returns:

  • (Hash)

    Anthropic Claude tool specification



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

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



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

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



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

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