Skip to content

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

FEATURE: Add MCP theme tools with dedicated read and write scopes (#43976)

Penar Musarajc8e3857107f8
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 24f92ba3c0d..e4520fa76c6 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -58,6 +58,10 @@ en:
site_setting_value_forbidden: "Do not provide a value when resetting a site setting"
site_setting_value_required: "Provide a value when setting a site setting"
site_setting_verification_failed: "The site setting update could not be verified"
+ theme_create_failed: "The theme could not be created"
+ theme_not_found: "Theme not found"
+ theme_update_failed: "The theme could not be updated"
+ theme_update_required: "Provide at least one theme field to update"
topic_not_found: "Topic not found"
topic_update_required: "Provide at least one topic field to update"
top_period_only_for_top_view: "Top period is only supported by the top view"
diff --git a/lib/discourse_mcp.rb b/lib/discourse_mcp.rb
index 8f6b9f6ecca..2c0429ed196 100644
--- a/lib/discourse_mcp.rb
+++ b/lib/discourse_mcp.rb
@@ -14,6 +14,8 @@ module DiscourseMcp
MODERATION_WRITE = "mcp:moderation:write"
SITE_SETTINGS_READ = "mcp:site-settings:read"
SITE_SETTINGS_WRITE = "mcp:site-settings:write"
+ THEMES_READ = "mcp:themes:read"
+ THEMES_WRITE = "mcp:themes:write"
end
PROTOCOL_VERSION = "2026-07-28"
diff --git a/lib/discourse_mcp/core_primitives.rb b/lib/discourse_mcp/core_primitives.rb
index 3685d093d1d..1c051187d5d 100644
--- a/lib/discourse_mcp/core_primitives.rb
+++ b/lib/discourse_mcp/core_primitives.rb
@@ -67,6 +67,7 @@ module DiscourseMcp
register_write_tools(registry)
register_moderation_tools(registry)
register_site_setting_tools(registry)
+ register_theme_tools(registry)
register_resources(registry)
register_prompts(registry)
end
@@ -1491,6 +1492,159 @@ module DiscourseMcp
)
end
+ def register_theme_tools(registry)
+ register_tool(
+ registry,
+ "discourse_get_theme",
+ title: "Get theme",
+ description:
+ "Reads a theme or theme component and its fields. Requires administrator access.",
+ implementation: Tools::GetTheme,
+ input_schema: object_schema({ theme_id: { type: "integer" } }, required: %w[theme_id]),
+ annotations: READ_ONLY,
+ risk: :administration,
+ )
+
+ theme_field_schema = {
+ type: "object",
+ properties: {
+ name: {
+ type: "string",
+ minLength: 1,
+ maxLength: 255,
+ },
+ target: {
+ type: "string",
+ enum: Tools::ThemeSupport::TARGETS,
+ },
+ value: {
+ type: "string",
+ maxLength: 1_000_000,
+ },
+ type: {
+ type: "string",
+ enum: Tools::ThemeSupport::FIELD_TYPES,
+ },
+ type_id: {
+ type: "integer",
+ minimum: 0,
+ },
+ upload_id: {
+ type: "integer",
+ minimum: 1,
+ },
+ },
+ required: %w[name target],
+ additionalProperties: false,
+ }
+ register_tool(
+ registry,
+ "discourse_create_theme",
+ title: "Create theme",
+ description:
+ "Creates a theme or theme component with optional HTML, SCSS, and settings fields.",
+ implementation: Tools::CreateTheme,
+ input_schema:
+ object_schema(
+ {
+ name: {
+ type: "string",
+ minLength: 1,
+ maxLength: 100,
+ },
+ user_selectable: {
+ type: "boolean",
+ default: false,
+ },
+ color_scheme_id: {
+ type: "integer",
+ minimum: 1,
+ },
+ component: {
+ type: "boolean",
+ default: false,
+ },
+ default: {
+ type: "boolean",
+ default: false,
+ },
+ theme_fields: {
+ type: "array",
+ items: theme_field_schema,
+ maxItems: 100,
+ },
+ },
+ required: %w[name],
+ ),
+ annotations: WRITE,
+ risk: :administration,
+ )
+ register_tool(
+ registry,
+ "discourse_update_theme",
+ title: "Update theme",
+ description:
+ "Updates attributes and fields on a theme when the authenticated user may edit it.",
+ implementation: Tools::UpdateTheme,
+ input_schema:
+ object_schema(
+ {
+ theme_id: {
+ type: "integer",
+ },
+ name: {
+ type: "string",
+ minLength: 1,
+ maxLength: 100,
+ },
+ color_scheme_id: {
+ type: "integer",
+ minimum: 1,
+ },
+ dark_color_scheme_id: {
+ type: "integer",
+ minimum: 1,
+ },
+ user_selectable: {
+ type: "boolean",
+ },
+ enabled: {
+ type: "boolean",
+ },
+ auto_update: {
+ type: "boolean",
+ },
+ default: {
+ type: "boolean",
+ },
+ theme_fields: {
+ type: "array",
+ items: theme_field_schema,
+ maxItems: 100,
+ },
+ child_theme_ids: {
+ type: "array",
+ items: {
+ type: "integer",
+ minimum: 1,
+ },
+ maxItems: 50,
+ },
+ parent_theme_ids: {
+ type: "array",
+ items: {
+ type: "integer",
+ },
+ maxItems: 50,
+ },
+ },
+ required: %w[theme_id],
+ ),
+ annotations: WRITE,
+ risk: :administration,
+ )
+ end
+
def register_resources(registry)
register_resource_template(
registry,
diff --git a/lib/discourse_mcp/core_tools.rb b/lib/discourse_mcp/core_tools.rb
index a4a9be5b5a8..6f7d1116d56 100644
--- a/lib/discourse_mcp/core_tools.rb
+++ b/lib/discourse_mcp/core_tools.rb
@@ -13,3 +13,4 @@ require "discourse_mcp/tools/uploads"
require "discourse_mcp/tools/private_messages"
require "discourse_mcp/tools/moderation"
require "discourse_mcp/tools/site_settings"
+require "discourse_mcp/tools/themes"
diff --git a/lib/discourse_mcp/tools/themes.rb b/lib/discourse_mcp/tools/themes.rb
new file mode 100644
index 00000000000..98004f355b9
--- /dev/null
+++ b/lib/discourse_mcp/tools/themes.rb
@@ -0,0 +1,241 @@
+# frozen_string_literal: true
+
+module DiscourseMcp
+ module Tools
+ module ThemeSupport
+ ASSIGNABLE_FIELDS = %w[
+ name
+ color_scheme_id
+ dark_color_scheme_id
+ user_selectable
+ enabled
+ auto_update
+ ].freeze
+ # Mirrors Theme.targets / ThemeField.types; inlined so registration does not
+ # depend on app autoloading during boot.
+ TARGETS = %w[
+ common
+ desktop
+ mobile
+ settings
+ translations
+ extra_scss
+ extra_js
+ tests_js
+ migrations
+ about
+ ].freeze
+ FIELD_TYPES = %w[
+ html
+ scss
+ theme_upload_var
+ theme_color_var
+ theme_var
+ yaml
+ js
+ theme_screenshot_upload_var
+ json
+ ].freeze
+
+ module_function
+
+ def ensure_admin!(guardian)
+ guardian.ensure_is_admin!
+ end
+
+ def normalize_fields(fields)
+ Array(fields).map do |field|
+ field.slice("name", "target", "value", "type", "type_id", "upload_id").compact_blank
+ end
+ end
+
+ def theme_json(theme)
+ {
+ id: theme.id,
+ name: theme.name,
+ component: theme.component,
+ enabled: theme.enabled,
+ user_selectable: theme.user_selectable,
+ color_scheme_id: theme.color_scheme_id,
+ dark_color_scheme_id: theme.dark_color_scheme_id,
+ default: theme.default?,
+ remote_theme_id: theme.remote_theme_id,
+ child_theme_ids: theme.child_theme_ids,
+ parent_theme_ids: theme.parent_theme_ids,
+ created_at: theme.created_at.iso8601,
+ updated_at: theme.updated_at.iso8601,
+ theme_fields: theme.theme_fields.map { |field| theme_field_json(field) },
+ }
+ end
+
+ def theme_field_json(field)
+ {
+ name: field.name,
+ target: ::Theme.lookup_target(field.target_id)&.to_s,
+ type_id: field.type_id,
+ value: field.value,
+ upload_id: field.upload_id,
+ error: field.error.presence,
+ }
+ end
+
+ def raise_create_failure!(result)
+ if result["result.policy.ensure_remote_themes_are_not_allowlisted"]&.failure?
+ raise Discourse::InvalidAccess
+ end
+
+ contract = result["result.contract.default"]
+ raise ToolError, contract.errors.full_messages.to_sentence if contract&.failure?
+
+ model_failure = result["result.model.theme"]
+ raise ToolError, model_failure.exception.message if model_failure&.exception
+
+ theme = result[:theme]
+ raise ToolError, theme.errors.full_messages.join(", ") if theme&.errors.present?
+
+ raise ToolError, I18n.t("mcp.errors.theme_create_failed")
+ end
+
+ def ensure_fields_editable!(theme)
+ raise Discourse::InvalidAccess if !::Theme.allowed_remote_theme_ids.nil?
+ raise Discourse::InvalidAccess if theme.remote_theme&.is_git?
+ end
+ end
+
+ class GetTheme
+ REQUIRED_SCOPES = [Scopes::THEMES_READ].freeze
+ OUTPUT_SCHEMA = OutputSchema.object(theme: OutputSchema::OBJECT)
+
+ def self.call(arguments:, request_context:)
+ ThemeSupport.ensure_admin!(request_context.guardian)
+ theme = ::Theme.include_relations.find_by(id: arguments.fetch("theme_id"))
+ raise ToolError, I18n.t("mcp.errors.theme_not_found") if theme.blank?
+
+ ToolHelpers.text_and_structured(theme: ThemeSupport.theme_json(theme))
+ end
+ end
+
+ class CreateTheme
+ REQUIRED_SCOPES = [Scopes::THEMES_WRITE].freeze
+ OUTPUT_SCHEMA = OutputSchema.object(theme: OutputSchema::OBJECT)
+
+ def self.call(arguments:, request_context:)
+ guardian = request_context.guardian
+ guardian.ensure_can_create_theme!
+
+ result =
+ Themes::Create.call(
+ guardian:,
+ params: {
+ name: arguments.fetch("name"),
+ user_id: request_context.user_id,
+ user_selectable: arguments.fetch("user_selectable", false),
+ color_scheme_id: arguments["color_scheme_id"],
+ component: arguments.fetch("component", false),
+ default: arguments.fetch("default", false),
+ theme_fields: ThemeSupport.normalize_fields(arguments["theme_fields"]),
+ },
+ )
+
+ ThemeSupport.raise_create_failure!(result) if result.failure?
+
+ ToolHelpers.text_and_structured(theme: ThemeSupport.theme_json(result.theme))
+ end
+ end
+
+ class UpdateTheme
+ REQUIRED_SCOPES = [Scopes::THEMES_WRITE].freeze
+ OUTPUT_SCHEMA = OutputSchema.object(theme: OutputSchema::OBJECT)
+
+ def self.call(arguments:, request_context:)
+ guardian = request_context.guardian
+ ThemeSupport.ensure_admin!(guardian)
+
+ theme = ::Theme.include_relations.find_by(id: arguments.fetch("theme_id"))
+ raise ToolError, I18n.t("mcp.errors.theme_not_found") if theme.blank?
+
+ ensure_system_theme_editable!(theme, arguments)
+ if arguments.keys == ["theme_id"]
+ raise ToolError, I18n.t("mcp.errors.theme_update_required")
+ end
+
+ if arguments["default"] && theme.component?
+ raise ToolError, I18n.t("themes.errors.component_no_default")
+ end
+
+ original_json = ThemeSerializer.new(theme, root: false).to_json
+ ::Theme.transaction do
+ assign_changes(theme, arguments)
+ if !theme.save
+ raise ToolError,
+ theme.errors.full_messages.join(", ").presence ||
+ I18n.t("mcp.errors.theme_update_failed")
+ end
+
+ apply_default(theme, arguments)
+ theme = ::Theme.include_relations.find(theme.id)
+ logger = StaffActionLogger.new(guardian.user)
+ logger.log_theme_change(original_json, theme)
+ if theme.component? && arguments.key?("enabled")
+ if theme.enabled?
+ logger.log_theme_component_enabled(theme)
+ else
+ logger.log_theme_component_disabled(theme)
+ end
+ end
+ end
+
+ ToolHelpers.text_and_structured(theme: ThemeSupport.theme_json(theme))
+ rescue Discourse::InvalidParameters,
+ ::Theme::InvalidFieldTargetError,
+ ::Theme::InvalidFieldTypeError => error
+ raise ToolError, error.message.presence || I18n.t("mcp.errors.theme_update_failed")
+ end
+
+ def self.ensure_system_theme_editable!(theme, arguments)
+ return if !theme.system?
+
+ requested = arguments.keys - %w[theme_id]
+ raise Discourse::InvalidAccess if (requested - ::Theme::EDITABLE_SYSTEM_ATTRIBUTES).present?
+ end
+ private_class_method :ensure_system_theme_editable!
+
+ def self.assign_changes(theme, arguments)
+ ThemeSupport::ASSIGNABLE_FIELDS.each do |field|
+ theme.public_send("#{field}=", arguments[field]) if arguments.key?(field)
+ end
+
+ theme.child_theme_ids = arguments["child_theme_ids"] if arguments.key?("child_theme_ids")
+ theme.parent_theme_ids = arguments["parent_theme_ids"] if arguments.key?("parent_theme_ids")
+
+ return if !arguments.key?("theme_fields")
+
+ ThemeSupport.ensure_fields_editable!(theme)
+ ThemeSupport
+ .normalize_fields(arguments["theme_fields"])
+ .each do |field|
+ theme.set_field(
+ target: field.fetch("target"),
+ name: field.fetch("name"),
+ value: field["value"],
+ type: field["type"],
+ type_id: field["type_id"],
+ upload_id: field["upload_id"],
+ )
+ end
+ end
+ private_class_method :assign_changes
+
+ def self.apply_default(theme, arguments)
+ return if !arguments.key?("default")
+
+ if arguments["default"]
+ theme.set_default!
+ elsif theme.default?
+ ::Theme.clear_default!
+ end
+ end
+ private_class_method :apply_default
+ end
+ end
+end
diff --git a/spec/lib/discourse_mcp/staff_tools_spec.rb b/spec/lib/discourse_mcp/staff_tools_spec.rb
index b0de4dfbd2c..348614c0a0d 100644
--- a/spec/lib/discourse_mcp/staff_tools_spec.rb
+++ b/spec/lib/discourse_mcp/staff_tools_spec.rb
@@ -589,6 +589,288 @@ describe DiscourseMcp::Tools do
end
end
+ describe DiscourseMcp::Tools::CreateTheme do
+ it "creates a theme with fields for an admin and rejects everyone else" do
+ expect do
+ described_class.call(
+ arguments: {
+ "name" => "MCP Theme",
+ "user_selectable" => true,
+ "theme_fields" => [
+ { "name" => "header", "target" => "common", "value" => "<div>hello</div>" },
+ { "name" => "scss", "target" => "desktop", "value" => "body { color: red; }" },
+ ],
+ },
+ request_context: request_context(user),
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+
+ result =
+ described_class.call(
+ arguments: {
+ "name" => "MCP Theme",
+ "user_selectable" => true,
+ "theme_fields" => [
+ { "name" => "header", "target" => "common", "value" => "<div>hello</div>" },
+ { "name" => "scss", "target" => "desktop", "value" => "body { color: red; }" },
+ ],
+ },
+ request_context: request_context(admin),
+ ).fetch(:structuredContent)
+
+ theme = result[:theme]
+ expect(theme).to include(
+ name: "MCP Theme",
+ user_selectable: true,
+ component: false,
+ default: false,
+ )
+ expect(theme[:id]).to be_present
+ expect(theme[:theme_fields]).to contain_exactly(
+ include(name: "header", target: "common", value: "<div>hello</div>"),
+ include(name: "scss", target: "desktop", value: "body { color: red; }"),
+ )
+ expect(Theme.find(theme[:id])).to have_attributes(name: "MCP Theme", user_id: admin.id)
+ end
+
+ it "reports contract and model failures from the create service" do
+ expect do
+ described_class.call(arguments: { "name" => "" }, request_context: request_context(admin))
+ end.to raise_error(DiscourseMcp::ToolError)
+
+ expect do
+ described_class.call(
+ arguments: {
+ "name" => "Bad component",
+ "component" => true,
+ "color_scheme_id" => Fabricate(:color_scheme).id,
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(DiscourseMcp::ToolError, /color/i)
+ end
+
+ it "blocks creation when remote themes are allowlisted" do
+ GlobalSetting.stubs(:allowed_theme_repos).returns("https://github.com/discourse/sample-theme")
+
+ expect do
+ described_class.call(
+ arguments: {
+ "name" => "Blocked Theme",
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+ end
+ end
+
+ describe DiscourseMcp::Tools::UpdateTheme do
+ fab!(:theme) { Fabricate(:theme, user: admin) }
+
+ it "is admin-only" do
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "name" => "Renamed",
+ },
+ request_context: request_context(moderator),
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+ end
+
+ it "updates attributes and fields" do
+ result =
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "name" => "Renamed Theme",
+ "user_selectable" => true,
+ "enabled" => false,
+ "theme_fields" => [
+ { "name" => "header", "target" => "common", "value" => "<div>updated</div>" },
+ ],
+ },
+ request_context: request_context(admin),
+ ).fetch(:structuredContent)
+
+ expect(result[:theme]).to include(
+ id: theme.id,
+ name: "Renamed Theme",
+ user_selectable: true,
+ enabled: false,
+ )
+ expect(result[:theme][:theme_fields]).to contain_exactly(
+ include(name: "header", target: "common", value: "<div>updated</div>"),
+ )
+ expect(theme.reload).to have_attributes(
+ name: "Renamed Theme",
+ user_selectable: true,
+ enabled: false,
+ )
+ expect(theme.theme_fields.find_by(name: "header").value).to eq("<div>updated</div>")
+ end
+
+ it "records component enable and disable actions with the acting admin" do
+ component = Fabricate(:theme, component: true)
+ freeze_time
+
+ described_class.call(
+ arguments: {
+ "theme_id" => component.id,
+ "enabled" => false,
+ },
+ request_context: request_context(admin),
+ )
+
+ expect(component.reload.disabled_by).to eq(admin)
+ expect(component.disabled_at).to eq_time(Time.current)
+
+ described_class.call(
+ arguments: {
+ "theme_id" => component.id,
+ "enabled" => true,
+ },
+ request_context: request_context(admin),
+ )
+
+ expect(component.reload).to be_enabled
+ expect(
+ UserHistory.where(context: component.id.to_s).pluck(:action, :acting_user_id),
+ ).to contain_exactly(
+ [UserHistory.actions[:disable_theme_component], admin.id],
+ [UserHistory.actions[:enable_theme_component], admin.id],
+ )
+ end
+
+ it "clears a field when its value is blank" do
+ theme.set_field(target: :common, name: "header", value: "<div>bye</div>")
+ theme.save!
+
+ result =
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "theme_fields" => [{ "name" => "header", "target" => "common", "value" => "" }],
+ },
+ request_context: request_context(admin),
+ ).fetch(:structuredContent)
+
+ expect(result[:theme][:theme_fields]).to be_empty
+ expect(theme.reload.theme_fields.find_by(name: "header")).to be_nil
+ end
+
+ it "requires at least one field to update" do
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(DiscourseMcp::ToolError, I18n.t("mcp.errors.theme_update_required"))
+ end
+
+ it "reports a missing theme" do
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => 999_999,
+ "name" => "Nope",
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(DiscourseMcp::ToolError, I18n.t("mcp.errors.theme_not_found"))
+ end
+
+ it "rejects non-editable attributes on system themes" do
+ system_theme = Theme.find(-1)
+
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => system_theme.id,
+ "name" => "Renamed system theme",
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => system_theme.id,
+ "enabled" => false,
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(Discourse::InvalidAccess)
+ end
+
+ it "preserves component attributes when the requested default is invalid" do
+ component = Fabricate(:theme, component: true)
+ original_name = component.name
+ original_default = SiteSetting.default_theme_id
+
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => component.id,
+ "name" => "Not saved",
+ "default" => true,
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(DiscourseMcp::ToolError, I18n.t("themes.errors.component_no_default"))
+
+ expect(component.reload.name).to eq(original_name)
+ expect(SiteSetting.default_theme_id).to eq(original_default)
+ end
+
+ it "rolls back component relationships when a later field assignment fails" do
+ original_component = Fabricate(:theme, component: true)
+ replacement_component = Fabricate(:theme, component: true)
+ theme.update!(child_theme_ids: [original_component.id])
+
+ expect do
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "child_theme_ids" => [replacement_component.id],
+ "theme_fields" => [
+ { "name" => "unknown_field", "target" => "common", "value" => "content" },
+ ],
+ },
+ request_context: request_context(admin),
+ )
+ end.to raise_error(DiscourseMcp::ToolError, /No type could be guessed/)
+
+ expect(theme.reload.child_theme_ids).to contain_exactly(original_component.id)
+ end
+
+ it "sets and clears the default theme" do
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "default" => true,
+ },
+ request_context: request_context(admin),
+ )
+ expect(theme.reload).to be_default
+ expect(SiteSetting.default_theme_id).to eq(theme.id)
+
+ described_class.call(
+ arguments: {
+ "theme_id" => theme.id,
+ "default" => false,
+ },
+ request_context: request_context(admin),
+ )
+ expect(theme.reload).not_to be_default
+ expect(SiteSetting.default_theme_id).not_to eq(theme.id)
+ end
+ end
+
it "registers the selected tools under separate read and write scopes" do
expected = {
"discourse_get_review_queue_count" => [DiscourseMcp::Scopes::MODERATION_READ],
@@ -600,6 +882,9 @@ describe DiscourseMcp::Tools do
"discourse_perform_reviewable_action" => [DiscourseMcp::Scopes::MODERATION_WRITE],
"discourse_list_site_settings" => [DiscourseMcp::Scopes::SITE_SETTINGS_READ],
"discourse_update_site_setting" => [DiscourseMcp::Scopes::SITE_SETTINGS_WRITE],
+ "discourse_get_theme" => [DiscourseMcp::Scopes::THEMES_READ],
+ "discourse_create_theme" => [DiscourseMcp::Scopes::THEMES_WRITE],
+ "discourse_update_theme" => [DiscourseMcp::Scopes::THEMES_WRITE],
}
actual =
diff --git a/spec/requests/mcp_content_access_spec.rb b/spec/requests/mcp_content_access_spec.rb
index c5098d552c4..705267e4cc8 100644
--- a/spec/requests/mcp_content_access_spec.rb
+++ b/spec/requests/mcp_content_access_spec.rb
@@ -61,7 +61,7 @@ describe "MCP content access" do
expect(response.body).not_to include(message.raw)
end
- it "requires the dedicated moderation and site-setting scopes" do
+ it "requires the dedicated moderation, site-setting, and theme scopes" do
authorize
operations = {
"discourse_get_review_queue_count" => [{}, "mcp:moderation:read"],
@@ -88,6 +88,9 @@ describe "MCP content access" do
},
"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)|
@@ -98,6 +101,150 @@ describe "MCP content access" do
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")