Class: RailsAiBridge::Tools::BaseTool

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/base_tool.rb

Overview

Base class for all MCP tools exposed by rails-ai-bridge. Inherits from the official MCP::Tool to get schema validation, annotations, and protocol compliance for free.

Class Method Summary collapse

Class Method Details

.cached_contextHash

Full introspection hash (TTL + fingerprint invalidation).

Returns:

  • (Hash)

    context payload keyed by introspector symbol



25
26
27
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 25

def cached_context
  ContextProvider.fetch(rails_app)
end

.cached_section(section) ⇒ Object?

Returns a single introspection section via the shared context provider.

Parameters:

  • section (Symbol)

    introspector key

Returns:

  • (Object, nil)

    section payload



33
34
35
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 33

def cached_section(section)
  ContextProvider.fetch_section(section, rails_app)
end

.configRailsAiBridge::Configuration

Returns gem configuration.

Returns:



18
19
20
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 18

def config
  RailsAiBridge.configuration
end

.rails_appRails::Application

Returns the host Rails application.

Returns:

  • (Rails::Application)

    the host Rails application



13
14
15
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 13

def rails_app
  Rails.application
end

.reset_cache!void

This method returns an undefined value.

Clears the shared introspection cache used by MCP tools.



40
41
42
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 40

def reset_cache!
  ContextProvider.reset!
end

.text_response(text) ⇒ MCP::Tool::Response

Wraps markdown (or plain text) in a +MCP::Tool::Response+, truncating when +max_tool_response_chars+ is set on the gem configuration.

Parameters:

  • text (String)

    tool body to return to the client

Returns:

  • (MCP::Tool::Response)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails_ai_bridge/tools/base_tool.rb', line 49

def text_response(text)
  max = RailsAiBridge.configuration.max_tool_response_chars
  if max && text.length > max
    suffix = "\n\n---\n_Response truncated (#{text.length} chars). Use `detail:\"summary\"` for an overview, or filter by a specific item (e.g. `table:\"users\"`)._"
    available_chars = max - suffix.length
    truncated = if available_chars.positive?
                  "#{text[0...available_chars]}#{suffix}"
                else
                  suffix[0...max]
                end
    MCP::Tool::Response.new([{ type: 'text', text: truncated }])
  else
    MCP::Tool::Response.new([{ type: 'text', text: text }])
  end
end