auth

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth provides the mcp auth provider registry and the built-in providers: none, bearer, client-credentials, and oauth.

A provider turns flag/environment input into an SDK OAuthHandler. The streamable HTTP transport calls the handler's TokenSource to set the Authorization header (a nil source means no header), and on a 401/403 it calls Authorize and retries the request once when Authorize succeeds.

Index

Constants

This section is empty.

Variables

View Source
var TokenDir = defaultTokenDir()

TokenDir is the directory holding the token cache file "tokens.json". It defaults to <os.UserConfigDir()>/.mcpcli (falling back to os.TempDir()/.mcpcli when the user config directory is unavailable) and may be reassigned; tests point it at a temporary directory.

Functions

func DeleteToken

func DeleteToken(target string) error

DeleteToken removes the cached token for target. A missing cache file or key is not an error.

func LoadToken

func LoadToken(target string) (*oauth2.Token, error)

LoadToken returns the cached token for target. A missing cache file or a target that was never saved yields (nil, nil), not an error.

func Names

func Names() []string

Names returns all registered provider names, sorted ascending, for help text and validation messages.

func ReadCacheFile added in v1.4.0

func ReadCacheFile(name string, v any) (bool, error)

ReadCacheFile reads the JSON cache file name within TokenDir into v. It is an unlocked primitive: callers doing a read-modify-write cycle hold their own lock (tokenMu for the token trio). A missing file yields (false, nil); a corrupt one is an error wrapping "parsing <path>" — the corrupt-vs-missing discipline every cache in TokenDir inherits.

func Register

func Register(p Provider)

Register adds p to the registry. It panics on a nil provider or a duplicate name: both are programming errors visible at startup, not runtime states.

func RegisterBuiltins

func RegisterBuiltins()

RegisterBuiltins registers the built-in providers: none, bearer, client-credentials and oauth. It is invoked from the cli package's init (root.go) so the registry is populated before any flag validation runs; it is safe to call repeatedly.

func SaveToken

func SaveToken(target string, tok *oauth2.Token) error

SaveToken stores tok under the cache key for target.

func WriteCacheFile added in v1.4.0

func WriteCacheFile(name string, v any) error

WriteCacheFile atomically writes v as JSON to the cache file name within TokenDir: MarshalIndent 2-space plus a trailing newline, a temp file named after the cache file's stem (cacheTempPattern), then a rename over the target. It is an unlocked primitive, paired with ReadCacheFile.

Types

type Credentials

type Credentials struct {
	ClientID     string
	ClientSecret string
}

Credentials holds OAuth client credentials resolved from flags and environment. For the bearer provider ClientSecret carries the static bearer token (mcp reuses --client-secret for it).

func ResolveCredentials

func ResolveCredentials(idFlag, secretFlag string) Credentials

ResolveCredentials resolves client credentials from flag values with environment fallback: a non-empty flag wins over the corresponding MCPCLI_CLIENT_ID / MCPCLI_CLIENT_SECRET variable. Fields resolve independently, and missing values are left empty so that each provider can report a specific, actionable error instead of a generic one.

type Params

type Params struct {
	Target string
	Creds  Credentials
	Stderr io.Writer
	Stdin  io.Reader
	Flow   string
}

Params carries everything a provider may need to build its handler.

Target is the server URL for streamable HTTP (or the stdio command string); it doubles as the token-cache key. Creds holds client credentials resolved from flags and environment. Stderr/Stdin are the command's streams for providers that interact with the user (the OAuth provider added later uses them). Flow is the OAuth flow flavor: "loopback" or "manual".

type Provider

type Provider interface {
	// Name returns the provider's --auth value.
	Name() string

	// Build returns the handler for the transport. A (nil, nil, nil) result
	// means the provider needs no auth handler, so the transport sends no
	// Authorization header. cleanup, when non-nil, releases provider
	// resources when the command finishes; providers with nothing to clean
	// up return nil.
	Build(ctx context.Context, p Params) (handler sdkauth.OAuthHandler, cleanup func(), err error)
}

Provider supplies auth handlers for one --auth mode.

func ProviderFor

func ProviderFor(name string) (Provider, error)

ProviderFor looks up a registered provider by name. An unknown name returns an error listing the registered provider names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL