fix(mcp): validate bearer during initialize#104
Conversation
jiashuoz
left a comment
There was a problem hiding this comment.
Thanks for the fix — code looks good. Two follow-ups before merge:
-
I've approved CI on the branch; let's confirm it goes green.
-
Could you tighten the
WWW-Authenticateassertion in the new test? The fix adds, error="invalid_token"on the invalid-bearer path (per RFC 6750 §3.1), but the test only matches/Bearer realm="e2a"/— same regex as the missing-bearer test. MCP clients may key off theerrorparameter for the re-auth prompt, so it's worth asserting it explicitly:
expect(res.headers.get("www-authenticate")).toMatch(
/Bearer realm="e2a".*error="invalid_token"/,
);Also, a small note for the PR description: this change makes listAgents() run on every initialize even when E2A_AGENT_EMAIL is preset (previously zero round-trip in that path). That's intentional and required for the fix — just worth calling out so operators aren't surprised by the added per-session call.
Summary
Hi, I am an AI agent helping with the MCP auth/session issue described in #102.
This PR validates the bearer during MCP
initializebefore session allocation, so a bogus non-empty bearer no longer receives a successful initialize response or anMcp-Session-Id.Changes:
WWW-Authenticatebearer challenge helper across MCP auth failures401+WWW-Authenticatefor invalid bearers duringinitializelistAgents()failures best-effort so transient backend hiccups do not block initializeMcp-Session-IdlistAgents()now validates the bearer while still avoiding client reconstruction whenagentEmailis already setAddresses #102.
Verification
The local environment routes HTTP through a proxy by default, so MCP loopback tests need
NO_PROXY=127.0.0.1,localhost.npm_config_cache=/tmp/npm-cache-e2a-102 npm --workspace sdks/typescript run buildNO_PROXY=127.0.0.1,localhost npm_config_cache=/tmp/npm-cache-e2a-102 npm --workspace mcp test -- tests/http.test.tsNO_PROXY=127.0.0.1,localhost npm_config_cache=/tmp/npm-cache-e2a-102 npm --workspace mcp testnpm_config_cache=/tmp/npm-cache-e2a-102 npm --workspace mcp run buildgit diff --checkNote: the first plain
npm installattempt failed because the local user npm cache had root-owned files. I reran withnpm_config_cache=/tmp/npm-cache-e2a-102; no lockfile changes are included.