client

package
v0.32.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 12 Imported by: 131

Documentation

Overview

Package client provides MCP (Model Context Protocol) client implementations.

Index

Constants

This section is empty.

Variables

View Source
var GenerateCodeChallenge = transport.GenerateCodeChallenge

GenerateCodeChallenge generates a code challenge from a code verifier

View Source
var GenerateCodeVerifier = transport.GenerateCodeVerifier

GenerateCodeVerifier generates a code verifier for PKCE

View Source
var GenerateState = transport.GenerateState

GenerateState generates a state parameter for OAuth

View Source
var NewMemoryTokenStore = transport.NewMemoryTokenStore

NewMemoryTokenStore is a convenience function that wraps transport.NewMemoryTokenStore

Functions

func GetEndpoint added in v0.21.0

func GetEndpoint(c *Client) *url.URL

GetEndpoint returns the current endpoint URL for the SSE connection.

Note: This method only works with SSE transport, or it will panic.

func GetOAuthHandler added in v0.30.0

func GetOAuthHandler(err error) *transport.OAuthHandler

GetOAuthHandler extracts the OAuthHandler from an OAuthAuthorizationRequiredError

func GetStderr added in v0.21.0

func GetStderr(c *Client) (io.Reader, bool)

GetStderr returns a reader for the stderr output of the subprocess. This can be used to capture error messages or logs from the subprocess.

func IsOAuthAuthorizationRequiredError added in v0.30.0

func IsOAuthAuthorizationRequiredError(err error) bool

IsOAuthAuthorizationRequiredError checks if an error is an OAuthAuthorizationRequiredError

func WithHTTPClient added in v0.25.0

func WithHTTPClient(httpClient *http.Client) transport.ClientOption

func WithHeaderFunc added in v0.30.0

func WithHeaderFunc(headerFunc transport.HTTPHeaderFunc) transport.ClientOption

func WithHeaders added in v0.17.0

func WithHeaders(headers map[string]string) transport.ClientOption

Types

type Client added in v0.21.0

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

Client implements the MCP client.

func NewClient added in v0.21.0

func NewClient(transport transport.Interface, options ...ClientOption) *Client

NewClient creates a new MCP client with the given transport. Usage:

stdio := transport.NewStdio("./mcp_server", nil, "xxx")
client, err := NewClient(stdio)
if err != nil {
    log.Fatalf("Failed to create client: %v", err)
}

func NewInProcessClient added in v0.23.0

func NewInProcessClient(server *server.MCPServer) (*Client, error)

NewInProcessClient connect directly to a mcp server object in the same process

func NewOAuthSSEClient added in v0.32.0

func NewOAuthSSEClient(baseURL string, oauthConfig OAuthConfig, options ...transport.ClientOption) (*Client, error)

NewOAuthStreamableHttpClient creates a new streamable-http-based MCP client with OAuth support. Returns an error if the URL is invalid.

func NewOAuthStreamableHttpClient added in v0.30.0

func NewOAuthStreamableHttpClient(baseURL string, oauthConfig OAuthConfig, options ...transport.StreamableHTTPCOption) (*Client, error)

NewOAuthStreamableHttpClient creates a new streamable-http-based MCP client with OAuth support. Returns an error if the URL is invalid.

func NewSSEMCPClient added in v0.2.0

func NewSSEMCPClient(baseURL string, options ...transport.ClientOption) (*Client, error)

NewSSEMCPClient creates a new SSE-based MCP client with the given base URL. Returns an error if the URL is invalid.

func NewStdioMCPClient

func NewStdioMCPClient(
	command string,
	env []string,
	args ...string,
) (*Client, error)

NewStdioMCPClient creates a new stdio-based MCP client that communicates with a subprocess. It launches the specified command with given arguments and sets up stdin/stdout pipes for communication. Returns an error if the subprocess cannot be started or the pipes cannot be created.

NOTICE: NewStdioMCPClient will start the connection automatically. Don't call the Start method manually. This is for backward compatibility.

func NewStreamableHttpClient added in v0.22.0

func NewStreamableHttpClient(baseURL string, options ...transport.StreamableHTTPCOption) (*Client, error)

NewStreamableHttpClient is a convenience method that creates a new streamable-http-based MCP client with the given base URL. Returns an error if the URL is invalid.

func (*Client) CallTool added in v0.21.0

