Skip to main content
@phake/mcp supports five authentication strategies. Set AUTH_STRATEGY in your environment to pick one explicitly, or let the server infer it automatically from whichever variables are present.

Automatic inference

If you do not set AUTH_STRATEGY, the server inspects your environment and picks the best match: Set AUTH_STRATEGY explicitly when you want deterministic behaviour regardless of which other variables happen to be set.

Strategy reference

When to use it

Use oauth when you want end-users to authenticate with a third-party provider (Google, GitHub, etc.) through a full browser-based OAuth 2.1 PKCE flow. The server issues its own RS (Resource Server) tokens and maps them to provider access tokens stored in KV.

Required environment variables

Tool context

When AUTH_STRATEGY=oauth, successful authentication populates the tool context with:
  • context.providerToken — the mapped provider access token (e.g., Google access token)
  • context.resolvedHeaders — pre-built headers with Authorization: Bearer <providerToken>
  • context.provider — provider info object (accessToken, refreshToken, expiresAt)
  • context.authStrategy"oauth"

Example tool

Example environment setup

.dev.vars
For Google OAuth, set OAUTH_EXTRA_AUTH_PARAMS=access_type=offline&prompt=consent to receive a refresh token.

When to use it

Use bearer for server-to-server integrations where a single long-lived token authenticates all requests. There is no user login flow — the token is read from BEARER_TOKEN and injected into every tool call.

Required environment variables

Tool context

  • context.providerToken — the value of BEARER_TOKEN
  • context.resolvedHeaders{ Authorization: "Bearer <BEARER_TOKEN>" }
  • context.authStrategy"bearer"

Example tool

Example environment setup

When to use it

Use api_key when the upstream service authenticates via a custom header (e.g., x-api-key) rather than a standard Authorization header. The key is read from API_KEY and injected under the header named by API_KEY_HEADER.

Required environment variables

Tool context

  • context.providerToken — the value of API_KEY
  • context.resolvedHeaders{ "<API_KEY_HEADER>": "<API_KEY>" }
  • context.authStrategy"api_key"

Example tool

Example environment setup

When to use it

Use custom when the upstream service requires multiple non-standard headers (e.g., workspace IDs, tenant identifiers, or proprietary auth schemes). All headers are defined in a single CUSTOM_HEADERS variable and injected verbatim into every tool call.

Required environment variables

Format: Header-Name:value,Another-Header:value2

Tool context

  • context.providerTokenundefined (no single token concept)
  • context.resolvedHeaders — all parsed custom headers as a plain object
  • context.authStrategy"custom"

Example tool

Example environment setup

When to use it

Use none for public tools that do not call authenticated APIs, or during local development before you configure a real auth strategy. All tools remain accessible without any token.

Required environment variables

None.

Tool context

  • context.providerTokenundefined
  • context.resolvedHeaders{} (empty)
  • context.authStrategy"none"

Example tool

Example environment setup

You can omit AUTH_STRATEGY entirely and leave API_KEY and BEARER_TOKEN unset — the server defaults to none automatically.

Summary table


Using assertProviderToken

When a tool requires a token to function, use the assertProviderToken helper instead of writing a manual if check. It throws an "Authentication required" error if context.providerToken is absent, and narrows the TypeScript type to string for the rest of the handler.
requiresAuth: true and assertProviderToken serve different purposes. requiresAuth causes the dispatcher to reject unauthenticated requests before the handler is called. assertProviderToken is a type guard inside the handler that makes TypeScript aware the token is guaranteed to be a string.
Setting requiresAuth: true is the recommended way to protect a tool. Use assertProviderToken in addition when you need the narrowed TypeScript type, or in tools that call helper functions that expect a string rather than string | undefined.