Skip to content

A platform for community discussion. Free, open, simple.

FIX: Respect PM read scope when listing MCP notifications (#43677)

Natalie Tayb301f2c84acf
diff --git a/lib/discourse_mcp/tools/notifications.rb b/lib/discourse_mcp/tools/notifications.rb
index ab34227f459..23003277317 100644
--- a/lib/discourse_mcp/tools/notifications.rb
+++ b/lib/discourse_mcp/tools/notifications.rb
@@ -14,6 +14,13 @@ module DiscourseMcp
.includes(:topic)
.order(id: :desc)
.limit(limit)
+ if !request_context.has_scopes?("mcp:private-messages:read")
+ notifications =
+ notifications.left_joins(:topic).where(
+ "topics.id IS NULL OR topics.archetype <> ?",
+ Archetype.private_message,
+ )
+ end
notifications =
Notification.filter_inaccessible_topic_notifications(
request_context.guardian,
diff --git a/spec/lib/discourse_mcp/regular_user_tools_spec.rb b/spec/lib/discourse_mcp/regular_user_tools_spec.rb
index e68ab263165..26116ffafe1 100644
--- a/spec/lib/discourse_mcp/regular_user_tools_spec.rb
+++ b/spec/lib/discourse_mcp/regular_user_tools_spec.rb
@@ -239,11 +239,11 @@ describe DiscourseMcp::Tools do
private_topic =
Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))
hidden_notification = Fabricate(:notification, user:, topic: private_topic)
+ context = request_context(user)
+ allow(context).to receive(:has_scopes?).with("mcp:private-messages:read").and_return(true)
result =
- described_class.call(arguments: {}, request_context: request_context(user)).fetch(
- :structuredContent,
- )
+ described_class.call(arguments: {}, request_context: context).fetch(:structuredContent)
notification_ids = result.fetch(:notifications).pluck(:id)
expect(notification_ids).to include(visible_notification.id)
diff --git a/spec/requests/mcp_content_access_spec.rb b/spec/requests/mcp_content_access_spec.rb
index 6886cdd4e39..90e3456b22b 100644
--- a/spec/requests/mcp_content_access_spec.rb
+++ b/spec/requests/mcp_content_access_spec.rb
@@ -300,6 +300,26 @@ describe "MCP content access" do
)
end
+ it "filters private-message notifications before the limit unless the read scope is granted" do
+ public_notification = Fabricate(:notification, user:)
+ other_notification = Fabricate(:notification, user:, topic: nil)
+ private_notification = Fabricate(:private_message_notification, user:, topic: message.topic)
+ authorize
+
+ call_tool("discourse_notification_list", { limit: 2 })
+
+ expect(response.status).to eq(200)
+ notifications = response.parsed_body.dig("result", "structuredContent", "notifications")
+ expect(notifications.pluck("id")).to eq([other_notification.id, public_notification.id])
+
+ authorize("mcp:private-messages:read")
+ call_tool("discourse_notification_list", { limit: 2 })
+
+ expect(response.status).to eq(200)
+ notifications = response.parsed_body.dig("result", "structuredContent", "notifications")
+ expect(notifications.pluck("id")).to eq([private_notification.id, other_notification.id])
+ end
+
it "filters hidden private-message posts when category moderation is enabled" do
SiteSetting.enable_category_group_moderation = true
authorize("mcp:private-messages:read")