client

package
v0.4.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func IsAuthError(err error) bool

IsAuthError reports whether err is an authentication or authorisation failure (401 or 403) that is unlikely to self-heal on retry.

func IsForbidden

func IsForbidden(err error) bool

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

func IsUnauthorized(err error) bool

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

func New(url, name, version string, opts ...Opt) (*Client, error)

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

func (c *Client) ListPrompts(_ context.Context) ([]llm.Prompt, error)

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

func (c *Client) ListResources(_ context.Context) ([]llm.Resource, error)

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

func (c *Client) ListTools(_ context.Context) ([]llm.Tool, error)

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

func (c *Client) Probe(ctx context.Context) (*schema.ConnectorState, error)

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

func (c *Client) Run(ctx context.Context) error

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

func (c *Client) ServerInfo() (name, version, protocol string)

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 MetaValue

type MetaValue struct {
	Key   string
	Value any
}

MetaValue is a single key/value pair for the MCP _meta field.

func Meta

func Meta(key string, value any) MetaValue

Meta returns a MetaValue that can be passed to CallTool to populate the protocol-level _meta object sent with the request.

type OnLoggingMessage

type OnLoggingMessage func(ctx context.Context, level, logger string, data any)

OnLoggingMessage is called when the server sends a logging message.

type OnProgress

type OnProgress func(ctx context.Context, token any, progress, total float64, message string)

OnProgress is called when the server sends a progress notification.

type OnPromptListChanged

type OnPromptListChanged func(ctx context.Context)

OnPromptListChanged is called when the server sends a prompt-list-changed notification.

type OnResourceListChanged

type OnResourceListChanged func(ctx context.Context)

OnResourceListChanged is called when the server sends a resource-list-changed notification.

type OnResourceUpdated

type OnResourceUpdated func(ctx context.Context, resource llm.Resource)

OnResourceUpdated is called when the server sends a resource-updated notification.

type OnToolListChanged

type OnToolListChanged func(ctx context.Context)

OnToolListChanged is called when the server sends a tool-list-changed notification.

type Opt

type Opt func(*Client) error

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

func WithAuth(fn func(context.Context, string) error) Opt

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

func WithClientOpt(goOpts ...client.ClientOpt) Opt

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.

Jump to

Keyboard shortcuts

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