Class: ModelContextProtocol::Server::Tool

Inherits:
Object
  • Object
show all
Includes:
Cancellable, ContentHelpers, Progressable
Defined in:
lib/model_context_protocol/server/tool.rb

Defined Under Namespace

Classes: DefinitionDSL, OutputSchemaValidationError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Progressable

#progressable

Methods included from ContentHelpers

#audio_content, #embedded_resource_content, #image_content, #resource_link, #text_content

Methods included from Cancellable

#cancellable

Constructor Details

#initialize(arguments, logger, context = {}) ⇒ Tool

Returns a new instance of Tool.



14
15
16
17
18
19
# File 'lib/model_context_protocol/server/tool.rb', line 14

def initialize(arguments, logger, context = {})
  validate!(arguments)
  @arguments = arguments
  @context = context
  @logger = logger
end

Class Attribute Details

.descriptionObject (readonly)

Returns the value of attribute description.



85
86
87
# File 'lib/model_context_protocol/server/tool.rb', line 85

def description
  @description
end

.input_schemaObject (readonly)

Returns the value of attribute input_schema.



85
86
87
# File 'lib/model_context_protocol/server/tool.rb', line 85

def input_schema
  @input_schema
end

.nameObject (readonly)

Returns the value of attribute name.



85
86
87
# File 'lib/model_context_protocol/server/tool.rb', line 85

def name
  @name
end

.output_schemaObject (readonly)

Returns the value of attribute output_schema.



85
86
87
# File 'lib/model_context_protocol/server/tool.rb', line 85

def output_schema
  @output_schema
end

.titleObject (readonly)

Returns the value of attribute title.



85
86
87
# File 'lib/model_context_protocol/server/tool.rb', line 85

def title
  @title
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



12
13
14
# File 'lib/model_context_protocol/server/tool.rb', line 12

def arguments
  @arguments
end

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/model_context_protocol/server/tool.rb', line 12

def context
  @context
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/model_context_protocol/server/tool.rb', line 12

def logger
  @logger
end

Class Method Details

.call(arguments, logger, context = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/model_context_protocol/server/tool.rb', line 106

def call(arguments, logger, context = {})
  new(arguments, logger, context).call
rescue JSON::Schema::ValidationError => validation_error
  raise ModelContextProtocol::Server::ParameterValidationError, validation_error.message
rescue OutputSchemaValidationError, ModelContextProtocol::Server::ResponseArgumentsError => tool_error
  raise tool_error, tool_error.message
rescue Server::Cancellable::CancellationError
  raise
rescue => error
  ErrorResponse[error: error.message]
end

.define(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/model_context_protocol/server/tool.rb', line 87

def define(&block)
  definition_dsl = DefinitionDSL.new
  definition_dsl.instance_eval(&block)

  @name = definition_dsl.name
  @description = definition_dsl.description
  @title = definition_dsl.title
  @input_schema = definition_dsl.input_schema
  @output_schema = definition_dsl.output_schema
end

.definitionObject



118
119
120
121
122
123
# File 'lib/model_context_protocol/server/tool.rb', line 118

def definition
  result = {name: @name, description: @description, inputSchema: @input_schema}
  result[:title] = @title if @title
  result[:outputSchema] = @output_schema if @output_schema
  result
end

.inherited(subclass) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/model_context_protocol/server/tool.rb', line 98

def inherited(subclass)
  subclass.instance_variable_set(:@name, @name)
  subclass.instance_variable_set(:@description, @description)
  subclass.instance_variable_set(:@title, @title)
  subclass.instance_variable_set(:@input_schema, @input_schema)
  subclass.instance_variable_set(:@output_schema, @output_schema)
end

Instance Method Details

#callObject

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/model_context_protocol/server/tool.rb', line 21

def call
  raise NotImplementedError, "Subclasses must implement the call method"
end