client

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*authclient.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

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, arguments json.RawMessage, meta ...schema.MetaValue) (any, error)

CallTool invokes a tool on the connected MCP server by name with the given Returns ErrNotConnected if no session is active.

func (*Client) Close

func (c *Client) Close() error

Close closes the current MCP session and clears all cached state.

func (*Client) Connect

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

Connect establishes an MCP session, populates the tool/prompt/resource caches and leaves the session available for subsequent API calls.

func (*Client) GetPrompt

func (c *Client) GetPrompt(ctx context.Context, name string, arguments map[string]string) (*sdkmcp.GetPromptResult, error)

GetPrompt fetches a prepared prompt from the connected MCP server.

func (*Client) GetResource

func (c *Client) GetResource(ctx context.Context, uri string) (llm.Resource, error)

GetResource returns a resource handle for the given URI. If the URI does not exist in the cached metadata list, a minimal resource handle is still returned so callers can attempt a direct read.

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 ErrServiceUnavailable 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 ErrServiceUnavailable 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 ErrServiceUnavailable if not active.

func (*Client) Probe

func (c *Client) Probe(ctx context.Context, authfn func(err error, config *authclient.Config) error) (*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 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 OnStateChange

type OnStateChange func(ctx context.Context, state *schema.ConnectorState)

OnStateChange is called once after a successful connection handshake. The state carries the server name, version and capabilities from the MCP initialize response.

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 OptOnStateChange

func OptOnStateChange(fn OnStateChange) Opt

OptOnStateChange registers a callback invoked once after each successful connection handshake. The state carries the server name, version and capabilities from the MCP initialize response.

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 WithClientOpt

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

WithClientOpt wraps one or more go-client options so they can be passed alongside mcp Opt values to New().

Jump to

Keyboard shortcuts

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