Class: VectorMCP::Rails::Tool
Overview
Rails-aware base class for declarative tool definitions.
Adds ergonomics for the common patterns that show up in ActiveRecord- backed MCP tools:
-
find!– fetch a record or raiseVectorMCP::NotFoundError -
respond_with– standard success/error payload from a record -
with_transaction– wrap a mutation in an AR transaction -
Auto-rescue of
ActiveRecord::RecordNotFound(-> NotFoundError) andActiveRecord::RecordInvalid(-> error payload) -
Arguments delivered to
#callas aHashWithIndifferentAccesssoargs[:id]and args both work
Constant Summary
Constants inherited from Tool
Tool::COERCERS, Tool::TYPE_MAP
Instance Method Summary collapse
-
#find!(model, id) ⇒ ActiveRecord::Base
Finds a record by id or raises VectorMCP::NotFoundError.
-
#respond_with(record, **extras) ⇒ Hash
Builds a standard response payload from a record.
-
#with_transaction ⇒ Object
Runs the given block inside an ActiveRecord transaction.
Methods inherited from Tool
#call, coerce_args, description, inherited, param, to_definition, tool_name
Instance Method Details
#find!(model, id) ⇒ ActiveRecord::Base
Finds a record by id or raises VectorMCP::NotFoundError.
58 59 60 61 |
# File 'lib/vector_mcp/rails/tool.rb', line 58 def find!(model, id) model.find_by(id: id) || raise(VectorMCP::NotFoundError, "#{model.name} #{id} not found") end |
#respond_with(record, **extras) ⇒ Hash
Builds a standard response payload from a record.
Success shape: { success: true, id: record.id, **extras } Error shape: { success: false, errors: record.errors.full_messages }
71 72 73 74 75 76 77 |
# File 'lib/vector_mcp/rails/tool.rb', line 71 def respond_with(record, **extras) if record.persisted? && record.errors.empty? { success: true, id: record.id, **extras } else { success: false, errors: record.errors. } end end |
#with_transaction ⇒ Object
Runs the given block inside an ActiveRecord transaction.
80 81 82 |
# File 'lib/vector_mcp/rails/tool.rb', line 80 def with_transaction(&) ActiveRecord::Base.transaction(&) end |