fix(mcp): scope HF bearer token to HuggingFace endpoints only#323
Open
lockezhou18 wants to merge 1 commit into
Open
fix(mcp): scope HF bearer token to HuggingFace endpoints only#323lockezhou18 wants to merge 1 commit into
lockezhou18 wants to merge 1 commit into
Conversation
ToolRouter injected `Authorization: Bearer <hf_token>` into the headers of *every* configured MCP server. This leaks the user's HF token to unrelated third-party MCP servers, and breaks auth-enforcing servers: a local data-gateway that grants a default identity to unauthenticated localhost callers instead receives an unrecognized `hf_...` token and rejects every tool call with `invalid_token: bearer token not recognized`. Only attach the HF bearer when the server URL is a HuggingFace endpoint, matched via the parsed hostname (huggingface.co or a subdomain) so lookalike hosts like `huggingface.co.evil.com` don't match. Stdio servers (no URL) and all non-HF remotes now receive no Authorization header. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
ToolRouter.__init__(agent/core/tools.py) injectsAuthorization: Bearer <hf_token>into the headers of every configured MCP server:Two issues:
hf_...bearer and rejects every tool call. In my setup a local gateway returnedinvalid_token: bearer token not recognizedon all calls; removing the stray header let it fall back to its default localhost identity and work.Fix
Only attach the HF bearer when the server URL is a HuggingFace endpoint, matched on the parsed hostname (
huggingface.coor a subdomain such asrouter.huggingface.co) rather than a substring — so lookalike hosts likehuggingface.co.evil.comdon't match. Stdio servers (no URL) and all non-HF remotes now receive noAuthorizationheader.The default
hf-mcp-server(https://huggingface.co/mcp?login) still receives the token unchanged.Verification
_is_hf_endpointbehavior:https://huggingface.co/mcp?loginhttps://router.huggingface.co/v1http://127.0.0.1:8001/mcphttps://huggingface.co.evil.com/mcpConfirmed end-to-end: with the default
hf-mcp-serverplus a local data-gateway MCP, tool calls to both succeed after the change (before, the data-gateway rejected every call withinvalid_token).🤖 Generated with Claude Code