client

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Overview

Package client implements a high-level Go client for the Model Context Protocol (MCP).

It provides a thin wrapper around the protocol interface defined in the github.com/viant/mcp-protocol module and adds:

  • Automatic `initialize` handshake and capability negotiation.
  • Pluggable JSON-RPC transports (STDIO, HTTP/SSE, Streaming …).
  • Optional authorization interceptor that can acquire OAuth2/OIDC tokens on the fly and transparently retry failed requests.
  • Convenience helpers such as strongly typed `ListResources`, `CallTool`, `Complete`, … methods that avoid manual request/response handling.

The package is transport-agnostic; callers supply any implementation that satisfies the jsonrpc/transport.Transport interface.

Example:

sseTransport, _ := sse.New(ctx, "https://mcp.example.com/sse")
cli := client.New("demo", "1.0", sseTransport, client.WithCapabilities(schema.ClientCapabilities{}))
res, _ := cli.ListResources(ctx, nil)
fmt.Println(res.Resources)

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(name, version string, transport transport.Transport, options ...Option) *Client

func (*Client) CallTool

func (*Client) Complete

func (*Client) CreateMessage added in v0.2.10

func (*Client) CreateUserInteraction added in v0.2.10

func (*Client) Elicit added in v0.2.10

func (*Client) GetPrompt

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) (*schema.InitializeResult, error)

func (*Client) ListPrompts

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

func (*Client) ListResourceTemplates

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

func (*Client) ListResources

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

func (*Client) ListRoots added in v0.2.10

func (*Client) ListTools

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

func (*Client) Ping

func (*Client) ReadResource

func (*Client) SetLevel

func (*Client) Subscribe

func (*Client) Unsubscribe

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func NewHandler added in v0.3.1

func NewHandler(client client.Client) *Handler

NewHandler create client handler

func (*Handler) CreateMessageRequest

func (h *Handler) CreateMessageRequest(ctx context.Context, request *jsonrpc.Request) (*schema.CreateMessageResult, *jsonrpc.Error)

CreateMessageRequest handles the sampling/createMessage method

func (*Handler) CreateUserInteractionRequest added in v0.2.10

func (h *Handler) CreateUserInteractionRequest(ctx context.Context, request *jsonrpc.Request) (*schema.CreateUserInteractionResult, *jsonrpc.Error)

CreateUserInteractionRequest handles the "interaction/create" method.

func (*Handler) Elicit added in v0.2.10

func (h *Handler) Elicit(ctx context.Context, request *jsonrpc.Request) (*schema.ElicitResult, *jsonrpc.Error)

Elicit handles the "elicitation/create" method.

func (*Handler) ListRoots

func (h *Handler) ListRoots(ctx context.Context, request *jsonrpc.Request) (*schema.ListRootsResult, *jsonrpc.Error)

ListRoots handles the root/listRoots method

func (*Handler) OnNotification

func (s *Handler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)

func (*Handler) Serve

func (h *Handler) Serve(ctx context.Context, request *jsonrpc.Request, response *jsonrpc.Response)

type Interface added in v0.2.5

type Interface interface {
	// Initialize initializes the client
	Initialize(ctx context.Context) (*schema.InitializeResult, error)

	// ListResourceTemplates lists resource templates
	ListResourceTemplates(ctx context.Context, cursor *string) (*schema.ListResourceTemplatesResult, error)

	// ListResources lists resources
	ListResources(ctx context.Context, cursor *string) (*schema.ListResourcesResult, error)

	// ListPrompts lists prompts
	ListPrompts(ctx context.Context, cursor *string) (*schema.ListPromptsResult, error)

	// ListTools lists tools
	ListTools(ctx context.Context, cursor *string) (*schema.ListToolsResult, error)

	// ReadResource reads a resource
	ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams) (*schema.ReadResourceResult, error)

	// GetPrompt gets a prompt
	GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams) (*schema.GetPromptResult, error)

	// CallTool calls a tool
	CallTool(ctx context.Context, params *schema.CallToolRequestParams) (*schema.CallToolResult, error)

	// Complete completes a request
	Complete(ctx context.Context, params *schema.CompleteRequestParams) (*schema.CompleteResult, error)

	// Ping pings the server
	Ping(ctx context.Context, params *schema.PingRequestParams) (*schema.PingResult, error)

	// Subscribe subscribes to a resource
	Subscribe(ctx context.Context, params *schema.SubscribeRequestParams) (*schema.SubscribeResult, error)

	// Unsubscribe unsubscribes from a resource
	Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams) (*schema.UnsubscribeResult, error)

	// SetLevel sets the logging level
	SetLevel(ctx context.Context, params *schema.SetLevelRequestParams) (*schema.SetLevelResult, error)

	// ListRoots lists client roots (client side capability discovery)
	ListRoots(ctx context.Context, params *schema.ListRootsRequestParams) (*schema.ListRootsResult, error)

	// CreateMessage creates a sampling message on the client side
	CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams) (*schema.CreateMessageResult, error)

	// Elicit is a server-initiated request asking the client to elicit additional information from the end-user
	Elicit(ctx context.Context, params *schema.ElicitRequestParams) (*schema.ElicitResult, error)

	// CreateUserInteraction asks the client to display a UI interaction to the user and return their response
	CreateUserInteraction(ctx context.Context, params *schema.CreateUserInteractionRequestParams) (*schema.CreateUserInteractionResult, error)
}

Interface defines the client interface for all exported methods

type Option

type Option func(c *Client)

Option represents option

func WithAuthInterceptor

func WithAuthInterceptor(authorizer *auth.Authorizer) Option

WithAuthInterceptor attaches an Authorizer to the client, enabling automatic retry of requests when receiving a 401 Unauthorized response. The interceptor's Intercept method will be called after each Send.

func WithCapabilities

func WithCapabilities(capabilities schema.ClientCapabilities) Option

WithCapabilities set capabilites

func WithImplementer

func WithImplementer(impl client.Operations) Option

WithImplementer with client

func WithMetadata

func WithMetadata(metadata map[string]any) Option

WithMetadata with meta

func WithProtocolVersion added in v0.2.7

func WithProtocolVersion(version string) Option

Directories

Path Synopsis
Package auth contains supporting helpers that enable fine-grained client side authorization when talking to an MCP server.
Package auth contains supporting helpers that enable fine-grained client side authorization when talking to an MCP server.
mock
Package mock provides in-memory and stub implementations that facilitate unit testing of the client-side authorization flow.
Package mock provides in-memory and stub implementations that facilitate unit testing of the client-side authorization flow.
store
Package store defines simple token and client-configuration stores used by the authorization helpers in the parent `auth` package.
Package store defines simple token and client-configuration stores used by the authorization helpers in the parent `auth` package.
transport
Package transport implements an http.RoundTripper that performs the OAuth 2.1 [Protected Resource Metadata](https://www.rfc-editor.org/rfc/rfc9728) discovery, token acquisition and automatic request retry logic required by MCP when a server challenges the client with `401 Unauthorized`.
Package transport implements an http.RoundTripper that performs the OAuth 2.1 [Protected Resource Metadata](https://www.rfc-editor.org/rfc/rfc9728) discovery, token acquisition and automatic request retry logic required by MCP when a server challenges the client with `401 Unauthorized`.

Jump to

Keyboard shortcuts

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