maquina-generators 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +201 -12
- data/lib/generators/maquina/app/USAGE +4 -0
- data/lib/generators/maquina/app/app_generator.rb +38 -7
- data/lib/generators/maquina/app/templates/README.md.erb +21 -0
- data/lib/generators/maquina/app/templates/app/views/home/index.html.erb +1 -1
- data/lib/generators/maquina/auth_flavor.rb +59 -0
- data/lib/generators/maquina/backstage_signin/backstage_signin.rb +320 -0
- data/lib/generators/maquina/backstage_signin/templates/test/integration/backstage_access_test.rb.tt +104 -0
- data/lib/generators/maquina/csp_reports/csp_reports_generator.rb +13 -1
- data/lib/generators/maquina/csp_reports/templates/app/models/mcp/tools/csp_reports.rb +71 -0
- data/lib/generators/maquina/csp_reports/templates/test/models/mcp/tools/csp_reports_test.rb +35 -0
- data/lib/generators/maquina/error_context/USAGE +22 -0
- data/lib/generators/maquina/error_context/error_context_generator.rb +51 -0
- data/lib/generators/maquina/mcp/USAGE +38 -0
- data/lib/generators/maquina/mcp/mcp_generator.rb +194 -0
- data/lib/generators/maquina/mcp/mcp_sibling_tools.rb +122 -0
- data/lib/generators/maquina/mcp/templates/app/controllers/concerns/mcp_authenticatable.rb +63 -0
- data/lib/generators/maquina/mcp/templates/app/controllers/mcp_controller.rb +205 -0
- data/lib/generators/maquina/mcp/templates/app/models/mcp/tool_definitions.rb +38 -0
- data/lib/generators/maquina/mcp/templates/app/models/mcp/tool_error.rb +17 -0
- data/lib/generators/maquina/mcp/templates/app/models/mcp/tools/app.rb +51 -0
- data/lib/generators/maquina/mcp/templates/app/models/mcp/tools.rb +83 -0
- data/lib/generators/maquina/mcp/templates/app/models/mcp/user_scope.rb +50 -0
- data/lib/generators/maquina/mcp/templates/config/initializers/mcp_cors.rb +38 -0
- data/lib/generators/maquina/mcp/templates/test/integration/mcp_conformance_test.rb +223 -0
- data/lib/generators/maquina/mcp/templates/test/integration/mcp_flow_test.rb.tt +65 -0
- data/lib/generators/maquina/mcp/templates/test/models/mcp/tools_test.rb.tt +145 -0
- data/lib/generators/maquina/mcp/templates/test/test_helpers/mcp_test_helper.rb.tt +103 -0
- data/lib/generators/maquina/mcp_tool/USAGE +26 -0
- data/lib/generators/maquina/mcp_tool/mcp_tool_generator.rb +239 -0
- data/lib/generators/maquina/mcp_tool/templates/test/models/mcp/tools/tool_test.rb.tt +49 -0
- data/lib/generators/maquina/mission_control_jobs/USAGE +5 -0
- data/lib/generators/maquina/mission_control_jobs/mission_control_jobs_generator.rb +21 -5
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/backstage_dashboard/index.html.erb.tt +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/layouts/_admin_navigation.html.erb.tt +12 -11
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/jobs/_filters.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/jobs/_general_information.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/jobs/_job.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/jobs/failed/_job.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/queues/_job.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/shared/_job.html.erb +1 -1
- data/lib/generators/maquina/mission_control_jobs/templates/app/views/mission_control/jobs/workers/_worker.html.erb +1 -1
- data/lib/generators/maquina/oauth/USAGE +35 -0
- data/lib/generators/maquina/oauth/oauth_generator.rb +359 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/connected_applications_controller.rb +26 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/oauth/authorization_servers_controller.rb +24 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/oauth/authorizations_controller.rb +92 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/oauth/protected_resources_controller.rb +14 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/oauth/revocations_controller.rb +20 -0
- data/lib/generators/maquina/oauth/templates/app/controllers/oauth/tokens_controller.rb +67 -0
- data/lib/generators/maquina/oauth/templates/app/models/access_token.rb +83 -0
- data/lib/generators/maquina/oauth/templates/app/models/connected_application.rb +49 -0
- data/lib/generators/maquina/oauth/templates/app/models/mcp.rb.tt +33 -0
- data/lib/generators/maquina/oauth/templates/app/models/oauth_authorization_code.rb +52 -0
- data/lib/generators/maquina/oauth/templates/app/models/oauth_client/metadata_fetcher.rb +195 -0
- data/lib/generators/maquina/oauth/templates/app/models/oauth_client.rb +50 -0
- data/lib/generators/maquina/oauth/templates/app/views/connected_applications/index.html.erb +43 -0
- data/lib/generators/maquina/oauth/templates/app/views/oauth/authorizations/error.html.erb +8 -0
- data/lib/generators/maquina/oauth/templates/app/views/oauth/authorizations/new.html.erb +36 -0
- data/lib/generators/maquina/oauth/templates/config/locales/oauth.en.yml +34 -0
- data/lib/generators/maquina/oauth/templates/config/locales/oauth.es.yml +34 -0
- data/lib/generators/maquina/oauth/templates/migration_create_access_tokens.rb.tt +20 -0
- data/lib/generators/maquina/oauth/templates/migration_create_oauth_authorization_codes.rb.tt +19 -0
- data/lib/generators/maquina/oauth/templates/migration_create_oauth_clients.rb.tt +14 -0
- data/lib/generators/maquina/oauth/templates/test/fixtures/access_tokens.yml +48 -0
- data/lib/generators/maquina/oauth/templates/test/fixtures/oauth_authorization_codes.yml +11 -0
- data/lib/generators/maquina/oauth/templates/test/fixtures/oauth_clients.yml +7 -0
- data/lib/generators/maquina/oauth/templates/test/integration/connected_applications_test.rb.tt +70 -0
- data/lib/generators/maquina/oauth/templates/test/integration/oauth_authorization_test.rb.tt +150 -0
- data/lib/generators/maquina/oauth/templates/test/integration/oauth_discovery_test.rb +44 -0
- data/lib/generators/maquina/oauth/templates/test/integration/oauth_tokens_test.rb +147 -0
- data/lib/generators/maquina/oauth/templates/test/models/access_token_test.rb.tt +80 -0
- data/lib/generators/maquina/oauth/templates/test/models/connected_application_test.rb +41 -0
- data/lib/generators/maquina/oauth/templates/test/models/oauth_authorization_code_test.rb +58 -0
- data/lib/generators/maquina/oauth/templates/test/test_helpers/oauth_test_helper.rb.tt +82 -0
- data/lib/generators/maquina/passkeys/USAGE +4 -2
- data/lib/generators/maquina/passkeys/passkeys_generator.rb +16 -11
- data/lib/generators/maquina/passkeys/templates/app/controllers/session/{passkeys_controller.rb → passkeys_controller.rb.tt} +6 -2
- data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.en.yml → passkeys.en.yml.tt} +15 -0
- data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.es.yml → passkeys.es.yml.tt} +15 -0
- data/lib/generators/maquina/passkeys/templates/test/integration/{passkeys_test.rb → passkeys_test.rb.tt} +6 -0
- data/lib/generators/maquina/rack_attack/rack_attack_generator.rb +4 -1
- data/lib/generators/maquina/rack_attack/templates/config/initializers/rack_attack.rb.tt +317 -4
- data/lib/generators/maquina/registration/registration_generator.rb +32 -0
- data/lib/generators/maquina/registration/templates/app/views/registrations/new.html.erb.tt +4 -0
- data/lib/generators/maquina/registration/templates/app/views/sessions/new.html.erb.tt +5 -0
- data/lib/generators/maquina/security/USAGE +12 -0
- data/lib/generators/maquina/security/security_generator.rb +79 -7
- data/lib/generators/maquina/security/templates/app/models/mcp/tools/security.rb.tt +114 -0
- data/lib/generators/maquina/security/templates/app/views/backstage_dashboard/index.html.erb.tt +1 -1
- data/lib/generators/maquina/security/templates/app/views/layouts/_admin_navigation.html.erb.tt +12 -11
- data/lib/generators/maquina/security/templates/test/models/mcp/tools/security_test.rb +99 -0
- data/lib/generators/maquina/solid_errors/USAGE +21 -10
- data/lib/generators/maquina/solid_errors/error_context_files.rb +82 -0
- data/lib/generators/maquina/solid_errors/solid_errors_generator.rb +52 -31
- data/lib/generators/maquina/solid_errors/templates/app/agents/errors_query.rb +86 -29
- data/lib/generators/maquina/solid_errors/templates/app/agents/failure_triage.rb +4 -2
- data/lib/generators/maquina/solid_errors/templates/app/agents/list_failures_tool.rb +9 -4
- data/lib/generators/maquina/solid_errors/templates/app/models/mcp/tools/solid_errors.rb.tt +132 -0
- data/lib/generators/maquina/solid_errors/templates/app/views/backstage_dashboard/index.html.erb.tt +1 -1
- data/lib/generators/maquina/solid_errors/templates/app/views/layouts/_admin_navigation.html.erb.tt +12 -11
- data/lib/generators/maquina/solid_errors/templates/app/views/solid_errors/errors/show/_actions.html.erb +0 -15
- data/lib/generators/maquina/solid_errors/templates/app/views/solid_errors/errors/show/_error_details.html.erb +5 -5
- data/lib/generators/maquina/solid_errors/templates/app/views/solid_errors/occurrences/_collection.html.erb +1 -1
- data/lib/generators/maquina/solid_errors/templates/config/initializers/error_context.rb +33 -0
- data/lib/generators/maquina/solid_errors/templates/test/models/mcp/tools/solid_errors_filters_test.rb +68 -0
- data/lib/generators/maquina/solid_errors/templates/test/models/mcp/tools/solid_errors_test.rb +109 -0
- data/lib/generators/maquina/solid_queue/USAGE +11 -0
- data/lib/generators/maquina/solid_queue/solid_queue_generator.rb +25 -1
- data/lib/generators/maquina/solid_queue/templates/app/models/mcp/tools/solid_queue.rb.tt +163 -0
- data/lib/generators/maquina/solid_queue/templates/test/models/mcp/tools/solid_queue_test.rb +99 -0
- data/lib/maquina_generators/version.rb +1 -1
- data/test/fixtures/error_context/0.10.0/app/agents/errors_query.rb +105 -0
- data/test/fixtures/error_context/0.10.0/app/agents/failure_triage.rb +21 -0
- data/test/fixtures/error_context/0.10.0/app/agents/list_failures_tool.rb +20 -0
- data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors.rb +81 -0
- data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors_write.rb +101 -0
- data/test/generators/maquina/app_generator_test.rb +67 -5
- data/test/generators/maquina/backstage_signin_test.rb +296 -0
- data/test/generators/maquina/csp_reports_generator_test.rb +125 -6
- data/test/generators/maquina/error_context_generator_test.rb +188 -0
- data/test/generators/maquina/mcp_generator_test.rb +429 -0
- data/test/generators/maquina/mcp_tool_generator_test.rb +235 -0
- data/test/generators/maquina/mission_control_jobs_generator_test.rb +23 -0
- data/test/generators/maquina/oauth_client_metadata_fetcher_test.rb +290 -0
- data/test/generators/maquina/oauth_generator_test.rb +568 -0
- data/test/generators/maquina/passkeys_generator_test.rb +62 -2
- data/test/generators/maquina/rack_attack_generator_test.rb +71 -0
- data/test/generators/maquina/rack_attack_rules_test.rb +304 -0
- data/test/generators/maquina/registration_generator_test.rb +62 -0
- data/test/generators/maquina/security_generator_test.rb +188 -13
- data/test/generators/maquina/solid_errors_generator_test.rb +299 -33
- data/test/generators/maquina/solid_queue_generator_test.rb +175 -25
- data/test/support/rails_auth_app.rb +48 -0
- data/test/test_helper.rb +1 -0
- metadata +90 -41
- data/lib/generators/maquina/solid_errors/templates/app/controllers/failures_mcp_controller.rb.tt +0 -65
- data/test/tmp/Gemfile +0 -2
- data/test/tmp/app/controllers/application_controller.rb +0 -3
- data/test/tmp/app/controllers/concerns/authentication.rb +0 -63
- data/test/tmp/app/controllers/registration/verification_resends_controller.rb +0 -38
- data/test/tmp/app/controllers/registration/verifications_controller.rb +0 -64
- data/test/tmp/app/controllers/registrations_controller.rb +0 -63
- data/test/tmp/app/controllers/session/verification_resends_controller.rb +0 -44
- data/test/tmp/app/controllers/session/verifications_controller.rb +0 -64
- data/test/tmp/app/controllers/sessions_controller.rb +0 -56
- data/test/tmp/app/helpers/authentication_helper.rb +0 -20
- data/test/tmp/app/jobs/authentication_cleanup_job.rb +0 -13
- data/test/tmp/app/mailers/verification_mailer.rb +0 -15
- data/test/tmp/app/models/account.rb +0 -5
- data/test/tmp/app/models/current.rb +0 -5
- data/test/tmp/app/models/email_verification.rb +0 -58
- data/test/tmp/app/models/session.rb +0 -18
- data/test/tmp/app/models/user.rb +0 -37
- data/test/tmp/app/views/registration/verifications/new.html.erb +0 -43
- data/test/tmp/app/views/registrations/new.html.erb +0 -39
- data/test/tmp/app/views/session/verifications/new.html.erb +0 -43
- data/test/tmp/app/views/sessions/new.html.erb +0 -40
- data/test/tmp/app/views/verification_mailer/verification_code.html.erb +0 -37
- data/test/tmp/app/views/verification_mailer/verification_code.text.erb +0 -11
- data/test/tmp/config/locales/clave.en.yml +0 -102
- data/test/tmp/config/locales/clave.es.yml +0 -102
- data/test/tmp/config/routes.rb +0 -12
- data/test/tmp/db/migrate/20261019191386_create_accounts.rb +0 -9
- data/test/tmp/db/migrate/20261019191387_create_users.rb +0 -17
- data/test/tmp/db/migrate/20261019191388_create_sessions.rb +0 -12
- data/test/tmp/db/migrate/20261019191389_create_email_verifications.rb +0 -17
- data/test/tmp/test/test_helpers/session_test_helper.rb +0 -19
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module Mcp
|
|
2
|
+
module Tools
|
|
3
|
+
# The app's own MCP tools (generated by maquina:mcp). `rails g
|
|
4
|
+
# maquina:mcp_tool NAME` adds to this file at the three maquina:mcp_tool
|
|
5
|
+
# markers; leave them in place and edit everything else freely.
|
|
6
|
+
#
|
|
7
|
+
# A tool is a DEFINITIONS entry, which is what the model reads, plus a
|
|
8
|
+
# `def self.<name>(scope:, **arguments)` that returns a Hash. Reach records
|
|
9
|
+
# only through scope (an Mcp::UserScope): scope.records(:posts),
|
|
10
|
+
# scope.find(:posts, id). Raise Mcp::ToolError with a message for the
|
|
11
|
+
# model when a call cannot be done. A tool that changes data has
|
|
12
|
+
# readOnlyHint: false and its name in WRITE_TOOLS.
|
|
13
|
+
module App
|
|
14
|
+
ADMIN_ONLY = false
|
|
15
|
+
|
|
16
|
+
DEFINITIONS = [
|
|
17
|
+
{
|
|
18
|
+
name: "whoami",
|
|
19
|
+
description: "Shows who this connection acts as. Returns the signed-in user's email address, " \
|
|
20
|
+
"the OAuth scopes this connection was granted (read, or read write), the server version and " \
|
|
21
|
+
"the MCP protocol revision. Takes no arguments. Call it to check that the connection works.",
|
|
22
|
+
inputSchema: {
|
|
23
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {},
|
|
26
|
+
required: [],
|
|
27
|
+
additionalProperties: false
|
|
28
|
+
},
|
|
29
|
+
annotations: {readOnlyHint: true}
|
|
30
|
+
}
|
|
31
|
+
# maquina:mcp_tool definitions go above this line
|
|
32
|
+
].freeze
|
|
33
|
+
|
|
34
|
+
# maquina:mcp_tool write tools are listed on the next line
|
|
35
|
+
WRITE_TOOLS = %w[].freeze
|
|
36
|
+
|
|
37
|
+
def self.whoami(scope:)
|
|
38
|
+
result = {
|
|
39
|
+
email: scope.user.email_address,
|
|
40
|
+
scope: scope.oauth_scopes.join(" "),
|
|
41
|
+
server_version: Mcp::SERVER_VERSION,
|
|
42
|
+
protocol_version: Mcp::PROTOCOL_VERSION
|
|
43
|
+
}
|
|
44
|
+
result[:flag] = {name: Mcp::FLAG.to_s, enabled: scope.enabled?} if Mcp::FLAG
|
|
45
|
+
result
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# maquina:mcp_tool methods go above this line
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module Mcp
|
|
2
|
+
# Runs a tool call (generated by maquina:mcp). Dispatch is an allowlist:
|
|
3
|
+
# public_send only ever reaches a name declared in a visible module's
|
|
4
|
+
# DEFINITIONS, which is what keeps a tool call from being a remote method
|
|
5
|
+
# call. Inside each tool module, helpers are private_class_method.
|
|
6
|
+
#
|
|
7
|
+
# The arguments are checked against the tool's inputSchema first: required
|
|
8
|
+
# keys, unknown keys, the primitive type and enum membership. They reach the
|
|
9
|
+
# tool as keyword arguments.
|
|
10
|
+
module Tools
|
|
11
|
+
InvalidParams = Class.new(StandardError)
|
|
12
|
+
|
|
13
|
+
TYPES = {
|
|
14
|
+
"string" => [String],
|
|
15
|
+
"integer" => [Integer],
|
|
16
|
+
"number" => [Integer, Float],
|
|
17
|
+
"boolean" => [TrueClass, FalseClass],
|
|
18
|
+
"array" => [Array],
|
|
19
|
+
"object" => [Hash]
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
# A result Hash for tools/call. Raises InvalidParams for an unknown or
|
|
23
|
+
# hidden tool and for arguments that do not fit the schema.
|
|
24
|
+
def self.call(name, arguments, scope:)
|
|
25
|
+
owner = Mcp::ToolDefinitions.owner(name, scope.user) || raise(InvalidParams, "Unknown tool: #{name}")
|
|
26
|
+
definition = owner::DEFINITIONS.find { |candidate| candidate[:name] == name }
|
|
27
|
+
|
|
28
|
+
run(owner, name, scope, validate_arguments(definition[:inputSchema], arguments))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Symbol-keyed arguments that fit the schema. A null value counts as not sent.
|
|
32
|
+
def self.validate_arguments(schema, arguments)
|
|
33
|
+
arguments = {} if arguments.nil?
|
|
34
|
+
raise InvalidParams, "arguments must be an object" unless arguments.is_a?(Hash)
|
|
35
|
+
|
|
36
|
+
properties = schema.fetch(:properties, {}).transform_keys(&:to_s)
|
|
37
|
+
arguments = arguments.transform_keys(&:to_s).compact
|
|
38
|
+
|
|
39
|
+
unknown = arguments.keys - properties.keys
|
|
40
|
+
raise InvalidParams, "Unknown argument: #{unknown.first}" if unknown.any?
|
|
41
|
+
|
|
42
|
+
Array(schema[:required]).map(&:to_s).each do |key|
|
|
43
|
+
raise InvalidParams, "Missing required argument: #{key}" unless arguments.key?(key)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
arguments.each { |key, value| check_value(key, properties[key], value) }
|
|
47
|
+
arguments.to_h { |key, value| [key.to_sym, value] }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.check_value(key, property, value)
|
|
51
|
+
type = property[:type].to_s
|
|
52
|
+
if TYPES.key?(type) && TYPES[type].none? { |klass| value.is_a?(klass) }
|
|
53
|
+
raise InvalidParams, "#{key} must be of type #{type}"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
enum = property[:enum]
|
|
57
|
+
raise InvalidParams, "#{key} must be one of: #{enum.join(", ")}" if enum && !enum.include?(value)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.run(owner, name, scope, arguments)
|
|
61
|
+
success(owner.public_send(name, scope: scope, **arguments))
|
|
62
|
+
rescue Mcp::ToolError => error
|
|
63
|
+
failure(error.message)
|
|
64
|
+
rescue ActiveRecord::RecordNotFound
|
|
65
|
+
failure(Mcp::ToolError.not_found.message)
|
|
66
|
+
rescue => error
|
|
67
|
+
# An unexpected failure is still a result the model can read; the
|
|
68
|
+
# details go to the error reporter, not to the client.
|
|
69
|
+
Rails.error.report(error, handled: true, context: {mcp_tool: name})
|
|
70
|
+
failure("#{name} failed on the server. The error was recorded; retrying will not help until it is fixed.")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.success(structured)
|
|
74
|
+
{content: [{type: "text", text: structured.to_json}], structuredContent: structured, isError: false}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def self.failure(message)
|
|
78
|
+
{content: [{type: "text", text: message}], isError: true}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private_class_method :check_value, :run, :success, :failure
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module Mcp
|
|
2
|
+
# The one way a tool reaches data (generated by maquina:mcp). A token
|
|
3
|
+
# reaches its own user's records, never the whole account, so every tool
|
|
4
|
+
# gets this scope instead of a user and reads through the user's
|
|
5
|
+
# associations:
|
|
6
|
+
#
|
|
7
|
+
# scope.records(:posts).where(status: "draft")
|
|
8
|
+
# scope.find(:posts, id) # someone else's id is the same not found
|
|
9
|
+
#
|
|
10
|
+
# When Mcp.enabled_for?(user) is false (a feature flag that is off), every
|
|
11
|
+
# association comes back empty.
|
|
12
|
+
class UserScope
|
|
13
|
+
attr_reader :user, :oauth_scopes
|
|
14
|
+
|
|
15
|
+
def initialize(user, oauth_scopes: [])
|
|
16
|
+
@user = user
|
|
17
|
+
@oauth_scopes = oauth_scopes
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def enabled?
|
|
21
|
+
return @enabled if defined?(@enabled)
|
|
22
|
+
|
|
23
|
+
@enabled = Mcp.enabled_for?(user)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The user's association by name, or none of it when MCP is off for them.
|
|
27
|
+
def records(association)
|
|
28
|
+
raise ArgumentError, "User has no association #{association.inspect}" unless user.class.reflect_on_association(association)
|
|
29
|
+
|
|
30
|
+
relation = user.public_send(association)
|
|
31
|
+
enabled? ? relation : relation.none
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# One of the user's records. A missing id, a malformed id and another
|
|
35
|
+
# user's id all raise the same Mcp::ToolError.not_found.
|
|
36
|
+
def find(association, id)
|
|
37
|
+
records(association).find_by(id: record_id(id)) || raise(ToolError.not_found)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def write?
|
|
41
|
+
oauth_scopes.include?("write")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def record_id(value)
|
|
47
|
+
value.to_s[/\A\d+\z/]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# CORS for the MCP and OAuth endpoints (generated by maquina:mcp).
|
|
2
|
+
#
|
|
3
|
+
# Browser-based MCP clients, such as the MCP Inspector, run discovery, the
|
|
4
|
+
# token exchange and the MCP calls with fetch() from their own origin.
|
|
5
|
+
# Without these headers discovery "works" but its answer is blocked, and the
|
|
6
|
+
# client quietly falls back to default endpoints that do not exist.
|
|
7
|
+
#
|
|
8
|
+
# The wildcard origin is safe only because none of these paths use cookies:
|
|
9
|
+
# never add Access-Control-Allow-Credentials. /oauth/authorize is left out on
|
|
10
|
+
# purpose; it is a top-level page on the signed-in session.
|
|
11
|
+
#
|
|
12
|
+
# It sits at position 0 so a preflight is answered before host authorization
|
|
13
|
+
# or anything else sees it. Middleware does not reload: restart after editing.
|
|
14
|
+
class McpCors
|
|
15
|
+
PATHS = %r{\A/(?:mcp\z|oauth/(?:token|revoke)\z|\.well-known/oauth-)}
|
|
16
|
+
HEADERS = {
|
|
17
|
+
"access-control-allow-origin" => "*",
|
|
18
|
+
"access-control-allow-methods" => "GET, POST, DELETE, OPTIONS",
|
|
19
|
+
"access-control-allow-headers" => "Authorization, Content-Type, MCP-Protocol-Version, Mcp-Method, Mcp-Name",
|
|
20
|
+
"access-control-expose-headers" => "WWW-Authenticate",
|
|
21
|
+
"access-control-max-age" => "86400"
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
def initialize(app)
|
|
25
|
+
@app = app
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call(env)
|
|
29
|
+
return @app.call(env) unless PATHS.match?(env["PATH_INFO"].to_s)
|
|
30
|
+
return [204, HEADERS.dup, []] if env["REQUEST_METHOD"] == "OPTIONS"
|
|
31
|
+
|
|
32
|
+
status, headers, body = @app.call(env)
|
|
33
|
+
HEADERS.each { |name, value| headers[name] = value }
|
|
34
|
+
[status, headers, body]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
Rails.application.config.middleware.insert_before 0, McpCors
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
# The MCP 2026-07-28 rules this server follows, one test each (generated by
|
|
4
|
+
# maquina:mcp). mcp_post builds a valid request; each test breaks one thing.
|
|
5
|
+
class McpConformanceTest < ActionDispatch::IntegrationTest
|
|
6
|
+
WHOAMI = {method: "tools/call", params: {name: "whoami", arguments: {}}}.freeze
|
|
7
|
+
|
|
8
|
+
setup do
|
|
9
|
+
@user = users(:mcp_member)
|
|
10
|
+
@token = oauth_token_for(@user, scope: "read")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Headers mirror the body (Mcp-Method, Mcp-Name, MCP-Protocol-Version)
|
|
14
|
+
|
|
15
|
+
test "a missing Mcp-Method header is a header mismatch" do
|
|
16
|
+
mcp_post({method: "tools/list"}, token: @token, headers: {"Mcp-Method" => nil})
|
|
17
|
+
|
|
18
|
+
assert_rpc_error(-32020, :bad_request)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test "an Mcp-Method header that differs from the body is a header mismatch" do
|
|
22
|
+
mcp_post({method: "tools/list"}, token: @token, headers: {"Mcp-Method" => "tools/call"})
|
|
23
|
+
|
|
24
|
+
assert_rpc_error(-32020, :bad_request)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test "a missing Mcp-Name header on tools/call is a header mismatch" do
|
|
28
|
+
mcp_post(WHOAMI, token: @token, headers: {"Mcp-Name" => nil})
|
|
29
|
+
|
|
30
|
+
assert_rpc_error(-32020, :bad_request)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test "an Mcp-Name header that differs from params.name is a header mismatch" do
|
|
34
|
+
mcp_post(WHOAMI, token: @token, headers: {"Mcp-Name" => "other_tool"})
|
|
35
|
+
|
|
36
|
+
assert_rpc_error(-32020, :bad_request)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test "a missing MCP-Protocol-Version header is a header mismatch" do
|
|
40
|
+
mcp_post({method: "tools/list"}, token: @token, headers: {"MCP-Protocol-Version" => nil})
|
|
41
|
+
|
|
42
|
+
assert_rpc_error(-32020, :bad_request)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
test "an MCP-Protocol-Version header that differs from _meta is a header mismatch" do
|
|
46
|
+
mcp_post({method: "tools/list"}, token: @token, headers: {"MCP-Protocol-Version" => "2025-11-25"})
|
|
47
|
+
|
|
48
|
+
assert_rpc_error(-32020, :bad_request)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test "an Mcp-Name header in the base64 sentinel form is decoded before comparing" do
|
|
52
|
+
mcp_post(WHOAMI, token: @token, headers: {"Mcp-Name" => "=?base64?#{Base64.strict_encode64("whoami")}?="})
|
|
53
|
+
|
|
54
|
+
assert_response :success
|
|
55
|
+
assert_equal false, response.parsed_body.dig("result", "isError")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Per-request protocol version in params._meta
|
|
59
|
+
|
|
60
|
+
test "an unsupported protocol version lists the supported ones" do
|
|
61
|
+
meta = {McpTestHelper::MCP_PROTOCOL_VERSION_META => "2025-11-25", McpTestHelper::MCP_CLIENT_CAPABILITIES_META => {}}
|
|
62
|
+
mcp_post({method: "tools/list", params: {_meta: meta}}, token: @token)
|
|
63
|
+
|
|
64
|
+
assert_rpc_error(-32022, :bad_request)
|
|
65
|
+
assert_equal [Mcp::PROTOCOL_VERSION], response.parsed_body.dig("error", "data", "supported")
|
|
66
|
+
assert_equal "2025-11-25", response.parsed_body.dig("error", "data", "requested")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
test "a request without the protocol version in _meta is invalid params" do
|
|
70
|
+
mcp_post({method: "tools/list", params: {_meta: {}}}, token: @token)
|
|
71
|
+
|
|
72
|
+
assert_rpc_error(-32602, :bad_request)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Methods
|
|
76
|
+
|
|
77
|
+
test "server/discover answers without a token" do
|
|
78
|
+
mcp_post({method: "server/discover"})
|
|
79
|
+
|
|
80
|
+
assert_response :success
|
|
81
|
+
result = response.parsed_body["result"]
|
|
82
|
+
assert_equal [Mcp::PROTOCOL_VERSION], result["supportedVersions"]
|
|
83
|
+
assert result["capabilities"].key?("tools")
|
|
84
|
+
assert_equal Mcp::SERVER_VERSION, result.dig("_meta", "io.modelcontextprotocol/serverInfo", "version")
|
|
85
|
+
assert result.dig("_meta", "io.modelcontextprotocol/serverInfo", "name").present?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
test "tools/list and tools/call need a token" do
|
|
89
|
+
[{method: "tools/list"}, WHOAMI].each do |payload|
|
|
90
|
+
mcp_post(payload)
|
|
91
|
+
|
|
92
|
+
assert_response :unauthorized
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test "initialize is a 404 that names the supported revision" do
|
|
97
|
+
legacy = {protocolVersion: "2025-11-25", capabilities: {}, clientInfo: {name: "legacy", version: "1"}}
|
|
98
|
+
post mcp_path, params: {jsonrpc: "2.0", id: 1, method: "initialize", params: legacy}.to_json,
|
|
99
|
+
headers: {"Content-Type" => "application/json"}
|
|
100
|
+
|
|
101
|
+
assert_response :not_found
|
|
102
|
+
assert_includes response.parsed_body.dig("error", "message"), "2026-07-28"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
test "an unknown method is method not found" do
|
|
106
|
+
mcp_post({method: "resources/list"}, token: @token)
|
|
107
|
+
|
|
108
|
+
assert_rpc_error(-32601, :not_found)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
test "GET and DELETE are not allowed" do
|
|
112
|
+
get mcp_path
|
|
113
|
+
assert_response :method_not_allowed
|
|
114
|
+
|
|
115
|
+
delete mcp_path
|
|
116
|
+
assert_response :method_not_allowed
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Results
|
|
120
|
+
|
|
121
|
+
test "every result is complete" do
|
|
122
|
+
mcp_post({method: "server/discover"})
|
|
123
|
+
assert_equal "complete", response.parsed_body.dig("result", "resultType")
|
|
124
|
+
|
|
125
|
+
mcp_post({method: "tools/list"}, token: @token)
|
|
126
|
+
assert_equal "complete", response.parsed_body.dig("result", "resultType")
|
|
127
|
+
|
|
128
|
+
mcp_post(WHOAMI, token: @token)
|
|
129
|
+
assert_equal "complete", response.parsed_body.dig("result", "resultType")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
test "tools/list is private, cached for five minutes and in registry order" do
|
|
133
|
+
mcp_post({method: "tools/list"}, token: @token)
|
|
134
|
+
result = response.parsed_body["result"]
|
|
135
|
+
names = result["tools"].map { |tool| tool["name"] }
|
|
136
|
+
|
|
137
|
+
assert_equal 300_000, result["ttlMs"]
|
|
138
|
+
assert_equal "private", result["cacheScope"]
|
|
139
|
+
assert_equal Mcp::ToolDefinitions.for(@user).map { |definition| definition[:name] }, names
|
|
140
|
+
assert_includes names, "whoami"
|
|
141
|
+
|
|
142
|
+
mcp_post({method: "tools/list"}, token: @token)
|
|
143
|
+
assert_equal names, response.parsed_body.dig("result", "tools").map { |tool| tool["name"] }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Error codes
|
|
147
|
+
|
|
148
|
+
test "a body that is not JSON is a parse error" do
|
|
149
|
+
post mcp_path, params: "{not json", headers: {"Content-Type" => "application/json", "Mcp-Method" => "tools/list"}
|
|
150
|
+
|
|
151
|
+
assert_rpc_error(-32700, :bad_request)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
test "a batch is an invalid request" do
|
|
155
|
+
post mcp_path, params: [{jsonrpc: "2.0", id: 1, method: "tools/list"}].to_json,
|
|
156
|
+
headers: {"Content-Type" => "application/json"}
|
|
157
|
+
|
|
158
|
+
assert_rpc_error(-32600, :bad_request)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
test "an unknown tool is invalid params, never -32002" do
|
|
162
|
+
mcp_post({method: "tools/call", params: {name: "no_such_tool", arguments: {}}}, token: @token)
|
|
163
|
+
|
|
164
|
+
assert_rpc_error(-32602, :ok)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
test "the error table holds every code" do
|
|
168
|
+
codes = McpController::ERRORS.values.map { |error| error[:code] }
|
|
169
|
+
|
|
170
|
+
assert_empty [-32700, -32600, -32601, -32602, -32603, -32020, -32021, -32022] - codes
|
|
171
|
+
assert_not_includes codes, -32002
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Authorization challenges
|
|
175
|
+
|
|
176
|
+
test "a 401 points at the protected resource metadata" do
|
|
177
|
+
mcp_post({method: "tools/list"}, token: "not-a-token")
|
|
178
|
+
|
|
179
|
+
assert_response :unauthorized
|
|
180
|
+
assert_equal %(Bearer resource_metadata="http://www.example.com/.well-known/oauth-protected-resource/mcp", scope="read"),
|
|
181
|
+
response.headers["WWW-Authenticate"]
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
test "a token's last use is recorded at most once a minute" do
|
|
185
|
+
access_token = AccessToken.authenticate(@token)
|
|
186
|
+
|
|
187
|
+
mcp_post(WHOAMI, token: @token)
|
|
188
|
+
first_use = access_token.reload.last_used_at
|
|
189
|
+
assert first_use
|
|
190
|
+
|
|
191
|
+
travel 30.seconds do
|
|
192
|
+
mcp_post(WHOAMI, token: @token)
|
|
193
|
+
assert_equal first_use, access_token.reload.last_used_at
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
travel 2.minutes do
|
|
197
|
+
mcp_post(WHOAMI, token: @token)
|
|
198
|
+
assert_operator access_token.reload.last_used_at, :>, first_use
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
test "a write tool with a read token is 403 insufficient_scope" do
|
|
203
|
+
admin = users(:mcp_admin)
|
|
204
|
+
name = Mcp::ToolDefinitions.modules.flat_map { |mod| mod::WRITE_TOOLS }
|
|
205
|
+
.find { |tool| Mcp::ToolDefinitions.owner(tool, admin) }
|
|
206
|
+
skip "No write tool yet: this runs once one is added (maquina:mcp_tool NAME --write)" unless name
|
|
207
|
+
|
|
208
|
+
mcp_post({method: "tools/call", params: {name: name, arguments: {}}}, token: oauth_token_for(admin, scope: "read"))
|
|
209
|
+
|
|
210
|
+
assert_response :forbidden
|
|
211
|
+
challenge = response.headers["WWW-Authenticate"]
|
|
212
|
+
assert_match(/\ABearer error="insufficient_scope"/, challenge)
|
|
213
|
+
assert_includes challenge, %(scope="read write")
|
|
214
|
+
assert_includes challenge, %(resource_metadata="http://www.example.com/.well-known/oauth-protected-resource/mcp")
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
private
|
|
218
|
+
|
|
219
|
+
def assert_rpc_error(code, status)
|
|
220
|
+
assert_response status
|
|
221
|
+
assert_equal code, response.parsed_body.dig("error", "code"), response.body
|
|
222
|
+
end
|
|
223
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
require_relative "../test_helpers/session_test_helper"
|
|
3
|
+
|
|
4
|
+
# An MCP client connecting end to end (generated by maquina:mcp): discovery,
|
|
5
|
+
# consent, the token exchange, a tool call, and revocation from the Connected
|
|
6
|
+
# apps page. The inspector fixture's metadata is fresh, so nothing is fetched.
|
|
7
|
+
class McpFlowTest < ActionDispatch::IntegrationTest
|
|
8
|
+
test "a client discovers, is authorized, calls whoami and loses access when revoked" do
|
|
9
|
+
user = users(:mcp_member)
|
|
10
|
+
client = oauth_clients(:inspector)
|
|
11
|
+
|
|
12
|
+
mcp_post({method: "server/discover"})
|
|
13
|
+
assert_response :success
|
|
14
|
+
assert_equal [Mcp::PROTOCOL_VERSION], response.parsed_body.dig("result", "supportedVersions")
|
|
15
|
+
|
|
16
|
+
mcp_post({method: "tools/list"})
|
|
17
|
+
assert_response :unauthorized
|
|
18
|
+
metadata_url = response.headers["WWW-Authenticate"][/resource_metadata="([^"]+)"/, 1]
|
|
19
|
+
|
|
20
|
+
get URI(metadata_url).path
|
|
21
|
+
assert_equal "http://www.example.com/mcp", response.parsed_body["resource"]
|
|
22
|
+
get "/.well-known/oauth-authorization-server"
|
|
23
|
+
assert_equal "/oauth/authorize", URI(response.parsed_body["authorization_endpoint"]).path
|
|
24
|
+
|
|
25
|
+
verifier, challenge = pkce_pair
|
|
26
|
+
sign_in_as user
|
|
27
|
+
<% unless options[:skip_views] -%>
|
|
28
|
+
get "/oauth/authorize", params: authorize_params(challenge: challenge)
|
|
29
|
+
assert_response :success
|
|
30
|
+
assert_includes response.body, client.client_name
|
|
31
|
+
<% end -%>
|
|
32
|
+
|
|
33
|
+
post "/oauth/authorize", params: authorize_params(challenge: challenge).merge(decision: "allow")
|
|
34
|
+
assert_equal "xyz-state", redirect_params["state"]
|
|
35
|
+
assert_equal "http://www.example.com", redirect_params["iss"]
|
|
36
|
+
|
|
37
|
+
post "/oauth/token", params: {
|
|
38
|
+
grant_type: "authorization_code",
|
|
39
|
+
code: redirect_params["code"],
|
|
40
|
+
redirect_uri: OauthTestHelper::INSPECTOR_REDIRECT_URI,
|
|
41
|
+
client_id: client.client_id,
|
|
42
|
+
code_verifier: verifier,
|
|
43
|
+
resource: "http://www.example.com/mcp"
|
|
44
|
+
}
|
|
45
|
+
assert_response :success
|
|
46
|
+
token = response.parsed_body["access_token"]
|
|
47
|
+
assert_equal "read", response.parsed_body["scope"]
|
|
48
|
+
|
|
49
|
+
mcp_post({method: "tools/call", params: {name: "whoami", arguments: {}}}, token: token)
|
|
50
|
+
assert_response :success
|
|
51
|
+
assert_equal user.email_address, response.parsed_body.dig("result", "structuredContent", "email")
|
|
52
|
+
assert_equal "read", response.parsed_body.dig("result", "structuredContent", "scope")
|
|
53
|
+
|
|
54
|
+
<% unless options[:skip_views] -%>
|
|
55
|
+
get connected_apps_path
|
|
56
|
+
assert_includes response.body, client.client_name
|
|
57
|
+
|
|
58
|
+
<% end -%>
|
|
59
|
+
delete connected_app_path(client)
|
|
60
|
+
assert_response :see_other
|
|
61
|
+
|
|
62
|
+
mcp_post({method: "tools/call", params: {name: "whoami", arguments: {}}}, token: token)
|
|
63
|
+
assert_response :unauthorized
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
<% if options[:flag] -%>
|
|
3
|
+
require_relative "../../test_helpers/session_test_helper"
|
|
4
|
+
<% end -%>
|
|
5
|
+
|
|
6
|
+
# The tool registry (generated by maquina:mcp): every module in
|
|
7
|
+
# app/models/mcp/tools/ is well formed, and no tool that changes data can be
|
|
8
|
+
# called with a read-only token because it was left out of WRITE_TOOLS.
|
|
9
|
+
class McpToolsTest < ActionDispatch::IntegrationTest
|
|
10
|
+
test "every tool that is not read-only is in its module's WRITE_TOOLS" do
|
|
11
|
+
assert_not_empty Mcp::ToolDefinitions.modules
|
|
12
|
+
|
|
13
|
+
Mcp::ToolDefinitions.modules.each do |mod|
|
|
14
|
+
mod::DEFINITIONS.each do |definition|
|
|
15
|
+
next if definition.dig(:annotations, :readOnlyHint) == true
|
|
16
|
+
|
|
17
|
+
assert_includes mod::WRITE_TOOLS, definition[:name], "#{mod}: #{definition[:name]} changes data, add it to WRITE_TOOLS"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test "every write tool is declared and marked as not read-only" do
|
|
23
|
+
assert_not_empty Mcp::ToolDefinitions.modules
|
|
24
|
+
|
|
25
|
+
Mcp::ToolDefinitions.modules.each do |mod|
|
|
26
|
+
mod::WRITE_TOOLS.each do |name|
|
|
27
|
+
definition = mod::DEFINITIONS.find { |candidate| candidate[:name] == name }
|
|
28
|
+
|
|
29
|
+
assert definition, "#{mod}: WRITE_TOOLS names #{name}, which has no definition"
|
|
30
|
+
assert_equal false, definition.dig(:annotations, :readOnlyHint), "#{mod}: #{name} needs readOnlyHint: false"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test "every module declares its tools the same way" do
|
|
36
|
+
names = Mcp::ToolDefinitions.modules.flat_map do |mod|
|
|
37
|
+
assert mod::DEFINITIONS.frozen?, "#{mod}::DEFINITIONS must be frozen"
|
|
38
|
+
assert mod::WRITE_TOOLS.frozen?, "#{mod}::WRITE_TOOLS must be frozen"
|
|
39
|
+
assert_includes [true, false], mod::ADMIN_ONLY, "#{mod}::ADMIN_ONLY must be true or false"
|
|
40
|
+
|
|
41
|
+
mod::DEFINITIONS.map do |definition|
|
|
42
|
+
schema = definition[:inputSchema]
|
|
43
|
+
assert definition[:description].present?, "#{definition[:name]} needs a description"
|
|
44
|
+
assert_equal "https://json-schema.org/draft/2020-12/schema", schema[:$schema], definition[:name]
|
|
45
|
+
assert_equal "object", schema[:type], definition[:name]
|
|
46
|
+
assert_equal false, schema[:additionalProperties], definition[:name]
|
|
47
|
+
assert mod.respond_to?(definition[:name]), "#{mod} has no method #{definition[:name]}"
|
|
48
|
+
definition[:name]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
assert_equal names.uniq, names, "tool names must be unique across modules"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test "whoami reports the user, the scope and the protocol revision" do
|
|
56
|
+
scope = Mcp::UserScope.new(users(:mcp_member), oauth_scopes: %w[read])
|
|
57
|
+
|
|
58
|
+
result = Mcp::Tools.call("whoami", {}, scope: scope)
|
|
59
|
+
|
|
60
|
+
assert_equal false, result[:isError]
|
|
61
|
+
assert_equal users(:mcp_member).email_address, result.dig(:structuredContent, :email)
|
|
62
|
+
assert_equal "read", result.dig(:structuredContent, :scope)
|
|
63
|
+
assert_equal Mcp::PROTOCOL_VERSION, result.dig(:structuredContent, :protocol_version)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test "arguments are checked against the schema before the tool runs" do
|
|
67
|
+
scope = Mcp::UserScope.new(users(:mcp_member), oauth_scopes: %w[read])
|
|
68
|
+
|
|
69
|
+
error = assert_raises(Mcp::Tools::InvalidParams) { Mcp::Tools.call("whoami", {"verbose" => true}, scope: scope) }
|
|
70
|
+
assert_includes error.message, "verbose"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
test "a member does not see admin-only tools" do
|
|
74
|
+
admin_only = Mcp::ToolDefinitions.modules.select { |mod| mod::ADMIN_ONLY }.flat_map { |mod| mod::DEFINITIONS }
|
|
75
|
+
visible = Mcp::ToolDefinitions.for(users(:mcp_member))
|
|
76
|
+
|
|
77
|
+
assert_empty admin_only & visible
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
test "a record that is not the user's is the same not found as a missing one" do
|
|
81
|
+
scope = Mcp::UserScope.new(users(:mcp_member))
|
|
82
|
+
other = users(:mcp_other).sessions.create!
|
|
83
|
+
|
|
84
|
+
foreign = assert_raises(Mcp::ToolError) { scope.find(:sessions, other.id) }
|
|
85
|
+
missing = assert_raises(Mcp::ToolError) { scope.find(:sessions, 0) }
|
|
86
|
+
assert_equal missing.message, foreign.message
|
|
87
|
+
assert_equal Mcp::ToolError::NOT_FOUND, foreign.message
|
|
88
|
+
end
|
|
89
|
+
<% if options[:flag] -%>
|
|
90
|
+
|
|
91
|
+
# Mcp::FLAG is :<%= options[:flag] %>. With it off for the user:
|
|
92
|
+
|
|
93
|
+
test "with the flag off the scope reaches nothing" do
|
|
94
|
+
user = users(:mcp_member)
|
|
95
|
+
user.sessions.create!
|
|
96
|
+
mcp_flag(false)
|
|
97
|
+
|
|
98
|
+
scope = Mcp::UserScope.new(user)
|
|
99
|
+
|
|
100
|
+
assert_not scope.enabled?
|
|
101
|
+
assert_empty scope.records(:sessions)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
test "with the flag off consent is refused" do
|
|
105
|
+
sign_in_as users(:mcp_member)
|
|
106
|
+
mcp_flag(false)
|
|
107
|
+
|
|
108
|
+
get "/oauth/authorize", params: authorize_params(challenge: pkce_pair.last)
|
|
109
|
+
|
|
110
|
+
assert_response :see_other
|
|
111
|
+
assert_equal "access_denied", redirect_params["error"]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
test "with the flag off the Connected apps page is not found" do
|
|
115
|
+
sign_in_as users(:mcp_member)
|
|
116
|
+
mcp_flag(false)
|
|
117
|
+
|
|
118
|
+
get connected_apps_path
|
|
119
|
+
|
|
120
|
+
assert_response :not_found
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
test "with the flag off server/discover still answers" do
|
|
124
|
+
mcp_flag(false)
|
|
125
|
+
|
|
126
|
+
mcp_post({method: "server/discover"})
|
|
127
|
+
|
|
128
|
+
assert_response :success
|
|
129
|
+
assert_equal [Mcp::PROTOCOL_VERSION], response.parsed_body.dig("result", "supportedVersions")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
test "with the flag off whoami says so and admin-only tools are hidden" do
|
|
133
|
+
token = oauth_token_for(users(:mcp_admin))
|
|
134
|
+
mcp_flag(false)
|
|
135
|
+
|
|
136
|
+
mcp_post({method: "tools/list"}, token: token)
|
|
137
|
+
names = response.parsed_body.dig("result", "tools").map { |tool| tool["name"] }
|
|
138
|
+
admin_only = Mcp::ToolDefinitions.modules.select { |mod| mod::ADMIN_ONLY }.flat_map { |mod| mod::DEFINITIONS.map { |d| d[:name] } }
|
|
139
|
+
assert_empty admin_only & names
|
|
140
|
+
|
|
141
|
+
mcp_post({method: "tools/call", params: {name: "whoami", arguments: {}}}, token: token)
|
|
142
|
+
assert_equal({"name" => "<%= options[:flag] %>", "enabled" => false}, response.parsed_body.dig("result", "structuredContent", "flag"))
|
|
143
|
+
end
|
|
144
|
+
<% end -%>
|
|
145
|
+
end
|