-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add Bearer Authentication to the MCP server plugin #21622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeroSteiner
wants to merge
7
commits into
rapid7:master
Choose a base branch
from
zeroSteiner:feat/mcp/plugin-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−21
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
002555e
Suppress stack traces when the address is in use
zeroSteiner 558cc37
Add authentication to the MCP plugin
zeroSteiner 8c3f2e3
Update the specs
zeroSteiner cf55f30
Log bind failures too
zeroSteiner 0b877c3
Update the validator to process the auth token
zeroSteiner af86bf4
Use a case insensitive key comparison
zeroSteiner 7209a46
Add additional specs
zeroSteiner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ class McpCommandDispatcher | |
| # Valid option keys accepted by `mcp start` and `mcp restart` | ||
| unless defined?(VALID_OPTIONS) | ||
| VALID_OPTIONS = %w[ | ||
| AuthToken | ||
| ServerHost ServerPort | ||
| RpcHost RpcPort RpcUser RpcPass RpcSSL | ||
| RateLimit | ||
|
|
@@ -67,6 +68,7 @@ def cmd_mcp_help | |
| print_line(' help - Show this help message') | ||
| print_line | ||
| print_line('Options (for start/restart):') | ||
| print_line(' AuthToken=<token> MCP server authentication token (default: random, blank: disabled)') | ||
| print_line(' ServerHost=<host> MCP server bind address (default: localhost)') | ||
| print_line(' ServerPort=<port> MCP server port (default: 3000)') | ||
| print_line(' RpcHost=<host> RPC server host (default: 127.0.0.1)') | ||
|
|
@@ -130,7 +132,8 @@ def parse_options(args) | |
| opts = {} | ||
| args.each do |arg| | ||
| key, value = arg.split('=', 2) | ||
| unless key && value && !value.empty? | ||
| # AuthToken can be blank while the others have to be set (blank is explicitly disabled) | ||
| unless key && value && (key.casecmp('AuthToken').zero? || !value.empty?) | ||
| print_error("Invalid option format: #{arg} (expected Key=Value)") | ||
| return nil | ||
| end | ||
|
|
@@ -286,13 +289,27 @@ def start_mcp_server(rpc, config) | |
|
|
||
| host = mcp_config[:host] | ||
| port = mcp_config[:port] | ||
| if mcp_config.key?(:auth_token) | ||
| auth_token = mcp_config[:auth_token] | ||
|
zeroSteiner marked this conversation as resolved.
|
||
| auth_token_generated = false | ||
| else | ||
| auth_token = @mcp_server.class.generate_auth_token | ||
| auth_token_generated = true | ||
| end | ||
|
zeroSteiner marked this conversation as resolved.
|
||
|
|
||
| print_status('Starting MCP server on HTTP transport...') | ||
| @server_thread = framework.threads.spawn('MCPServer', false) do | ||
| @mcp_server.start(transport: :http, host: host, port: port) | ||
| @mcp_server.start(transport: :http, host: host, port: port, auth_token: auth_token) | ||
|
zeroSteiner marked this conversation as resolved.
|
||
| end | ||
|
|
||
| @started_at = Time.now | ||
| print_server_status(mcp_config) | ||
| print_status("MCP server listening on http://#{Rex::Socket.to_authority(mcp_config[:host], mcp_config[:port])}/") | ||
| if auth_token_generated | ||
| print_status("Authentication: Bearer token (auto-generated)") | ||
| print_status(" Configure your MCP client with: Authorization: Bearer #{auth_token}") | ||
| else | ||
| print_status("Authentication: #{auth_token ? 'enabled' : 'disabled'}") | ||
| end | ||
|
Comment on lines
301
to
+312
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this code is duplicated, I took care so that what is printed is consistent between the plugin and the |
||
| rescue Msf::MCP::Metasploit::AuthenticationError => e | ||
| raise Msf::MCP::Metasploit::AuthenticationError, "RPC authentication failed: #{e.message}" | ||
| rescue Msf::MCP::Metasploit::ConnectionError => e | ||
|
|
@@ -301,10 +318,6 @@ def start_mcp_server(rpc, config) | |
| raise Msf::MCP::Error, "Address already in use: #{Rex::Socket.to_authority(mcp_config[:host], mcp_config[:port])}" | ||
| end | ||
|
|
||
| def print_server_status(mcp_config) | ||
| print_status("MCP server started on #{Rex::Socket.to_authority(mcp_config[:host], mcp_config[:port])} (transport: http)") | ||
| end | ||
|
|
||
| def format_uptime | ||
| return 'N/A' unless @started_at | ||
|
|
||
|
|
@@ -442,6 +455,10 @@ def resolve_config(opts) | |
| port: Integer(opts['ServerPort'] || Msf::MCP::Config::Defaults::MCP_PORT) | ||
| } | ||
|
|
||
| if opts.key?('AuthToken') | ||
| mcp_config[:auth_token] = opts['AuthToken'].blank? ? nil : opts['AuthToken'] | ||
| end | ||
|
|
||
| rate_limit_value = Integer(opts['RateLimit'] || Msf::MCP::Config::Defaults::RATE_LIMIT_REQUESTS_PER_MINUTE) | ||
|
|
||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.