Documentation
¶
Overview ¶
Package auth provides the mcp auth provider registry and the built-in providers: none, bearer, client-credentials, oauth, and basic.
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 ¶
- Variables
- func DeleteToken(target string) error
- func IsLoopbackHost(addr string) bool
- func LoadToken(target string) (*oauth2.Token, error)
- func Names() []string
- func ReadCacheFile(name string, v any) (bool, error)
- func Register(p Provider)
- func RegisterBuiltins()
- func SaveToken(target string, tok *oauth2.Token) error
- func WriteCacheFile(name string, v any) error
- type Credentials
- type Params
- type Provider
Constants ¶
This section is empty.
Variables ¶
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 ¶
DeleteToken removes the cached token for target. A missing cache file or key is not an error.
func IsLoopbackHost ¶ added in v1.6.1
IsLoopbackHost reports whether a URL host (a bare host or a host:port) is the literal "localhost" or a loopback IP (127.0.0.0/8, ::1) — a literal string/IP test only, no name resolution. It is the shared predicate for transport-policy gates that allow plain http on loopback hosts only (the CLI's plain-http credential gate, the tus and bws wire boundaries), which is why it is exported: those packages have no other way to reach it, and duplicating the parse would fork the allowance. It also serves this package's SSRF check on the PRM fetch URL.
func LoadToken ¶
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
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, oauth, and basic. 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 WriteCacheFile ¶ added in v1.4.0
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 ¶
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
// Resource carries the --resource flag: the explicit RFC 8707 resource
// indicator that replaces the server-advertised one in the
// client-credentials grant. Only the client-credentials provider reads
// it; the root gate keeps it out of every other mode. Flag-only by
// design — resource is not a secret, and the house env vars are
// credentials and selectors.
Resource 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". Resource is the RFC 8707 resource override for client-credentials auth — flag-only, no env.
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 ¶
ProviderFor looks up a registered provider by name. An unknown name returns an error listing the registered provider names.