Add Authentication to the MCP server's HTTP transport#21527
Open
zeroSteiner wants to merge 3 commits into
Open
Add Authentication to the MCP server's HTTP transport#21527zeroSteiner wants to merge 3 commits into
zeroSteiner wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds optional Bearer-token authentication to the MCP server’s HTTP transport and wires it into startup/config so msfmcpd --mcp-transport http can enable auth by default (auto-generating and printing a token only when it’s randomly generated).
Changes:
- Add a Rack middleware (
Msf::MCP::Middleware::BearerAuth) to enforceAuthorization: Bearer <token>on HTTP requests. - Add config + environment-variable support for
mcp.auth_token, including explicit disabling via empty string / null and env-precedence behavior. - Update startup output and documentation to explain authentication modes and client configuration.
Impact Analysis:
- Blast radius: medium — affects all MCP clients using the HTTP transport (authorization header now required by default for the CLI flow); stdio transport unaffected.
- Data and contract effects: HTTP request contract changes (401 +
WWW-Authenticateon missing/incorrect header); config semantics addmcp.auth_tokenwith env override. - Rollback and test focus: verify auth precedence (
configvsMSF_MCP_AUTH_TOKEN), auto-generation/printing rules, and 401 behavior (including challenge header) for missing/incorrect tokens.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/msf/core/mcp/server.rb | Pass auth token into HTTP transport and mount BearerAuth middleware when configured. |
| lib/msf/core/mcp/middleware/bearer_auth.rb | New Rack middleware implementing Bearer token enforcement. |
| lib/msf/core/mcp/config/loader.rb | Normalize mcp.auth_token and add MSF_MCP_AUTH_TOKEN override semantics. |
| lib/msf/core/mcp/application.rb | Resolve auth mode at startup, auto-generate token when needed, and print status/messages. |
| lib/msf/core/mcp.rb | Require the new middleware. |
| docs/metasploit-framework.wiki/How-to-use-Metasploit-MCP-Server.md | Document HTTP auth behavior, env var, config options, and disabling. |
| config/mcp_config.yaml.example | Add commented auth_token example line. |
| config/mcp_config.yaml | Add a sample config file including an auth token (currently hard-coded). |
| .gitignore | Ignore local MCP config files. |
622e2e1 to
28ba0e7
Compare
11ddb5d to
d72c3b5
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This adds authentication support to the MCP server's HTTP transport by default. The docs have been updated to reflect what this is and how it works.
Verification
List the steps needed to make sure this thing works
./msfmcpd --mcp-transport httpand see it print a random tokenMSF_MCP_AUTH_TOKEN="" ./msfmcpd --mcp-transport httpand see that it does not print a token and that authentication is disabledMSF_MCP_AUTH_TOKEN="whatever" ./msfmcpd --mcp-transport httpand see that authentication is enabled, it's set to "whatever" but should not be displayed./msfmcpd --config path/to/config.yamlsee that authentication is enabledMSF_MCP_AUTH_TOKEN="" ./msfmcpd --mcp-transport httpagain and see that authentication is disabled because the environment takes priority over the configThe token should only be printed when it's randomly generated; if it's set via the config or environment variable, the server should only print that authentication is enabled. Additionally, when authentication is disabled, that should be printed too so the user always knows the status, and the token is only printed when necessary.
Demo
Start it for a random token.
Configure Claude Code using the
--headeroption: