Skip to content

feat: add opt-in token passthrough for running behind an OAuth gateway - #295

Open
CVThien2k2 wants to merge 1 commit into
nulab:mainfrom
CVThien2k2:feat/hub-bearer-passthrough
Open

CVThien2k2 wants to merge 1 commit into
nulab:mainfrom
CVThien2k2:feat/hub-bearer-passthrough

Conversation

@CVThien2k2

Copy link
Copy Markdown

Summary

Adds an opt-in token passthrough mode for the HTTP transport, for deployments where a gateway in front of the server already holds each user's Backlog OAuth grant (for example an internal MCP hub serving users from several Backlog spaces).

When BACKLOG_TOKEN_PASSTHROUGH=true (or --backlog-token-passthrough) is set:

  • The Backlog access token is taken from each request's Authorization: Bearer header.
  • The space is taken from X-Backlog-Host, falling back to BACKLOG_DOMAIN when the header is absent.
  • X-Backlog-Host is limited to <space>.backlog.com, <space>.backlog.jp, <space>.backlogtool.com, or exactly BACKLOG_DOMAIN, so the token is never sent to another host. Anything else is answered 400 invalid_host.
  • A Backlog rejection of the token (reported through the existing reportBacklogAuthError) becomes a 401 with WWW-Authenticate, so the gateway can refresh and retry. Responses are JSON in this mode, and the 2025-era SSE fallback is buffered (stateless streams end with the result), so the status can still change after the tool runs.

Compatibility

Purely additive; the mode is off by default.

  • stdio, API key, multi-organization and the built-in OAuth server are unchanged.
  • Combining passthrough with BACKLOG_OAUTH_CLIENT_ID, or with the stdio transport, is rejected at startup.
  • It reuses the existing per-request backlogAuthContext and adds a createTokenPassthroughBacklogClientRegistry next to the OAuth one.

Changes

  • src/auth/backlogHost.ts: host validation and the per-request space context.
  • src/auth/tokenPassthroughMiddleware.ts: Bearer + X-Backlog-Host middleware.
  • src/utils/backlogClientRegistry.ts: createTokenPassthroughBacklogClientRegistry.
  • src/httpMcpServer.ts, src/index.ts: the tokenPassthrough option and CLI flag / env var.
  • README (en/ja) and .env.example: a "Token Passthrough" section.

Testing

  • pnpm run lint, pnpm run format, pnpm test (821 passed) and pnpm run build.
  • New tests cover host validation, per-request token and space isolation under concurrency, the default-host fallback, forced JSON responses, and a Backlog 401 becoming a transport 401 for both 2026-07-28 and 2025-era clients.
  • Manual run: no token → 401, foreign host → 400, valid token and host → tools/list returns all tools; stdio without an API key still fails as before.

🤖 Generated with Claude Code

With BACKLOG_TOKEN_PASSTHROUGH=true (or --backlog-token-passthrough) the HTTP
transport takes the Backlog access token from each request's
`Authorization: Bearer` header and the space from `X-Backlog-Host`, falling
back to BACKLOG_DOMAIN. This lets a gateway that already holds users' Backlog
OAuth grants serve users from several spaces through one server.

- X-Backlog-Host is limited to <space>.backlog.com / .backlog.jp /
  .backlogtool.com or exactly BACKLOG_DOMAIN, so the token never reaches
  another host; anything else is answered 400 invalid_host.
- A Backlog rejection of the token becomes a 401 so the gateway can refresh
  and retry. Responses are JSON in this mode, and the 2025-era SSE fallback is
  buffered, so the status can still change after the tool runs.

The mode is off by default. stdio, API key, multi-organization and the
built-in OAuth server are unchanged; combining passthrough with
BACKLOG_OAUTH_CLIENT_ID or the stdio transport is rejected at startup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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