Documentation
¶
Overview ¶
Package client is a client of a remote MCP server, over HTTP, with go-client as its transport, so its options, such as a user agent, headers, a token, and tracing, apply to the requests of the MCP SDK.
A server is reached at its URL, with the streamable HTTP transport of the 2025-03-26 protocol, or the SSE transport of the 2024-11-05 protocol, as the server has. A server which runs as a command, over stdio, isn't one.
Index ¶
- type ChangeFunc
- type Client
- func (c *Client) Authorized() bool
- func (c *Client) CallTool(ctx context.Context, name string, arguments json.RawMessage) (*sdkmcp.CallToolResult, error)
- func (c *Client) Challenge() *authclient.AuthError
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Connected() bool
- func (c *Client) Disconnect() error
- func (c *Client) Endpoint() string
- func (c *Client) Experimental(name string) map[string]any
- func (c *Client) GetPrompt(ctx context.Context, name string, arguments map[string]string) (*sdkmcp.GetPromptResult, error)
- func (c *Client) HasExperimental(name string) bool
- func (c *Client) HasResources() bool
- func (c *Client) Instructions() string
- func (c *Client) ListPrompts(ctx context.Context) ([]*sdkmcp.Prompt, error)
- func (c *Client) ListResourceTemplates(ctx context.Context) ([]*sdkmcp.ResourceTemplate, error)
- func (c *Client) ListResources(ctx context.Context, params *sdkmcp.ListResourcesParams) (*sdkmcp.ListResourcesResult, error)
- func (c *Client) ListTools(ctx context.Context) ([]*sdkmcp.Tool, error)
- func (c *Client) ReadResource(ctx context.Context, uri string) (*sdkmcp.ReadResourceResult, error)
- func (c *Client) ReadResourceParams(ctx context.Context, params *sdkmcp.ReadResourceParams) (*sdkmcp.ReadResourceResult, error)
- func (c *Client) Status() (*sdkmcp.Implementation, error)
- func (c *Client) Subscribe(ctx context.Context, uri string) error
- func (c *Client) Unsubscribe(ctx context.Context, uri string) error
- func (c *Client) WatchResources(ctx context.Context, ch chan<- string) error
- type Opt
- type TransportFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangeFunc ¶ added in v1.6.0
type ChangeFunc func(server *sdkmcp.Implementation, err error)
ChangeFunc is called, without a lock, when the client is connected, with the server, as it said, or disconnected, with nil, and why, when it wasn't disconnected, such as when the server closed the session
type Client ¶
type Client struct {
*authclient.Client
// contains filtered or unexported fields
}
Client is a client of a remote MCP server, at its URL, whose requests are made with the auth package's client, of go-client, so a server which needs to be authorized says how, which Discover, and DiscoverWithError, discover
func New ¶
New returns a client of the MCP server at the URL, such as https://mcp.example.com/mcp, or one which ends /sse, of a server with the SSE transport, with the options, such as its name, and version, which the server is told, with WithName, and go-client's, with WithClientOpts. It isn't connected, until Connect is called. With WithTransport, it's connected with the transport, and the URL is any which names the server, such as memory://weatherapi. Returns ErrBadParameter when the URL isn't an http, or https, URL with a host, or, with a transport, has no scheme.
func (*Client) Authorized ¶ added in v1.6.0
Authorized returns true when the client's requests are authorized, with a token
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, name string, arguments json.RawMessage) (*sdkmcp.CallToolResult, error)
CallTool calls the server's tool with the name, with its arguments, as JSON, which can be nil, and returns its result, which can be an error the tool returned, rather than one of the call. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) Challenge ¶ added in v1.6.0
func (c *Client) Challenge() *authclient.AuthError
Challenge returns the challenge of the server, as its last request wasn't authorized, such as as it was connected, which says how it's authorized, or nil
func (*Client) Connect ¶
Connect connects to the server, with the streamable HTTP transport, or with SSE, when the server hasn't it, and returns once the session is initialized, or at once, when it's connected. The context is the connection's, as it's made, rather than the session's, which has its own, so it lasts until it's disconnected, or the server closes it, as SSE's stream lasts as long as the context it's connected with. It's connected without the client's lock held, so its status can be asked for as it is, and once at a time: a Connect as it's being connected waits for it, and returns as it does, or as its own context is cancelled. Returns ErrServiceUnavailable when it can't be connected, with each transport's error, or the context's error, when it's cancelled, or disconnected, as it's connected.
func (*Client) Connected ¶ added in v1.6.0
Connected returns true when the client has a session with the server, which it hasn't once it's disconnected, or the server closes it
func (*Client) Disconnect ¶ added in v1.6.0
Disconnect closes the session, when it's connected, or does nothing when it isn't
func (*Client) Experimental ¶ added in v1.7.0
Experimental returns the value of the server's experimental capability with the name, such as a workspace's features, or nil, when it doesn't have it, or isn't connected
func (*Client) GetPrompt ¶
func (c *Client) GetPrompt(ctx context.Context, name string, arguments map[string]string) (*sdkmcp.GetPromptResult, error)
GetPrompt returns the messages of the server's prompt with the name, with its arguments. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) HasExperimental ¶ added in v1.7.0
HasExperimental returns true when the server says it has the experimental capability with the name, such as a workspace's
func (*Client) HasResources ¶ added in v1.7.0
HasResources returns true when the server says it has resources, which are listed, and read
func (*Client) Instructions ¶ added in v1.6.0
Instructions returns how to use the server, and its tools, as it said when it was connected, or nothing, when it isn't, or said nothing
func (*Client) ListPrompts ¶
ListPrompts returns the server's prompts, every page of them, or none, when it doesn't say it has any. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) ListResourceTemplates ¶ added in v1.7.0
ListResourceTemplates returns the server's resource templates, of each page, or none, when it doesn't say it has resources. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) ListResources ¶
func (c *Client) ListResources(ctx context.Context, params *sdkmcp.ListResourcesParams) (*sdkmcp.ListResourcesResult, error)
ListResources returns a page of the server's resources, from the params' cursor, with its _meta, which a server can use, such as to list a folder, or ignore, and the cursor of the next page, which is empty when there isn't one. Returns no resources when the server doesn't say it has any, and ErrServiceUnavailable when it isn't connected.
func (*Client) ListTools ¶
ListTools returns the server's tools, every page of them, or none, when it doesn't say it has any, such as one which only has prompts. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) ReadResource ¶ added in v1.7.0
ReadResource returns the contents of the server's resource with the URI. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) ReadResourceParams ¶ added in v1.7.0
func (c *Client) ReadResourceParams(ctx context.Context, params *sdkmcp.ReadResourceParams) (*sdkmcp.ReadResourceResult, error)
ReadResourceParams returns the contents of the server's resource, as the params ask, such as a chunk of it, in their _meta. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) Status ¶ added in v1.6.0
func (c *Client) Status() (*sdkmcp.Implementation, error)
Status returns the server's name, and version, as it said when it was connected, or nil when it isn't, and the error of the last connection, or of the session, when the server closed it, or nil
func (*Client) Subscribe ¶ added in v1.7.0
Subscribe asks the server to say when the resource with the URI is updated, which WatchResources sends, and returns once it has subscribed, so an update after that isn't missed. Returns ErrServiceUnavailable when it isn't connected, or the server doesn't acknowledge it subscribed.
func (*Client) Unsubscribe ¶ added in v1.7.0
Unsubscribe asks the server to stop saying when the resource with the URI is updated. Returns ErrServiceUnavailable when it isn't connected.
func (*Client) WatchResources ¶ added in v1.7.0
WatchResources sends the URI of each of the server's resources which is updated, which it was subscribed to, and an empty URI when the server says its list of resources changed, so it's listed again, to the channel, until the context is cancelled, when it returns nil. It sends an empty URI first, once it's watching, so a change after that isn't missed. Each is waited for the channel to receive. Returns ErrBadParameter when there's no channel.
type Opt ¶
type Opt func(*opt) error
Opt is an option of a client
func WithChange ¶ added in v1.6.0
func WithChange(fn ChangeFunc) Opt
WithChange is called when the client is connected, or disconnected, without a lock, which it isn't when a connection fails, or it's disconnected when it isn't connected
func WithClientOpts ¶ added in v1.6.0
WithClientOpts are options of go-client, such as client.OptHeader, client.OptUserAgent, client.OptReqToken, or client.OptTracer, which apply to the client's requests, and so to those of the MCP SDK. Its timeout, with client.OptTimeout, applies to its own requests, but not to an MCP session's, whose streams last as long as the session, and are cancelled with its context.
func WithName ¶ added in v1.6.0
WithName is the name, and version, of the client, which the server is told, as it connects, rather than schema.DefaultName, and schema.DefaultVersion. The version can be empty.
func WithTokenSource ¶ added in v1.6.0
func WithTokenSource(tokens oauth2.TokenSource) Opt
WithTokenSource is the source of the token the server's requests are authorized with, as a bearer token, such as one which is refreshed, as it expires, which is only sent to the server's origin
func WithToolsChanged ¶ added in v1.6.0
func WithToolsChanged(fn func()) Opt
WithToolsChanged is called when the server says its tools, or its prompts, changed, so they're listed again, without a lock
func WithTransport ¶ added in v1.7.0
func WithTransport(fn TransportFunc) Opt
WithTransport connects to the server with the transport the function returns, each time it's connected, rather than its URL, which is then any URL which names it, such as memory://weatherapi. Its requests aren't authorized, nor are go-client's options applied to them.