Skip to content

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

DEV: Keep MCP required scopes with their implementations (#43736)

Natalie Tayc4ed156337c8
diff --git a/lib/discourse_mcp.rb b/lib/discourse_mcp.rb
index 7d2c1441db4..054124c36ff 100644
--- a/lib/discourse_mcp.rb
+++ b/lib/discourse_mcp.rb
@@ -1,12 +1,23 @@
# frozen_string_literal: true
module DiscourseMcp
+ module Scopes
+ PROFILE_READ = "mcp:profile:read"
+ PROFILE_WRITE = "mcp:profile:write"
+ CONTENT_READ = "mcp:content:read"
+ CONTENT_WRITE = "mcp:content:write"
+ DRAFTS_READ = "mcp:drafts:read"
+ DRAFTS_WRITE = "mcp:drafts:write"
+ PRIVATE_MESSAGES_READ = "mcp:private-messages:read"
+ PRIVATE_MESSAGES_WRITE = "mcp:private-messages:write"
+ end
+
PROTOCOL_VERSION = "2026-07-28"
LEGACY_PROTOCOL_VERSION = "2025-11-25"
SUPPORTED_PROTOCOL_VERSIONS = [PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION].freeze
JSONRPC_VERSION = "2.0"
SERVER_NAME = "discourse"
- INITIAL_SCOPE = "mcp:profile:read"
+ INITIAL_SCOPE = Scopes::PROFILE_READ
class Error < StandardError
attr_reader :code, :data, :http_status, :headers
diff --git a/lib/discourse_mcp/core_primitives.rb b/lib/discourse_mcp/core_primitives.rb
index 3a13d8d37b9..df3c82369ca 100644
--- a/lib/discourse_mcp/core_primitives.rb
+++ b/lib/discourse_mcp/core_primitives.rb
@@ -38,6 +38,25 @@ module DiscourseMcp
identifier,
implementation:,
output_schema: implementation::OUTPUT_SCHEMA,
+ required_scopes: implementation::REQUIRED_SCOPES,
+ **attributes,
+ )
+ end
+
+ def register_resource_template(registry, identifier, implementation:, **attributes)
+ registry.register_resource_template(
+ identifier,
+ implementation:,
+ required_scopes: implementation::REQUIRED_SCOPES,
+ **attributes,
+ )
+ end
+
+ def register_prompt(registry, identifier, implementation:, **attributes)
+ registry.register_prompt(
+ identifier,
+ implementation:,
+ required_scopes: implementation::REQUIRED_SCOPES,
**attributes,
)
end
@@ -58,7 +77,6 @@ module DiscourseMcp
description:
"Returns the authenticated Discourse user, granted scopes, and MCP server URI.",
implementation: Tools::CurrentUser,
- required_scopes: [DiscourseMcp::INITIAL_SCOPE],
annotations: READ_ONLY,
)
register_tool(
@@ -85,7 +103,6 @@ module DiscourseMcp
},
required: %w[query],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -126,7 +143,6 @@ module DiscourseMcp
},
},
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -155,7 +171,6 @@ module DiscourseMcp
},
required: %w[query],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -212,7 +227,6 @@ module DiscourseMcp
},
required: %w[topic_id selection_mode],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -243,7 +257,6 @@ module DiscourseMcp
},
required: %w[post_id],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -258,7 +271,6 @@ module DiscourseMcp
object_schema(
{ before_post_id: { type: "integer", minimum: 1 }, replies_only: { type: "boolean" } },
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -287,7 +299,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -353,7 +364,6 @@ module DiscourseMcp
},
required: %w[period],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -382,7 +392,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -393,7 +402,6 @@ module DiscourseMcp
implementation: Tools::GetPost,
input_schema:
object_schema({ post_id: { type: "integer", minimum: 1 } }, required: %w[post_id]),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -403,7 +411,6 @@ module DiscourseMcp
description: "Lists recent topics visible to the authenticated user.",
implementation: Tools::ListTopics,
input_schema: object_schema({ limit: { type: "integer", minimum: 1, maximum: 50 } }),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -412,7 +419,6 @@ module DiscourseMcp
title: "List categories",
description: "Lists the visible category hierarchy.",
implementation: Tools::ListCategories,
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -422,7 +428,6 @@ module DiscourseMcp
description: "Lists visible tags ordered by usage.",
implementation: Tools::ListTags,
input_schema: object_schema({ limit: { type: "integer", minimum: 1, maximum: 200 } }),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -436,7 +441,6 @@ module DiscourseMcp
{ username: { type: "string", minLength: 1, maxLength: 60 } },
required: %w[username],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -465,7 +469,6 @@ module DiscourseMcp
},
required: %w[username],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -479,7 +482,6 @@ module DiscourseMcp
{ username: { type: "string", minLength: 1, maxLength: 60 } },
required: %w[username],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -522,7 +524,6 @@ module DiscourseMcp
},
required: %w[username],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -546,7 +547,6 @@ module DiscourseMcp
},
required: %w[draft_key],
),
- required_scopes: %w[mcp:drafts:read],
annotations: READ_ONLY,
)
register_tool(
@@ -556,7 +556,6 @@ module DiscourseMcp
description: "Lists the authenticated user's bookmarks.",
implementation: Tools::ListBookmarks,
input_schema: object_schema({ limit: { type: "integer", minimum: 1, maximum: 100 } }),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -566,7 +565,6 @@ module DiscourseMcp
description: "Lists the authenticated user's notifications.",
implementation: Tools::ListNotifications,
input_schema: object_schema({ limit: { type: "integer", minimum: 1, maximum: 100 } }),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
register_tool(
@@ -606,7 +604,6 @@ module DiscourseMcp
},
},
),
- required_scopes: %w[mcp:private-messages:read],
annotations: READ_ONLY,
)
register_tool(
@@ -635,7 +632,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:private-messages:read],
annotations: READ_ONLY,
)
end
@@ -678,7 +674,6 @@ module DiscourseMcp
},
required: %w[title raw],
),
- required_scopes: %w[mcp:content:write],
annotations: WRITE,
risk: :write,
)
@@ -712,7 +707,6 @@ module DiscourseMcp
},
required: %w[topic_id raw],
),
- required_scopes: %w[mcp:content:write],
annotations: WRITE,
risk: :write,
)
@@ -741,7 +735,6 @@ module DiscourseMcp
},
required: %w[post_id raw],
),
- required_scopes: %w[mcp:content:write],
annotations: WRITE,
risk: :write,
)
@@ -796,7 +789,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:content:write],
annotations: WRITE,
risk: :write,
)
@@ -857,7 +849,6 @@ module DiscourseMcp
},
required: %w[username],
),
- required_scopes: %w[mcp:profile:write],
annotations: WRITE,
risk: :write,
)
@@ -895,7 +886,6 @@ module DiscourseMcp
},
required: %w[upload_type],
),
- required_scopes: %w[mcp:content:write],
annotations: EXTERNAL_SIDE_EFFECT,
risk: :external_side_effect,
)
@@ -952,7 +942,6 @@ module DiscourseMcp
},
required: %w[title raw],
),
- required_scopes: %w[mcp:private-messages:write],
annotations: EXTERNAL_SIDE_EFFECT,
risk: :external_side_effect,
)
@@ -986,7 +975,6 @@ module DiscourseMcp
},
required: %w[topic_id raw],
),
- required_scopes: %w[mcp:private-messages:write],
annotations: WRITE,
risk: :write,
)
@@ -1034,7 +1022,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:private-messages:write],
annotations: EXTERNAL_SIDE_EFFECT,
risk: :external_side_effect,
)
@@ -1049,7 +1036,6 @@ module DiscourseMcp
{ post_id: { type: "integer", minimum: 1 }, deleted: { type: "boolean" } },
required: %w[post_id deleted],
),
- required_scopes: %w[mcp:content:write],
annotations: DESTRUCTIVE,
risk: :destructive,
)
@@ -1102,7 +1088,6 @@ module DiscourseMcp
},
required: %w[draft_key reply],
),
- required_scopes: %w[mcp:drafts:write],
annotations: WRITE,
risk: :write,
)
@@ -1127,7 +1112,6 @@ module DiscourseMcp
},
required: %w[draft_key sequence],
),
- required_scopes: %w[mcp:drafts:write],
annotations: DESTRUCTIVE,
risk: :destructive,
)
@@ -1162,7 +1146,6 @@ module DiscourseMcp
{ properties: { clear: { enum: [false] } }, required: %w[description emoji] },
],
),
- required_scopes: %w[mcp:content:write],
annotations: WRITE,
risk: :write,
availability: -> { SiteSetting.enable_user_status },
@@ -1170,7 +1153,8 @@ module DiscourseMcp
end
def register_resources(registry)
- registry.register_resource_template(
+ register_resource_template(
+ registry,
"discourse.topic",
title: "Discourse topic",
description: "A topic visible to the authenticated user.",
@@ -1180,10 +1164,10 @@ module DiscourseMcp
{ uri: { type: "string", pattern: "\\Adiscourse://topic/[0-9]+\\z" } },
required: %w[uri],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
- registry.register_resource_template(
+ register_resource_template(
+ registry,
"discourse.post",
title: "Discourse post",
description: "A post visible to the authenticated user.",
@@ -1193,13 +1177,13 @@ module DiscourseMcp
{ uri: { type: "string", pattern: "\\Adiscourse://post/[0-9]+\\z" } },
required: %w[uri],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
end
def register_prompts(registry)
- registry.register_prompt(
+ register_prompt(
+ registry,
"discourse.research_topic",
title: "Research discussions",
description: "Builds a prompt for researching visible Discourse discussions.",
@@ -1209,10 +1193,10 @@ module DiscourseMcp
{ question: { type: "string", minLength: 1, maxLength: 1000 } },
required: %w[question],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
- registry.register_prompt(
+ register_prompt(
+ registry,
"discourse.draft_reply",
title: "Draft a reply",
description: "Builds a prompt for drafting a grounded reply to a visible topic.",
@@ -1231,7 +1215,6 @@ module DiscourseMcp
},
required: %w[topic_id],
),
- required_scopes: %w[mcp:content:read],
annotations: READ_ONLY,
)
end
@@ -1239,6 +1222,8 @@ module DiscourseMcp
module Resources
class Topic
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
+
def self.call(uri:, request_context:)
id = uri.delete_prefix("discourse://topic/").to_i
topic = ::Topic.find_by(id: id)
@@ -1255,6 +1240,8 @@ module DiscourseMcp
end
class Post
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
+
def self.call(uri:, request_context:)
id = uri.delete_prefix("discourse://post/").to_i
post = ::Post.secured(request_context.guardian).find_by(id: id)
@@ -1269,6 +1256,8 @@ module DiscourseMcp
module Prompts
class ResearchTopic
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
+
def self.call(arguments:, request_context:)
{
description: "Research visible Discourse discussions",
@@ -1287,6 +1276,8 @@ module DiscourseMcp
end
class DraftReply
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
+
def self.call(arguments:, request_context:)
topic = ::Topic.find_by(id: arguments.fetch("topic_id").to_i)
if topic.blank? || !request_context.guardian.can_see?(topic)
diff --git a/lib/discourse_mcp/tools/bookmarks.rb b/lib/discourse_mcp/tools/bookmarks.rb
index dd8dbf7ea86..fd349fdf396 100644
--- a/lib/discourse_mcp/tools/bookmarks.rb
+++ b/lib/discourse_mcp/tools/bookmarks.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class ListBookmarks
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA = OutputSchema.object(bookmarks: OutputSchema::OBJECT_ARRAY)
def self.call(arguments:, request_context:)
diff --git a/lib/discourse_mcp/tools/drafts.rb b/lib/discourse_mcp/tools/drafts.rb
index 1972ec091d3..f5feab79778 100644
--- a/lib/discourse_mcp/tools/drafts.rb
+++ b/lib/discourse_mcp/tools/drafts.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class GetDraft
+ REQUIRED_SCOPES = [Scopes::DRAFTS_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
optional: %w[sequence data],
@@ -58,6 +59,7 @@ module DiscourseMcp
end
class SaveDraft
+ REQUIRED_SCOPES = [Scopes::DRAFTS_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
draft_key: OutputSchema::STRING,
@@ -105,6 +107,7 @@ module DiscourseMcp
end
class DeleteDraft
+ REQUIRED_SCOPES = [Scopes::DRAFTS_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(draft_key: OutputSchema::STRING, deleted: OutputSchema::BOOLEAN)
diff --git a/lib/discourse_mcp/tools/notifications.rb b/lib/discourse_mcp/tools/notifications.rb
index 23003277317..b2a11de3ae9 100644
--- a/lib/discourse_mcp/tools/notifications.rb
+++ b/lib/discourse_mcp/tools/notifications.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class ListNotifications
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA = OutputSchema.object(notifications: OutputSchema::OBJECT_ARRAY)
def self.call(arguments:, request_context:)
@@ -14,7 +15,7 @@ module DiscourseMcp
.includes(:topic)
.order(id: :desc)
.limit(limit)
- if !request_context.has_scopes?("mcp:private-messages:read")
+ if !request_context.has_scopes?(Scopes::PRIVATE_MESSAGES_READ)
notifications =
notifications.left_joins(:topic).where(
"topics.id IS NULL OR topics.archetype <> ?",
diff --git a/lib/discourse_mcp/tools/private_messages.rb b/lib/discourse_mcp/tools/private_messages.rb
index f48c9681a57..273f2426231 100644
--- a/lib/discourse_mcp/tools/private_messages.rb
+++ b/lib/discourse_mcp/tools/private_messages.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class ListPrivateMessages
+ REQUIRED_SCOPES = [Scopes::PRIVATE_MESSAGES_READ].freeze
MAILBOXES = %w[inbox sent archive unread new].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
@@ -140,6 +141,7 @@ module DiscourseMcp
end
class ReadPrivateMessage
+ REQUIRED_SCOPES = [Scopes::PRIVATE_MESSAGES_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
topic_id: OutputSchema::INTEGER,
@@ -212,6 +214,7 @@ module DiscourseMcp
end
class CreatePrivateMessage
+ REQUIRED_SCOPES = [Scopes::PRIVATE_MESSAGES_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -268,6 +271,7 @@ module DiscourseMcp
end
class ReplyPrivateMessage
+ REQUIRED_SCOPES = [Scopes::PRIVATE_MESSAGES_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -302,6 +306,7 @@ module DiscourseMcp
end
class InviteToPrivateMessage
+ REQUIRED_SCOPES = [Scopes::PRIVATE_MESSAGES_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
optional: %w[group notifications_requested user participant_added outcome_confirmed],
diff --git a/lib/discourse_mcp/tools/search.rb b/lib/discourse_mcp/tools/search.rb
index 61d69ac5ad9..dbf1daddc87 100644
--- a/lib/discourse_mcp/tools/search.rb
+++ b/lib/discourse_mcp/tools/search.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class Search
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(results: OutputSchema::OBJECT_ARRAY, meta: OutputSchema::OBJECT)
@@ -13,7 +14,7 @@ module DiscourseMcp
::Search.execute(
query,
guardian: request_context.guardian,
- exclude_private_messages: !request_context.has_scopes?("mcp:private-messages:read"),
+ exclude_private_messages: !request_context.has_scopes?(Scopes::PRIVATE_MESSAGES_READ),
)
topics = results.posts.filter_map(&:topic).uniq(&:id).first(limit + 1)
has_more = topics.length > limit
@@ -30,6 +31,7 @@ module DiscourseMcp
class FilterTopics
TOP_PERIODS = %w[daily weekly monthly quarterly yearly all].freeze
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(results: OutputSchema::OBJECT_ARRAY, meta: OutputSchema::OBJECT)
@@ -97,6 +99,7 @@ module DiscourseMcp
end
class SearchPosts
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
posts: OutputSchema::OBJECT_ARRAY,
@@ -120,7 +123,7 @@ module DiscourseMcp
search_type: :full_page,
page:,
blurb_length: 300,
- exclude_private_messages: !request_context.has_scopes?("mcp:private-messages:read"),
+ exclude_private_messages: !request_context.has_scopes?(Scopes::PRIVATE_MESSAGES_READ),
)
raise DiscourseMcp::ToolError, I18n.t("mcp.errors.invalid_search_query") if results.blank?
diff --git a/lib/discourse_mcp/tools/topic_reads.rb b/lib/discourse_mcp/tools/topic_reads.rb
index 56fbe865e23..5b4d70683bb 100644
--- a/lib/discourse_mcp/tools/topic_reads.rb
+++ b/lib/discourse_mcp/tools/topic_reads.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class ReadTopicPosts
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
SELECTION_KEYS = {
"latest" => %w[limit replies_only],
"earliest" => %w[limit replies_only],
@@ -138,6 +139,7 @@ module DiscourseMcp
end
class GetPostReplies
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
DIRECT_REPLIES_LIMIT = 20
MODES = %w[reply_ids direct_replies reply_history].freeze
OUTPUT_SCHEMA =
@@ -254,6 +256,7 @@ module DiscourseMcp
end
class ListLatestPosts
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(posts: OutputSchema::OBJECT_ARRAY, meta: OutputSchema::OBJECT)
@@ -298,6 +301,7 @@ module DiscourseMcp
end
class GetTopicViewStats
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
topic_id: OutputSchema::INTEGER,
@@ -345,6 +349,7 @@ module DiscourseMcp
end
class GetTopic
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -401,6 +406,7 @@ module DiscourseMcp
end
class GetPost
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
optional: %w[accepted_answer topic_accepted_answer],
@@ -436,6 +442,7 @@ module DiscourseMcp
end
class ListTopics
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA = OutputSchema.object(topics: OutputSchema::OBJECT_ARRAY)
def self.call(arguments:, request_context:)
@@ -453,6 +460,7 @@ module DiscourseMcp
end
class ListCategories
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA = OutputSchema.object(categories: OutputSchema::OBJECT_ARRAY)
def self.call(arguments:, request_context:)
@@ -473,6 +481,7 @@ module DiscourseMcp
end
class ListTags
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA = OutputSchema.object(tags: OutputSchema::OBJECT_ARRAY)
def self.call(arguments:, request_context:)
diff --git a/lib/discourse_mcp/tools/topic_writes.rb b/lib/discourse_mcp/tools/topic_writes.rb
index 642df5c5e9d..dc455ffe358 100644
--- a/lib/discourse_mcp/tools/topic_writes.rb
+++ b/lib/discourse_mcp/tools/topic_writes.rb
@@ -3,6 +3,7 @@
module DiscourseMcp
module Tools
class CreateTopic
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -40,6 +41,7 @@ module DiscourseMcp
end
class ReplyTopic
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -78,6 +80,7 @@ module DiscourseMcp
end
class EditPost
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -112,6 +115,7 @@ module DiscourseMcp
end
class UpdateTopic
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
MUTABLE_FIELDS = %w[title category_id tags featured_link].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
@@ -220,6 +224,7 @@ module DiscourseMcp
end
class SetPostDeleted
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(post_id: OutputSchema::INTEGER, deleted: OutputSchema::BOOLEAN)
diff --git a/lib/discourse_mcp/tools/uploads.rb b/lib/discourse_mcp/tools/uploads.rb
index ca71d91734d..4cfbb742a11 100644
--- a/lib/discourse_mcp/tools/uploads.rb
+++ b/lib/discourse_mcp/tools/uploads.rb
@@ -6,6 +6,7 @@ require "tempfile"
module DiscourseMcp
module Tools
class UploadFile
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
UPLOAD_TYPES = %w[avatar profile_background card_background composer].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
diff --git a/lib/discourse_mcp/tools/users.rb b/lib/discourse_mcp/tools/users.rb
index 2a7efdbfc77..4022597aad3 100644
--- a/lib/discourse_mcp/tools/users.rb
+++ b/lib/discourse_mcp/tools/users.rb
@@ -5,6 +5,7 @@ require "directory_items_query"
module DiscourseMcp
module Tools
class CurrentUser
+ REQUIRED_SCOPES = [DiscourseMcp::INITIAL_SCOPE].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -33,6 +34,7 @@ module DiscourseMcp
end
class ListDirectoryItems
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
PAGE_SIZE = ::DirectoryItemsQuery::PAGE_SIZE
PAGE_LIMIT = ::DirectoryItemsQuery::PAGE_LIMIT
OUTPUT_SCHEMA =
@@ -118,6 +120,7 @@ module DiscourseMcp
end
class GetUser
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(
id: OutputSchema::INTEGER,
@@ -148,6 +151,7 @@ module DiscourseMcp
end
class ListUserPosts
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
OUTPUT_SCHEMA =
OutputSchema.object(posts: OutputSchema::OBJECT_ARRAY, meta: OutputSchema::OBJECT)
@@ -178,6 +182,7 @@ module DiscourseMcp
end
class GetUserSummary
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
METRICS = %i[
likes_given
likes_received
@@ -238,6 +243,7 @@ module DiscourseMcp
end
class ListUserActions
+ REQUIRED_SCOPES = [Scopes::CONTENT_READ].freeze
ACTION_TYPES = {
"likes" => 1,
"was_liked" => 2,
@@ -286,7 +292,7 @@ module DiscourseMcp
guardian:,
ignore_private_messages:
arguments["action_types"].blank? ||
- !request_context.has_scopes?("mcp:private-messages:read"),
+ !request_context.has_scopes?(Scopes::PRIVATE_MESSAGES_READ),
acting_username: arguments["acting_username"],
).to_a
has_more = actions.length > limit
@@ -339,6 +345,7 @@ module DiscourseMcp
end
class UpdateUser
+ REQUIRED_SCOPES = [Scopes::PROFILE_WRITE].freeze
PROFILE_FIELDS = %w[
name
bio_raw
@@ -431,6 +438,7 @@ module DiscourseMcp
end
class SetUserStatus
+ REQUIRED_SCOPES = [Scopes::CONTENT_WRITE].freeze
OUTPUT_SCHEMA = OutputSchema.object(success: OutputSchema::BOOLEAN)
def self.call(arguments:, request_context:)
diff --git a/plugins/chat/lib/chat/mcp_tools.rb b/plugins/chat/lib/chat/mcp_tools.rb
index 3a5baaeefd6..91bdca64430 100644
--- a/plugins/chat/lib/chat/mcp_tools.rb
+++ b/plugins/chat/lib/chat/mcp_tools.rb
@@ -3,6 +3,7 @@
module Chat
module McpTools
class ListChannels
+ REQUIRED_SCOPES = %w[chat:read].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
channels: {
@@ -35,6 +36,7 @@ module Chat
end
class ListMessages
+ REQUIRED_SCOPES = %w[chat:read].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
channel_id: DiscourseMcp::OutputSchema::INTEGER,
@@ -98,6 +100,7 @@ module Chat
end
class CreateMessage
+ REQUIRED_SCOPES = %w[chat:write].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
id: DiscourseMcp::OutputSchema::INTEGER,
diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb
index 41b81c13898..5c70dec0ae0 100644
--- a/plugins/chat/plugin.rb
+++ b/plugins/chat/plugin.rb
@@ -658,7 +658,7 @@ after_initialize do
"Lists chat channels followed by the authenticated user, including direct-message channels.",
implementation: Chat::McpTools::ListChannels,
output_schema: Chat::McpTools::ListChannels::OUTPUT_SCHEMA,
- required_scopes: %w[chat:read],
+ required_scopes: Chat::McpTools::ListChannels::REQUIRED_SCOPES,
annotations: {
readOnlyHint: true,
destructiveHint: false,
@@ -700,7 +700,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: Chat::McpTools::ListMessages::OUTPUT_SCHEMA,
- required_scopes: %w[chat:read],
+ required_scopes: Chat::McpTools::ListMessages::REQUIRED_SCOPES,
annotations: {
readOnlyHint: true,
destructiveHint: false,
@@ -734,7 +734,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: Chat::McpTools::CreateMessage::OUTPUT_SCHEMA,
- required_scopes: %w[chat:write],
+ required_scopes: Chat::McpTools::CreateMessage::REQUIRED_SCOPES,
annotations: {
readOnlyHint: false,
destructiveHint: false,
diff --git a/plugins/discourse-events/lib/discourse_events/mcp_tools.rb b/plugins/discourse-events/lib/discourse_events/mcp_tools.rb
index 921df05df27..a4fbb03e9be 100644
--- a/plugins/discourse-events/lib/discourse_events/mcp_tools.rb
+++ b/plugins/discourse-events/lib/discourse_events/mcp_tools.rb
@@ -3,6 +3,7 @@
module DiscourseEvents
module McpTools
class ListEvents
+ REQUIRED_SCOPES = %w[discourse-calendar:read].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
events: {
diff --git a/plugins/discourse-events/plugin.rb b/plugins/discourse-events/plugin.rb
index 7febc3f9e84..374b35ddbe7 100644
--- a/plugins/discourse-events/plugin.rb
+++ b/plugins/discourse-events/plugin.rb
@@ -1098,7 +1098,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: DiscourseEvents::McpTools::ListEvents::OUTPUT_SCHEMA,
- required_scopes: %w[discourse-calendar:read],
+ required_scopes: DiscourseEvents::McpTools::ListEvents::REQUIRED_SCOPES,
annotations: {
readOnlyHint: true,
destructiveHint: false,
diff --git a/plugins/discourse-reactions/lib/discourse_reactions/mcp_tools.rb b/plugins/discourse-reactions/lib/discourse_reactions/mcp_tools.rb
index 3ea2a7a6ea9..f4add8ed36b 100644
--- a/plugins/discourse-reactions/lib/discourse_reactions/mcp_tools.rb
+++ b/plugins/discourse-reactions/lib/discourse_reactions/mcp_tools.rb
@@ -3,6 +3,7 @@
module DiscourseReactions
module McpTools
class SetReaction
+ REQUIRED_SCOPES = %w[discourse-reactions:write].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
post_id: DiscourseMcp::OutputSchema::INTEGER,
diff --git a/plugins/discourse-reactions/plugin.rb b/plugins/discourse-reactions/plugin.rb
index 5d92a38ce4d..2b4452deb0e 100644
--- a/plugins/discourse-reactions/plugin.rb
+++ b/plugins/discourse-reactions/plugin.rb
@@ -459,7 +459,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: DiscourseReactions::McpTools::SetReaction::OUTPUT_SCHEMA,
- required_scopes: %w[discourse-reactions:write],
+ required_scopes: DiscourseReactions::McpTools::SetReaction::REQUIRED_SCOPES,
annotations: {
readOnlyHint: false,
destructiveHint: false,
diff --git a/plugins/discourse-solved/lib/discourse_solved/mcp_tools.rb b/plugins/discourse-solved/lib/discourse_solved/mcp_tools.rb
index fe32aa062d7..d5bd3232219 100644
--- a/plugins/discourse-solved/lib/discourse_solved/mcp_tools.rb
+++ b/plugins/discourse-solved/lib/discourse_solved/mcp_tools.rb
@@ -3,6 +3,7 @@
module DiscourseSolved
module McpTools
class SetSolution
+ REQUIRED_SCOPES = %w[discourse-solved:write].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
post_id: DiscourseMcp::OutputSchema::INTEGER,
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index 8810a5e6553..9ce832d20f9 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -506,7 +506,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: DiscourseSolved::McpTools::SetSolution::OUTPUT_SCHEMA,
- required_scopes: %w[discourse-solved:write],
+ required_scopes: DiscourseSolved::McpTools::SetSolution::REQUIRED_SCOPES,
annotations: {
readOnlyHint: false,
destructiveHint: false,
diff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/mcp_tools.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/mcp_tools.rb
index ded92ed3971..12eab6df5fa 100644
--- a/plugins/discourse-topic-voting/lib/discourse_topic_voting/mcp_tools.rb
+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/mcp_tools.rb
@@ -3,6 +3,7 @@
module DiscourseTopicVoting
module McpTools
class SetVote
+ REQUIRED_SCOPES = %w[discourse-topic-voting:write].freeze
OUTPUT_SCHEMA =
DiscourseMcp::OutputSchema.object(
topic_id: DiscourseMcp::OutputSchema::INTEGER,
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index 6b34883afab..04f770f885d 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -276,7 +276,7 @@ after_initialize do
additionalProperties: false,
},
output_schema: DiscourseTopicVoting::McpTools::SetVote::OUTPUT_SCHEMA,
- required_scopes: %w[discourse-topic-voting:write],
+ required_scopes: DiscourseTopicVoting::McpTools::SetVote::REQUIRED_SCOPES,
annotations: {
readOnlyHint: false,
destructiveHint: false,
diff --git a/spec/lib/discourse_mcp/regular_user_tools_spec.rb b/spec/lib/discourse_mcp/regular_user_tools_spec.rb
index 26116ffafe1..41abfb6cdc6 100644
--- a/spec/lib/discourse_mcp/regular_user_tools_spec.rb
+++ b/spec/lib/discourse_mcp/regular_user_tools_spec.rb
@@ -240,7 +240,9 @@ describe DiscourseMcp::Tools do
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)
+ allow(context).to receive(:has_scopes?).with(
+ DiscourseMcp::Scopes::PRIVATE_MESSAGES_READ,
+ ).and_return(true)
result =
described_class.call(arguments: {}, request_context: context).fetch(:structuredContent)
@@ -764,6 +766,16 @@ describe DiscourseMcp::CorePrimitives do
expect(tools_without_output_schema).to be_empty
end
+ it "registers every primitive with the scopes declared by its implementation" do
+ primitives_with_different_scopes =
+ DiscourseMcp.registry.all.reject do |primitive|
+ primitive.implementation.const_defined?(:REQUIRED_SCOPES, false) &&
+ primitive.required_scopes == primitive.implementation::REQUIRED_SCOPES
+ end
+
+ expect(primitives_with_different_scopes.map(&:identifier)).to be_empty
+ end
+
it "registers the compatible regular-user tool names" do
expected_names = %w[
discourse_create_post