Class: Phlex::Reactive::MCP::BaseTool

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/phlex/reactive/mcp/base_tool.rb

Overview

The base class every phlex-reactive MCP tool subclasses (issue #168), mirroring pgbus's BaseTool. It sets the read-only annotation contract ONCE and overrides annotations_value so subclasses inherit it — MCP::Tool.inherited resets @annotations_value to nil per subclass, so without this each tool would advertise no annotations.

Every tool is READ-ONLY and NON-DESTRUCTIVE: it reads the loaded registry (via Inspector / Doctor) and reports NAMES, PATHS, and declared SCHEMAS only — never a token, secret, or runtime state (the instrumentation privacy contract extended to tooling). There is deliberately NO arbitrary-query tool.

Class Method Summary collapse

Class Method Details

.annotations_valueObject

Inherit the base annotations into every subclass (MCP::Tool.inherited nils @annotations_value, so fall through to the superclass's).



30
31
32
# File 'lib/phlex/reactive/mcp/base_tool.rb', line 30

def annotations_value
  super || (superclass.respond_to?(:annotations_value) ? superclass.annotations_value : nil)
end

.eager_load_app!Object

Populate the Streamable registry the Inspector/Doctor read — the tools call this before introspecting so every app component is loaded. Guarded for a non-Rails / not-yet-booted context.



50
51
52
# File 'lib/phlex/reactive/mcp/base_tool.rb', line 50

def eager_load_app!
  ::Rails.application.eager_load! if defined?(::Rails) && ::Rails.application
end

.error_response(message) ⇒ Object

An error response (the tool ran but has nothing to return — e.g. a find with no match handled by the tool, or a bad filter).



43
44
45
# File 'lib/phlex/reactive/mcp/base_tool.rb', line 43

def error_response(message)
  ::MCP::Tool::Response.new([{ type: "text", text: message }], error: true)
end

.json_response(value) ⇒ Object

A single text-content response carrying JSON.generate(value). The one place tool output is serialized — pretty-printed for a human reading the transcript, still valid JSON for the client.



37
38
39
# File 'lib/phlex/reactive/mcp/base_tool.rb', line 37

def json_response(value)
  ::MCP::Tool::Response.new([{ type: "text", text: JSON.pretty_generate(value) }])
end