func (c *Client) CallTool(
	ctx context.Context,
	request mcp.CallToolRequest,
) (*mcp.CallToolResult, error)

func (*Client) Close added in v0.21.0

func (c *Client) Close() error

Close shuts down the client and closes the transport.

func (*Client) Complete added in v0.21.0

func (c *Client) Complete(
	ctx context.Context,
	request mcp.CompleteRequest,
) (*mcp.CompleteResult, error)

func (*Client) GetClientCapabilities added in v0.23.0

func (c *Client) GetClientCapabilities() mcp.ClientCapabilities

GetClientCapabilities returns the client capabilities.

func (*Client) GetPrompt added in v0.21.0

func (c *Client) GetPrompt(
	ctx context.Context,
	request mcp.GetPromptRequest,
) (*mcp.GetPromptResult, error)

func (*Client) GetServerCapabilities added in v0.23.0

func (c *Client) GetServerCapabilities() mcp.ServerCapabilities

GetServerCapabilities returns the server capabilities.

func (*Client) GetTransport added in v0.21.0

func (c *Client) GetTransport() transport.Interface

GetTransport gives access to the underlying transport layer. Cast it to the specific transport type and obtain the other helper methods.

func (*Client) Initialize added in v0.21.0

func (c *Client) Initialize(
	ctx context.Context,
	request mcp.InitializeRequest,
) (*mcp.InitializeResult, error)

Initialize negotiates with the server. Must be called after Start, and before any request methods.

func (*Client) ListPrompts added in v0.21.0

func (c *Client) ListPrompts(
	ctx context.Context,
	request mcp.ListPromptsRequest,
) (*mcp.ListPromptsResult, error)

func (*Client) ListPromptsByPage added in v0.21.0

func (c *Client) ListPromptsByPage(
	ctx context.Context,
	request mcp.ListPromptsRequest,
) (*mcp.ListPromptsResult, error)

func (*Client) ListResourceTemplates added in v0.21.0

func (c *Client) ListResourceTemplates(
	ctx context.Context,
	request mcp.ListResourceTemplatesRequest,
) (*mcp.ListResourceTemplatesResult, error)

func (*Client) ListResourceTemplatesByPage added in v0.21.0

func (c *Client) ListResourceTemplatesByPage(
	ctx context.Context,
	request mcp.ListResourceTemplatesRequest,
) (*mcp.ListResourceTemplatesResult, error)

func (*Client) ListResources added in v0.21.0

func (c *Client) ListResources(
	ctx context.Context,
	request mcp.ListResourcesRequest,
) (*mcp.ListResourcesResult, error)

func (*Client) ListResourcesByPage added in v0.21.0

func (c *Client) ListResourcesByPage(
	ctx context.Context,
	request mcp.ListResourcesRequest,
) (*mcp.ListResourcesResult, error)

ListResourcesByPage manually list resources by page.

func (*Client) ListTools added in v0.21.0

func (c *Client) ListTools(
	ctx context.Context,
	request mcp.ListToolsRequest,
) (*mcp.ListToolsResult, error)

func (*Client) ListToolsByPage added in v0.21.0

func (c *Client) ListToolsByPage(
	ctx context.Context,
	request mcp.ListToolsRequest,
) (*mcp.ListToolsResult, error)

func (*Client) OnNotification added in v0.21.0

func (c *Client) OnNotification(
	handler func(notification mcp.JSONRPCNotification),
)

OnNotification registers a handler function to be called when notifications are received. Multiple handlers can be registered and will be called in the order they were added.

func (*Client) Ping added in v0.21.0

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

func (*Client) ReadResource added in v0.21.0

func (c *Client) ReadResource(
	ctx context.Context,
	request mcp.ReadResourceRequest,
) (*mcp.ReadResourceResult, error)

func (*Client) SetLevel added in v0.21.0

func (c *Client) SetLevel(
	ctx context.Context,
	request mcp.SetLevelRequest,
) error

func (*Client) Start added in v0.21.0

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

Start initiates the connection to the server. Must be called before using the client.

func (*Client) Subscribe added in v0.21.0

func (c *Client) Subscribe(
	ctx context.Context,
	request mcp.SubscribeRequest,
) error

func (*Client) Unsubscribe added in v0.21.0

func (c *Client) Unsubscribe(
	ctx context.Context,
	request mcp.UnsubscribeRequest,
) error

