Class: VectorMCP::Rails::Tool
- Inherits:
-
Tool
- Object
- Tool
- VectorMCP::Rails::Tool
- Defined in:
- lib/vector_mcp/rails/tool.rb
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::NotFoundErrorrespond_with-- standard success/error payload from a recordwith_transaction-- wrap a mutation in an AR transaction- Auto-rescue of
ActiveRecord::RecordNotFound(-> NotFoundError) andActiveRecord::RecordInvalid(-> error payload) - Arguments delivered to
#callas aHashWithIndifferentAccesssoargs[:id]andargs["id"]both work
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.
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 |