Skip to content

feat(claude-code): give the GitHub MCP server the gh login's token - #316

Open
gatezh wants to merge 4 commits into
claude-code/bash-shell-and-disk-prunefrom
claude-code/github-mcp-token
Open

gatezh wants to merge 4 commits into
claude-code/bash-shell-and-disk-prunefrom
claude-code/github-mcp-token

Conversation

@gatezh

@gatezh gatezh commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

What

Both claude-code images set GITHUB_PERSONAL_ACCESS_TOKEN from the container's gh login, so the MCP server of the official github@claude-plugins-official plugin works without a personal access token. Resolves part 2 of #118.

Why

The plugin's .mcp.json sends Authorization: Bearer ${GITHUB_PERSONAL_ACCESS_TOKEN} and has no OAuth fallback. With the variable unset it sends a bare Bearer , and every request fails with HTTP 400 (Authorization header is badly formatted).

#118 (2c) says a PAT is unavoidable. That isn't so: api.githubcopilot.com/mcp/ accepts the gho_ OAuth token that gh auth token prints. Probed: HTTP 200 on initialize, and claude mcp list shows Connected. Claude Code 2.1.332 expands GITHUB_PERSONAL_ACCESS_TOKEN in a remote server's headers; it isn't on the list of credential variables Claude Code reads as empty. No PAT is created or stored.

Design

  • Chosen: a snippet baked into the image. /etc/profile.d/github-mcp-token.sh is copied in the shared stage, and ~/.zshrc and ~/.bashrc source it. zsh never reads /etc/profile.d, and terminals are non-login shells, so the image wires both rc files. VS Code's user environment probe (loginInteractiveShell by default) carries the export into the extension host. This covers both targets and every consumer with no template change. It makes no claude calls, so it can't race the extension's sign-in (fix(devcontainer): theme, login, node_modules isolation, self-contained claude-code config #58).

  • Rejected: Claude Code's native headersHelper. A helper that runs gh auth token would keep the token out of every process's environment, and it picks up a new login without a reload. A user-scope server with one was probed and connects. It has no image-level home, though:

    • managedMcpServers refuses entries with headersHelper or ${VAR}.
    • managed-mcp.json takes exclusive control and blocks plugin servers.
    • The plugin's .mcp.json is upstream and has only static headers.

    It would need a per-project claude mcp add-json step outside the plugin system, and installing the official plugin the usual way would still give 400s.

  • Rejected: a per-project postCreateCommand script. The default template has no postCreateCommand on purpose (fix(devcontainer): theme, login, node_modules isolation, self-contained claude-code config #58), and the snippet is the same for every consumer.

  • Rejected: remoteEnv with ${localEnv:GITHUB_PERSONAL_ACCESS_TOKEN} (claude-code: CLAUDE_CODE_OAUTH_TOKEN over-specified as Required; no path for plugins needing a personal token (github MCP) #118's suggestion). It needs a token on the host, which is the PAT this avoids.

The snippet is a single gh auth token --hostname github.com call. gh already prefers GH_TOKEN, then GITHUB_TOKEN, then its stored login. --hostname keeps a GH_HOST that points at GHE from exporting an enterprise token. A non-empty GITHUB_PERSONAL_ACCESS_TOKEN already in the environment wins. An empty one, as ${localEnv:...} gives for an unset host variable, counts as unset. With no token and no login, nothing is exported.

Changes

  • claude-code/.devcontainer/github-mcp-token.sh (new): sourced, committed 100755 per the repo rule, installed 0644.
  • claude-code/.devcontainer/Dockerfile: COPY into /etc/profile.d, and source it from both rc files, in the shared stage after the other baked scripts.
  • claude-code/verify-image.sh: with a throwaway hosts.yml, zsh -ic and bash -ic both export the login's token (checked with printenv, so the variable must be exported). With no login, the snippet exports nothing, not even an empty value.
  • claude-code/README.md: a "GitHub MCP server" section under Git and GitHub Authentication. It covers how to add the plugin and how the token is found. It says the token is in the environment of every process, including the agent's Bash tool and any approved project MCP server, and that the window needs a reload after a login or token change. It also covers the sandbox's api.githubcopilot.com allowlist entry, which the GitHub ranges don't cover. The "Both targets" list mentions the new file.
  • claude-code/.devcontainer/claude-sandbox/allowed-domains.txt: a commented api.githubcopilot.com entry, in the same style as the other plugins' entries.

Notes

  • Decision for the owner: env export over headersHelper, for the reasons above. It puts the gh token in every shell's environment. Anything running as node could already read ~/.config/gh/hosts.yml, so the added exposure is small. In the sandbox the token is the repo-scoped fine-grained one.
  • Checks: the full verify-image.sh default passed inside the published claude-code:latest, with the snippet mounted and the rc lines appended as the Dockerfile does. It fails when the ~/.bashrc line is removed. Probed precedence: GH_TOKEN beats GITHUB_TOKEN beats the login; an empty value falls through; GH_HOST=GHE still exports the github.com token; bash -lic exports it. Hadolint v2.12.0: no findings on master or the branch. shellcheck: clean. I didn't build the image locally; CI builds it and runs verify-image.sh.
  • Cost: gh auth token adds about 50 ms, and only to shells where the variable isn't already set. VS Code terminals inherit it from the probed environment.
  • claude-code: CLAUDE_CODE_OAUTH_TOKEN over-specified as Required; no path for plugins needing a personal token (github MCP) #118 stays open: part 1 (CLAUDE_CODE_OAUTH_TOKEN described as Required) is separate and not touched here.
  • Wiki: Troubleshooting.md ("Claude Code authentication", line 72) says there is no documented path for plugins that need a personal token. After merge, point it at the README's "GitHub MCP server" section.

The official github plugin's MCP server sends
`Authorization: Bearer ${GITHUB_PERSONAL_ACCESS_TOKEN}` with no OAuth fallback,
so an unset variable is a bare "Bearer " and HTTP 400. The image now sets it from
`gh auth token`, whose OAuth token that endpoint accepts, so no personal access
token has to be created. A token already in the environment (including an
injected repo-scoped one in the sandbox) wins; with no login nothing is exported.

Baked into the image as /etc/profile.d/github-mcp-token.sh, sourced from both
~/.zshrc and ~/.bashrc, so VS Code's user env probe carries it into the
extension host. No postCreateCommand step, so nothing can race the extension's
sign-in (#58).

Refs #118 (part 2).
…gin path

gh auth token already prefers GH_TOKEN, then GITHUB_TOKEN, then hosts.yml,
so the snippet's own branches go; --hostname github.com keeps a GH_HOST
pointing at GHE from exporting an enterprise token. verify-image.sh now
checks a gh login on disk through both rc files with printenv (export-
sensitive, no SC2016). The README says where the token ends up and when
to reload, and allowed-domains.txt gets a commented api.githubcopilot.com.
# Conflicts:
#	claude-code/.devcontainer/Dockerfile
@gatezh
gatezh changed the base branch from master to claude-code/bash-shell-and-disk-prune November 27, 2026 20:22
@gatezh
gatezh added this pull request to stack #321 November 27, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant