Skip to content

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

FEATURE: Add advanced OAuth options for MCP servers (#38913)

Samcbb63ef66a3b
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_mcp_servers_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_mcp_servers_controller.rb
index dccdfa6ac3e..406bc83ea5a 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_mcp_servers_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_mcp_servers_controller.rb
@@ -179,8 +179,13 @@ module DiscourseAi
:oauth_client_id,
:oauth_client_secret_ai_secret_id,
:oauth_scopes,
+ :oauth_require_refresh_token,
:enabled,
:timeout_seconds,
+ oauth_authorization_params: {
+ },
+ oauth_token_params: {
+ },
)
end
@@ -212,6 +217,14 @@ module DiscourseAi
},
oauth_scopes: {
},
+ oauth_authorization_params: {
+ type: :large_text,
+ },
+ oauth_token_params: {
+ type: :large_text,
+ },
+ oauth_require_refresh_token: {
+ },
oauth_token_type: {
},
oauth_access_token_expires_at: {
diff --git a/plugins/discourse-ai/app/models/ai_mcp_server.rb b/plugins/discourse-ai/app/models/ai_mcp_server.rb
index ad5b9767955..e5bca71c453 100644
--- a/plugins/discourse-ai/app/models/ai_mcp_server.rb
+++ b/plugins/discourse-ai/app/models/ai_mcp_server.rb
@@ -12,6 +12,9 @@ class AiMcpServer < ActiveRecord::Base
oauth_client_id
oauth_client_secret_ai_secret_id
oauth_scopes
+ oauth_authorization_params
+ oauth_token_params
+ oauth_require_refresh_token
].freeze
belongs_to :ai_secret, optional: true
@@ -49,6 +52,7 @@ class AiMcpServer < ActiveRecord::Base
validate :validate_ai_secret_id_exists
validate :validate_oauth_client_secret_id_exists
validate :validate_oauth_configuration
+ validate :validate_oauth_advanced_configuration
validate :validate_public_https_url
after_commit :flush_tool_cache
@@ -332,6 +336,15 @@ class AiMcpServer < ActiveRecord::Base
self.auth_type = auth_type.presence || (ai_secret_id.present? ? "header_secret" : "none")
self.auth_header = auth_header.presence || "Authorization"
self.oauth_client_registration ||= "client_metadata_document" if oauth?
+ if oauth?
+ self.oauth_authorization_params = normalize_oauth_params(oauth_authorization_params)
+ self.oauth_token_params = normalize_oauth_params(oauth_token_params)
+ self.oauth_require_refresh_token = !!oauth_require_refresh_token
+ else
+ self.oauth_authorization_params = {}
+ self.oauth_token_params = {}
+ self.oauth_require_refresh_token = false
+ end
if !oauth?
self.oauth_client_registration =
@@ -366,6 +379,21 @@ class AiMcpServer < ActiveRecord::Base
end
end
+ def validate_oauth_advanced_configuration
+ return unless oauth?
+
+ if !oauth_authorization_params.is_a?(Hash)
+ errors.add(
+ :oauth_authorization_params,
+ I18n.t("discourse_ai.mcp_servers.oauth_authorization_params_invalid"),
+ )
+ end
+
+ if !oauth_token_params.is_a?(Hash)
+ errors.add(:oauth_token_params, I18n.t("discourse_ai.mcp_servers.oauth_token_params_invalid"))
+ end
+ end
+
def validate_public_https_url
uri = self.class.parse_public_uri(url)
@@ -404,6 +432,14 @@ class AiMcpServer < ActiveRecord::Base
def clear_oauth_tokens
oauth_token_store.clear!
end
+
+ def normalize_oauth_params(value)
+ return {} if value.blank?
+
+ value.is_a?(Hash) ? value : value.to_h
+ rescue NoMethodError, TypeError
+ value
+ end
end
# == Schema Information
@@ -423,18 +459,21 @@ end
# name :string(100) not null
# oauth_access_token_expires_at :datetime
# oauth_authorization_endpoint :string(1000)
+# oauth_authorization_params :jsonb not null
# oauth_client_registration :string(50) default("client_metadata_document")
# oauth_granted_scopes :string(2000)
# oauth_issuer :string(1000)
# oauth_last_authorized_at :datetime
# oauth_last_error :string(1000)
# oauth_last_refreshed_at :datetime
+# oauth_require_refresh_token :boolean default(FALSE), not null
# oauth_registration_endpoint :string(1000)
# oauth_resource_metadata_url :string(1000)
# oauth_revocation_endpoint :string(1000)
# oauth_scopes :string(2000)
# oauth_status :string(50) default("disconnected"), not null
# oauth_token_endpoint :string(1000)
+# oauth_token_params :jsonb not null
# oauth_token_type :string(100)
# protocol_version :string(100)
# server_capabilities :jsonb not null
diff --git a/plugins/discourse-ai/app/serializers/ai_mcp_server_serializer.rb b/plugins/discourse-ai/app/serializers/ai_mcp_server_serializer.rb
index 45c4f023837..1d511b48298 100644
--- a/plugins/discourse-ai/app/serializers/ai_mcp_server_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/ai_mcp_server_serializer.rb
@@ -13,6 +13,9 @@ class AiMcpServerSerializer < ApplicationSerializer
:oauth_client_id,
:oauth_client_secret_ai_secret_id,
:oauth_scopes,
+ :oauth_authorization_params,
+ :oauth_token_params,
+ :oauth_require_refresh_token,
:oauth_granted_scopes,
:oauth_token_type,
:oauth_access_token_expires_at,
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-mcp-server.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-mcp-server.js
index 09bb0c149f8..8f62854a75b 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-mcp-server.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-mcp-server.js
@@ -14,6 +14,9 @@ const CREATE_ATTRIBUTES = [
"oauth_client_id",
"oauth_client_secret_ai_secret_id",
"oauth_scopes",
+ "oauth_authorization_params",
+ "oauth_token_params",
+ "oauth_require_refresh_token",
"oauth_granted_scopes",
"oauth_token_type",
"oauth_access_token_expires_at",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-mcp-server-editor-form.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-mcp-server-editor-form.gjs
index 4c420cd3a12..2795b536ea2 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-mcp-server-editor-form.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-mcp-server-editor-form.gjs
@@ -23,10 +23,12 @@ export default class AiMcpServerEditorForm extends Component {
@tracked testResult = null;
@tracked formData = {};
@tracked showOauthDetails = false;
+ @tracked showOauthAdvancedOptions = false;
constructor() {
super(...arguments);
this.formData = this.buildFormData();
+ this.showOauthAdvancedOptions = this.hasOauthAdvancedOptions(this.formData);
}
buildFormData() {
@@ -47,11 +49,77 @@ export default class AiMcpServerEditorForm extends Component {
oauth_client_secret_ai_secret_id:
model.oauth_client_secret_ai_secret_id || null,
oauth_scopes: model.oauth_scopes || "",
+ oauth_authorization_params: this.formatOauthParams(
+ model.oauth_authorization_params
+ ),
+ oauth_token_params: this.formatOauthParams(model.oauth_token_params),
+ oauth_require_refresh_token: model.oauth_require_refresh_token ?? false,
enabled: model.enabled ?? true,
timeout_seconds: model.timeout_seconds || 30,
};
}
+ formatOauthParams(value) {
+ if (!value || Object.keys(value).length === 0) {
+ return "";
+ }
+
+ return JSON.stringify(value, null, 2);
+ }
+
+ hasOauthAdvancedOptions(data) {
+ return (
+ !!data.oauth_authorization_params ||
+ !!data.oauth_token_params ||
+ !!data.oauth_require_refresh_token
+ );
+ }
+
+ parseOauthParams(value, fieldKey) {
+ const trimmedValue = value?.trim();
+
+ if (!trimmedValue) {
+ return {};
+ }
+
+ let parsedValue;
+
+ try {
+ parsedValue = JSON.parse(trimmedValue);
+ } catch {
+ throw new Error(
+ i18n("discourse_ai.mcp_servers.oauth_json_object_required", {
+ field: i18n(fieldKey),
+ })
+ );
+ }
+
+ if (
+ !parsedValue ||
+ Array.isArray(parsedValue) ||
+ typeof parsedValue !== "object"
+ ) {
+ throw new Error(
+ i18n("discourse_ai.mcp_servers.oauth_json_object_required", {
+ field: i18n(fieldKey),
+ })
+ );
+ }
+
+ return parsedValue;
+ }
+
+ handleError(error) {
+ if (error?.jqXHR || error?.responseJSON) {
+ popupAjaxError(error);
+ return;
+ }
+
+ this.dialog.alert({
+ message: error?.message || String(error),
+ });
+ }
+
get authTypes() {
return [
{
@@ -134,8 +202,21 @@ export default class AiMcpServerEditorForm extends Component {
payload.oauth_client_id = null;
payload.oauth_client_secret_ai_secret_id = null;
payload.oauth_scopes = null;
+ payload.oauth_authorization_params = {};
+ payload.oauth_token_params = {};
+ payload.oauth_require_refresh_token = false;
} else {
payload.oauth_client_registration ||= "client_metadata_document";
+ payload.oauth_authorization_params = this.parseOauthParams(
+ payload.oauth_authorization_params,
+ "discourse_ai.mcp_servers.oauth_authorization_params"
+ );
+ payload.oauth_token_params = this.parseOauthParams(
+ payload.oauth_token_params,
+ "discourse_ai.mcp_servers.oauth_token_params"
+ );
+ payload.oauth_require_refresh_token =
+ !!payload.oauth_require_refresh_token;
if (payload.oauth_client_registration !== "manual") {
payload.oauth_client_id = null;
@@ -177,7 +258,7 @@ export default class AiMcpServerEditorForm extends Component {
try {
await this.persist(data);
} catch (e) {
- popupAjaxError(e);
+ this.handleError(e);
} finally {
later(() => {
this.isSaving = false;
@@ -195,7 +276,7 @@ export default class AiMcpServerEditorForm extends Component {
this.normalizeData(data)
);
} catch (e) {
- popupAjaxError(e);
+ this.handleError(e);
} finally {
this.isTesting = false;
}
@@ -209,7 +290,7 @@ export default class AiMcpServerEditorForm extends Component {
await this.persist(data, { transition: false, toast: false });
window.location.assign(this.args.model.oauthStartPath);
} catch (e) {
- popupAjaxError(e);
+ this.handleError(e);
this.isConnecting = false;
}
}
@@ -233,6 +314,11 @@ export default class AiMcpServerEditorForm extends Component {
this.showOauthDetails = !this.showOauthDetails;
}
+ @action
+ toggleOauthAdvancedOptions() {
+ this.showOauthAdvancedOptions = !this.showOauthAdvancedOptions;
+ }
+
@action
delete() {
return this.dialog.confirm({
@@ -399,6 +485,69 @@ export default class AiMcpServerEditorForm extends Component {
<field.Control />
</form.Field>
+ <form.Button
+ @action={{this.toggleOauthAdvancedOptions}}
+ @label={{if
+ this.showOauthAdvancedOptions
+ "discourse_ai.mcp_servers.oauth_hide_advanced_options"
+ "discourse_ai.mcp_servers.oauth_show_advanced_options"
+ }}
+ class="btn-flat btn-small ai-mcp-server-editor__oauth-details-toggle"
+ />
+
+ {{#if this.showOauthAdvancedOptions}}
+ <form.Field
+ @name="oauth_authorization_params"
+ @title={{i18n
+ "discourse_ai.mcp_servers.oauth_authorization_params"
+ }}
+ @tooltip={{i18n
+ "discourse_ai.mcp_servers.oauth_authorization_params_help"
+ }}
+ @format="full"
+ @type="textarea"
+ as |field|
+ >
+ <field.Control
+ @height={{100}}
+ placeholder={{i18n
+ "discourse_ai.mcp_servers.oauth_authorization_params_placeholder"
+ }}
+ />
+ </form.Field>
+
+ <form.Field
+ @name="oauth_token_params"
+ @title={{i18n "discourse_ai.mcp_servers.oauth_token_params"}}
+ @tooltip={{i18n "discourse_ai.mcp_servers.oauth_token_params_help"}}
+ @format="full"
+ @type="textarea"
+ as |field|
+ >
+ <field.Control
+ @height={{100}}
+ placeholder={{i18n
+ "discourse_ai.mcp_servers.oauth_token_params_placeholder"
+ }}
+ />
+ </form.Field>
+
+ <form.Field
+ @name="oauth_require_refresh_token"
+ @title={{i18n
+ "discourse_ai.mcp_servers.oauth_require_refresh_token"
+ }}
+ @tooltip={{i18n
+ "discourse_ai.mcp_servers.oauth_require_refresh_token_help"
+ }}
+ @format="full"
+ @type="checkbox"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ {{/if}}
+
{{#unless @model.isNew}}
<div class="ai-mcp-server-editor__oauth-state">
<h3>{{i18n "discourse_ai.mcp_servers.oauth_connection"}}</h3>
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 58ac0c510cb..903ab289452 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -783,6 +783,17 @@ en:
oauth_client_id: "OAuth client ID"
oauth_client_secret: "OAuth client secret"
oauth_scopes: "OAuth scopes"
+ oauth_authorization_params: "OAuth authorization params"
+ oauth_authorization_params_help: "Optional JSON object merged into the authorization request. Use null to remove a default parameter."
+ oauth_authorization_params_placeholder: '{"access_type":"offline"}'
+ oauth_token_params: "OAuth token params"
+ oauth_token_params_help: "Optional JSON object merged into token requests. Use null to remove a default parameter."
+ oauth_token_params_placeholder: "{}"
+ oauth_require_refresh_token: "Require refresh token"
+ oauth_require_refresh_token_help: "Fail the OAuth connection if the provider does not return a refresh token."
+ oauth_json_object_required: "%{field} must be a JSON object."
+ oauth_show_advanced_options: "Show advanced options"
+ oauth_hide_advanced_options: "Hide advanced options"
oauth_connection: "OAuth connection"
oauth_status: "OAuth status"
oauth_statuses:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 1123fb75b71..946f2a58f19 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -326,10 +326,16 @@ en:
oauth_token_exchange_failed: "The OAuth token exchange failed with status %{status}."
oauth_https_required: "OAuth requires your site to be served over HTTPS."
oauth_callback_failed: "OAuth connection failed: %{message}"
+ oauth_refresh_token_required: "The OAuth authorization server did not return a refresh token. Adjust the advanced OAuth options and reconnect this MCP server."
oauth_client_metadata_public_https_required: "Client metadata document registration requires a public HTTPS site URL. Current client metadata URL: %{url}"
oauth_registration_endpoint_missing: "The OAuth authorization server does not advertise a registration endpoint."
+ oauth_manual_client_registration_required: "The OAuth authorization server %{issuer} does not advertise dynamic client registration. Switch this MCP server to Manual client credentials and provide a registered OAuth client ID."
+ oauth_client_secret_required: "The OAuth authorization server %{issuer} requires token endpoint client authentication (%{methods}). Configure manual client credentials with a client secret."
+ oauth_token_endpoint_auth_method_unsupported: "The OAuth authorization server %{issuer} only advertises unsupported token endpoint client authentication methods (%{methods}). This MCP server currently supports none, client_secret_basic, and client_secret_post."
oauth_client_registration_failed: "Dynamic client registration failed with status %{status}."
oauth_client_registration_failed_no_id: "Dynamic client registration succeeded but no client_id was returned."
+ oauth_authorization_params_invalid: "OAuth authorization params must be a JSON object."
+ oauth_token_params_invalid: "OAuth token params must be a JSON object."
ai_helper:
errors:
diff --git a/plugins/discourse-ai/db/migrate/20260327040745_add_oauth_advanced_options_to_ai_mcp_servers.rb b/plugins/discourse-ai/db/migrate/20260327040745_add_oauth_advanced_options_to_ai_mcp_servers.rb
new file mode 100644
index 00000000000..3e804b96578
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20260327040745_add_oauth_advanced_options_to_ai_mcp_servers.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddOauthAdvancedOptionsToAiMcpServers < ActiveRecord::Migration[7.2]
+ def up
+ add_column :ai_mcp_servers, :oauth_authorization_params, :jsonb, null: false, default: {}
+ add_column :ai_mcp_servers, :oauth_token_params, :jsonb, null: false, default: {}
+ add_column :ai_mcp_servers, :oauth_require_refresh_token, :boolean, null: false, default: false
+ end
+
+ def down
+ remove_column :ai_mcp_servers, :oauth_authorization_params
+ remove_column :ai_mcp_servers, :oauth_token_params
+ remove_column :ai_mcp_servers, :oauth_require_refresh_token
+ end
+end
diff --git a/plugins/discourse-ai/lib/mcp/client.rb b/plugins/discourse-ai/lib/mcp/client.rb
index 446c7b75bea..44b574ffd2e 100644
--- a/plugins/discourse-ai/lib/mcp/client.rb
+++ b/plugins/discourse-ai/lib/mcp/client.rb
@@ -67,6 +67,7 @@ module DiscourseAi
},
session_id: session_id,
accept_sse: true,
+ allow_result_error: true,
)
extract_result!(response.payload)
@@ -90,6 +91,7 @@ module DiscourseAi
session_id: nil,
accept_sse: false,
notification: false,
+ allow_result_error: false,
allow_oauth_retry: true
)
uri = validate_uri!
@@ -101,7 +103,12 @@ module DiscourseAi
headers = default_headers(session_id: session_id)
response, raw_body = perform_request(uri, payload, headers)
- handle_response(response, raw_body, session_id: session_id)
+ handle_response(
+ response,
+ raw_body,
+ session_id: session_id,
+ allow_result_error: allow_result_error,
+ )
rescue UnauthorizedError => e
if server.oauth? && allow_oauth_retry && server.oauth_token_store.refresh_token.present?
DiscourseAi::Mcp::OAuthFlow.refresh!(server)
@@ -113,6 +120,7 @@ module DiscourseAi
session_id: session_id,
accept_sse: accept_sse,
notification: notification,
+ allow_result_error: allow_result_error,
allow_oauth_retry: false,
)
)
@@ -165,7 +173,7 @@ module DiscourseAi
[response, raw_body]
end
- def handle_response(response, raw_body, session_id:)
+ def handle_response(response, raw_body, session_id:, allow_result_error:)
status = response.code.to_i
if status == 404 && session_id.present?
@@ -187,18 +195,17 @@ module DiscourseAi
end
if status < 200 || status >= 300
+ if allow_result_error && tool_result_error?(body)
+ return build_response(response, body, status)
+ end
+
message = body.is_a?(Hash) ? body.dig("error", "message") : nil
raise Error,
message.presence ||
I18n.t("discourse_ai.mcp_servers.errors.request_failed", status: status)
end
- Response.new(
- payload: body,
- session_id: response[MCP_SESSION_ID_HEADER],
- status: status,
- headers: response.to_hash,
- )
+ build_response(response, body, status)
end
def parse_json_body(body)
@@ -251,6 +258,19 @@ module DiscourseAi
payload["result"] || {}
end
+ def build_response(response, body, status)
+ Response.new(
+ payload: body,
+ session_id: response[MCP_SESSION_ID_HEADER],
+ status: status,
+ headers: response.to_hash,
+ )
+ end
+
+ def tool_result_error?(body)
+ body.is_a?(Hash) && body.dig("result", "isError") == true
+ end
+
def validate_uri!
uri = AiMcpServer.parse_public_uri(server.url)
raise Error, I18n.t("discourse_ai.mcp_servers.invalid_url_not_https") if uri.nil?
diff --git a/plugins/discourse-ai/lib/mcp/oauth_discovery.rb b/plugins/discourse-ai/lib/mcp/oauth_discovery.rb
index 3137c82bc3a..e480f665775 100644
--- a/plugins/discourse-ai/lib/mcp/oauth_discovery.rb
+++ b/plugins/discourse-ai/lib/mcp/oauth_discovery.rb
@@ -12,6 +12,7 @@ module DiscourseAi
:token_endpoint,
:revocation_endpoint,
:registration_endpoint,
+ :token_endpoint_auth_methods_supported,
keyword_init: true,
)
@@ -57,6 +58,8 @@ module DiscourseAi
token_endpoint: token_endpoint,
revocation_endpoint: revocation_endpoint,
registration_endpoint: registration_endpoint,
+ token_endpoint_auth_methods_supported:
+ Array(auth_server_metadata["token_endpoint_auth_methods_supported"]).presence,
)
end
diff --git a/plugins/discourse-ai/lib/mcp/oauth_flow.rb b/plugins/discourse-ai/lib/mcp/oauth_flow.rb
index 8f3105f3002..cf82e306cd9 100644
--- a/plugins/discourse-ai/lib/mcp/oauth_flow.rb
+++ b/plugins/discourse-ai/lib/mcp/oauth_flow.rb
@@ -31,6 +31,9 @@ module DiscourseAi
server.reload
end
+ validate_client_registration!(server, discovery)
+ token_endpoint_client_auth_method(server, discovery)
+
state = SecureRandom.hex(32)
code_verifier = generate_code_verifier
Rails.cache.write(
@@ -72,14 +75,19 @@ module DiscourseAi
token_request(
server: server,
endpoint: discovery.token_endpoint,
- params: {
- grant_type: "authorization_code",
- code: params[:code].to_s,
- redirect_uri: server.oauth_callback_url,
- code_verifier: payload["code_verifier"],
- client_id: server.effective_oauth_client_id,
- resource: discovery.resource.presence || server.url,
- },
+ auth_method: token_endpoint_client_auth_method(server, discovery),
+ params:
+ merged_oauth_params(
+ {
+ "grant_type" => "authorization_code",
+ "code" => params[:code].to_s,
+ "redirect_uri" => server.oauth_callback_url,
+ "code_verifier" => payload["code_verifier"],
+ "client_id" => server.effective_oauth_client_id,
+ "resource" => discovery.resource.presence || server.url,
+ },
+ server.oauth_token_params,
+ ),
)
store_token_response!(server, token_payload, preserve_refresh_token: false)
@@ -104,12 +112,17 @@ module DiscourseAi
token_request(
server: server,
endpoint: discovery.token_endpoint,
- params: {
- grant_type: "refresh_token",
- refresh_token: refresh_token,
- client_id: server.effective_oauth_client_id,
- resource: discovery.resource.presence || server.url,
- },
+ auth_method: token_endpoint_client_auth_method(server, discovery),
+ params:
+ merged_oauth_params(
+ {
+ "grant_type" => "refresh_token",
+ "refresh_token" => refresh_token,
+ "client_id" => server.effective_oauth_client_id,
+ "resource" => discovery.resource.presence || server.url,
+ },
+ server.oauth_token_params,
+ ),
)
store_token_response!(server, token_payload, preserve_refresh_token: true)
@@ -146,23 +159,27 @@ module DiscourseAi
code_challenge =
Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)).tr("+/", "-_").delete("=")
- query = {
- response_type: "code",
- client_id: server.effective_oauth_client_id,
- redirect_uri: server.oauth_callback_url,
- state: state,
- code_challenge: code_challenge,
- code_challenge_method: "S256",
- resource: discovery.resource.presence || server.url,
- }
- query[:scope] = server.oauth_scopes if server.oauth_scopes.present?
+ query =
+ merged_oauth_params(
+ {
+ "response_type" => "code",
+ "client_id" => server.effective_oauth_client_id,
+ "redirect_uri" => server.oauth_callback_url,
+ "state" => state,
+ "code_challenge" => code_challenge,
+ "code_challenge_method" => "S256",
+ "resource" => discovery.resource.presence || server.url,
+ "scope" => server.oauth_scopes.presence,
+ },
+ server.oauth_authorization_params,
+ )
uri = URI(discovery.authorization_endpoint)
uri.query = Rack::Utils.build_query(query)
uri.to_s
end
- def token_request(server:, endpoint:, params:)
+ def token_request(server:, endpoint:, auth_method:, params:)
connection =
Faraday.new(request: { timeout: server.timeout_seconds }) do |builder|
builder.request :url_encoded
@@ -172,14 +189,19 @@ module DiscourseAi
validate_endpoint!(endpoint)
headers = { "Accept" => "application/json" }
- if server.oauth_client_secret_value.present?
+ request_params = params.deep_dup
+
+ case auth_method
+ when "client_secret_basic"
headers["Authorization"] = basic_auth_header(
server.effective_oauth_client_id,
server.oauth_client_secret_value,
)
+ when "client_secret_post"
+ request_params["client_secret"] = server.oauth_client_secret_value
end
- response = connection.post(endpoint, params, headers)
+ response = connection.post(endpoint, request_params, headers)
if response.status != 200
message =
@@ -210,6 +232,11 @@ module DiscourseAi
token_payload["refresh_token"]
end
+ if server.oauth_require_refresh_token && refresh_token.blank?
+ raise DiscourseAi::Mcp::Client::Error,
+ I18n.t("discourse_ai.mcp_servers.errors.oauth_refresh_token_required")
+ end
+
server.update_oauth_tokens!(
access_token: token_payload["access_token"],
refresh_token: refresh_token,
@@ -253,6 +280,12 @@ module DiscourseAi
"Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}"
end
+ def merged_oauth_params(defaults, overrides)
+ defaults.merge(overrides.to_h.stringify_keys).compact
+ rescue NoMethodError, TypeError
+ defaults.compact
+ end
+
def generate_code_verifier
Base64.urlsafe_encode64(OpenSSL::Random.random_bytes(32)).delete("=")
end
@@ -301,6 +334,60 @@ module DiscourseAi
url: server.oauth_client_metadata_url,
)
end
+
+ def validate_client_registration!(server, discovery)
+ return if server.oauth_client_id.present?
+ return if server.oauth_client_registration == "manual"
+ return if discovery.registration_endpoint.present?
+
+ raise DiscourseAi::Mcp::Client::Error,
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_manual_client_registration_required",
+ issuer: oauth_issuer_label(discovery, server),
+ )
+ end
+
+ def token_endpoint_client_auth_method(server, discovery)
+ methods = Array(discovery.token_endpoint_auth_methods_supported).map(&:to_s).presence
+ if methods.blank?
+ return server.oauth_client_secret_value.present? ? "client_secret_basic" : "none"
+ end
+
+ if server.oauth_client_secret_value.present?
+ return "client_secret_basic" if methods.include?("client_secret_basic")
+ return "client_secret_post" if methods.include?("client_secret_post")
+ return "none" if methods.include?("none")
+
+ raise unsupported_token_endpoint_client_auth_method_error(server, discovery, methods)
+ end
+
+ return "none" if methods.include?("none")
+
+ if methods.any? { |method| %w[client_secret_basic client_secret_post].include?(method) }
+ raise DiscourseAi::Mcp::Client::Error,
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_client_secret_required",
+ issuer: oauth_issuer_label(discovery, server),
+ methods: methods.join(", "),
+ )
+ end
+
+ raise unsupported_token_endpoint_client_auth_method_error(server, discovery, methods)
+ end
+
+ def unsupported_token_endpoint_client_auth_method_error(server, discovery, methods)
+ DiscourseAi::Mcp::Client::Error.new(
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_token_endpoint_auth_method_unsupported",
+ issuer: oauth_issuer_label(discovery, server),
+ methods: methods.join(", "),
+ ),
+ )
+ end
+
+ def oauth_issuer_label(discovery, server)
+ discovery.issuer.presence || discovery.authorization_endpoint.presence || server.url
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/mcp_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/mcp_spec.rb
index 1b093841400..650b00b04df 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/mcp_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/mcp_spec.rb
@@ -153,4 +153,27 @@ RSpec.describe DiscourseAi::Agents::Tools::Mcp do
expect(second.invoke).to eq({ result: "Found results" })
expect(context.mcp_session_for(ai_mcp_server.id)).to eq("session-1")
end
+
+ it "returns tool execution errors as text the model can inspect" do
+ context = DiscourseAi::Agents::BotContext.new(messages: [])
+ DiscourseAi::Mcp::Client
+ .any_instance
+ .stubs(:initialize_session)
+ .returns({ session_id: "session-1", result: {} })
+ DiscourseAi::Mcp::Client
+ .any_instance
+ .stubs(:call_tool)
+ .returns(
+ {
+ "content" => [{ "type" => "text", "text" => "Not found: Project google.com:chops-prod" }],
+ "isError" => true,
+ },
+ )
+
+ tool = tool_class.new({ query: "bug" }, bot_user: user, llm: nil, context: context)
+
+ expect(tool.invoke).to eq(
+ { status: "error", error: "Not found: Project google.com:chops-prod" },
+ )
+ end
end
diff --git a/plugins/discourse-ai/spec/lib/mcp/client_spec.rb b/plugins/discourse-ai/spec/lib/mcp/client_spec.rb
index fe2a5ca13f0..86bd5874e66 100644
--- a/plugins/discourse-ai/spec/lib/mcp/client_spec.rb
+++ b/plugins/discourse-ai/spec/lib/mcp/client_spec.rb
@@ -81,6 +81,83 @@ RSpec.describe DiscourseAi::Mcp::Client do
expect(result).to eq("content" => [{ "type" => "text", "text" => "Hello from MCP" }])
end
+ it "returns tool execution errors from the response body even on non-2xx statuses" do
+ stub_request(:post, server.url).to_return(
+ status: 404,
+ body: {
+ jsonrpc: "2.0",
+ result: {
+ content: [{ type: "text", text: "Not found: Project google.com:chops-prod" }],
+ isError: true,
+ },
+ }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+
+ result = described_class.new(server).call_tool("lookup", { id: 1 })
+
+ expect(result).to eq(
+ "content" => [{ "type" => "text", "text" => "Not found: Project google.com:chops-prod" }],
+ "isError" => true,
+ )
+ end
+
+ it "still raises on transport failures without a tool result" do
+ stub_request(:post, server.url).to_return(
+ status: 500,
+ body: "",
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+
+ expect { described_class.new(server).call_tool("lookup", {}) }.to raise_error(
+ described_class::Error,
+ I18n.t("discourse_ai.mcp_servers.errors.request_failed", status: 500),
+ )
+ end
+
+ it "preserves allow_result_error when retrying after an OAuth refresh" do
+ server.update!(auth_type: "oauth", ai_secret_id: nil, oauth_status: "connected")
+ server.oauth_token_store.write!(
+ access_token: "expired-access-token",
+ refresh_token: "refresh-token",
+ )
+
+ server.stubs(:auth_header_value).returns(
+ "Bearer expired-access-token",
+ "Bearer fresh-access-token",
+ "Bearer fresh-access-token",
+ )
+ DiscourseAi::Mcp::OAuthFlow.expects(:refresh!).with(server).once
+
+ stub_request(:post, server.url).to_return(
+ { status: 401, body: "", headers: { "Content-Type" => "application/json" } },
+ {
+ status: 404,
+ body: {
+ jsonrpc: "2.0",
+ result: {
+ content: [{ type: "text", text: "Not found: Project google.com:chops-prod" }],
+ isError: true,
+ },
+ }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ },
+ )
+
+ result = described_class.new(server).call_tool("lookup", { id: 1 })
+
+ expect(result).to eq(
+ "content" => [{ "type" => "text", "text" => "Not found: Project google.com:chops-prod" }],
+ "isError" => true,
+ )
+ end
+
it "raises when a session expires" do
stub_request(:post, server.url).to_return(
status: 404,
diff --git a/plugins/discourse-ai/spec/lib/mcp/oauth_flow_spec.rb b/plugins/discourse-ai/spec/lib/mcp/oauth_flow_spec.rb
index 4082b2b5e12..1ff372fd0a7 100644
--- a/plugins/discourse-ai/spec/lib/mcp/oauth_flow_spec.rb
+++ b/plugins/discourse-ai/spec/lib/mcp/oauth_flow_spec.rb
@@ -91,6 +91,146 @@ RSpec.describe DiscourseAi::Mcp::OAuthFlow do
expect(url).to include("client_id=manual-id")
expect(a_request(:post, "https://auth.example.com/register")).not_to have_been_made
end
+
+ it "requires manual registration when the auth server does not advertise dynamic registration" do
+ Discourse.stubs(:base_url).returns("https://discourse.example.com")
+
+ discovery =
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://accounts.google.com",
+ authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth",
+ token_endpoint: "https://oauth2.googleapis.com/token",
+ revocation_endpoint: "https://oauth2.googleapis.com/revoke",
+ registration_endpoint: nil,
+ token_endpoint_auth_methods_supported: %w[client_secret_post client_secret_basic],
+ )
+ DiscourseAi::Mcp::OAuthDiscovery.stubs(:discover!).returns(discovery)
+
+ expect { described_class.start!(server: ai_mcp_server, user: user) }.to raise_error(
+ DiscourseAi::Mcp::Client::Error,
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_manual_client_registration_required",
+ issuer: "https://accounts.google.com",
+ ),
+ )
+ end
+
+ it "requires a client secret when the token endpoint does not support public clients" do
+ Discourse.stubs(:base_url).returns("https://discourse.example.com")
+ ai_mcp_server.update!(oauth_client_registration: "manual", oauth_client_id: "manual-id")
+
+ discovery =
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://accounts.google.com",
+ authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth",
+ token_endpoint: "https://oauth2.googleapis.com/token",
+ revocation_endpoint: "https://oauth2.googleapis.com/revoke",
+ registration_endpoint: nil,
+ token_endpoint_auth_methods_supported: %w[client_secret_post client_secret_basic],
+ )
+ DiscourseAi::Mcp::OAuthDiscovery.stubs(:discover!).returns(discovery)
+
+ expect { described_class.start!(server: ai_mcp_server, user: user) }.to raise_error(
+ DiscourseAi::Mcp::Client::Error,
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_client_secret_required",
+ issuer: "https://accounts.google.com",
+ methods: "client_secret_post, client_secret_basic",
+ ),
+ )
+ end
+
+ it "allows client_secret_post-only token endpoints when a client secret is configured" do
+ Discourse.stubs(:base_url).returns("https://discourse.example.com")
+ ai_mcp_server.update!(
+ oauth_client_registration: "manual",
+ oauth_client_id: "manual-id",
+ oauth_client_secret_ai_secret_id: oauth_client_secret.id,
+ )
+
+ discovery =
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://auth.example.com",
+ authorization_endpoint: "https://auth.example.com/authorize",
+ token_endpoint: "https://auth.example.com/token",
+ revocation_endpoint: nil,
+ registration_endpoint: nil,
+ token_endpoint_auth_methods_supported: %w[client_secret_post],
+ )
+ DiscourseAi::Mcp::OAuthDiscovery.stubs(:discover!).returns(discovery)
+
+ url = described_class.start!(server: ai_mcp_server, user: user)
+
+ expect(url).to include("client_id=manual-id")
+ end
+
+ it "rejects token endpoints that only advertise unsupported client auth methods" do
+ Discourse.stubs(:base_url).returns("https://discourse.example.com")
+ ai_mcp_server.update!(
+ oauth_client_registration: "manual",
+ oauth_client_id: "manual-id",
+ oauth_client_secret_ai_secret_id: oauth_client_secret.id,
+ )
+
+ discovery =
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://auth.example.com",
+ authorization_endpoint: "https://auth.example.com/authorize",
+ token_endpoint: "https://auth.example.com/token",
+ revocation_endpoint: nil,
+ registration_endpoint: nil,
+ token_endpoint_auth_methods_supported: %w[private_key_jwt],
+ )
+ DiscourseAi::Mcp::OAuthDiscovery.stubs(:discover!).returns(discovery)
+
+ expect { described_class.start!(server: ai_mcp_server, user: user) }.to raise_error(
+ DiscourseAi::Mcp::Client::Error,
+ I18n.t(
+ "discourse_ai.mcp_servers.errors.oauth_token_endpoint_auth_method_unsupported",
+ issuer: "https://auth.example.com",
+ methods: "private_key_jwt",
+ ),
+ )
+ end
+
+ it "merges advanced authorization params into the authorization request" do
+ Discourse.stubs(:base_url).returns("https://discourse.example.com")
+ ai_mcp_server.update!(
+ oauth_client_registration: "manual",
+ oauth_client_id: "manual-id",
+ oauth_authorization_params: {
+ "access_type" => "offline",
+ "resource" => nil,
+ },
+ )
+
+ discovery =
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://auth.example.com",
+ authorization_endpoint: "https://auth.example.com/authorize",
+ token_endpoint: "https://auth.example.com/token",
+ revocation_endpoint: nil,
+ registration_endpoint: "https://auth.example.com/register",
+ )
+ DiscourseAi::Mcp::OAuthDiscovery.stubs(:discover!).returns(discovery)
+
+ uri = URI(described_class.start!(server: ai_mcp_server, user: user))
+ query = Rack::Utils.parse_nested_query(uri.query)
+
+ expect(query["client_id"]).to eq("manual-id")
+ expect(query["access_type"]).to eq("offline")
+ expect(query).not_to have_key("resource")
+ end
end
describe ".complete!" do
@@ -130,6 +270,60 @@ RSpec.describe DiscourseAi::Mcp::OAuthFlow do
expect(ai_mcp_server.reload.oauth_status).to eq("error")
expect(ai_mcp_server.oauth_last_error).to eq("Client not found")
end
+
+ it "merges advanced token params and can require a refresh token" do
+ ai_mcp_server.update!(
+ oauth_client_registration: "manual",
+ oauth_client_id: "client-id",
+ oauth_require_refresh_token: true,
+ oauth_token_params: {
+ "audience" => "https://bigquery.googleapis.com/",
+ "resource" => nil,
+ },
+ )
+ ai_mcp_server.update_columns(
+ oauth_authorization_endpoint: "https://auth.example.com/authorize",
+ oauth_token_endpoint: "https://auth.example.com/token",
+ oauth_issuer: "https://auth.example.com",
+ )
+
+ state = SecureRandom.hex(32)
+ Rails.cache.write(
+ "discourse-ai:mcp-oauth-state:#{state}",
+ {
+ "ai_mcp_server_id" => ai_mcp_server.id,
+ "user_id" => user.id,
+ "code_verifier" => "test-verifier",
+ },
+ expires_in: 10.minutes,
+ )
+
+ stub_request(:post, "https://auth.example.com/token")
+ .with do |request|
+ decoded_body = Rack::Utils.parse_nested_query(request.body)
+ decoded_body["audience"] == "https://bigquery.googleapis.com/" &&
+ !decoded_body.key?("resource")
+ end
+ .to_return(
+ status: 200,
+ body: { access_token: "fresh-access-token", token_type: "Bearer" }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+
+ expect {
+ described_class.complete!(params: { state: state, code: "auth-code" }, current_user: user)
+ }.to raise_error(
+ described_class::OAuthError,
+ I18n.t("discourse_ai.mcp_servers.errors.oauth_refresh_token_required"),
+ )
+
+ expect(ai_mcp_server.reload.oauth_status).to eq("error")
+ expect(ai_mcp_server.oauth_last_error).to eq(
+ I18n.t("discourse_ai.mcp_servers.errors.oauth_refresh_token_required"),
+ )
+ end
end
describe ".refresh!" do
@@ -138,6 +332,10 @@ RSpec.describe DiscourseAi::Mcp::OAuthFlow do
oauth_client_registration: "manual",
oauth_client_id: "client-id",
oauth_client_secret_ai_secret_id: oauth_client_secret.id,
+ oauth_token_params: {
+ "audience" => "https://bigquery.googleapis.com/",
+ "resource" => nil,
+ },
)
ai_mcp_server.oauth_token_store.write!(
access_token: "expired-access-token",
@@ -159,7 +357,56 @@ RSpec.describe DiscourseAi::Mcp::OAuthFlow do
decoded_body = Rack::Utils.parse_nested_query(request.body)
request.headers["Authorization"] ==
"Basic #{Base64.strict_encode64("client-id:#{oauth_client_secret.secret}")}" &&
- decoded_body["refresh_token"] == "refresh-token" && !decoded_body.key?("client_secret")
+ decoded_body["refresh_token"] == "refresh-token" &&
+ decoded_body["audience"] == "https://bigquery.googleapis.com/" &&
+ !decoded_body.key?("client_secret") && !decoded_body.key?("resource")
+ end
+ .to_return(
+ status: 200,
+ body: {
+ access_token: "fresh-access-token",
+ refresh_token: "fresh-refresh-token",
+ token_type: "Bearer",
+ }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+
+ described_class.refresh!(ai_mcp_server)
+
+ expect(ai_mcp_server.reload.oauth_token.access_token).to eq("fresh-access-token")
+ expect(ai_mcp_server.oauth_token.refresh_token).to eq("fresh-refresh-token")
+ end
+
+ it "uses client_secret_post when the token endpoint only supports body authentication" do
+ ai_mcp_server.update!(
+ oauth_client_registration: "manual",
+ oauth_client_id: "client-id",
+ oauth_client_secret_ai_secret_id: oauth_client_secret.id,
+ )
+ ai_mcp_server.oauth_token_store.write!(
+ access_token: "expired-access-token",
+ refresh_token: "refresh-token",
+ )
+ ai_mcp_server.stubs(:oauth_discovery_result).returns(
+ DiscourseAi::Mcp::OAuthDiscovery::Result.new(
+ resource: ai_mcp_server.url,
+ resource_metadata_url: "#{ai_mcp_server.url}/.well-known/oauth-protected-resource",
+ issuer: "https://auth.example.com",
+ authorization_endpoint: "https://auth.example.com/authorize",
+ token_endpoint: "https://auth.example.com/token",
+ revocation_endpoint: nil,
+ token_endpoint_auth_methods_supported: %w[client_secret_post],
+ ),
+ )
+
+ stub_request(:post, "https://auth.example.com/token")
+ .with do |request|
+ decoded_body = Rack::Utils.parse_nested_query(request.body)
+ request.headers["Authorization"].blank? && decoded_body["client_id"] == "client-id" &&
+ decoded_body["client_secret"] == oauth_client_secret.secret &&
+ decoded_body["refresh_token"] == "refresh-token"
end
.to_return(
status: 200,
diff --git a/plugins/discourse-ai/spec/models/ai_mcp_server_spec.rb b/plugins/discourse-ai/spec/models/ai_mcp_server_spec.rb
index d92ba138af1..064769d7700 100644
--- a/plugins/discourse-ai/spec/models/ai_mcp_server_spec.rb
+++ b/plugins/discourse-ai/spec/models/ai_mcp_server_spec.rb
@@ -65,6 +65,41 @@ RSpec.describe AiMcpServer do
)
end
+ it "requires advanced OAuth params to be JSON objects" do
+ server =
+ Fabricate.build(
+ :ai_mcp_server,
+ auth_type: "oauth",
+ oauth_authorization_params: "access_type=offline",
+ oauth_token_params: ["audience"],
+ )
+
+ expect(server).not_to be_valid
+ expect(server.errors[:oauth_authorization_params]).to include(
+ I18n.t("discourse_ai.mcp_servers.oauth_authorization_params_invalid"),
+ )
+ expect(server.errors[:oauth_token_params]).to include(
+ I18n.t("discourse_ai.mcp_servers.oauth_token_params_invalid"),
+ )
+ end
+
+ it "clears advanced OAuth options when auth_type is not oauth" do
+ server =
+ Fabricate.build(
+ :ai_mcp_server,
+ auth_type: "header_secret",
+ ai_secret: ai_secret,
+ oauth_authorization_params: "access_type=offline",
+ oauth_token_params: ["audience"],
+ oauth_require_refresh_token: true,
+ )
+
+ expect(server).to be_valid
+ expect(server.oauth_authorization_params).to eq({})
+ expect(server.oauth_token_params).to eq({})
+ expect(server.oauth_require_refresh_token).to eq(false)
+ end
+
it "builds an OAuth bearer header from the stored access token" do
server =
Fabricate(
@@ -149,6 +184,23 @@ RSpec.describe AiMcpServer do
expect(server.oauth_token_store.refresh_token).to be_blank
end
+ it "clears stored OAuth credentials when advanced OAuth options change" do
+ server =
+ Fabricate(
+ :ai_mcp_server,
+ auth_type: "oauth",
+ oauth_status: "connected",
+ oauth_token_type: "Bearer",
+ )
+ server.oauth_token_store.write!(access_token: "access-token", refresh_token: "refresh-token")
+
+ server.update!(oauth_authorization_params: { "access_type" => "offline" })
+
+ expect(server.reload.oauth_status).to eq("disconnected")
+ expect(server.oauth_token_store.access_token).to be_blank
+ expect(server.oauth_token_store.refresh_token).to be_blank
+ end
+
it "memoizes serialized tools for count calculations" do
server = Fabricate(:ai_mcp_server)
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_mcp_servers_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_mcp_servers_controller_spec.rb
index ca85e200042..fff60c5f759 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_mcp_servers_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_mcp_servers_controller_spec.rb
@@ -98,6 +98,41 @@ RSpec.describe DiscourseAi::Admin::AiMcpServersController do
expect(response.parsed_body["ai_mcp_server"]["name"]).to eq("Jira")
end
+ it "persists advanced OAuth options" do
+ post "/admin/plugins/discourse-ai/ai-mcp-servers.json",
+ params: {
+ ai_mcp_server: {
+ name: "BigQuery",
+ description: "BigQuery MCP",
+ url: "https://bigquery.googleapis.com/mcp",
+ auth_type: "oauth",
+ oauth_client_registration: "manual",
+ oauth_client_id: "client-id",
+ oauth_client_secret_ai_secret_id: ai_secret.id,
+ oauth_scopes: "https://www.googleapis.com/auth/bigquery",
+ oauth_authorization_params: {
+ access_type: "offline",
+ },
+ oauth_token_params: {
+ audience: "https://bigquery.googleapis.com/",
+ },
+ oauth_require_refresh_token: true,
+ },
+ }.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ }
+
+ expect(response).to have_http_status(:created)
+ expect(response.parsed_body.dig("ai_mcp_server", "oauth_authorization_params")).to eq(
+ "access_type" => "offline",
+ )
+ expect(response.parsed_body.dig("ai_mcp_server", "oauth_token_params")).to eq(
+ "audience" => "https://bigquery.googleapis.com/",
+ )
+ expect(response.parsed_body.dig("ai_mcp_server", "oauth_require_refresh_token")).to eq(true)
+ end
+
it "rejects localhost urls" do
expect {
post "/admin/plugins/discourse-ai/ai-mcp-servers.json",