Skip to content

fix(claude-code): send the saved Server credential to the plugin MCP server - #2669

Merged
hidb4ai merged 2 commits into
oceanbase:masterfrom
Fengzdadi:fix/claude-code-mcp-saved-credential
Nov 17, 2026
Merged

hidb4ai merged 2 commits into
oceanbase:masterfrom
Fengzdadi:fix/claude-code-mcp-saved-credential

Conversation

@Fengzdadi

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

No tracking issue. It follows #1530 and #1531 and was found while writing a real-host Claude Code E2E.

Rationale for this change

powercontext setup claude-code saves a URL-bound credential under $CLAUDE_CONFIG_DIR/powercontext/credentials.json, and the hooks read it. The plugin's MCP server sent only ${POWERCONTEXT_CLAUDE_AUTHORIZATION:-}. When that variable was missing from the process that starts Claude Code, for example a session that Desktop starts, claude -p 2.1.277 recalled and captured normally. The MCP server, however, reported failed, Claude Code offered no PowerContext tools, and the turn still succeeded.

#1531 replaced a headersHelper with the static header because the helper "ignored" the credential (#1530). That helper read POWERCONTEXT_CLAUDE_AUTHORIZATION from its environment. On Claude Code 2.1.277, the variable never reaches a plugin MCP server's headersHelper when its value looks like a credential. With a token containing digits and _ + / =, the helper saw no variable and emitted {}. A token made only of letters and hyphens did reach it. That stripping explains #1530. The helper in this PR reads the saved file instead of the environment.

What changes are included in this PR?

  • scripts/mcp_headers.py is the plugin MCP server's headersHelper.
    • It emits the credential saved for the endpoint that Claude Code passes in CLAUDE_CODE_MCP_SERVER_URL.
    • It emits nothing for another endpoint, for malformed storage, or when the file's POSIX permissions are unsafe. These are the same checks the hooks apply (claude_code_settings.mcp_authorization).
    • It never reads the environment.
    • Any failure, including an import failure, emits {}.
  • .mcp.json keeps the static ${POWERCONTEXT_CLAUDE_AUTHORIZATION:-} header and adds headersHelper: python3 "${CLAUDE_PLUGIN_ROOT}/scripts/mcp_headers.py".
    • The helper's header overrides the static one, so the saved credential wins for MCP. The environment value still applies when nothing is saved.
    • The plugin root is quoted because Claude Code substitutes it raw into a shell command. An unquoted root under a path with a space failed the helper on the real host.
  • The plugin version goes from 0.1.3 to 0.1.4 in the manifest, the Marketplace entry and the User-Agent strings.
  • The docs (en and zh, the integration guide and troubleshooting) and both READMEs now describe:
    • the saved credential;
    • that the Hook prefers an explicit environment value, while MCP uses it only when nothing is saved;
    • token rotation by rerunning setup.
  • The setup commands in those sections no longer pass --server-url "$POWERCONTEXT_CLAUDE_SERVER_URL", a variable the section never sets.
  • Tests:
    • The helper sends the saved credential only to its own endpoint and ignores the environment.
    • The shipped command runs from a plugin root containing a space.
    • The helper runs on Python 3.9.

Are there any user-facing changes?

Yes.

  • Sessions that do not inherit POWERCONTEXT_CLAUDE_AUTHORIZATION now get the PowerContext MCP tools when setup saved a credential.
  • Behavior change: a token changed only in the environment no longer reaches MCP while an older credential is saved for that Server. Rerun setup to rotate the token, as the docs now say. Before this change, MCP used the environment value and ignored the saved one.

Known limitation, not changed here: neither the hooks nor the helper use a saved credential for a non-loopback HTTP Server, even with insecure-HTTP consent, because the URL comparison rejects plain HTTP to a non-loopback host. The docs say to export the value in that case.

How was this change tested?

  • Real Claude Code 2.1.277 (claude -p, stream-json) against an in-process bearer-auth Server and a Messages API fake. This used the E2E from the unpublished branch feat/claude-code-host-e2e, run in a scratch checkout with this change applied and the plugin under a path containing a space. A realistic token (digits, _ + / =) was used.
    • Saved credential, no environment variable: MCP connects, and the approved remember_memory and search_memory calls succeed.
    • Same, with headersHelper removed from .mcp.json: the tools case fails.
    • Same, with the plugin root unquoted: the tools case fails.
    • No saved credential, environment variable only: MCP connects through the static header.
    • Instrumenting the helper showed it receives no POWERCONTEXT_CLAUDE_AUTHORIZATION for the realistic token, while it does for a token of letters and hyphens.
  • Older Claude Code releases, including the 2.1.89 and 2.1.258 named in bug(claude-code): authenticated MCP ignores headersHelper and returns 401 #1530, were not run. On a release without headersHelper support, the static environment header still applies as before.
  • uv run pytest tests/claude_code_plugin tests/e2e/test_claude_code_service_chain.py tests/test_docs_remote_access_contract.py --require-claude-code-python-floor (Python 3.9.6 floor); related CLI and wizard tests; make check.
  • A self-review (code-review, high) and an independent review. Fixed from them:
    • the unquoted plugin root;
    • the environment precedence the docs claimed for MCP, which the real host does not honor;
    • the unset --server-url variable in the docs;
    • the import outside the helper's failure guard.

AI usage statement

Claude Code (Claude Opus 5.5) wrote the change, the tests and this description, and ran the verification above.

…server

