Documentation
¶
Overview ¶
Package client is a drop-in compatibility shim for github.com/mark3labs/mcp-go/client, reimplemented on top of the official github.com/modelcontextprotocol/go-sdk.
It presents mcp-go's flat client API (NewStreamableHttpClient, Start, Initialize, ListTools, CallTool, ...) while delegating the actual protocol to a go-sdk Client and ClientSession underneath. Data types are exchanged as the mcp-go-shaped types from mcpcompat/mcp; conversion to and from the go-sdk's own types happens at this boundary via JSON round-trips, which is robust because both encode the identical MCP wire format.
Stability: Alpha.
Index ¶
- type Client
- func NewSSEMCPClient(baseURL string, options ...transport.ClientOption) (*Client, error)
- func NewStreamableHttpClient(baseURL string, options ...transport.StreamableHTTPCOption) (*Client, error)
- func NewStreamableHttpClientWithOpts(baseURL string, transportOpts []transport.StreamableHTTPCOption, ...) (*Client, error)
- func (c *Client) CallTool(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
- func (c *Client) Close() error
- func (c *Client) GetPrompt(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error)
- func (c *Client) GetSessionId() string
- func (c *Client) GetTransport() transport.Interface
- func (c *Client) Initialize(ctx context.Context, request mcp.InitializeRequest) (*mcp.InitializeResult, error)
- func (c *Client) IsInitialized() bool
- func (c *Client) ListPrompts(ctx context.Context, request mcp.ListPromptsRequest) (*mcp.ListPromptsResult, error)
- func (c *Client) ListResourceTemplates(ctx context.Context, request mcp.ListResourceTemplatesRequest) (*mcp.ListResourceTemplatesResult, error)
- func (c *Client) ListResources(ctx context.Context, request mcp.ListResourcesRequest) (*mcp.ListResourcesResult, error)
- func (c *Client) ListTools(ctx context.Context, request mcp.ListToolsRequest) (*mcp.ListToolsResult, error)
- func (c *Client) OnConnectionLost(handler func(error))
- func (c *Client) OnNotification(handler func(notification mcp.JSONRPCNotification))
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) ReadResource(ctx context.Context, request mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error)
- func (c *Client) SetLevel(ctx context.Context, request mcp.SetLevelRequest) error
- func (c *Client) SetLoggingLevel(ctx context.Context, level mcp.LoggingLevel) error
- func (*Client) Start(_ context.Context) error
- type ClientOption
- type ElicitationHandler
- type ElicitationHandlerFunc
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 is an MCP client backed by the official go-sdk. It mirrors the subset of mcp-go's client.Client that ToolHive uses.
func NewSSEMCPClient ¶
func NewSSEMCPClient(baseURL string, options ...transport.ClientOption) (*Client, error)
NewSSEMCPClient creates an SSE MCP client for baseURL. The returned client is not yet connected; call Start then Initialize.
func NewStreamableHttpClient ¶
func NewStreamableHttpClient(baseURL string, options ...transport.StreamableHTTPCOption) (*Client, error)
NewStreamableHttpClient creates a Streamable HTTP MCP client for baseURL. Like mcp-go, the returned client is not yet connected; call Start then Initialize. Transport options configure the underlying Streamable HTTP transport; client options (client.WithElicitationHandler, ...) configure client-level behavior.
func NewStreamableHttpClientWithOpts ¶ added in v0.0.28
func NewStreamableHttpClientWithOpts( baseURL string, transportOpts []transport.StreamableHTTPCOption, clientOpts []ClientOption, ) (*Client, error)
NewStreamableHttpClientWithOpts creates a Streamable HTTP MCP client for baseURL, applying transport-level options followed by client-level options. It is the entry point for options that are not transport options (e.g. WithElicitationHandler); NewStreamableHttpClient remains for callers that only need transport options.
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
CallTool invokes a tool.
func (*Client) GetPrompt ¶
func (c *Client) GetPrompt(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error)
GetPrompt gets a prompt, rendered with the provided arguments.
func (*Client) GetSessionId ¶
GetSessionId returns the current MCP session ID, if any.
func (*Client) GetTransport ¶
GetTransport returns the transport handle. For a Streamable HTTP client the dynamic type is *transport.StreamableHTTP (as ToolHive expects); otherwise it is nil.
func (*Client) Initialize ¶
func (c *Client) Initialize(ctx context.Context, request mcp.InitializeRequest) (*mcp.InitializeResult, error)
Initialize connects the underlying go-sdk client and performs the MCP initialize handshake using the supplied client info and capabilities.
LIMITATION (issue #156, item U2): if a preset session ID was supplied via transport.WithSession, Initialize does NOT honor it. Initialize always negotiates a fresh, server-assigned session ID: it builds a go-sdk StreamableClientTransport (buildTransport) which has no preset session-ID field (go-sdk has no such field in any version, verified through v1.6.1), performs the initialize handshake, and then adopts the ID the server returns (overwriting any value set via WithSession). To resume a pre-existing session by ID, skip Initialize and use the resume path instead (see resume.go). The resume path is exercised by resume_test.go.
func (*Client) IsInitialized ¶
IsInitialized reports whether the client has completed initialization.
func (*Client) ListPrompts ¶
func (c *Client) ListPrompts(ctx context.Context, request mcp.ListPromptsRequest) (*mcp.ListPromptsResult, error)
ListPrompts lists the server's prompts.
func (*Client) ListResourceTemplates ¶
func (c *Client) ListResourceTemplates( ctx context.Context, request mcp.ListResourceTemplatesRequest, ) (*mcp.ListResourceTemplatesResult, error)
ListResourceTemplates lists the server's resource templates.
func (*Client) ListResources ¶
func (c *Client) ListResources(ctx context.Context, request mcp.ListResourcesRequest) (*mcp.ListResourcesResult, error)
ListResources lists the server's resources.
func (*Client) ListTools ¶
func (c *Client) ListTools(ctx context.Context, request mcp.ListToolsRequest) (*mcp.ListToolsResult, error)
ListTools lists the server's tools.
func (*Client) OnConnectionLost ¶
OnConnectionLost registers a handler invoked when the connection to the server is lost. It mirrors mcp-go's client.Client.OnConnectionLost, which is used to handle transport-level disconnections (e.g. an HTTP/2 idle timeout) that should not be treated as fatal errors.
The go-sdk signals a dropped connection by having ClientSession.Wait return. The handler is invoked once, with the error Wait reports (nil on a clean close). If registered before Initialize, the watch starts once the session connects; if registered after, it starts immediately.
func (*Client) OnNotification ¶
func (c *Client) OnNotification(handler func(notification mcp.JSONRPCNotification))
OnNotification registers a handler invoked for server-initiated notifications. Handlers must be registered before Initialize so they can be wired into the underlying go-sdk client. The go-sdk exposes typed notification handlers rather than a single catch-all, so this shim synthesizes JSONRPCNotification values (method + params) for each notification type and forwards each to every registered handler. The following notification types are forwarded:
- notifications/tools/list_changed
- notifications/prompts/list_changed
- notifications/resources/list_changed
- notifications/resources/updated (from resources/subscribe)
- notifications/elicitation/complete (out-of-band elicitation completion)
- notifications/progress (server->client progress)
- notifications/message (server->client logging)
Server-initiated notifications (including the list_changed notifications, which the server emits outside any in-flight request) are only delivered if the client enabled continuous listening via transport.WithContinuousListening(). Without it the go-sdk streamable transport has no standalone SSE stream to carry such notifications and they are silently dropped; no callback fires.
func (*Client) ReadResource ¶
func (c *Client) ReadResource(ctx context.Context, request mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error)
ReadResource reads a resource by URI.
func (*Client) SetLevel ¶ added in v0.0.28
SetLevel is a drop-in compatibility alias for mcp-go's client.Client.SetLevel, forwarding to SetLoggingLevel. It is provided so downstream code using the upstream idiom `c.SetLevel(ctx, mcp.SetLevelRequest{...})` compiles against the shim unchanged. SetLevelRequest and SetLevelParams are re-exported from the mcp package for the same reason.
func (*Client) SetLoggingLevel ¶ added in v0.0.28
SetLoggingLevel sets the server's logging level. This is a renamed and simplified counterpart to mcp-go's client.Client.SetLevel: rather than taking an mcp.SetLevelRequest, it accepts the mcp.LoggingLevel directly. The server only delivers notifications/message notifications at or above the requested level, so this must be called before the OnNotification handler will see logging notifications. level is one of the MCP logging levels ("debug", "info", "notice", "warning", "error", "critical", "alert", "emergency").
type ClientOption ¶ added in v0.0.28
type ClientOption func(*Client)
ClientOption configures a Client. It is applied at construction time via NewStreamableHttpClientWithOpts (the only constructor that accepts ClientOptions); NewStreamableHttpClient and NewSSEMCPClient take only transport-level options.
func WithElicitationHandler ¶ added in v0.0.28
func WithElicitationHandler(h ElicitationHandler) ClientOption
WithElicitationHandler installs a handler for server->client elicitation/create requests. The handler must be registered before Initialize so it is wired into the underlying go-sdk client, which (a) declares the elicitation capability at initialize time so the server's Elicit calls pass the go-sdk capability gate, and (b) dispatches incoming elicitation/create requests to the handler.
Elicitation under the Streamable HTTP transport requires the client to hold an open standalone SSE stream: the shim server replies with application/json (JSONResponse is on by design), so the go-sdk routes a server->client elicitation request made during request handling to the standalone SSE stream rather than the POST response. Configure the client with transport.WithContinuousListening() to open that stream; without it (DisableStandaloneSSE defaults to true) elicitation cannot be delivered and the server's Elicit call is rejected. See the build() doc comment in mcpcompat/server/transports.go.
It mirrors mcp-go's client.WithElicitationHandler.
type ElicitationHandler ¶ added in v0.0.28
type ElicitationHandler interface {
Elicit(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error)
}
ElicitationHandler handles server->client elicitation/create requests. It mirrors mcp-go's client.ElicitationHandler.
type ElicitationHandlerFunc ¶ added in v0.0.28
type ElicitationHandlerFunc func(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error)
ElicitationHandlerFunc is an adapter to allow the use of ordinary functions as elicitation handlers. It is a shim convenience adapter not present in mcp-go (mcp-go's client.WithElicitationHandler accepts a plain func), kept here so the shim's ElicitationHandler interface and the func form compose.
func (ElicitationHandlerFunc) Elicit ¶ added in v0.0.28
func (f ElicitationHandlerFunc) Elicit(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error)
Elicit calls f(ctx, request).