type ClientOption added in v0.17.0

type ClientOption func(*Client)

func WithClientCapabilities added in v0.23.0

func WithClientCapabilities(capabilities mcp.ClientCapabilities) ClientOption

WithClientCapabilities sets the client capabilities for the client.

type MCPClient

type MCPClient interface {
	// Initialize sends the initial connection request to the server
	Initialize(
		ctx context.Context,
		request mcp.InitializeRequest,
	) (*mcp.InitializeResult, error)

	// Ping checks if the server is alive
	Ping(ctx context.Context) error

	// ListResourcesByPage manually list resources by page.
	ListResourcesByPage(
		ctx context.Context,
		request mcp.ListResourcesRequest,
	) (*mcp.ListResourcesResult, error)

	// ListResources requests a list of available resources from the server
	ListResources(
		ctx context.Context,
		request mcp.ListResourcesRequest,
	) (*mcp.ListResourcesResult, error)

	// ListResourceTemplatesByPage manually list resource templates by page.
	ListResourceTemplatesByPage(
		ctx context.Context,
		request mcp.ListResourceTemplatesRequest,
	) (*mcp.ListResourceTemplatesResult,
		error)

	// ListResourceTemplates requests a list of available resource templates from the server
	ListResourceTemplates(
		ctx context.Context,
		request mcp.ListResourceTemplatesRequest,
	) (*mcp.ListResourceTemplatesResult,
		error)

	// ReadResource reads a specific resource from the server
	ReadResource(
		ctx context.Context,
		request mcp.ReadResourceRequest,
	) (*mcp.ReadResourceResult, error)

	// Subscribe requests notifications for changes to a specific resource
	Subscribe(ctx context.Context, request mcp.SubscribeRequest) error

	// Unsubscribe cancels notifications for a specific resource
	Unsubscribe(ctx context.Context, request mcp.UnsubscribeRequest) error

	// ListPromptsByPage manually list prompts by page.
	ListPromptsByPage(
		ctx context.Context,
		request mcp.ListPromptsRequest,
	) (*mcp.ListPromptsResult, error)

	// ListPrompts requests a list of available prompts from the server
	ListPrompts(
		ctx context.Context,
		request mcp.ListPromptsRequest,
	) (*mcp.ListPromptsResult, error)

	// GetPrompt retrieves a specific prompt from the server
	GetPrompt(
		ctx context.Context,
		request mcp.GetPromptRequest,
	) (*mcp.GetPromptResult, error)

	// ListToolsByPage manually list tools by page.
	ListToolsByPage(
		ctx context.Context,
		request mcp.ListToolsRequest,
	) (*mcp.ListToolsResult, error)

	// ListTools requests a list of available tools from the server
	ListTools(
		ctx context.Context,
		request mcp.ListToolsRequest,
	) (*mcp.ListToolsResult, error)

	// CallTool invokes a specific tool on the server
	CallTool(
		ctx context.Context,
		request mcp.CallToolRequest,
	) (*mcp.CallToolResult, error)

	// SetLevel sets the logging level for the server
	SetLevel(ctx context.Context, request mcp.SetLevelRequest) error

	// Complete requests completion options for a given argument
	Complete(
		ctx context.Context,
		request mcp.CompleteRequest,
	) (*mcp.CompleteResult, error)

	// Close client connection and cleanup resources
	Close() error

	// OnNotification registers a handler for notifications
	OnNotification(handler func(notification mcp.JSONRPCNotification))
}

MCPClient represents an MCP client interface

type MemoryTokenStore added in v0.30.0

type MemoryTokenStore = transport.MemoryTokenStore

MemoryTokenStore is a convenience type that wraps transport.MemoryTokenStore

type OAuthAuthorizationRequiredError added in v0.30.0

type OAuthAuthorizationRequiredError = transport.OAuthAuthorizationRequiredError

OAuthAuthorizationRequiredError is returned when OAuth authorization is required

type OAuthConfig added in v0.30.0

type OAuthConfig = transport.OAuthConfig

OAuthConfig is a convenience type that wraps transport.OAuthConfig

type Token added in v0.30.0

type Token = transport.Token

Token is a convenience type that wraps transport.Token

type TokenStore added in v0.30.0

type TokenStore = transport.TokenStore

TokenStore is a convenience type that wraps transport.TokenStore

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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