# Add plugin data to native MCP topics

**URL:** <https://meta.discourse.org/t/add-plugin-data-to-native-mcp-topics/418891>\
**Category:** Developer Guides\
**Created:** [2 Dicembre 2026, 3:19pm UTC](https://meta.discourse.org/t/add-plugin-data-to-native-mcp-topics/418891 "2026-12-02T15:19:41Z")\
**Posts on this page:** 1\
**Page:** 1

## 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](/t/-/418891/1) demonstrates how to register the modifier and conditionally add data based on site settings and guardian checks.

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2 Dicembre 2026, 3:19pm UTC](https://meta.discourse.org/t/add-plugin-data-to-native-mcp-topics/418891/1 "2026-12-02T15:19:41Z")

</div>

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.

```ruby
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](https://github.com/discourse/discourse/blob/main/docs/developer-guides/docs/04-plugins/08-mcp-topic-data.md).
