Method: ActionMCP::Tool#call

Defined in:
lib/action_mcp/tool.rb

#callObject

Public entry point for executing the tool Returns an array of Content objects collected from render calls



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/action_mcp/tool.rb', line 362

def call
  @response = ToolResponse.new
  performed = false            # ← track execution

  if valid?
    begin
      run_callbacks :perform do
        performed = true       # ← set if we reach the block
        perform
      end
    rescue StandardError => e
      # Show generic error message for HTTP requests, detailed for direct calls
      error_message = if execution_context[:request].present?
                         "An unexpected error occurred."
      else
        e.message
      end
      @response.mark_as_error!(:internal_error, message: error_message)
    end
  else
    @response.mark_as_error!(:invalid_params,
                             message: "Invalid input",
                             data: errors.full_messages)
  end

  # If callbacks halted execution (`performed` still false) and
  # nothing else marked an error, surface it as invalid_params.
  if !performed && !@response.error?
    @response.mark_as_error!(:invalid_params, message: "Tool execution was aborted")
  end

  @response
end