Class: Tools::PublishFaqsTool

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

Overview

MCP Tool for publishing existing FAQ articles.

Sunny can create FAQs (create_faq) and embed them into a blog post
(insert_faqs), but embedding alone does NOT make them appear on the live
site: the blog re-renders each FAQ widget through Oembed::FaqProvider with
published_only: true, so a draft FAQ renders as nothing on the public page
even though the editor's widget count includes it. This tool closes that
gap — it flips existing draft FAQs to published, then refreshes the posts
that embed them so the stored body re-renders with the now-visible FAQs.

Examples:

Publish three FAQs curated for a blog post

{ ids: [4817, 2654, 4625] }

Class Method Summary collapse

Class Method Details

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

Publish requested FAQs and refresh the posts that embed them.

Parameters:

  • ids (Array<Integer>)

    FAQ article IDs to publish

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

    MCP server context (unused)

Returns:

  • (MCP::Tool::Response)

    JSON response with publish and refresh outcomes



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/mcp/tools/publish_faqs_tool.rb', line 51

def call(ids:, _server_context: nil)
  # Keep 0 / non-numeric ids: they simply won't match an ArticleFaq and
  # get reported as not_found rather than vanishing from the response.
  lookup_ids = Array(ids).map(&:to_i).uniq
  return error_response('No FAQ IDs provided') if lookup_ids.empty?

  faqs = ArticleFaq.where(id: lookup_ids).index_by(&:id)

  results = lookup_ids.map { |faq_id| publish_one(faqs[faq_id], faq_id) }

  published_ids = results.select { |r| r[:published] }.pluck(:id)
  refreshed_posts = refresh_embedding_posts(published_ids)

  json_response(
    results: results,
    published_count: published_ids.size,
    refreshed_posts: refreshed_posts,
    message: summary_message(results, refreshed_posts)
  )
rescue StandardError => e
  ErrorReporting.error(e, tool: 'publish_faqs')
  error_response("Unexpected error publishing FAQs: #{e.message}")
end