Class: RailsAiBridge::Tools::ExplainSymbol

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/explain_symbol.rb,
lib/rails_ai_bridge/tools/explain_symbol/cli_explorer.rb

Overview

MCP tool that explains a Ruby/Rails symbol from a local CodeGraph index.

When .codegraph/ exists under Rails.root, this tool runs codegraph explore (or a stubbed explorer in tests) and returns truncated markdown. Missing indexes and CLI failures return a setup message — the tool never raises to the MCP host and never makes network calls.

Examples:

Explain a class

rails_explain_symbol query=User

Alias input

rails_explain_symbol symbol=GetSchema

Defined Under Namespace

Classes: CliExplorer, ExploreError

Constant Summary collapse

INDEX_DIR_NAME =
'.codegraph'
SETUP_HINT =
'Generate a local index with `codegraph init` or `codegraph index` ' \
'(no network required), then retry.'
MISSING_INDEX_MESSAGE =
"No local CodeGraph index found at `#{INDEX_DIR_NAME}/`. #{SETUP_HINT}".freeze
BLANK_QUERY_MESSAGE =
'Provide a `symbol` or `query` string (for example `User` or `User#save`).'

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from BaseTool

cached_context, cached_section, config, rails_app, reset_cache!, text_response

Class Attribute Details

.explorer#explore

Returns explorer responding to explore(query).

Returns:

  • (#explore)

    explorer responding to explore(query)



70
71
72
# File 'lib/rails_ai_bridge/tools/explain_symbol.rb', line 70

def explorer
  @explorer ||= CliExplorer.new(root: rails_root)
end

Class Method Details

.call(symbol: nil, query: nil, _server_context: nil) ⇒ MCP::Tool::Response

Returns markdown explanation or a setup/error message.

Parameters:

  • symbol (String, nil) (defaults to: nil)

    symbol name alias for query

  • query (String, nil) (defaults to: nil)

    CodeGraph explore query

  • _server_context (Object, nil) (defaults to: nil)

    reserved for MCP transport metadata (unused)

Returns:

  • (MCP::Tool::Response)

    markdown explanation or a setup/error message

Raises:

  • (ExploreError)

    when the local CLI fails (rescued into a tool response)

  • (StandardError)

    unexpected explorer failures (rescued into a tool response)



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_ai_bridge/tools/explain_symbol.rb', line 56

def call(symbol: nil, query: nil, _server_context: nil)
  resolved = resolve_query(symbol, query)
  return text_response(BLANK_QUERY_MESSAGE) if resolved.empty?
  return text_response(MISSING_INDEX_MESSAGE) unless index_present?

  text_response(explorer.explore(resolved))
rescue ExploreError => error
  text_response(command_failure_message(error))
rescue StandardError => error
  log_unexpected_error(error)
  text_response(command_failure_message(error))
end

.index_present?Boolean

Returns whether .codegraph/ exists under the application root.

Returns:

  • (Boolean)

    whether .codegraph/ exists under the application root



82
83
84
# File 'lib/rails_ai_bridge/tools/explain_symbol.rb', line 82

def index_present?
  Dir.exist?(File.join(rails_root, INDEX_DIR_NAME))
end

.reset!void

This method returns an undefined value.

Clears a stubbed explorer so the next call builds the default.



77
78
79
# File 'lib/rails_ai_bridge/tools/explain_symbol.rb', line 77

def reset!
  @explorer = nil
end