Documentation
¶
Overview ¶
Package clientHandler implements a high-level Go clientHandler for the Model Context Protocol (MCP).
It provides a thin wrapper around the protocol interface defined in the github.com/eberle1080/mcp-protocol module and adds:
- Automatic `server/discover` negotiation for 2026-07-28, with legacy `initialize` support when an older protocol is selected.
- 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 := clientHandler.New("demo", "1.0", sseTransport, clientHandler.WithCapabilities(schema.ClientCapabilities{}))
res, _ := cli.ListResources(ctx, nil)
fmt.Println(res.Resources)
Index ¶
- func DirectoryReadDeclared(capabilities *schema.ServerCapabilities) bool
- type Client
- func (c *Client) AwaitTask(ctx context.Context, task tasks.Task, options ...RequestOption) (*schema.CallToolResult, error)
- func (c *Client) CallTool(ctx context.Context, params *schema.CallToolRequestParams, ...) (*schema.CallToolResult, error)
- func (c *Client) CallToolTask(ctx context.Context, params *schema.CallToolRequestParams, ...) (*schema.CallToolResult, *tasks.Task, error)
- func (c *Client) CancelTask(ctx context.Context, taskID string, options ...RequestOption) error
- func (c *Client) Close()
- func (c *Client) Complete(ctx context.Context, params *schema.CompleteRequestParams, ...) (*schema.CompleteResult, error)
- func (c *Client) CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams, ...) (*schema.CreateMessageResult, error)
- func (c *Client) Discover(ctx context.Context, options ...RequestOption) (*schema.DiscoverResult, error)
- func (c *Client) Elicit(ctx context.Context, params *schema.ElicitRequestParams, ...) (*schema.ElicitResult, error)
- func (c *Client) GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams, ...) (*schema.GetPromptResult, error)
- func (c *Client) GetSkill(ctx context.Context, uri string, options ...RequestOption) (*schema.GetSkillResult, error)
- func (c *Client) GetTask(ctx context.Context, taskID string, options ...RequestOption) (*tasks.Task, error)
- func (c *Client) Initialize(ctx context.Context, options ...RequestOption) (*schema.InitializeResult, error)
- func (c *Client) ListPrompts(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListPromptsResult, error)
- func (c *Client) ListResourceTemplates(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourceTemplatesResult, error)
- func (c *Client) ListResources(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourcesResult, error)
- func (c *Client) ListResourcesWithParams(ctx context.Context, params *schema.ListResourcesRequestParams, ...) (*schema.ListResourcesResult, error)
- func (c *Client) ListRoots(ctx context.Context, params *schema.ListRootsRequestParams, ...) (*schema.ListRootsResult, error)
- func (c *Client) ListSkills(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListSkillsResult, error)
- func (c *Client) ListTools(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListToolsResult, error)
- func (c *Client) Listen(ctx context.Context, filter schema.SubscriptionFilter, ...) (*schema.SubscriptionsListenResult, error)
- func (c *Client) NegotiatedServer() (*schema.InitializeResult, error)
- func (c *Client) Ping(ctx context.Context, params *schema.PingRequestParams, ...) (*schema.PingResult, error)
- func (c *Client) ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams, ...) (*schema.ReadResourceResult, error)
- func (c *Client) ReadResourceDirectory(ctx context.Context, uri string, cursor *string, options ...RequestOption) (*schema.ReadResourceDirectoryResult, error)
- func (c *Client) SetLevel(ctx context.Context, params *schema.SetLevelRequestParams, ...) (*schema.SetLevelResult, error)
- func (c *Client) Subscribe(ctx context.Context, params *schema.SubscribeRequestParams, ...) (*schema.SubscribeResult, error)
- func (c *Client) Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams, ...) (*schema.UnsubscribeResult, error)
- func (c *Client) UpdateTask(ctx context.Context, taskID string, responses map[string]json.RawMessage, ...) error
- type DiscoveryInterface
- type Handler
- func (h *Handler) CreateMessageRequest(ctx context.Context, request *jsonrpc.Request) (*schema.CreateMessageResult, *jsonrpc.Error)
- func (h *Handler) Elicit(ctx context.Context, request *jsonrpc.Request) (*schema.ElicitResult, *jsonrpc.Error)
- func (h *Handler) ListRoots(ctx context.Context, request *jsonrpc.Request) (*schema.ListRootsResult, *jsonrpc.Error)
- func (s *Handler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
- func (h *Handler) Serve(ctx context.Context, request *jsonrpc.Request, response *jsonrpc.Response)
- type Interface
- type Option
- func WithAuthInterceptor(authorizer *auth.Authorizer) Option
- func WithCapabilities(capabilities schema.ClientCapabilities) Option
- func WithClientHandler(handler pclient.Handler) Option
- func WithExtension(id string, settings map[string]interface{}) Option
- func WithMetadata(metadata map[string]any) Option
- func WithPingInterval(interval time.Duration) Option
- func WithProtocolVersion(version string) Option
- func WithReconnect(reconnect func(ctx context.Context) (transport.Transport, error)) Option
- type RequestOption
- type RequestOptions
- type ResourceListInterface
- type SubscriptionInterface
- type TaskError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DirectoryReadDeclared ¶
func DirectoryReadDeclared(capabilities *schema.ServerCapabilities) bool
DirectoryReadDeclared reports whether server capabilities declare resources/directory/read: the skills extension with directoryRead: true.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) AwaitTask ¶
func (c *Client) AwaitTask(ctx context.Context, task tasks.Task, options ...RequestOption) (*schema.CallToolResult, error)
AwaitTask polls a task until it ends and returns its result. It honours the server's poll interval. It answers the task's input requests (elicitation, sampling, roots) with this client's handler, as it would the same requests sent directly, and answers each one once. If ctx ends first, it asks the server to cancel the task. A failed or cancelled task is a *TaskError.
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, params *schema.CallToolRequestParams, options ...RequestOption) (*schema.CallToolResult, error)
CallTool calls a tool and returns its result. If the client declared the Tasks extension and the server answers with a task, CallTool waits for the task (see AwaitTask) and returns its result, so callers need not know a task was involved. CallToolTask returns the task instead.
func (*Client) CallToolTask ¶
func (c *Client) CallToolTask(ctx context.Context, params *schema.CallToolRequestParams, options ...RequestOption) (*schema.CallToolResult, *tasks.Task, error)
CallToolTask calls a tool and returns either its result or, when the server answered with a task (the Tasks extension, MCP 2026-07-28), the task. A client that declared the extension with WithExtension(tasks.ExtensionID, nil) may get either.
func (*Client) CancelTask ¶
CancelTask asks the server to stop a task. Cancellation is cooperative: the task may still finish another way.
func (*Client) Close ¶
func (c *Client) Close()
Close stops background routines (like pinger) and closes the underlying transport when it implements io.Closer-like semantics. Closing the transport is required for HTTP streaming transports whose long-lived SSE goroutines would otherwise leak (see github.com/eberle1080/jsonrpc streamable Client.Close).
func (*Client) Complete ¶
func (c *Client) Complete(ctx context.Context, params *schema.CompleteRequestParams, options ...RequestOption) (*schema.CompleteResult, error)
func (*Client) CreateMessage ¶
func (c *Client) CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams, options ...RequestOption) (*schema.CreateMessageResult, error)
func (*Client) Discover ¶
func (c *Client) Discover(ctx context.Context, options ...RequestOption) (*schema.DiscoverResult, error)
Discover performs the stateless 2026-07-28 server discovery operation. Legacy clients should continue to use Initialize with an explicit legacy protocol version.
func (*Client) Elicit ¶
func (c *Client) Elicit(ctx context.Context, params *schema.ElicitRequestParams, options ...RequestOption) (*schema.ElicitResult, error)
func (*Client) GetPrompt ¶
func (c *Client) GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams, options ...RequestOption) (*schema.GetPromptResult, error)
func (*Client) GetSkill ¶
func (c *Client) GetSkill(ctx context.Context, uri string, options ...RequestOption) (*schema.GetSkillResult, error)
func (*Client) GetTask ¶
func (c *Client) GetTask(ctx context.Context, taskID string, options ...RequestOption) (*tasks.Task, error)
GetTask returns a task's current state.
func (*Client) Initialize ¶
func (c *Client) Initialize(ctx context.Context, options ...RequestOption) (*schema.InitializeResult, error)
func (*Client) ListPrompts ¶
func (c *Client) ListPrompts(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListPromptsResult, error)
func (*Client) ListResourceTemplates ¶
func (c *Client) ListResourceTemplates(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourceTemplatesResult, error)
func (*Client) ListResources ¶
func (c *Client) ListResources(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourcesResult, error)
func (*Client) ListResourcesWithParams ¶
func (c *Client) ListResourcesWithParams(ctx context.Context, params *schema.ListResourcesRequestParams, options ...RequestOption) (*schema.ListResourcesResult, error)
ListResourcesWithParams lists resources with caller-supplied pagination and metadata. Metadata extensions are opaque to the client. Like ListResources, this method uses the client's standard authentication and protocol handling. It does not modify params.
func (*Client) ListRoots ¶
func (c *Client) ListRoots(ctx context.Context, params *schema.ListRootsRequestParams, options ...RequestOption) (*schema.ListRootsResult, error)
func (*Client) ListSkills ¶
func (c *Client) ListSkills(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListSkillsResult, error)
ListSkills retrieves metadata only. It never prefetches skill content.
func (*Client) ListTools ¶
func (c *Client) ListTools(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListToolsResult, error)
func (*Client) Listen ¶
func (c *Client) Listen(ctx context.Context, filter schema.SubscriptionFilter, options ...RequestOption) (*schema.SubscriptionsListenResult, error)
Listen opens a July long-lived notification stream. The caller owns the context and closes the subscription by cancelling it.
func (*Client) NegotiatedServer ¶
func (c *Client) NegotiatedServer() (*schema.InitializeResult, error)
NegotiatedServer returns detached capability/protocol state from the completed handshake. It performs no discovery or metadata RPC.
func (*Client) Ping ¶
func (c *Client) Ping(ctx context.Context, params *schema.PingRequestParams, options ...RequestOption) (*schema.PingResult, error)
func (*Client) ReadResource ¶
func (c *Client) ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams, options ...RequestOption) (*schema.ReadResourceResult, error)
func (*Client) ReadResourceDirectory ¶
func (c *Client) ReadResourceDirectory(ctx context.Context, uri string, cursor *string, options ...RequestOption) (*schema.ReadResourceDirectoryResult, error)
ReadResourceDirectory lists a directory resource's direct children with SEP-2640's resources/directory/read. The extension forbids calling it on a server that has not declared directoryRead, so without that declaration the call fails here and nothing is sent.
func (*Client) SetLevel ¶
func (c *Client) SetLevel(ctx context.Context, params *schema.SetLevelRequestParams, options ...RequestOption) (*schema.SetLevelResult, error)
func (*Client) Subscribe ¶
func (c *Client) Subscribe(ctx context.Context, params *schema.SubscribeRequestParams, options ...RequestOption) (*schema.SubscribeResult, error)
func (*Client) Unsubscribe ¶
func (c *Client) Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams, options ...RequestOption) (*schema.UnsubscribeResult, error)
func (*Client) UpdateTask ¶
func (c *Client) UpdateTask(ctx context.Context, taskID string, responses map[string]json.RawMessage, options ...RequestOption) error
UpdateTask answers some of a task's input requests, keyed as the task listed them.
type DiscoveryInterface ¶
type DiscoveryInterface interface {
Discover(ctx context.Context, options ...RequestOption) (*schema.DiscoverResult, error)
}
DiscoveryInterface is implemented by July-capable clients without widening the legacy Interface contract.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
NewHandler create clientHandler clientHandler
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) Elicit ¶
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)
type Interface ¶
type Interface interface {
// Initialize initializes the clientHandler
Initialize(ctx context.Context, options ...RequestOption) (*schema.InitializeResult, error)
// ListResourceTemplates lists resource templates
ListResourceTemplates(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourceTemplatesResult, error)
// ListResources lists resources
ListResources(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListResourcesResult, error)
// ListPrompts lists prompts
ListPrompts(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListPromptsResult, error)
// ListTools lists tools
ListTools(ctx context.Context, cursor *string, options ...RequestOption) (*schema.ListToolsResult, error)
// ReadResource reads a resource
ReadResource(ctx context.Context, params *schema.ReadResourceRequestParams, options ...RequestOption) (*schema.ReadResourceResult, error)
// GetPrompt gets a prompt
GetPrompt(ctx context.Context, params *schema.GetPromptRequestParams, options ...RequestOption) (*schema.GetPromptResult, error)
// CallTool calls a tool
CallTool(ctx context.Context, params *schema.CallToolRequestParams, options ...RequestOption) (*schema.CallToolResult, error)
// Complete completes a request
Complete(ctx context.Context, params *schema.CompleteRequestParams, options ...RequestOption) (*schema.CompleteResult, error)
// Ping pings the server
Ping(ctx context.Context, params *schema.PingRequestParams, options ...RequestOption) (*schema.PingResult, error)
// Subscribe subscribes to a resource
Subscribe(ctx context.Context, params *schema.SubscribeRequestParams, options ...RequestOption) (*schema.SubscribeResult, error)
// Unsubscribe unsubscribes from a resource
Unsubscribe(ctx context.Context, params *schema.UnsubscribeRequestParams, options ...RequestOption) (*schema.UnsubscribeResult, error)
// SetLevel sets the logging level
SetLevel(ctx context.Context, params *schema.SetLevelRequestParams, options ...RequestOption) (*schema.SetLevelResult, error)
// ListRoots lists clientHandler roots (clientHandler side capability discovery)
ListRoots(ctx context.Context, params *schema.ListRootsRequestParams, options ...RequestOption) (*schema.ListRootsResult, error)
// CreateMessage creates a sampling message on the clientHandler side
CreateMessage(ctx context.Context, params *schema.CreateMessageRequestParams, options ...RequestOption) (*schema.CreateMessageResult, error)
// Elicit is a server-initiated request asking the clientHandler to elicit additional information from the end-user
Elicit(ctx context.Context, params *schema.ElicitRequestParams, options ...RequestOption) (*schema.ElicitResult, error)
}
Interface defines the clientHandler 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 clientHandler, 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 WithClientHandler ¶
WithClientHandler with clientHandler
func WithExtension ¶
WithExtension declares support for a protocol extension, such as MCP Apps (apps.ExtensionID with apps.ClientSettings()). The declaration is sent with initialize, and with every request in 2026-07-28.
func WithPingInterval ¶
WithPingInterval enables a background pinger with the specified interval. Set to a positive duration (e.g., 30*time.Second) to keep sessions warm and detect/recover from transport issues. Zero or negative disables pinger.
func WithProtocolVersion ¶
func WithReconnect ¶
WithReconnect sets reconnect function that can rebuild transport and perform re-initialization. It is used internally to automatically recover from transport-level errors like expired sessions. External callers typically do not need to set this option directly – it is configured by the mcp.NewClient helper that builds an MCP client from ClientOptions.
type RequestOption ¶
type RequestOption func(*RequestOptions)
func WithAuthToken ¶
func WithAuthToken(token string) RequestOption
func WithJsonRpcRequestId ¶
func WithJsonRpcRequestId(requestId jsonrpc.RequestId) RequestOption
func WithJsonRpcVersion ¶
func WithJsonRpcVersion(version string) RequestOption
func WithNoRetry ¶
func WithNoRetry() RequestOption
WithNoRetry suppresses automatic remote request replay for this request only.
type RequestOptions ¶
type RequestOptions struct {
NoRetry bool
RequestId jsonrpc.RequestId
JsonrpcVersion string
StringToken string
}
func NewRequestOptions ¶
func NewRequestOptions(options []RequestOption) *RequestOptions
type ResourceListInterface ¶
type ResourceListInterface interface {
ListResourcesWithParams(ctx context.Context, params *schema.ListResourcesRequestParams, options ...RequestOption) (*schema.ListResourcesResult, error)
}
ResourceListInterface is an optional extension for callers that need to pass request metadata when listing resources. Interface remains backward compatible.
type SubscriptionInterface ¶
type SubscriptionInterface interface {
Listen(ctx context.Context, filter schema.SubscriptionFilter, options ...RequestOption) (*schema.SubscriptionsListenResult, error)
}
SubscriptionInterface is the July long-lived notification stream API.
type TaskError ¶
type TaskError struct {
TaskID string
Status tasks.Status
// Message is the task's status message.
Message string
// Err is the JSON-RPC error a failed task carries.
Err *jsonrpc.Error
}
TaskError is a task that ended without a result: it failed with a JSON-RPC error (Err is set) or was cancelled.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apphost is the Go side of a host that renders MCP Apps (io.modelcontextprotocol/ui, SEP-1865): the part that talks to the MCP server.
|
Package apphost is the Go side of a host that renders MCP Apps (io.modelcontextprotocol/ui, SEP-1865): the part that talks to the MCP server. |
|
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. |
|
config
Package config defines host-neutral OAuth configuration, requirement and credential types shared by MCP clients.
|
Package config defines host-neutral OAuth configuration, requirement and credential types shared by MCP clients. |
|
link
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core.
|
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core. |
|
link/provider
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core.
|
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core. |
|
link/providerregistry
Package providerregistry implements the workspace OAuth provider registry.
|
Package providerregistry implements the workspace OAuth provider registry. |
|
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`. |
|
Package catalog provides immutable, indexed MCP tool metadata.
|
Package catalog provides immutable, indexed MCP tool metadata. |
|
index
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core; provider bindings are independent of agent execution.
|
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core; provider bindings are independent of agent execution. |
|
SPDX-License-Identifier: Apache-2.0 MCP client configuration extracted from Viant Agently Core.
|
SPDX-License-Identifier: Apache-2.0 MCP client configuration extracted from Viant Agently Core. |
|
async
SPDX-License-Identifier: Apache-2.0 MCP async tool configuration extracted from Viant Agently Core.
|
SPDX-License-Identifier: Apache-2.0 MCP async tool configuration extracted from Viant Agently Core. |
|
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core; provider bindings are independent of agent execution.
|
SPDX-License-Identifier: Apache-2.0 Extracted from Viant Agently Core; provider bindings are independent of agent execution. |
|
SPDX-License-Identifier: Apache-2.0 Forge resource client pooling extracted from Viant Agently Core.
|
SPDX-License-Identifier: Apache-2.0 Forge resource client pooling extracted from Viant Agently Core. |
|
Package skillhost loads skills that an MCP server serves (the io.modelcontextprotocol/skills extension, SEP-2640) and enforces what the extension requires of a host when it reads them:
|
Package skillhost loads skills that an MCP server serves (the io.modelcontextprotocol/skills extension, SEP-2640) and enforces what the extension requires of a host when it reads them: |