Add plugin data to native MCP topics

AI-generated summary

The mcp_topic_plugin_data modifier enables plugins to attach custom data to native MCP topics, specifically affecting the discourse_read_topic and discourse_filter_topics tools. This modifier executes once per returned topic batch and receives three arguments: data (a hash keyed by topic ID), topics (the array of authorized topics), and request_context (which provides the current guardian and scope checks).

Developers must return the complete data hash while preserving entries from other plugins and using the plugin name as a namespace. It is crucial not to alter the topics or return data for unauthorized topic IDs. All values must be JSON-serializable, noting that integer hash keys are converted to strings in the response. This data appears in both text and structured content, but returning an invalid value or raising an exception causes the tool call to fail.

The guide emphasizes strict security practices. A topic’s visibility does not automatically grant access to all plugin data; developers must verify the current user’s permissions and delegated scopes for each field. Sensitive information such as secrets or email addresses should never be returned. Additionally, reply visibility must be checked before including reply-related data, and accessing private messages requires a dedicated MCP scope.

For performance, related records should be loaded in batches rather than per topic. If a user cannot see any data for a specific namespace, that namespace should be omitted. Finally, native tools will exclude the plugin_data key entirely if the topic entry’s plugin data is empty. An example implementation demonstrates how to register the modifier and conditionally add data based on site settings and guardian checks.

The mcp_topic_plugin_data modifier adds plugin-owned data to discourse_read_topic
and discourse_filter_topics. It runs once for each returned topic batch.

The modifier receives three arguments:

  • data: a hash keyed by integer topic ID. Each value is a hash keyed by plugin name.
  • topics: the array of topics that the native tool has authorized for this request.
  • request_context: the current DiscourseMcp::RequestContext. It provides the
    current guardian and delegated scope checks through has_scopes? and ensure_scopes!.

Return the complete data hash. Preserve data from other plugins. Use the plugin
name as a namespace. Do not change the topics or return data for other topic IDs.
Values must be JSON-serializable. Integer hash keys become strings in the response.
The data appears in both text content and structured content. An invalid modifier
return value or an exception fails the tool call.

after_initialize do
  register_modifier(:mcp_topic_plugin_data) do |data, topics, request_context|
    next data unless SiteSetting.example_enabled

    topics.each do |topic|
      next unless request_context.guardian.can_edit?(topic)

      data[topic.id] = data.fetch(topic.id, {}).merge(
        "example" => { editable: true },
      )
    end
    data
  end
end

A topic’s visibility does not grant access to all plugin data. Check the current
user’s permissions and any required delegated scopes for each field. Do not return
secrets or email addresses. Check reply visibility before returning data about a
reply. Private-message access also requires the dedicated MCP scope.

Load related records for the whole batch, not once per topic. Omit a namespace when
there is no data that the user can see. Native tools omit plugin_data when the
entire topic entry is empty.


This document is version controlled - suggest changes on github.