Documentation
¶
Index ¶
- Variables
- func IsAuthError(err error) bool
- func IsForbidden(err error) bool
- func IsUnauthorized(err error) bool
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, arguments json.RawMessage, meta ...MetaValue) (any, error)
- func (c *Client) ListPrompts(_ context.Context) ([]llm.Prompt, error)
- func (c *Client) ListResources(_ context.Context) ([]llm.Resource, error)
- func (c *Client) ListTools(_ context.Context) ([]llm.Tool, error)
- func (c *Client) Probe(ctx context.Context) (*schema.ConnectorState, error)
- func (c *Client) Run(ctx context.Context) error
- func (c *Client) ServerInfo() (name, version, protocol string)
- type MetaValue
- type OnLoggingMessage
- type OnProgress
- type OnPromptListChanged
- type OnResourceListChanged
- type OnResourceUpdated
- type OnToolListChanged
- type Opt
- func OptOnLoggingMessage(fn OnLoggingMessage) Opt
- func OptOnProgress(fn OnProgress) Opt
- func OptOnPromptListChanged(fn OnPromptListChanged) Opt
- func OptOnResourceListChanged(fn OnResourceListChanged) Opt
- func OptOnResourceUpdated(fn OnResourceUpdated) Opt
- func OptOnToolListChanged(fn OnToolListChanged) Opt
- func WithAuth(fn func(context.Context, string) error) Opt
- func WithClientOpt(goOpts ...client.ClientOpt) Opt
- type UnauthorizedError
Constants ¶
This section is empty.
Variables ¶
var ErrNotConnected = errors.New("not connected")
ErrNotConnected is returned by methods that require an active session when no session has been established yet.
Functions ¶
func IsAuthError ¶
IsAuthError reports whether err is an authentication or authorisation failure (401 or 403) that is unlikely to self-heal on retry.
func IsForbidden ¶
IsForbidden reports whether err represents a 403 Forbidden response from the MCP transport layer. The MCP SDK surfaces these as plain-text errors in the form `sending "<method>": Forbidden` — we match that specific pattern rather than any mention of "Forbidden" to avoid false-positives from tool results.
func IsUnauthorized ¶
IsUnauthorized reports whether err is (or wraps) an UnauthorizedError / 401.
Types ¶
type Client ¶
type Client struct {
*client.Client
sdkmcp.Implementation
// contains filtered or unexported fields
}
Client is an MCP HTTP client that wraps the base HTTP client and provides typed methods for interacting with the MCP server.
func New ¶
New creates a new HTTP client with the given base URL and options. The url parameter should point to the MCP server endpoint, e.g. https://mcp.asana.com/sse
authFn is called when the server returns 401 to perform the OAuth flow. Pass nil to disable auth.
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, name string, arguments json.RawMessage, meta ...MetaValue) (any, error)
CallTool invokes a tool on the connected MCP server by name with the given arguments as JSON. Follows the same return convention as pkg/llm.Toolkit.Run: tool errors (IsError==true in the MCP result) are returned as a Go error; on success the plain value is returned: StructuredContent if present, the single content item's value if there is exactly one, or a []any slice of item values for multiple content items. Returns ErrNotConnected if no session is active. Optional MetaValue values are collected into the protocol _meta object.
func (*Client) ListPrompts ¶
ListPrompts returns the cached list of prompts advertised by the connected MCP server. The cache is populated on connect and refreshed automatically on each PromptListChanged notification. Returns ErrNotConnected if not active.
func (*Client) ListResources ¶
ListResources returns the cached list of resources advertised by the connected MCP server. The cache is populated on connect and refreshed automatically on each ResourceListChanged notification. Returns ErrNotConnected if no session is active.
func (*Client) ListTools ¶
ListTools returns the cached list of tools advertised by the connected MCP server. The cache is populated on connect and refreshed automatically on each ToolListChanged notification. Returns ErrNotConnected if not active.
func (*Client) Probe ¶
Probe makes a single MCP connection, reads the initialisation result (server name, version, instructions and capabilities), closes the session and returns the collected data as a ConnectorState ready to persist. It never leaves a persistent session open.
The provided ctx governs the connection timeout; cancelling it before the server responds causes Probe to return ctx.Err().
func (*Client) Run ¶
Run establishes an MCP session (including OAuth if required) and drives it until ctx is cancelled or the server closes the connection. It blocks until all in-flight messages have been drained and the underlying transport is torn down cleanly.
Run is safe to call concurrently with tool-call methods (CallTool, etc.); those methods return ErrNotConnected until the session is established and continue to work until Run returns.
Server-sent log messages and progress notifications are written to the default slog logger while Run is blocking.
func (*Client) ServerInfo ¶
ServerInfo returns the name, version and negotiated protocol version of the connected MCP server. It returns empty strings if the client is not connected.
type OnLoggingMessage ¶
OnLoggingMessage is called when the server sends a logging message.
type OnProgress ¶
OnProgress is called when the server sends a progress notification.
type OnPromptListChanged ¶
OnPromptListChanged is called when the server sends a prompt-list-changed notification.
type OnResourceListChanged ¶
OnResourceListChanged is called when the server sends a resource-list-changed notification.
type OnResourceUpdated ¶
OnResourceUpdated is called when the server sends a resource-updated notification.
type OnToolListChanged ¶
OnToolListChanged is called when the server sends a tool-list-changed notification.
type Opt ¶
Opt is a functional option for configuring a Client.
func OptOnLoggingMessage ¶
func OptOnLoggingMessage(fn OnLoggingMessage) Opt
OptOnLoggingMessage registers a callback invoked whenever the server sends a logging message notification.
func OptOnProgress ¶
func OptOnProgress(fn OnProgress) Opt
OptOnProgress registers a callback invoked whenever the server sends a progress notification.
func OptOnPromptListChanged ¶
func OptOnPromptListChanged(fn OnPromptListChanged) Opt
OptOnPromptListChanged registers a callback invoked with the refreshed prompt list whenever the server notifies the client that its prompt list has changed.
func OptOnResourceListChanged ¶
func OptOnResourceListChanged(fn OnResourceListChanged) Opt
OptOnResourceListChanged registers a callback invoked with the refreshed resource list whenever the server notifies the client that its resource list has changed.
func OptOnResourceUpdated ¶
func OptOnResourceUpdated(fn OnResourceUpdated) Opt
OptOnResourceUpdated registers a callback invoked with the URI of the resource whenever the server sends a resource-updated notification.
func OptOnToolListChanged ¶
func OptOnToolListChanged(fn OnToolListChanged) Opt
OptOnToolListChanged registers a callback invoked whenever the server notifies the client that its tool list has changed.
func WithAuth ¶
WithAuth registers a function called when the server returns 401 to perform the OAuth discovery and authorization flow. Pass nil to disable auth.
func WithClientOpt ¶
WithClientOpt wraps one or more go-client options so they can be passed alongside mcp Opt values to New().
type UnauthorizedError ¶
type UnauthorizedError struct {
// contains filtered or unexported fields
}
UnauthorizedError is returned by connect when the server replies with 401. It wraps httpresponse.ErrNotAuthorized and carries the parsed fields from the Www-Authenticate response header as a map of key→value pairs.
func AsUnauthorized ¶
func AsUnauthorized(err error) *UnauthorizedError
AsUnauthorized returns the *UnauthorizedError inside err, or nil.
func NewUnauthorizedError ¶
func NewUnauthorizedError(header http.Header) *UnauthorizedError
NewUnauthorizedError parses the Www-Authenticate values from header into an UnauthorizedError.
func (*UnauthorizedError) Error ¶
func (e *UnauthorizedError) Error() string
Error implements the error interface. It returns the error_description if present, falling back to the error code, then a generic message.
func (*UnauthorizedError) Get ¶
func (e *UnauthorizedError) Get(key string) string
Get returns the value of an arbitrary field from the Www-Authenticate header, or an empty string if the field is absent.
func (*UnauthorizedError) Keys ¶
func (e *UnauthorizedError) Keys() []string
Keys returns the field names present in the Www-Authenticate header.
func (*UnauthorizedError) ResourceMetadata ¶
func (e *UnauthorizedError) ResourceMetadata() string
ResourceMetadata returns the resource_metadata URL from the Www-Authenticate header (RFC 9728), or an empty string if absent.
func (*UnauthorizedError) Unwrap ¶
func (e *UnauthorizedError) Unwrap() error
Unwrap returns httpresponse.ErrNotAuthorized so that errors.Is checks work.