Class: Tools::SearchSupportNotesTool

Inherits:
ApplicationTool show all
Defined in:
app/mcp/tools/search_support_notes_tool.rb

Overview

MCP Tool for semantic search across support case activity notes and communications.
Uses pgvector embeddings to find notes/emails matching natural language queries,
filtered to those attached to SupportCase resources.

Activity notes contain technician observations, customer requests, troubleshooting
steps, and resolution details. Communications contain email threads with customers.

rubocop:disable Metrics/ClassLength -- MCP tool: schema + format paths are naturally verbose

Examples:

Search for installation complaints

{ query: "customer having trouble with thermostat wiring" }

Search communications about returns

{ query: "requesting RMA for damaged heating mat", source: "communications" }

Class Method Summary collapse

Class Method Details

.call(query:, source: nil, limit: 10, server_context: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/mcp/tools/search_support_notes_tool.rb', line 48

def call(query:, source: nil, limit: 10, server_context: nil)
  limit = [[limit.to_i, 1].max, 30].min

  types = resolve_types(source)
  return json_response(error: 'source must be omitted or one of: notes, activities, communications, emails') if types.nil?

  results = ContentEmbedding.hybrid_search(
    query,
    limit: limit * 2, # over-fetch to compensate for post-filtering
    types: types,
    exclude_sensitive: false
  )

  # Post-filter to only results linked to a SupportCase
  support_results = results.select do |emb|
    record = emb.embeddable
    next false unless record

    record.respond_to?(:resource_type) && record.resource_type == 'SupportCase'
  end.first(limit)

  embeddables = support_results.filter_map(&:embeddable).compact
  ActiveRecord::Associations::Preloader.new(records: embeddables, associations: :resource).call if embeddables.any?
  support_cases = embeddables.filter_map(&:resource).grep(SupportCase).uniq
  if support_cases.any?
    ActiveRecord::Associations::Preloader.new(
      records: support_cases,
      associations: { support_case_participants: :party }
    ).call
  end

  json_response(
    query: query,
    source_filter: source,
    total_results: support_results.size,
    results: support_results.filter_map { |emb| format_result(emb) }
  )
end