auth

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package auth is the client side of MCP authorization over HTTP: it finds the server's authorization server, runs the authorization code flow with PKCE on a loopback redirect, and sends and refreshes the token on every request. The client is either registered with the authorization server beforehand or identified by its Client ID Metadata Document.

Index

Constants

This section is empty.

Variables

View Source
var ErrClientRequired = errors.New("auth: the authorization server needs a client registered with it")

ErrClientRequired is the authorization server taking only clients registered with it beforehand, and Config naming none.

Functions

This section is empty.

Types

type Authorizer

type Authorizer struct {
	// contains filtered or unexported fields
}

Authorizer holds the authorization of one MCP server. It is safe for concurrent use.

func New

func New(cfg Config) *Authorizer

func (*Authorizer) Login

func (a *Authorizer) Login(ctx context.Context) (*Login, error)

Login starts an authorization: it finds the authorization server and listens for the redirect on a loopback port. Wait completes it.

func (*Authorizer) Logout

func (a *Authorizer) Logout() error

Logout forgets the token.

func (*Authorizer) Transport

func (a *Authorizer) Transport(base http.RoundTripper) http.RoundTripper

Transport returns a RoundTripper that sends the stored token, refreshing it when it expires or the server rejects it. When no token works, the server's 401 or 403 is returned as is; Login then asks the user.

type Challenge

type Challenge struct {
	ResourceMetadata string
	Scope            string
	Error            string
}

Challenge is the Bearer challenge of a 401 or 403 response.

func ParseChallenge

func ParseChallenge(h http.Header) (Challenge, bool)

ParseChallenge returns the Bearer challenge among h's WWW-Authenticate headers.

type Config

type Config struct {
	Server string // the MCP endpoint URL
	Store  Store
	// ClientID, with ClientSecret if it has one, is a client registered
	// beforehand with the server's authorization server, its redirect URI
	// http://127.0.0.1/callback on any port (RFC 8252 Section 7.3).
	ClientID     string
	ClientSecret string
	// ClientMetadataURL is the https URL of the client's Client ID Metadata
	// Document, its client_id with authorization servers that accept one.
	// The document lists the loopback redirect URIs to listen on.
	ClientMetadataURL string
	// HTTPClient makes the metadata and token requests; nil means a client
	// with a 30-second timeout.
	HTTPClient *http.Client
}

type Login

type Login struct {
	// URL is the authorization page the user must open in a browser.
	URL string
	// contains filtered or unexported fields
}

Login is an authorization waiting for the user.

func (*Login) Wait

func (l *Login) Wait(ctx context.Context) error

Wait waits for the browser to come back, redeems the code and stores the token.

type Store

type Store interface {
	Token(server string) (*Token, error)     // nil when absent
	SaveToken(server string, t *Token) error // nil deletes
}

Store persists tokens by MCP server URL. They are secrets.

type Token

type Token struct {
	Issuer       string    `json:"issuer"`
	ClientID     string    `json:"client_id"` // the client it was issued to
	Resource     string    `json:"resource"`  // the RFC 8707 resource it was issued for
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	Expiry       time.Time `json:"expiry,omitzero"`
	Scope        string    `json:"scope,omitempty"`
}

Token is an access token for one MCP server.

Jump to

Keyboard shortcuts

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