Setup saves a URL-bound credential that the hooks read, but the plugin's
MCP server sent only `${POWERCONTEXT_CLAUDE_AUTHORIZATION:-}`. Without that
variable in the process that starts Claude Code, MCP failed to connect and
offered no tools while the hooks kept working.

The MCP server now runs a `headersHelper` that emits the credential saved
for the endpoint Claude Code connects to, and nothing for another endpoint.
The static environment header stays for when nothing is saved. Claude Code
removes credential-like variables before it runs a plugin's helper, so the
helper does not read the environment and the saved credential wins for MCP;
the docs say to rerun setup to rotate the token.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed a6000df. I did not establish a correctness defect introduced by this patch. Reusing the URL-bound credential reader is reasonable, and the documented MCP-versus-Hook precedence makes the token-rotation behavior explicit.

Merge readiness remains blocked:

  • Merging current master e725225 conflicts in integrations/claude-code/plugins/powercontext/hooks/session_start.py. Please resolve and revalidate the combined behavior.
  • All three native-service CI jobs fail the migration retry assertion expecting v1_5_0_004 while receiving v1_5_0_005. These failures are outside the changed plugin code; this review does not attribute them to the patch.

Validation limit: the related local suite finished with 156 passes and four failures (one oversized-response outcome mismatch and three Server startup failures), so it is not a clean acceptance run. The new helper tests and Python 3.9 checks passed, as did additional endpoint/permission/malformed-storage probes. I did not independently replay the author's real-Claude-host experiment.

No database tables or HTTP/MCP operations are added. There is a new host headersHelper contract and an exported mcp_authorization function, so this does not qualify for the requested no-new-interface automatic approval.

…cp-saved-credential

# Conflicts:
#	integrations/claude-code/plugins/powercontext/hooks/session_start.py
@Fengzdadi

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. I merged master (fd16583) into the branch as deef2b5.

Conflict resolution

  • hooks/session_start.py: took master's version, since refactor(integrations): run the Claude Code and Codex SessionStart flow from the lifecycle package #2550 moved the SessionStart flow into powercontext_lifecycle. The only change this PR made there was the User-Agent version.
  • That version string now lives in claude_code_host.py (CLAUDE_CODE.user_agent), and I bumped it to powercontext-claude-code-plugin/0.1.4.
  • No 0.1.3 remains in the Claude Code plugin, the Marketplace entry or its tests. The manifest, the Marketplace entry, the host profile, user_prompt_submit.py and workspace_scope.py all say 0.1.4.
  • Relative to master, the PR diff is unchanged apart from that one line in claude_code_host.py.

Validation on deef2b5

  • uv run pytest tests/claude_code_plugin tests/e2e/test_claude_code_service_chain.py tests/test_docs_remote_access_contract.py --require-claude-code-python-floor (Python 3.9.6 floor) on macOS arm64: 161 passed with -n 4, serially, and twice with -n 10 at load averages around 12. The count is 161 because master added a test since your run.
  • make check passed.
  • I did not reproduce the four failures from your run: the oversized-response outcome mismatch and the three Server startup failures. This PR changes neither the hook transport nor the Server startup path. The oversized-error-body cases read their body byte by byte, and the test notes that this can take seconds on a busy runner, so I suspect timing there. I have no explanation for the Server startup failures from this side.
  • Real Claude Code 2.1.277 (claude -p, stream-json) against an in-process bearer-auth Server and a Messages API fake. The plugin was installed by powercontext setup claude-code from a checkout of deef2b5 under a path containing a space, using a token with digits and _ + / =. After setup, POWERCONTEXT_CLAUDE_AUTHORIZATION was removed from Claude Code's environment, so only the saved credential was available.
    • SessionStart through the lifecycle package (startup, resume, the bootstrap receipt) and prompt recall and capture all passed.
    • The plugin MCP server connected, and the approved remember_memory and search_memory calls landed in the workspace Scope.
    • With headersHelper removed from .mcp.json, the MCP case failed.
    • This run used the E2E test I have not yet opened as a PR, applied in a scratch checkout. It is not part of this PR.
  • The three native-service failures you noted (expected v1_5_0_004, got v1_5_0_005) came from master before fix(plugins): host Pipes, Scope API, user-change evidence and checks, page access to Artifacts, and storage on OceanBase and seekdb #2660 fixed that literal; this merge includes the fix.

@hireview001 hireview001 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PsiACE PsiACE left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed deef2b5. The SessionStart conflict resolution preserves master's lifecycle delegation, with the version bump correctly moved to CLAUDE_CODE.user_agent. The previous merge conflict and all three native-service CI failures are cleared. I found no new patch-specific correctness defect.

The remaining Linux Desktop check fails while downloading OpenAPI Generator from Maven Central (HTTP 429), before application validation. Please rerun that check once the rate limit clears; the green aggregate CI result does not make this individual check successful.

Independent validation is still limited: my local run reports one Python 3.9 trickle-body case failing at scope_resolve instead of context_prepare, plus four service-chain cases failing to start the Server. The new helper tests pass. These failures do not establish a regression in this patch, and I have not independently replayed the real-Claude-host experiment.

No database tables or HTTP/MCP operations are added. The new host headersHelper contract and exported mcp_authorization remain, so the requested no-new-interface automatic-approval condition is not met.

@hidb4ai
hidb4ai merged commit e4f7c84 into oceanbase:master Nov 17, 2026
86 of 87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.6.0 v1.6.0 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants