A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
describe "MCP content access" do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:group)
fab!(:message) { Fabricate(:private_message_post, user:, recipient: Fabricate(:user)) }
before do
SiteSetting.mcp_server_enabled = true
group.add(user)
SearchIndexer.enable
end
after { SearchIndexer.disable }
def authorize(*extra_scopes)
scopes = %w[mcp:profile:read mcp:content:read mcp:content:write] + extra_scopes
scopes.each { |scope| McpGroupScope.find_or_create_by!(group:, scope:) }
client =
McpOauthClient.create!(
client_id: SecureRandom.hex,
name: "Scope test client",
registration_type: "pre_registered",
trust_state: "approved",
redirect_uris: ["http://127.0.0.1/callback"],
)
authorization =
DiscourseMcp::OAuth::AuthorizationGrant.create!(
user:,
client:,
redirect_uri: client.redirect_uris.first,
requested_scopes: scopes,
)
@token = McpOauthAccessToken.issue!(authorization:)
end
def call_mcp(method, params)
post "/mcp",
params: { jsonrpc: "2.0", id: 1, method:, params: }.to_json,
headers: {
"HTTP_AUTHORIZATION" => "Bearer #{@token}",
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json, text/event-stream",
"HTTP_MCP_PROTOCOL_VERSION" => DiscourseMcp::LEGACY_PROTOCOL_VERSION,
}
end
def call_tool(name, arguments)
McpPrimitive.find_or_create_by!(kind: "tool", identifier: name) do |primitive|
primitive.enabled = true
end
call_mcp("tools/call", { name:, arguments: })
end
def expect_missing_scope(scope)
expect(response.status).to eq(403)
expect(response.headers["WWW-Authenticate"]).to include(
'error="insufficient_scope"',
%(scope="#{scope}"),
)
expect(response.body).not_to include(message.raw)
end
it "requires the dedicated moderation, site-setting, and theme scopes" do
authorize
operations = {
"discourse_get_review_queue_count" => [{}, "mcp:moderation:read"],
"discourse_list_reviewables" => [{}, "mcp:moderation:read"],
"discourse_list_reviewable_topics" => [{}, "mcp:moderation:read"],
"discourse_get_reviewable" => [{ reviewable_id: 1 }, "mcp:moderation:read"],
"discourse_get_user_moderation_summary" => [
{ username: user.username },
"mcp:moderation:read",
],
"discourse_get_post_revision" => [{ post_id: 1 }, "mcp:moderation:read"],
"discourse_perform_reviewable_action" => [
{ reviewable_id: 1, action_id: "reject", confirm: true },
"mcp:moderation:write",
],
"discourse_list_site_settings" => [{}, "mcp:site-settings:read"],
"discourse_update_site_setting" => [
{
setting: "title",
operation: "set",
value: "Not applied",
expected_current_value: SiteSetting.title,
confirm_change: true,
},
"mcp:site-settings:write",
],
"discourse_get_theme" => [{ theme_id: -1 }, "mcp:themes:read"],
"discourse_create_theme" => [{ name: "Not created" }, "mcp:themes:write"],
"discourse_update_theme" => [{ theme_id: 1, name: "Not applied" }, "mcp:themes:write"],
}
operations.each do |name, (arguments, scope)|
call_tool(name, arguments)
aggregate_failures(name) { expect_missing_scope(scope) }
end
expect(SiteSetting.title).not_to eq("Not applied")
end
it "requires theme write scope even when site-setting scopes are granted" do
user.update!(admin: true)
authorize("mcp:site-settings:read", "mcp:site-settings:write")
theme = Fabricate(:theme)
call_tool("discourse_create_theme", { name: "Not created" })
expect_missing_scope("mcp:themes:write")
call_tool("discourse_update_theme", { theme_id: theme.id, name: "Not applied" })
expect_missing_scope("mcp:themes:write")
end
it "allows theme readers to inspect themes without granting write access" do
user.update!(admin: true)
authorize("mcp:themes:read")
theme = Fabricate(:theme)
theme.set_field(target: :common, name: "header", value: "<div>Theme header</div>")
theme.save!
call_tool("discourse_get_theme", { theme_id: theme.id })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "theme")).to include(
"id" => theme.id,
"name" => theme.name,
"theme_fields" => [include("name" => "header", "value" => "<div>Theme header</div>")],
)
call_tool("discourse_get_theme", { theme_id: -1 })
expect(response.parsed_body.dig("result", "structuredContent", "theme", "id")).to eq(-1)
call_tool("discourse_create_theme", { name: "Not created" })
expect_missing_scope("mcp:themes:write")
call_tool("discourse_update_theme", { theme_id: theme.id, name: "Not applied" })
expect_missing_scope("mcp:themes:write")
end
it "returns component relationships so writers can preserve existing attachments" do
user.update!(admin: true)
authorize("mcp:themes:read", "mcp:themes:write")
existing_component = Fabricate(:theme, component: true)
new_component = Fabricate(:theme, component: true)
theme = Fabricate(:theme, child_theme_ids: [existing_component.id])
call_tool("discourse_get_theme", { theme_id: theme.id })
theme_json = response.parsed_body.dig("result", "structuredContent", "theme")
expect(theme_json["child_theme_ids"]).to contain_exactly(existing_component.id)
expect(theme_json["parent_theme_ids"]).to eq([])
call_tool(
"discourse_update_theme",
{ theme_id: theme.id, child_theme_ids: theme_json["child_theme_ids"] + [new_component.id] },
)
expect(
response.parsed_body.dig("result", "structuredContent", "theme", "child_theme_ids"),
).to contain_exactly(existing_component.id, new_component.id)
call_tool("discourse_get_theme", { theme_id: existing_component.id })
expect(
response.parsed_body.dig("result", "structuredContent", "theme", "parent_theme_ids"),
).to contain_exactly(theme.id)
end
it "allows readers to inspect remote theme source" do
user.update!(admin: true)
authorize("mcp:themes:read")
remote_theme = RemoteTheme.create!(remote_url: "https://example.com/theme.git")
theme = Fabricate(:theme, remote_theme:)
source = "<div>Remote theme header</div>"
theme.set_field(target: :common, name: "header", value: source)
theme.save!
call_tool("discourse_get_theme", { theme_id: theme.id })
expect(response.status).to eq(200)
expect(
response.parsed_body.dig("result", "structuredContent", "theme", "theme_fields"),
).to contain_exactly(include("name" => "header", "value" => source))
end
it "keeps theme tools admin-only when theme scopes are granted" do
authorize("mcp:themes:read", "mcp:themes:write")
{
"discourse_get_theme" => {
theme_id: -1,
},
"discourse_create_theme" => {
name: "Not created",
},
"discourse_update_theme" => {
theme_id: -1,
user_selectable: true,
},
}.each do |name, arguments|
call_tool(name, arguments)
expect(response.status).to eq(403)
expect(response.parsed_body.dig("error", "message")).to eq("Not authorized")
end
end
it "allows theme writers to attach components to built-in themes" do
user.update!(admin: true)
authorize("mcp:themes:write")
component = Fabricate(:theme, component: true)
foundation = Theme.find(-1)
horizon = Theme.find(-2)
call_tool(
"discourse_update_theme",
{ theme_id: foundation.id, child_theme_ids: [component.id] },
)
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "isError")).to eq(false)
expect(foundation.reload.child_theme_ids).to contain_exactly(component.id)
call_tool("discourse_update_theme", { theme_id: component.id, parent_theme_ids: [horizon.id] })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "isError")).to eq(false)
expect(component.reload.parent_theme_ids).to contain_exactly(horizon.id)
end
it "returns an actionable tool error for an unknown theme field type" do
user.update!(admin: true)
authorize("mcp:themes:write")
theme = Fabricate(:theme)
call_tool(
"discourse_update_theme",
{
theme_id: theme.id,
theme_fields: [{ name: "unknown_field", target: "common", value: "content" }],
},
)
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "isError")).to eq(true)
expect(response.parsed_body.dig("result", "content", 0, "text")).to include(
"No type could be guessed",
)
end
it "does not let a moderation scope bypass Guardian" do
authorize("mcp:moderation:read")
call_tool("discourse_get_review_queue_count", {})
expect(response.status).to eq(403)
expect(response.parsed_body.dig("error", "message")).to eq("Not authorized")
end
it "does not let site-setting scopes bypass Guardian" do
original_title = SiteSetting.title
authorize("mcp:site-settings:read", "mcp:site-settings:write")
call_tool("discourse_list_site_settings", {})
expect(response.status).to eq(403)
expect(response.parsed_body.dig("error", "message")).to eq("Not authorized")
call_tool(
"discourse_update_site_setting",
{
setting: "title",
operation: "set",
value: "Unauthorized title",
expected_current_value: original_title,
confirm_change: true,
},
)
expect(response.status).to eq(403)
expect(SiteSetting.title).to eq(original_title)
end
it "requires private-message read scope for private moderation content" do
user.update!(moderator: true)
Group.refresh_automatic_groups_for_user!(user)
reviewable =
Fabricate(
:reviewable_flagged_post,
topic: message.topic,
target: message,
target_created_by: message.user,
)
SiteSetting.editing_grace_period = 0
PostRevisor.new(message).revise!(message.user, raw: "Revised private message")
authorize("mcp:moderation:read")
call_tool("discourse_get_review_queue_count", {})
expect(response.parsed_body.dig("result", "structuredContent", "count")).to eq(0)
call_tool("discourse_list_reviewables", {})
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "reviewables")).to eq([])
call_tool("discourse_list_reviewable_topics", {})
expect(response.parsed_body.dig("result", "structuredContent", "topics")).to eq([])
call_tool("discourse_get_reviewable", { reviewable_id: reviewable.id })
expect_missing_scope("mcp:private-messages:read")
call_tool("discourse_get_post_revision", { post_id: message.id })
expect_missing_scope("mcp:private-messages:read")
expect(response.body).not_to include("Revised private message")
authorize("mcp:moderation:read", "mcp:private-messages:read")
call_tool("discourse_get_review_queue_count", {})
expect(response.parsed_body.dig("result", "structuredContent", "count")).to eq(1)
call_tool("discourse_get_reviewable", { reviewable_id: reviewable.id })
expect(response.parsed_body.dig("result", "isError")).to eq(false)
call_tool("discourse_get_post_revision", { post_id: message.id })
expect(response.parsed_body.dig("result", "isError")).to eq(false)
end
it "omits private flag conversations without PM read scope while returning the public reviewable" do
user.update!(moderator: true)
Group.refresh_automatic_groups_for_user!(user)
flag_reason = "private flag conversation 猫"
result =
PostActionCreator.notify_moderators(
Fabricate(:user, refresh_auto_groups: true),
Fabricate(:post),
flag_reason,
)
authorize("mcp:moderation:read")
call_tool("discourse_get_reviewable", { reviewable_id: result.reviewable.id })
structured_content = response.parsed_body.dig("result", "structuredContent")
expect(structured_content.dig("reviewable", "id")).to eq(result.reviewable.id)
expect(structured_content["reviewable_conversations"]).to be_blank
expect(structured_content["conversation_posts"]).to be_blank
expect(response.body).not_to include(flag_reason)
authorize("mcp:moderation:read", "mcp:private-messages:read")
call_tool("discourse_get_reviewable", { reviewable_id: result.reviewable.id })
expect(response.body).to include(flag_reason)
end
it "requires private-message write scope for private reviewable actions" do
user.update!(moderator: true)
Group.refresh_automatic_groups_for_user!(user)
reviewable =
Fabricate(
:reviewable_flagged_post,
topic: message.topic,
target: message,
target_created_by: message.user,
)
authorize("mcp:moderation:write", "mcp:private-messages:read")
call_tool(
"discourse_perform_reviewable_action",
{
reviewable_id: reviewable.id,
action_id: "agree_and_keep",
expected_version: reviewable.version,
confirm: true,
},
)
expect_missing_scope("mcp:private-messages:write")
expect(reviewable.reload).to be_pending
end
it "requires the read scope across generic private-message reads" do
authorize("mcp:private-messages:write")
operations = {
"discourse_read_post" => {
post_id: message.id,
},
"discourse_read_topic" => {
topic_id: message.topic_id,
},
"discourse_read_topic_posts" => {
topic_id: message.topic_id,
selection_mode: "latest",
},
"discourse_get_post_replies" => {
post_id: message.id,
},
"discourse_get_topic_view_stats" => {
topic_id: message.topic_id,
},
}
operations.each do |name, arguments|
call_tool(name, arguments)
aggregate_failures(name) { expect_missing_scope("mcp:private-messages:read") }
end
end
it "requires private-message read scope for live and soft-deleted private-message drafts" do
topic = Fabricate(:topic, user:)
topic_draft_key = "topic_#{topic.id}"
message_draft_key = "topic_#{message.topic_id}"
compose_draft_key = "#{Draft::NEW_PRIVATE_MESSAGE}_#{Time.zone.now.to_i}"
private_reply = "Private reply draft"
private_title = "Private compose title"
private_compose = "Private compose draft"
Draft.set(user, topic_draft_key, 0, { reply: "Public topic draft" }.to_json)
Draft.set(user, message_draft_key, 0, { reply: private_reply }.to_json)
Draft.set(user, compose_draft_key, 0, { title: private_title, reply: private_compose }.to_json)
authorize("mcp:drafts:read")
call_tool("discourse_get_draft", { draft_key: topic_draft_key })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "data", "reply")).to eq(
"Public topic draft",
)
{
message_draft_key => private_reply,
compose_draft_key => private_compose,
}.each do |draft_key, reply|
call_tool("discourse_get_draft", { draft_key: })
expect_missing_scope("mcp:private-messages:read")
expect(response.body).not_to include(reply)
end
expect(response.body).not_to include(private_title)
message.topic.trash!
call_tool("discourse_get_draft", { draft_key: message_draft_key })
expect_missing_scope("mcp:private-messages:read")
expect(response.body).not_to include(private_reply)
authorize("mcp:drafts:read", "mcp:private-messages:read")
call_tool("discourse_get_draft", { draft_key: compose_draft_key })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "data", "reply")).to eq(
private_compose,
)
expect(response.parsed_body.dig("result", "structuredContent", "data", "title")).to eq(
private_title,
)
end
it "requires private-message write scope for private-message draft mutations" do
draft_key = "topic_#{message.topic_id}"
original_reply = "Private reply draft"
Draft.set(user, draft_key, 0, { reply: original_reply }.to_json)
authorize("mcp:drafts:write")
call_tool(
"discourse_save_draft",
{ draft_key:, reply: "Overwritten without private-message scope", sequence: 0 },
)
expect_missing_scope("mcp:private-messages:write")
expect(response.parsed_body.dig("error", "message")).to eq("Insufficient scope")
expect(Draft.find_by!(user:, draft_key:).parsed_data["reply"]).to eq(original_reply)
call_tool("discourse_delete_draft", { draft_key:, sequence: 0 })
expect_missing_scope("mcp:private-messages:write")
expect(Draft.find_by!(user:, draft_key:)).to be_present
authorize("mcp:drafts:write", "mcp:private-messages:write")
call_tool("discourse_save_draft", { draft_key:, reply: "Updated private reply", sequence: 0 })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "saved")).to eq(true)
call_tool("discourse_delete_draft", { draft_key:, sequence: 1 })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "deleted")).to eq(true)
expect(Draft.find_by(user:, draft_key:)).to be_nil
end
it "requires private-message write scope for soft-deleted private-message reply drafts" do
draft_key = "topic_#{message.topic_id}"
original_reply = "Retained private reply draft"
Draft.set(user, draft_key, 0, { reply: original_reply }.to_json)
message.topic.trash!
authorize("mcp:drafts:write")
call_tool(
"discourse_save_draft",
{ draft_key:, reply: "Overwritten without private-message scope", sequence: 0 },
)
expect_missing_scope("mcp:private-messages:write")
expect(Draft.find_by!(user:, draft_key:).parsed_data["reply"]).to eq(original_reply)
call_tool("discourse_delete_draft", { draft_key:, sequence: 0 })
expect_missing_scope("mcp:private-messages:write")
expect(Draft.find_by!(user:, draft_key:).parsed_data["reply"]).to eq(original_reply)
end
it "requires private-message write scope for fixed and suffixed compose keys" do
public_draft_key = "topic_#{Fabricate(:topic, user:).id}"
compose_keys = [
Draft::NEW_PRIVATE_MESSAGE,
"#{Draft::NEW_PRIVATE_MESSAGE}_#{Time.zone.now.to_i}",
]
authorize("mcp:drafts:write")
call_tool(
"discourse_save_draft",
{ draft_key: public_draft_key, reply: "Public edit", sequence: 0 },
)
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "saved")).to eq(true)
compose_keys.each do |compose_key|
call_tool(
"discourse_save_draft",
{ draft_key: compose_key, reply: "Blocked compose", sequence: 0 },
)
aggregate_failures(compose_key) do
expect_missing_scope("mcp:private-messages:write")
expect(Draft.exists?(user_id: user.id, draft_key: compose_key)).to eq(false)
end
call_tool("discourse_delete_draft", { draft_key: compose_key, sequence: 0 })
aggregate_failures(compose_key) { expect_missing_scope("mcp:private-messages:write") }
end
authorize("mcp:drafts:write", "mcp:private-messages:write")
compose_keys.each do |compose_key|
call_tool(
"discourse_save_draft",
{ draft_key: compose_key, reply: "Allowed compose", sequence: 0 },
)
aggregate_failures(compose_key) do
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "saved")).to eq(true)
end
end
end
it "requires the read scope for private-message resources and draft prompts" do
authorize
{ "topic" => message.topic_id, "post" => message.id }.each do |type, id|
McpPrimitive.create!(
kind: "resource_template",
identifier: "discourse.#{type}",
enabled: true,
)
call_mcp("resources/read", { uri: "discourse://#{type}/#{id}" })
aggregate_failures(type) { expect_missing_scope("mcp:private-messages:read") }
end
McpPrimitive.create!(kind: "prompt", identifier: "discourse.draft_reply", enabled: true)
call_mcp(
"prompts/get",
{ name: "discourse.draft_reply", arguments: { topic_id: message.topic_id } },
)
expect_missing_scope("mcp:private-messages:read")
end
it "requires the write scope before modifying private messages" do
authorize("mcp:private-messages:read")
reply = Fabricate(:post, topic: message.topic, user:)
recoverable_reply = Fabricate(:post, topic: message.topic, user:, user_deleted: true)
original_raw = message.raw
original_title = message.topic.title
operations = {
"discourse_create_post" => {
topic_id: message.topic_id,
raw: "A reply requiring message access",
},
"discourse_update_post" => {
post_id: message.id,
raw: "An edit requiring message access",
},
"discourse_update_topic" => {
topic_id: message.topic_id,
title: "A changed private topic title",
},
"discourse_post_set_deleted" => {
post_id: reply.id,
deleted: true,
},
}
operations.each do |name, arguments|
call_tool(name, arguments)
aggregate_failures(name) { expect_missing_scope("mcp:private-messages:write") }
end
call_tool("discourse_post_set_deleted", { post_id: recoverable_reply.id, deleted: false })
expect_missing_scope("mcp:private-messages:write")
expect(reply.reload.user_deleted).to eq(false)
expect(recoverable_reply.reload.user_deleted).to eq(true)
expect(message.reload.raw).to eq(original_raw)
expect(message.deleted_at).to be_nil
expect(message.topic.reload.title).to eq(original_title)
expect(message.topic.posts.count).to eq(3)
authorize("mcp:private-messages:read", "mcp:private-messages:write")
operations.each do |name, arguments|
call_tool(name, arguments)
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "isError")).to eq(false)
end
expect(message.reload.raw).to eq(operations.fetch("discourse_update_post").fetch(:raw))
expect(message.topic.reload.title).to eq(
operations.fetch("discourse_update_topic").fetch(:title),
)
expect(reply.reload.user_deleted).to eq(true)
call_tool("discourse_post_set_deleted", { post_id: reply.id, deleted: false })
expect(response.status).to eq(200)
expect(reply.reload.user_deleted).to eq(false)
end
it "excludes private search matches without revealing whether they exist" do
authorize
SearchIndexer.index(message, force: true)
public_post = Fabricate(:post, raw: "A public secret conversation")
SearchIndexer.index(public_post, force: true)
%w[discourse_search discourse_search_posts].each do |name|
[
"in:messages",
"topic:#{message.topic_id}",
"personal_messages:#{user.username}",
].each do |filter|
%w[secret nonexistentkeywordxyz].each do |term|
call_tool(name, { query: "#{filter} #{term}" })
expect(response.status).to eq(200)
result = response.parsed_body.fetch("result").fetch("structuredContent")
expect(result.fetch(name == "discourse_search" ? "results" : "posts")).to eq([])
expect(result.fetch("meta").fetch("has_more")).to eq(false)
end
end
call_tool(name, { query: "in:all secret" })
expect(response.status).to eq(200)
result = response.parsed_body.fetch("result").fetch("structuredContent")
rows = result.fetch(name == "discourse_search" ? "results" : "posts")
expect(rows.pluck("id")).to eq(
[name == "discourse_search" ? public_post.topic_id : public_post.id],
)
end
end
it "excludes private-message activity before pagination unless the read scope is granted" do
public_post = Fabricate(:post, user:, raw: "A public activity entry")
private_reply = Fabricate(:post, topic: message.topic, user:, raw: "Private activity reply")
received_message = Fabricate(:private_message_post, recipient: user)
[
[UserAction::REPLY, public_post],
[UserAction::NEW_PRIVATE_MESSAGE, message],
[UserAction::REPLY, private_reply],
[UserAction::GOT_PRIVATE_MESSAGE, received_message],
].each do |action_type, target_post|
UserAction.create!(
action_type:,
user:,
acting_user: user,
target_topic: target_post.topic,
target_post:,
)
end
authorize
%w[private_messages_sent private_messages_received].each do |action_type|
call_tool(
"discourse_list_user_actions",
{ username: user.username, action_types: [action_type] },
)
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "actions")).to eq([])
end
arguments = {
username: user.username,
action_types: %w[replies private_messages_sent private_messages_received],
limit: 1,
}
call_tool("discourse_list_user_actions", arguments)
result = response.parsed_body.fetch("result").fetch("structuredContent")
expect(result.fetch("actions").pluck("post_id")).to eq([public_post.id])
expect(result.fetch("meta")).to include(
"returned" => 1,
"has_more" => false,
"next_offset" => nil,
)
authorize("mcp:private-messages:read")
call_tool("discourse_list_user_actions", arguments.merge(limit: 10))
actions = response.parsed_body.dig("result", "structuredContent", "actions")
expect(actions.pluck("post_id")).to contain_exactly(
public_post.id,
message.id,
private_reply.id,
received_message.id,
)
end
it "preserves content access and allows private-message access only with its scopes" do
SiteSetting.login_required = true
public_post = Fabricate(:post, user:)
unrelated_message = Fabricate(:private_message_post)
authorize
call_tool("discourse_read_post", { post_id: public_post.id })
expect(response.parsed_body.dig("result", "structuredContent", "raw")).to eq(public_post.raw)
call_tool(
"discourse_update_post",
{ post_id: public_post.id, raw: "Updated public content through MCP" },
)
expect(public_post.reload.raw).to eq("Updated public content through MCP")
authorize("mcp:private-messages:read", "mcp:private-messages:write")
call_tool("discourse_read_post", { post_id: message.id })
expect(response.parsed_body.dig("result", "structuredContent", "raw")).to eq(message.raw)
call_tool(
"discourse_update_post",
{ post_id: message.id, raw: "Updated private content through MCP 猫" },
)
expect(message.reload.raw).to eq("Updated private content through MCP 猫")
SearchIndexer.index(message, force: true)
call_tool("discourse_search_posts", { query: "in:messages private" })
expect(response.parsed_body.dig("result", "structuredContent", "posts").pluck("id")).to eq(
[message.id],
)
call_tool("discourse_read_post", { post_id: unrelated_message.id })
expect(response.parsed_body.dig("result", "isError")).to eq(true)
expect(response.body).not_to include(unrelated_message.raw)
end
it "filters hidden topic posts before pagination while preserving authorized access" do
authorize
visible = Fabricate(:post)
hidden = Fabricate(:post, topic: visible.topic, hidden: true)
last_visible = Fabricate(:post, topic: visible.topic)
arguments = { topic_id: visible.topic_id, start_post_number: hidden.post_number, post_limit: 1 }
expect(user.guardian.can_see?(hidden)).to eq(false)
call_tool("discourse_read_topic", arguments)
result = response.parsed_body.fetch("result").fetch("structuredContent")
expect(result.fetch("posts").pluck("id")).to eq([last_visible.id])
expect(result.fetch("meta")).to include("returned" => 1, "has_more" => false)
hidden.update!(user:)
call_tool("discourse_read_topic", arguments)
result = response.parsed_body.fetch("result").fetch("structuredContent")
expect(result.fetch("posts").pluck("id")).to eq([hidden.id])
expect(result.fetch("meta")).to include("returned" => 1, "has_more" => true)
hidden.update!(user: visible.user)
user.update!(moderator: true)
Group.refresh_automatic_groups_for_user!(user)
call_tool("discourse_read_topic", arguments)
expect(response.parsed_body.dig("result", "structuredContent", "posts").pluck("id")).to eq(
[hidden.id],
)
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)
group_summary_notification =
Fabricate(
:notification,
user:,
topic: nil,
notification_type: Notification.types[:group_message_summary],
data: { group_id: group.id, group_name: group.name, inbox_count: 2 }.to_json,
)
private_notification = Fabricate(:private_message_notification, user:, topic: message.topic)
authorize
call_tool("discourse_notification_list", { limit: 3 })
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])
expect(response.body).not_to include(group.name)
authorize("mcp:private-messages:read")
call_tool("discourse_notification_list", { limit: 3 })
expect(response.status).to eq(200)
notifications = response.parsed_body.dig("result", "structuredContent", "notifications")
expect(notifications.pluck("id")).to eq(
[private_notification.id, group_summary_notification.id, other_notification.id],
)
expect(
notifications.find { |notification| notification["id"] == group_summary_notification.id },
).to include("data" => include("group_name" => group.name, "inbox_count" => 2))
end
it "filters private-message bookmarks before the limit unless the read scope is granted" do
public_bookmark = Fabricate(:bookmark, user:, updated_at: 3.minutes.ago)
private_topic_bookmark =
Fabricate(
:bookmark,
user:,
bookmarkable: message.topic,
name: "Private topic bookmark note",
updated_at: 2.minutes.ago,
)
private_post_bookmark =
Fabricate(
:bookmark,
user:,
bookmarkable: message,
name: "Private post bookmark note",
updated_at: 1.minute.ago,
)
authorize
call_tool("discourse_bookmark_list", { limit: 2 })
expect(response.status).to eq(200)
bookmarks = response.parsed_body.dig("result", "structuredContent", "bookmarks")
expect(bookmarks.pluck("id")).to eq([public_bookmark.id])
expect(response.body).not_to include(private_topic_bookmark.name, private_post_bookmark.name)
authorize("mcp:private-messages:read")
call_tool("discourse_bookmark_list", { limit: 2 })
expect(response.status).to eq(200)
bookmarks = response.parsed_body.dig("result", "structuredContent", "bookmarks")
expect(bookmarks.pluck("id")).to eq([private_post_bookmark.id, private_topic_bookmark.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")
Fabricate(
:post,
topic: message.topic,
user: message.topic.allowed_users.where.not(id: user.id).first,
hidden: true,
)
call_tool("discourse_read_topic", { topic_id: message.topic_id })
expect(response.status).to eq(200)
expect(response.parsed_body.dig("result", "structuredContent", "posts").pluck("id")).to eq(
[message.id],
)
end
end