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 ¶
- Variables
- func DeleteToken(target string) error
- 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 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 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 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 ¶
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 ¶
ProviderFor looks up a registered provider by name. An unknown name returns an error listing the registered provider names.