client

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

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

func New(t transport.Transport, opts *Options) *Client

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

func (c *Client) Call(ctx context.Context, method string, params, result any) error

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

CallTool invokes a tool, transparently running the MRTR fulfillment loop (see Options.Elicitor / NoAutoInput).

func (*Client) Close

func (c *Client) Close() error

func (*Client) Complete added in v1.3.0

func (*Client) Discover added in v1.3.0

func (c *Client) Discover(ctx context.Context) (*protocol.DiscoverResult, error)

Discover fetches the server's supported versions, capabilities and identity.

func (*Client) GetPrompt

GetPrompt resolves a prompt, transparently running the MRTR loop.

func (*Client) ListPrompts

func (c *Client) ListPrompts(ctx context.Context, cursor string) (*protocol.ListPromptsResult, error)

func (*Client) ListResourceTemplates added in v1.1.2

func (c *Client) ListResourceTemplates(ctx context.Context, cursor string) (*protocol.ListResourceTemplatesResult, error)

func (*Client) ListResources

func (c *Client) ListResources(ctx context.Context, cursor string) (*protocol.ListResourcesResult, error)

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context, cursor string) (*protocol.ListToolsResult, error)

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) Prompts added in v1.3.0

func (c *Client) Prompts(ctx context.Context) iter.Seq2[*protocol.Prompt, error]

Prompts iterates all prompts across pages.

func (*Client) ReadResource

ReadResource reads a resource, transparently running the MRTR loop.

func (*Client) Resources added in v1.3.0

func (c *Client) Resources(ctx context.Context) iter.Seq2[*protocol.Resource, error]

Resources iterates all resources across pages.

func (*Client) Tools added in v1.3.0

func (c *Client) Tools(ctx context.Context) iter.Seq2[*protocol.Tool, error]

Tools iterates all tools across pages.

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

func (e Event) ResourceUpdated() (uri string, ok bool)

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

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

Jump to

Keyboard shortcuts

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