mcpproxy-go icon indicating copy to clipboard operation
mcpproxy-go copied to clipboard

[Bug]: OAuth reconnects send an unauthenticated POST to the MCP endpoint, which servers count as a failed login

Open felipecrs opened this issue 1 week ago • 0 comments

What happened?

Every time mcpproxy builds the OAuth config for an HTTP upstream, it sends POST <server url> with body {} and no Authorization header, to get a 401 and read the resource_metadata URL from WWW-Authenticate. The config is built on every connect and reconnect, even when a stored access token and refresh token already exist.

Two places send this probe:

  • autoDetectResource (internal/oauth/config.go), called from CreateOAuthConfigWithEnv when oauth.extra_params.resource isn't set. Its result isn't cached, so it runs on every build.
  • discoverAuthServerURL (internal/oauth/discovery.go), called from preflightMetadataURL. Its result is cached for an hour, so it runs on the first build after mcpproxy starts and again once the cache expires.

Servers that track failed logins count each probe as one. Home Assistant's MCP server (/api/mcp) answers with a 401, and its ban middleware logs a warning and shows a "Login attempt failed" notification with the proxy's IP. It also counts each one toward login_attempts_threshold (off by default) and bans the IP once that's reached. A request with a valid bearer token doesn't reset the count; only a frontend or login-flow sign-in from the same IP does.

Counted against a Home Assistant-like test server, these are the unauthenticated POSTs per config build:

First build after a start Next builds After the 1 h discovery cache expires
No extra_params.resource 2 1 2
extra_params.resource pinned 1 0 1

A real Home Assistant log shows the same pattern: two failed logins about 20 ms apart right after mcpproxy starts, then one on each reconnect.

Signing in sends the same POST once more, from ValidateOAuthMetadata, so it also raises a "Login attempt failed" notification.

Upstreams without an oauth block also try the no-auth strategy first on every connect, which sends an unauthenticated initialize. Reading the well-known metadata doesn't avoid that one.

preflightPRMScopes also sends HEAD <server url> (cached). Home Assistant answers HEAD with 405, which it doesn't count, but a server that answers HEAD with 401 would.

Expected behavior

mcpproxy reads the Protected Resource Metadata without sending an unauthenticated request to the MCP endpoint when it can. RFC 9728 §3.1 defines a well-known URL for it, https://ha.example.com/.well-known/oauth-protected-resource/api/mcp, and the MCP authorization spec requires clients to support that URL. Home Assistant serves it without auth, and its 401 points to the same URL. mcpproxy could read that URL first and fall back to the 401 probe only when the URL is missing or names a different resource (RFC 9728 §3.3).

Steps to reproduce

  1. Add a Home Assistant MCP upstream with OAuth, e.g. https://ha.example.com/api/mcp, and log in.
  2. Restart mcpproxy, or let it reconnect.
  3. About a second after mcpproxy logs Auto-detected resource parameter from Protected Resource Metadata (RFC 9728), Home Assistant shows a "Login attempt failed" notification and logs the warning below.

Pinning oauth.extra_params.resource to the server URL removes the per-reconnect probe but not the one after each start or cache expiry. Setting oauth.auth_server_metadata_url as well removes that one too. Either edit clears the stored token, because config.OAuthConfigChanged treats any change to these fields as a new OAuth config. This happens even when the new resource is the value auto-detection was already sending.

MCPProxy version

v0.76.0-rc.1, and current main.

Operating system

Windows

Relevant logs

WARNING (MainThread) [homeassistant.components.http.ban] Login attempt or request with invalid authentication from <proxy host> (<proxy ip>). Requested URL: '/api/mcp'. (Go-http-client/2.0)

Pre-submission checklist

  • [x] I searched existing issues and did not find a duplicate
  • [ ] I ran mcpproxy doctor and reviewed the output

felipecrs avatar Nov 28 '26 01:11 felipecrs