Documentation
¶
Overview ¶
Package client implements a stateless MCP client for protocol revision 2026-07-28. There is no session: a Client is configuration plus a transport; every request carries its own context in _meta.
Index ¶
- type Client
- func (c *Client) Call(ctx context.Context, method string, params, result any) error
- func (c *Client) CallTool(ctx context.Context, p *protocol.CallToolParams) (*protocol.CallToolResult, error)
- func (c *Client) Close() error
- func (c *Client) Complete(ctx context.Context, p *protocol.CompleteParams) (*protocol.CompleteResult, error)
- func (c *Client) Discover(ctx context.Context) (*protocol.DiscoverResult, error)
- func (c *Client) GetPrompt(ctx context.Context, p *protocol.GetPromptParams) (*protocol.GetPromptResult, error)
- func (c *Client) ListPrompts(ctx context.Context, cursor string) (*protocol.ListPromptsResult, error)
- func (c *Client) ListResourceTemplates(ctx context.Context, cursor string) (*protocol.ListResourceTemplatesResult, error)
- func (c *Client) ListResources(ctx context.Context, cursor string) (*protocol.ListResourcesResult, error)
- func (c *Client) ListTools(ctx context.Context, cursor string) (*protocol.ListToolsResult, error)
- func (c *Client) Listen(ctx context.Context, filter protocol.SubscriptionFilter) (*Subscription, error)
- func (c *Client) Prompts(ctx context.Context) iter.Seq2[*protocol.Prompt, error]
- func (c *Client) ReadResource(ctx context.Context, p *protocol.ReadResourceParams) (*protocol.ReadResourceResult, error)
- func (c *Client) Resources(ctx context.Context) iter.Seq2[*protocol.Resource, error]
- func (c *Client) Tools(ctx context.Context) iter.Seq2[*protocol.Tool, error]
- type ElicitHandler
- type Event
- type InputRequiredError
- type Options
- type Subscription
- type UnexpectedResultTypeError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func New ¶
New builds a client. It panics on unmarshalable Options.Extensions values — configuration errors must fail loudly, not degrade into empty declarations.
func (*Client) Call ¶ added in v1.3.0
Call issues a raw request and decodes its result — the escape hatch for extension methods (it satisfies the tasks extension's Caller interface). Error responses surface as *protocol.Error.
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, p *protocol.CallToolParams) (*protocol.CallToolResult, error)
CallTool invokes a tool, transparently running the MRTR fulfillment loop (see Options.Elicitor / NoAutoInput).
func (*Client) Complete ¶ added in v1.3.0
func (c *Client) Complete(ctx context.Context, p *protocol.CompleteParams) (*protocol.CompleteResult, error)
func (*Client) Discover ¶ added in v1.3.0
Discover fetches the server's supported versions, capabilities and identity.
func (*Client) GetPrompt ¶
func (c *Client) GetPrompt(ctx context.Context, p *protocol.GetPromptParams) (*protocol.GetPromptResult, error)
GetPrompt resolves a prompt, transparently running the MRTR loop.
func (*Client) ListPrompts ¶
func (*Client) ListResourceTemplates ¶ added in v1.1.2
func (*Client) ListResources ¶
func (*Client) ListTools ¶
ListTools fetches one page of tools. Tools with invalid x-mcp-header bindings are excluded per spec; their bindings are cached for header generation on later CallTool requests.
func (*Client) Listen ¶ added in v1.3.0
func (c *Client) Listen(ctx context.Context, filter protocol.SubscriptionFilter) (*Subscription, error)
Listen opens a subscriptions/listen stream. It blocks until the server's acknowledgment (the required first message) arrives.
func (*Client) ReadResource ¶
func (c *Client) ReadResource(ctx context.Context, p *protocol.ReadResourceParams) (*protocol.ReadResourceResult, error)
ReadResource reads a resource, transparently running the MRTR loop.
type ElicitHandler ¶ added in v1.3.0
type ElicitHandler func(ctx context.Context, p *protocol.ElicitParams) (*protocol.ElicitResult, error)
ElicitHandler answers a form-mode elicitation input request.
type Event ¶ added in v1.3.0
type Event struct {
Method string
Params json.RawMessage
}
Event is one notification delivered on a subscription stream.
func (Event) ResourceUpdated ¶ added in v1.3.0
ResourceUpdated decodes the event as notifications/resources/updated.
type InputRequiredError ¶ added in v1.3.0
type InputRequiredError struct {
Requests protocol.InputRequests
State string
Reason string
}
InputRequiredError surfaces an MRTR interim result the client could not (or was configured not to) fulfill automatically. For manual continuation, set InputResponses and RequestState on the original params and call again.
func (*InputRequiredError) Error ¶ added in v1.3.0
func (e *InputRequiredError) Error() string
type Options ¶ added in v1.3.0
type Options struct {
// Info is sent as clientInfo in every request's _meta.
Info *protocol.Implementation
// Elicitor enables form-mode elicitation: it is declared in the client
// capabilities and drives the automatic MRTR fulfillment loop.
Elicitor ElicitHandler
// URLOpener enables url-mode elicitation. It should return once the user
// has completed the out-of-band interaction; the outcome is learned by
// retrying the original request.
URLOpener func(ctx context.Context, url, message string) error
// OnProgress receives notifications/progress for requests issued by this
// client. When set, every request carries an auto-generated progressToken.
OnProgress func(p *protocol.ProgressParams)
// Extensions is declared verbatim in clientCapabilities.extensions on
// every request (e.g. tasks.ID -> struct{}{}).
Extensions map[string]any
// RouteNames maps extension methods to the params key whose string value
// must be sent as the Mcp-Name routing header over Streamable HTTP (the
// tasks extension maps its methods to "taskId"; see tasks.EnableClient).
// Core methods are built in.
RouteNames map[string]string
// MaxInputRounds caps MRTR retries per call (default 10).
MaxInputRounds int
// MaxLoadSheddingRetries caps retries when the server returns an
// input_required result with no inputRequests (default 3).
MaxLoadSheddingRetries int
// NoAutoInput disables the automatic MRTR fulfillment loop; interim
// results surface as *InputRequiredError for manual continuation.
NoAutoInput bool
// Logger receives warnings (e.g. invalid tools excluded from tools/list).
// Defaults to slog.Default().
Logger *slog.Logger
}
type Subscription ¶ added in v1.3.0
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is one open subscriptions/listen stream.
func (*Subscription) Ack ¶ added in v1.3.0
func (s *Subscription) Ack() protocol.SubscriptionFilter
Ack returns the filter subset the server agreed to honor. Compare it with what you requested to detect unsupported notification types.
func (*Subscription) Close ¶ added in v1.3.0
func (s *Subscription) Close() error
Close cancels the subscription. On HTTP this closes the response stream — the wire-level cancellation signal; on stdio the transport sends notifications/cancelled. Events still buffered are discarded.
func (*Subscription) Err ¶ added in v1.3.0
func (s *Subscription) Err() error
Err reports why Events closed: nil for graceful teardown or client Close, otherwise the transport error. The spec leaves reconnection to the caller (re-issue Listen; the server holds no subscription state).
func (*Subscription) Events ¶ added in v1.3.0
func (s *Subscription) Events() <-chan Event
Events yields notifications until the stream ends. The channel closes on graceful server teardown, cancellation and disconnects alike; check Err afterwards.
type UnexpectedResultTypeError ¶ added in v1.3.0
type UnexpectedResultTypeError struct {
ResultType string
Raw json.RawMessage
}
UnexpectedResultTypeError reports a resultType this client does not handle itself. Extensions inspect Raw (e.g. the tasks extension unwraps resultType "task" into a CreateTaskResult).
func (*UnexpectedResultTypeError) Error ¶ added in v1.3.0
func (e *UnexpectedResultTypeError) Error() string