client

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package client provides the MCP client implementation.

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
}

Client represents a generic MCP client instance.

func NewClient

func NewClient(clientName string, opts ClientOptions) (*Client, error)

NewClient creates a new generic Client instance. Transport must be provided via ClientOptions by specific constructors (NewSSEClient, NewWebSocketClient, etc.).

func NewSSEClient added in v0.1.11

func NewSSEClient(clientName string, baseURL string, basePath string, opts ClientOptions) (*Client, error)

NewSSEClient creates a new MCP client using the Streamable HTTP transport (SSE + POST). The baseURL should include the scheme and host (e.g., "http://localhost:8080"). The basePath argument defines the single path for the MCP endpoint (e.g., "/mcp").

func NewStdioClient added in v0.1.11

func NewStdioClient(clientName string, opts ClientOptions) (*Client, error)

NewStdioClient creates a new MCP client using stdio transport. This is useful for command-line tools and testing.

func NewWebSocketClient added in v0.1.11

func NewWebSocketClient(clientName string, baseURL string, basePath string, opts ClientOptions) (*Client, error)

NewWebSocketClient creates a new MCP client using WebSocket transport. The baseURL should include the scheme and host (e.g., "ws://localhost:8080" or "wss://localhost:8080"). The basePath argument defines the URL prefix for the WebSocket endpoint (e.g., "/mcp", resulting in "/mcp/ws").

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, params protocol.CallToolParams, progressToken interface{}) (*protocol.CallToolResult, error)

CallTool sends a 'tools/call' request via the transport.

func (*Client) CancelRequest added in v1.0.8

func (c *Client) CancelRequest(ctx context.Context, id interface{}, reason *string) error

CancelRequest sends a '$/cancelRequest' notification to the server.

func (*Client) Close

func (c *Client) Close() error

Close gracefully shuts down the client connection and its transport.

func (*Client) Connect

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

Connect performs the MCP handshake using the configured transport.

func (*Client) CurrentState added in v1.0.7

func (c *Client) CurrentState() (connected bool, initialized bool, closed bool)

CurrentState returns the current connection, initialization, and closed states.

func (*Client) GetRoots added in v1.0.8

func (c *Client) GetRoots() []protocol.Root

GetRoots returns the current list of roots known to the client.

func (*Client) IsClosed added in v1.0.7

func (c *Client) IsClosed() bool

IsClosed returns true if the client has been explicitly closed.

func (*Client) IsConnected added in v0.1.11

func (c *Client) IsConnected() bool

IsConnected returns true if the client has successfully completed the handshake and the underlying transport is not known to be closed.

func (*Client) IsInitialized added in v1.0.7

func (c *Client) IsInitialized() bool

IsInitialized returns true if the client has successfully completed the initialization handshake.

func (*Client) ListTools

ListTools sends a 'tools/list' request via the transport.

func (*Client) Ping added in v1.0.8

func (c *Client) Ping(ctx context.Context, timeout time.Duration) error

Ping sends a 'ping' request to the server and waits for the response.

func (*Client) RegisterNotificationHandler

func (c *Client) RegisterNotificationHandler(method string, handler NotificationHandlerFunc) error

func (*Client) RegisterRequestHandler

func (c *Client) RegisterRequestHandler(method string, handler RequestHandlerFunc) error

RegisterRequestHandler registers a handler for generic server-to-client requests. Note: Sampling requests (sampling/request) should be handled via RegisterSamplingHandler.

func (*Client) RegisterSamplingHandler added in v1.0.8

func (c *Client) RegisterSamplingHandler(handler SamplingRequestHandlerFunc) error

RegisterSamplingHandler registers the specific handler for 'sampling/request'. Providing a handler implies the client supports sampling and will advertise it.

func (*Client) ServerCapabilities

func (c *Client) ServerCapabilities() protocol.ServerCapabilities

ServerCapabilities returns the capabilities reported by the server.

func (*Client) ServerInfo

func (c *Client) ServerInfo() protocol.Implementation

ServerInfo returns the information about the connected server.

func (*Client) SetRoots added in v1.0.8

func (c *Client) SetRoots(ctx context.Context, roots []protocol.Root) error

SetRoots updates the client's list of roots. If the client advertised the 'roots.listChanged' capability, it sends a 'notifications/roots/list_changed' notification to the server.

type ClientOptions

type ClientOptions struct {
	Logger                   types.Logger
	ClientCapabilities       protocol.ClientCapabilities // User can pre-configure capabilities including Roots.ListChanged
	PreferredProtocolVersion *string                     // Optional: Defaults to OldProtocolVersion
	Transport                types.Transport             // Required: Must be provided by specific constructors
	SamplingHandler          SamplingRequestHandlerFunc  // Added for Sampling
	InitialRoots             []protocol.Root             // Optional initial roots

	// Hooks
	BeforeSendRequestHooks              []hooks.ClientBeforeSendRequestHook
	BeforeSendNotificationHooks         []hooks.ClientBeforeSendNotificationHook
	OnReceiveRawMessageHooks            []hooks.OnReceiveRawMessageHook
	BeforeHandleResponseHooks           []hooks.ClientBeforeHandleResponseHook
	ClientBeforeHandleNotificationHooks []hooks.ClientBeforeHandleNotificationHook
	ClientBeforeHandleRequestHooks      []hooks.ClientBeforeHandleRequestHook
}

ClientOptions contains configuration options for creating a Client.

type NotificationHandlerFunc

type NotificationHandlerFunc func(ctx context.Context, params interface{}) error

NotificationHandlerFunc defines the signature for functions that handle server-to-client notifications.

type RequestHandlerFunc

type RequestHandlerFunc func(ctx context.Context, id interface{}, params interface{}) error

RequestHandlerFunc defines the signature for functions that handle generic server-to-client requests.

type SamplingRequestHandlerFunc added in v1.0.8

type SamplingRequestHandlerFunc func(ctx context.Context, id interface{}, params protocol.SamplingRequestParams) (*protocol.SamplingResult, error)

SamplingRequestHandlerFunc defines the signature for the specific sampling request handler. It must return a result or an error to be sent back to the server.

Jump to

Keyboard shortcuts

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