Documentation
¶
Index ¶
- Constants
- Variables
- func CancelTask(ctx context.Context, c *Client, taskID string) error
- func CancelTaskV1(ctx context.Context, c *Client, taskID string) (*core.CancelTaskResultV1, error)
- func CardRemote(card *core.ServerCard, mcpURL string) (*core.Remote, bool)
- func DoWithAuthRetry(ts core.TokenSource, buildReq func() (*http.Request, error), ...) (*http.Response, error)
- func ExtractMethodFromJSON(data []byte) string
- func FetchServerCard(ctx context.Context, mcpURL string, opts ...CardFetchOption) (*core.ServerCard, error)
- func FileInputsFromTool(tool core.ToolDef) map[string]core.FileInputDescriptor
- func GetTask(ctx context.Context, c *Client, taskID string) (*core.DetailedTask, error)
- func GetTaskPayloadV1(ctx context.Context, c *Client, taskID string) (*core.ToolResult, string, error)
- func GetTaskV1(ctx context.Context, c *Client, taskID string) (*core.GetTaskResultV1, error)
- func IsToolTaskV1(tool core.ToolDef) bool
- func IsTransientError(err error) bool
- func ListTasksV1(ctx context.Context, c *Client, cursor string) (*core.ListTasksResultV1, error)
- func PrepareFileArg(path string, desc *core.FileInputDescriptor) (string, error)
- func ResolveEndpointURL(baseSSEURL, endpointRef string) (string, error)
- func SetDefaultClientMode(m ClientMode)
- func ToolCallAsTaskV1(ctx context.Context, c *Client, name string, args any, ...) (*core.CreateTaskResultV1, error)
- func ToolCallTyped[T any](ctx context.Context, c *Client, name string, args any) (T, error)
- func UpdateTask(ctx context.Context, c *Client, req core.UpdateTaskRequest) error
- func WaitForTask(ctx context.Context, c *Client, taskID string, opts ...WaitOptions) (*core.DetailedTask, error)
- func WaitForTaskV1(ctx context.Context, c *Client, taskID string, pollInterval time.Duration) (*core.GetTaskResultV1, error)
- func WaitForTaskWithInput(ctx context.Context, c *Client, taskID string, handler InputHandler, ...) (*core.DetailedTask, error)
- type BackgroundTask
- type CallContext
- type CallOption
- type CallResult
- type CardFetchOption
- type Client
- func (c *Client) Call(ctx context.Context, method string, params any) (*CallResult, error)
- func (c *Client) CallContext(ctx context.Context, cc *CallContext, method string, params any) (*CallResult, error)
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Discover() (*DiscoverResult, error)
- func (c *Client) GetPromptWith(ctx context.Context, name string, args map[string]any, opts ...CallOption) (*core.PromptResult, error)
- func (c *Client) HandleServerRequest(req *core.Request) *core.Response
- func (c *Client) HandleServerRequestWithContext(ctx context.Context, req *core.Request) *core.Response
- func (c *Client) ListPrompts(ctx context.Context) ([]core.PromptDef, error)
- func (c *Client) ListPromptsPage(ctx context.Context, cursor string) (*core.PromptsListResult, error)
- func (c *Client) ListResourceTemplates(ctx context.Context) ([]core.ResourceTemplate, error)
- func (c *Client) ListResourceTemplatesPage(ctx context.Context, cursor string) (*core.ResourceTemplatesListResult, error)
- func (c *Client) ListResources(ctx context.Context) ([]core.ResourceDef, error)
- func (c *Client) ListResourcesPage(ctx context.Context, cursor string) (*core.ResourcesListResult, error)
- func (c *Client) ListTools(ctx context.Context) ([]core.ToolDef, error)
- func (c *Client) ListToolsForModel(ctx context.Context) ([]core.ToolDef, error)
- func (c *Client) ListToolsPage(ctx context.Context, cursor string) (*core.ToolsListResult, error)
- func (c *Client) Listen(ctx context.Context, opts ListenOptions) (*Listener, error)
- func (c *Client) NotifyRootsChanged(ctx context.Context) errordeprecated
- func (c *Client) Prompts(ctx context.Context) iter.Seq2[core.PromptDef, error]
- func (c *Client) ReadResource(ctx context.Context, uri string) (string, error)
- func (c *Client) ReadResourceFull(ctx context.Context, uri string) (*core.ResourceResult, error)
- func (c *Client) ReadResourceWith(ctx context.Context, uri string, opts ...CallOption) (*core.ResourceResult, error)
- func (c *Client) ResourceTemplates(ctx context.Context) iter.Seq2[core.ResourceTemplate, error]
- func (c *Client) Resources(ctx context.Context) iter.Seq2[core.ResourceDef, error]
- func (c *Client) ServerExtensionCapability(id string) (core.ExtensionCapability, bool)
- func (c *Client) ServerSupportsExtension(id string) bool
- func (c *Client) ServerSupportsUI() bool
- func (c *Client) SessionID() string
- func (c *Client) SetLogLevel(ctx context.Context, level string) error
- func (c *Client) SetTransport(t core.Transport)
- func (c *Client) SetURL(url string)
- func (c *Client) SubscribeResource(ctx context.Context, uri string) error
- func (c *Client) ToolCall(ctx context.Context, name string, args any) (string, error)
- func (c *Client) ToolCallFull(ctx context.Context, name string, args any) (*core.ToolResult, error)
- func (c *Client) ToolCallWith(ctx context.Context, name string, args any, opts ...CallOption) (*core.ToolResult, error)
- func (c *Client) Tools(ctx context.Context) iter.Seq2[core.ToolDef, error]
- func (c *Client) URL() string
- func (c *Client) UnsubscribeResource(ctx context.Context, uri string) error
- func (c *Client) UsingStatelessWire() bool
- type ClientAuthError
- type ClientCallFunc
- type ClientMiddleware
- type ClientMode
- type ClientOption
- func WithClientBearerToken(token string) ClientOption
- func WithClientKeepalive(interval time.Duration, maxFailures int) ClientOption
- func WithClientLogging(logger *log.Logger) ClientOption
- func WithClientMiddleware(mw ...ClientMiddleware) ClientOption
- func WithClientMode(m ClientMode) ClientOption
- func WithCommandTransport(name string, args []string, opts ...CommandOption) ClientOption
- func WithConnectTimeout(d time.Duration) ClientOption
- func WithContentChunkHandler(fn func(chunk core.ContentChunk)) ClientOption
- func WithElicitationCompleteHandler(h ElicitationCompleteHandler) ClientOption
- func WithElicitationHandler(h ElicitationHandler) ClientOption
- func WithElicitationURLSupport() ClientOption
- func WithElicitationValidation(enabled bool) ClientOption
- func WithExtension(id string, cap core.ClientExtensionCap) ClientOption
- func WithFileInputs() ClientOption
- func WithGetSSEStream() ClientOption
- func WithIOTransport(r io.Reader, w io.Writer, opts ...StdioTransportOption) ClientOption
- func WithInspectResponse(fn func(*http.Response)) ClientOption
- func WithMaxListPages(n int) ClientOption
- func WithMaxRetries(n int) ClientOption
- func WithModifyRequest(fn func(*http.Request)) ClientOption
- func WithNotificationCallback(fn func(method string, params any)) ClientOption
- func WithReconnectBackoff(d time.Duration) ClientOption
- func WithReconnectWithoutReplay() ClientOption
- func WithRootsHandler(h RootsHandler) ClientOptiondeprecated
- func WithSSEClient() ClientOption
- func WithSamplingHandler(h SamplingHandler) ClientOptiondeprecated
- func WithStdioTransport(r io.Reader, w io.Writer, opts ...StdioTransportOption) ClientOption
- func WithTasksExtension() ClientOption
- func WithTokenSource(ts core.TokenSource) ClientOption
- func WithToolsListChangedHandler(fn func()) ClientOption
- func WithTracerProvider(tp core.TracerProvider) ClientOption
- func WithTransport(t core.Transport) ClientOption
- func WithUIExtension() ClientOption
- type CommandOption
- type CommandTransport
- func (ct *CommandTransport) Call(ctx context.Context, req *core.Request) (*core.Response, error)
- func (ct *CommandTransport) Close() error
- func (ct *CommandTransport) Connect(ctx context.Context) error
- func (ct *CommandTransport) Notify(ctx context.Context, req *core.Request) error
- func (ct *CommandTransport) SessionID() string
- func (ct *CommandTransport) Stderr() string
- type ConnState
- type DiscoverResult
- type ElicitationCompleteHandler
- type ElicitationHandler
- type Group
- func (g *Group) Add(id string, c *Client, required bool)
- func (g *Group) Close() error
- func (g *Group) Reconnect(id string)
- func (g *Group) Start(ctx context.Context)
- func (g *Group) State(id string) (ConnState, bool)
- func (g *Group) Status() []MemberStatus
- func (g *Group) WaitRequired(ctx context.Context) error
- func (g *Group) WaitSettled(ctx context.Context) error
- type GroupOption
- type HTTPStatusError
- type InputHandler
- type ListenOptions
- type Listener
- type MRTROption
- type MemberStatus
- type RPCError
- type RootsHandlerdeprecated
- type SamplingHandlerdeprecated
- type ServerCardCache
- type StateChange
- type StdioFraming
- type StdioTransport
- func (t *StdioTransport) Call(ctx context.Context, req *core.Request) (*core.Response, error)
- func (t *StdioTransport) Close() error
- func (t *StdioTransport) Connect(ctx context.Context) error
- func (t *StdioTransport) Notify(ctx context.Context, req *core.Request) error
- func (t *StdioTransport) SessionID() string
- type StdioTransportOption
- type TaskCallOptionsV1
- type ToolCallResult
- type UnsupportedDiscoverError
- type WaitOptions
Examples ¶
Constants ¶
const ( // FramingNewlineDelimited, the default, is the MCP spec framing: one JSON // message per line. Every spec-following stdio server expects it. FramingNewlineDelimited = stdioframe.NewlineDelimited // FramingContentLength is LSP-style "Content-Length: N\r\n\r\n" framing. // Use it to talk to an mcpkit server built before #1517, which accepts // nothing else. Current mcpkit servers accept both. FramingContentLength = stdioframe.ContentLength )
const ClientModeEnvVar = "MCPKIT_CLIENT_MODE"
ClientModeEnvVar names the override-via-environment knob.
const DefaultTaskGrace = 10 * time.Second
DefaultTaskGrace is the recommended grace window for callers that opt into background detach via WaitForTaskOrBackground.
Variables ¶
var ( // ErrCardInsecure means the card URL is not https and // WithInsecureCardFetch was not passed. Security-relevant extension // info must come from a card fetched over HTTPS. ErrCardInsecure = errors.New("mcpkit: server card must be fetched over https") // ErrCardCrossOrigin means the card endpoint redirected to another // origin. A card reached that way must not be used. ErrCardCrossOrigin = errors.New("mcpkit: server card redirected to another origin") )
Errors returned by FetchServerCard and ServerCardCache.Fetch. Wrap-check them with errors.Is.
var DefaultClientMode = ClientModeLegacyOnly
DefaultClientMode is the wire mode used when no WithClientMode option is passed and MCPKIT_CLIENT_MODE is unset.
Default: ClientModeLegacyOnly — preserves pre-SEP-2575 behavior so existing client code keeps working on upgrade. Opting into Adaptive (the recommended migration path) is one of:
client.NewClient(url, info, client.WithClientMode(client.ClientModeAdaptive))
export MCPKIT_CLIENT_MODE=adaptive
func init() { client.DefaultClientMode = client.ClientModeAdaptive }
The shipping default may flip to Adaptive in a future major release once the stateless wire is in widespread use. Until then, an extra server/discover probe on every Connect would be a silent latency regression for clients talking only to legacy servers.
Tests use SetDefaultClientMode to flip under write-lock for safe concurrent test execution.
var ErrListPaginationOverrun = errors.New("client: list pagination exceeded max pages")
ErrListPaginationOverrun is yielded by every paginated list iterator and returned by every zero-arg list helper (ListTools, ListResources, ListResourceTemplates, ListPrompts) when the per-client max page count is exceeded.
The default cap is 1000 pages; configure via WithMaxListPages. A cap of 0 disables the check (unbounded iteration — at your own risk).
This signals a likely-buggy server emitting an unbounded nextCursor, not a transient transport error. Callers should NOT retry blindly. The pages already yielded before the cap fired are safe to consume; what is uncertain is whether more pages legitimately existed beyond the cap or the server was wedged.
var ErrListenNeedsStatelessWire = errors.New("subscriptions/listen needs the 2026-07-28 stateless wire")
ErrListenNeedsStatelessWire is returned by Listen on a client that connected over the legacy session wire. That wire has no subscriptions/listen; its notifications arrive on the GET stream and reach WithNotificationCallback instead.
var ErrMRTRMaxRounds = errors.New("MRTR max rounds exceeded")
ErrMRTRMaxRounds is returned by CallToolWithInputs when the server keeps asking for more input past the configured round cap. Wrap-able via errors.Is.
Functions ¶
func CancelTask ¶ added in v0.2.41
CancelTask cancels a running task. Returns nil on success — the server response is an empty ack per SEP-2663 (no task state). Issue GetTask if you need to observe the resulting "cancelled" status.
func CancelTaskV1 ¶ added in v0.2.41
CancelTaskV1 cancels a running v1 task. Returns an error if the task is already in a terminal state. Per MCP spec 2025-11-25 §tasks/cancel: returns flat Result & Task. (V2 returns an empty ack; the v2 helper signature reflects that.)
func CardRemote ¶ added in v0.8.22
CardRemote returns the remote on card whose url is mcpURL, ignoring a trailing slash. A server can list several remotes, and extensionsInfo is per remote, so a caller reads the entry for the endpoint it connects to.
func DoWithAuthRetry ¶
func DoWithAuthRetry( ts core.TokenSource, buildReq func() (*http.Request, error), do func(*http.Request) (*http.Response, error), ) (*http.Response, error)
DoWithAuthRetry executes an HTTP request with automatic retry on 401/403. Wraps core.TokenSource into servicekit's callback-based auth retry.
On 401: calls ts.Token() to refresh, retries once. On 403: parses WWW-Authenticate for required scopes, calls core.ScopeAwareTokenSource.TokenForScopes if available, retries once.
Lazy token sources (those returning core.ErrNoTokenYet from Token) are supported: the first request is sent WITHOUT an Authorization header so the server's 401 can select the scope, and the OnUnauthorized path below arms the source via TokenForScopes for the retry (RFC 6750 §3.1).
func ExtractMethodFromJSON ¶
extractMethodFromJSON extracts the "method" field from a JSON-RPC envelope without full deserialization. Returns "<unknown>" if extraction fails.
func FetchServerCard ¶ added in v0.8.22
func FetchServerCard(ctx context.Context, mcpURL string, opts ...CardFetchOption) (*core.ServerCard, error)
FetchServerCard fetches the SEP-2127 Server Card for the MCP endpoint at mcpURL, from <mcpURL>/server-card. It refuses a non-https URL (unless WithInsecureCardFetch) and any redirect that leaves mcpURL's origin, so the card it returns came from the origin the client connects to. That is the condition the Server Card spec sets for trusting security-relevant extensionsInfo, such as webhook signing keys.
It decodes the card but does not run ServerCard.Validate, so a card with, say, a long description still reaches a caller that only needs one remote. Use a ServerCardCache to revalidate with ETags across calls.
func FileInputsFromTool ¶ added in v0.2.44
func FileInputsFromTool(tool core.ToolDef) map[string]core.FileInputDescriptor
FileInputsFromTool returns a map of `propertyPath -> descriptor` for every file-input property in the tool's inputSchema. Handles both the single-file shape (`properties.<name>.x-mcp-file`) and the array shape (`properties.<name>.items.x-mcp-file`).
Property paths use bracket notation for arrays so callers can distinguish the two shapes:
upload_image → {"image": <desc>}
analyze_documents → {"documents[]": <desc>}
Returns an empty map if no file-input properties are declared (or the schema isn't a `map[string]any`-shaped object).
func GetTask ¶ added in v0.2.41
GetTask fetches the current state of a v2 task as a DetailedTask, with inlined result / error / inputRequests depending on status. Idempotent — safe to call as often as needed; servers gate this method on the io.modelcontextprotocol/tasks extension being negotiated.
func GetTaskPayloadV1 ¶ added in v0.2.41
func GetTaskPayloadV1(ctx context.Context, c *Client, taskID string) (*core.ToolResult, string, error)
GetTaskPayloadV1 fetches the result payload for a v1 task. Blocks until the task reaches a terminal state via the tasks/result long-poll. Per MCP spec 2025-11-25 §tasks/result: returns the original ToolResult with _meta["io.modelcontextprotocol/related-task"]. (V2 removed tasks/result — DetailedTask inlines the result instead.)
func GetTaskV1 ¶ added in v0.2.41
GetTaskV1 polls the status of a v1 task by ID. Non-blocking. Per MCP spec 2025-11-25 §tasks/get: returns flat Result & Task.
func IsToolTaskV1 ¶ added in v0.2.41
IsToolTaskV1 checks whether a tool supports task invocation based on its Execution.TaskSupport field. Returns true for "required" or "optional". Per MCP spec 2025-11-25: absent Execution = forbidden. (V2 servers decide task creation unilaterally — there is no equivalent client-side check.)
Use with ListTools to decide whether to call ToolCallAsTaskV1 or ToolCall:
tools, _ := c.ListTools()
for _, t := range tools {
if client.IsToolTaskV1(t) {
client.ToolCallAsTaskV1(c, t.Name, args)
} else {
c.ToolCall(t.Name, args)
}
}
func IsTransientError ¶
isTransientError returns true if the error indicates a recoverable transport failure that may succeed on reconnection. Network errors (EOF, connection reset, refused) are transient. Auth errors (401/403) and JSON-RPC errors are NOT transient — the server responded, just said no.
func ListTasksV1 ¶ added in v0.2.41
ListTasksV1 returns all v1 tasks with cursor-based pagination. Pass an empty cursor to start from the beginning. (Removed in v2 — tasks/list is no longer part of the protocol.)
func PrepareFileArg ¶ added in v0.2.44
func PrepareFileArg(path string, desc *core.FileInputDescriptor) (string, error)
PrepareFileArg reads a file from disk, validates it against the descriptor (size + MIME / extension), and returns an RFC 2397 base64 data URI suitable for use as a tool argument.
MIME detection follows two rules in order:
- mime.TypeByExtension on the filename — fast and deterministic.
- Fallback to http.DetectContentType on the first 512 bytes — used when the extension is unknown.
The detected media type is what the validator checks against `descriptor.Accept`; the SAME type is embedded in the resulting data URI so the server's decoder reads back what we sent.
A nil descriptor skips validation but still encodes — useful for the unconstrained `process_any_file`-style tool where any file is allowed.
Returns the validator's typed error (`*core.FileTooLargeError`, `*core.FileTypeNotAcceptedError`) on failure so callers can branch with `errors.As` and render rich error UX.
func ResolveEndpointURL ¶
ResolveEndpointURL resolves an SSE endpoint event URL against the base SSE connection URL per RFC 3986. Delegates to servicekit's ResolveURL.
func SetDefaultClientMode ¶ added in v0.2.47
func SetDefaultClientMode(m ClientMode)
SetDefaultClientMode mutates DefaultClientMode under write-lock. Prefer this over direct assignment in tests so ResolveClientMode's read-lock sees a consistent value.
func ToolCallAsTaskV1 ¶ added in v0.2.41
func ToolCallAsTaskV1(ctx context.Context, c *Client, name string, args any, opts ...*TaskCallOptionsV1) (*core.CreateTaskResultV1, error)
ToolCallAsTaskV1 invokes a v1 tool with a task hint, returning a CreateTaskResultV1 instead of the immediate tool result. The server creates a task and runs the tool asynchronously.
Pass nil for opts to use server defaults. Per MCP spec 2025-11-25: task hint at params.task, progressToken at params._meta.progressToken. (V2 removes the client task hint — the server decides unilaterally — so there is no V2 equivalent to this helper.)
func ToolCallTyped ¶ added in v0.1.13
ToolCallTyped invokes a tool and unmarshals the structured content into T. This is for tools that declare an OutputSchema and return StructuredContent. Returns an error if the tool has no structured content or if unmarshaling fails.
Example:
type SearchResult struct {
Results []string `json:"results"`
Total int `json:"total"`
}
result, err := client.ToolCallTyped[SearchResult](c, "search", map[string]any{"query": "test"})
func UpdateTask ¶ added in v0.2.41
UpdateTask resumes a task parked in input_required by delivering the inputResponses the server's pending inputRequests are waiting on. The server matches keys, hands the payloads to the waiting goroutine, and the task transitions back to working (or directly to a terminal state if the tool finishes immediately after).
Returns nil on success — the server response is an empty ack per SEP-2663. Observe the resulting state via the next GetTask poll.
func WaitForTask ¶ added in v0.2.41
func WaitForTask(ctx context.Context, c *Client, taskID string, opts ...WaitOptions) (*core.DetailedTask, error)
WaitForTask polls tasks/get until the task reaches a terminal state or ctx fires. Each iteration honors:
- opts[0].PollInterval if non-zero (caller override),
- else the server's PollIntervalMs on the most recent response,
- else the 1-second default.
Returns the final DetailedTask snapshot (which inlines the result / error / inputRequests per SEP-2663). Note that input_required is NOT terminal — callers wanting to handle the MRTR resume should poll until terminal or use a tighter loop that checks for input_required and calls UpdateTask in between.
Cancel-poll abort (SEP-2663): a caller that issues tasks/cancel may stop polling immediately without waiting for the server to surface "cancelled" status. The recommended pattern is to derive a child context for the wait and cancel it after CancelTask returns:
pollCtx, stopPoll := context.WithCancel(ctx)
defer stopPoll()
go func() {
<-userCancelSignal
client.CancelTask(c, taskID)
stopPoll()
}()
dt, err := client.WaitForTask(pollCtx, taskID) // err == context.Canceled on abort
func WaitForTaskV1 ¶ added in v0.2.41
func WaitForTaskV1(ctx context.Context, c *Client, taskID string, pollInterval time.Duration) (*core.GetTaskResultV1, error)
WaitForTaskV1 polls tasks/get until the v1 task reaches a terminal state or the context is cancelled. Returns the final task info.
Use pollInterval of 0 for a 1-second default. The context controls the overall timeout — use context.WithTimeout for deadline-based waiting.
func WaitForTaskWithInput ¶ added in v0.4.0
func WaitForTaskWithInput(ctx context.Context, c *Client, taskID string, handler InputHandler, opts ...WaitOptions) (*core.DetailedTask, error)
WaitForTaskWithInput polls like WaitForTask but also services input_required pauses: the pending inputRequests are resolved through handler (the same InputHandler shape CallToolWithInputs uses for ephemeral MRTR, so one handler covers mid-call input on both the ephemeral and task-backed paths), delivered via tasks/update, and polling continues to a terminal state. A handler error aborts the wait; the task keeps running server-side and can be resumed by a later call or cancelled explicitly.
This is the "tighter loop" WaitForTask's doc alludes to: WaitForTask deliberately never answers input (its callers poll fire-and-forget workloads), so its behavior is unchanged.
Types ¶
type BackgroundTask ¶ added in v0.4.0
type BackgroundTask struct {
// TaskID and Tool identify the task for surfaces (task listings); Tool
// is caller-supplied (the tool name that created it) and may be empty.
TaskID string
Tool string
// StartedAt is when the originating call began.
StartedAt time.Time
// contains filtered or unexported fields
}
BackgroundTask is a handle to a task poll that outlived its grace window and detached (see WaitForTaskOrBackground). The poll keeps running on a context independent of the caller's, so later input_required pauses still reach the same InputHandler; the terminal outcome is retained and Done signals its arrival.
Safe for concurrent use.
func WaitForTaskOrBackground ¶ added in v0.4.0
func WaitForTaskOrBackground(ctx context.Context, c *Client, taskID, tool string, handler InputHandler, grace time.Duration, opts ...WaitOptions) (*core.DetailedTask, *BackgroundTask, error)
WaitForTaskOrBackground races a task against a grace window. It polls (and services input_required through handler) like WaitForTaskWithInput, but returns as soon as EITHER the task reaches a terminal state within the grace (non-nil *core.DetailedTask, nil handle) OR the grace expires with the task still running (nil DetailedTask, non-nil *BackgroundTask whose poll continues in the background).
The grace HOLDS while the task is actively in input_required: an interactive pause is not a reason to detach, so a task that parks for input within the window stays inline until it is answered. A grace <= 0 is equivalent to WaitForTaskWithInput (never detaches).
The tool argument is stored on the returned handle for display only.
func (*BackgroundTask) Cancel ¶ added in v0.4.0
func (bt *BackgroundTask) Cancel(ctx context.Context) error
Cancel requests server-side cancellation (tasks/cancel) and stops the poll. Done still closes with the resulting (cancelled) outcome, so observers see one lifecycle regardless of how it ended.
ctx bounds the tasks/cancel round-trip only. The local poll is stopped regardless of whether that request succeeds, so a cancelled or failed ctx still detaches the handle — the error is reported, not swallowed.
func (*BackgroundTask) Done ¶ added in v0.4.0
func (bt *BackgroundTask) Done() <-chan struct{}
Done closes when the task reaches a terminal state or the poll aborts.
func (*BackgroundTask) Result ¶ added in v0.4.0
func (bt *BackgroundTask) Result() (*core.DetailedTask, error)
Result returns the terminal DetailedTask (valid after Done). The error covers a poll abort or transport failure; task-level failed/cancelled status rides the DetailedTask.
func (*BackgroundTask) Status ¶ added in v0.4.0
func (bt *BackgroundTask) Status() core.TaskStatus
Status returns the most recent polled status.
type CallContext ¶ added in v0.2.44
type CallContext struct {
context.Context
// Headers are additional HTTP headers to attach to the outbound request
// (Streamable HTTP transport only). Used by ToolCall to inject SEP-2243
// Mcp-Param-* headers derived from the tool's x-mcp-header inputSchema
// annotations; other transports ignore. Caller-supplied keys override the
// transport's defaults (Content-Type, Accept, Mcp-Session-Id) only if the
// caller deliberately sets them — which they shouldn't.
Headers map[string]string
// contains filtered or unexported fields
}
CallContext is a typed context for a single Client.CallContext invocation — the client-side analogue of the typed handler contexts in core/ (per constraint C1). Embeds context.Context for cancellation; carries a per-call notification hook for long-lived calls that need their own notification channel (events/stream and likely future tasks/progress streaming RPCs).
Construct via NewCallContext, configure via the chainable With* methods, then pass to Client.CallContext.
Goroutine model: the notify hook may be called concurrently with the session-global callback (set via WithNotificationCallback). Both fire on the same notification — the hook is additive, not a replacement.
func NewCallContext ¶ added in v0.2.44
func NewCallContext(ctx context.Context) *CallContext
NewCallContext wraps a context.Context as a CallContext for use with Client.CallContext. The wrapped context controls cancellation: on Streamable HTTP, the underlying http.Request is built with it so cancelling cancels the in-flight POST. Required for long-lived calls (events/stream) so Stop() actually closes the connection.
func (*CallContext) WithNotifyHook ¶ added in v0.2.44
func (cc *CallContext) WithNotifyHook(hook func(method string, params json.RawMessage)) *CallContext
WithNotifyHook installs a per-call notification hook. The hook fires for every notification arriving on this call's response stream — for Streamable HTTP, that's the POST SSE response stream the call holds open. ADDITIVE to WithNotificationCallback (both fire on the same notification).
Transport coverage: wired on the Streamable HTTP transport (the path used by events/stream). On stdio / SSE / in-memory transports the hook is a no-op for now — notifications still flow only via the global callback.
type CallOption ¶ added in v0.8.4
type CallOption func(*callOptions)
CallOption configures one ToolCallWith, ReadResourceWith or GetPromptWith call.
func WithProgress ¶ added in v0.8.4
func WithProgress(fn func(core.ProgressNotification)) CallOption
WithProgress asks the server for progress on this call and hands each notifications/progress for it to fn. The client mints the progress token and sends it as _meta.progressToken; fn sees only notifications carrying that token, while WithNotificationCallback still sees all of them.
fn runs on the transport's read goroutine, so it must not block, and it may run concurrently with fn for another call. Progress that arrives after the call has returned is dropped. Delivered on Streamable HTTP, SSE, stdio, and over WithTransport when the transport implements core.NotificationReceiver (server.NewInProcessTransport does); any other WithTransport transport delivers none.
Any request may carry a progress token, so this works for resources/read and prompts/get too. Whether progress arrives is up to the server's handler.
type CallResult ¶
type CallResult struct {
Raw json.RawMessage
}
CallResult holds the raw JSON result from a JSON-RPC call.
func (*CallResult) JSON ¶
func (r *CallResult) JSON() string
JSON returns the result as indented JSON.
func (*CallResult) Unmarshal ¶
func (r *CallResult) Unmarshal(v any) error
Unmarshal decodes the result into the given value.
type CardFetchOption ¶ added in v0.8.22
type CardFetchOption func(*cardFetchConfig)
CardFetchOption configures FetchServerCard and NewServerCardCache.
func WithCardHTTPClient ¶ added in v0.8.22
func WithCardHTTPClient(c *http.Client) CardFetchOption
WithCardHTTPClient sets the HTTP client used to fetch cards. Its CheckRedirect is replaced by the same-origin check, and its other settings (transport, timeout) are kept. The default is a client with a 10 second timeout.
func WithInsecureCardFetch ¶ added in v0.8.22
func WithInsecureCardFetch() CardFetchOption
WithInsecureCardFetch allows http card URLs. Use it for loopback tests and local demos only: the Server Card spec requires HTTPS before any security-relevant extension info on the card is trusted.
type Client ¶
type Client struct {
// ServerInfo is populated after Connect.
ServerInfo core.Implementation
// Instructions is the server's guidance for the model, read after
// Connect from the initialize or server/discover result. Empty when the
// server sent none, or when a stateless server answered no discover.
Instructions string
// contains filtered or unexported fields
}
Client is an MCP client that communicates over Streamable HTTP or SSE.
func NewClient ¶
func NewClient(url string, info core.Implementation, opts ...ClientOption) *Client
NewClient creates a new MCP client targeting the given server URL. By default uses Streamable HTTP. Use WithSSEClient() for SSE transport. Call Connect() to perform the protocol handshake.
func (*Client) Call ¶
Call makes a JSON-RPC call and returns the parsed response.
ctx bounds the call and is threaded through the client middleware chain and the trace middleware. A nil ctx is treated as context.Background.
func (*Client) CallContext ¶ added in v0.2.44
func (c *Client) CallContext(ctx context.Context, cc *CallContext, method string, params any) (*CallResult, error)
CallContext issues a JSON-RPC call with per-call configuration carried on the typed CallContext. Identical to Call when cc has no hooks set beyond a plain context.
ctx bounds the call. A cc that already carries its own Context keeps it — an explicitly built NewCallContext is the caller stating intent, and it wins over the ambient argument.
func (*Client) Connect ¶
Connect establishes the transport and performs the MCP initialize handshake.
ctx bounds the handshake only — it does NOT bound the resulting session. Once Connect returns nil the session is live and outlives ctx, so passing a short-lived timeout context here is safe and does not later tear down the connection. This mirrors grpc.DialContext. Use Close to end the session.
For command and stdio transports, Connect is additionally bounded by a default 30s timeout to prevent indefinite hangs when the subprocess doesn't speak the expected protocol. Override with WithConnectTimeout. HTTP transports are not bounded by this default (set WithConnectTimeout explicitly if needed). ctx and that timeout compose — whichever fires first wins.
A nil ctx is treated as context.Background.
func (*Client) Discover ¶ added in v0.2.47
func (c *Client) Discover() (*DiscoverResult, error)
Discover sends server/discover and returns the server's discovery response payload. Does not mutate the client's session state. On a server that does not implement server/discover (e.g., a legacy-only mcpkit server before SEP-2575 rolled out), Discover returns a typed *UnsupportedDiscoverError so callers can branch cleanly. Transport- level failures (network, TLS) propagate as-is.
Connect must be called before Discover so the underlying transport is initialized. Discover is the public entry point built on top of adaptiveProbe; Connect's ClientModeAdaptive path continues to call adaptiveProbe directly for the boolean fallback signal.
func (*Client) GetPromptWith ¶ added in v0.8.6
func (c *Client) GetPromptWith(ctx context.Context, name string, args map[string]any, opts ...CallOption) (*core.PromptResult, error)
GetPromptWith runs prompts/get for name with args, with per-call options such as WithProgress, and returns the typed result.
func (*Client) HandleServerRequest ¶
HandleServerRequest dispatches an incoming server-to-client JSON-RPC request to the appropriate registered handler (sampling, elicitation, or roots). Returns a JSON-RPC response to send back to the server.
Uses context.Background(); transports that wish to thread their own cancellation context (or callers needing to invoke the dispatch synthetically — e.g. SEP-2322 MRTR's CallToolWithInputs feeding inputRequests through the same routing logic) should call HandleServerRequestWithContext instead.
func (*Client) HandleServerRequestWithContext ¶ added in v0.2.41
func (c *Client) HandleServerRequestWithContext(ctx context.Context, req *core.Request) *core.Response
HandleServerRequestWithContext is the context-aware form of HandleServerRequest. Callers that have a real context (caller-driven cancellation, MRTR loops, future client middleware) thread it through here so handlers receive it instead of context.Background.
This is the single source of truth for "given an MCP method name and a registered handler, what's the response?" Both the transport's incoming-request path AND the SEP-2322 MRTR client-side input resolution route through this function.
func (*Client) ListPrompts ¶ added in v0.2.28
ListPrompts returns every registered prompt definition the server exposes. Same auto-iteration + cap contract as ListResources; see WithMaxListPages and ErrListPaginationOverrun.
func (*Client) ListPromptsPage ¶ added in v0.2.42
func (c *Client) ListPromptsPage(ctx context.Context, cursor string) (*core.PromptsListResult, error)
ListPromptsPage fetches one page of prompts/list and returns the typed result including the SEP-2549 ttlMs / cacheScope hints and pagination cursor.
func (*Client) ListResourceTemplates ¶
ListResourceTemplates returns every registered resource template the server exposes. Same auto-iteration + cap contract as ListResources; see WithMaxListPages and ErrListPaginationOverrun.
func (*Client) ListResourceTemplatesPage ¶ added in v0.2.42
func (c *Client) ListResourceTemplatesPage(ctx context.Context, cursor string) (*core.ResourceTemplatesListResult, error)
ListResourceTemplatesPage fetches one page of resources/templates/list and returns the typed result including the SEP-2549 ttlMs / cacheScope hints and pagination cursor.
func (*Client) ListResources ¶
ListResources returns every registered static resource the server exposes. Auto-iterates resources/list across every page the server advertises via nextCursor; capped at WithMaxListPages (default 1000) — a server emitting an unbounded cursor surfaces as ErrListPaginationOverrun rather than an infinite loop.
Discards the per-page envelope (TTLMs / CacheScope). Use ListResourcesPage(cursor) when you need the SEP-2549 cache hints.
func (*Client) ListResourcesPage ¶ added in v0.2.42
func (c *Client) ListResourcesPage(ctx context.Context, cursor string) (*core.ResourcesListResult, error)
ListResourcesPage fetches one page of resources/list and returns the typed result including the SEP-2549 ttlMs / cacheScope hints and pagination cursor.
func (*Client) ListTools ¶
ListTools returns every registered tool definition the server exposes. Auto-iterates tools/list across every page the server advertises via nextCursor; capped at WithMaxListPages (default 1000) — a server emitting an unbounded cursor surfaces as ErrListPaginationOverrun rather than an infinite loop.
As a side effect, caches each valid tool's full ToolDef (including inputSchema) keyed by name — ToolCall consults this cache to detect SEP-2243 x-mcp-header annotations without a second round-trip. The cache replace happens once, after every page has been drained, so concurrent lookupToolSchema readers never observe a partial cache.
Tools whose inputSchema fails SEP-2243 x-mcp-header validation (empty values, non-primitive types, name charset, case-insensitive duplicates) are silently filtered out. Spec: "Client MUST keep valid tools while excluding invalid ones." The cache and the returned slice are kept consistent — invalid tools never become callable through this client.
func (*Client) ListToolsForModel ¶
ListToolsForModel returns tools visible to the LLM, filtering out tools that are only visible to apps (visibility: ["app"]). Tools with no visibility set (nil/empty) are included — the default means visible to both model and app. This is a client-side convenience; the server always returns all tools.
func (*Client) ListToolsPage ¶ added in v0.2.42
ListToolsPage fetches one page of tools/list and returns the typed result including the SEP-2549 ttlMs / cacheScope hints and pagination cursor. Use Tools(ctx) for the auto-paginating item iterator when you don't need the envelope metadata, or the zero-arg ListTools() if you only want the items from the first page.
Example ¶
Paging through a list method. mcpkit's own server pages at 250 items by default, and other servers pick their own size, so loop until NextCursor comes back empty.
Use ListToolsPage rather than ListTools when you want the envelope, since it also carries the SEP-2549 ttlMs and cacheScope caching hints that the item-iterator forms discard.
This example is illustrative and does not run: it needs a live server.
package main
import (
"context"
"fmt"
"log"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func main() {
ctx := context.Background()
c := client.NewClient("http://localhost:8080/mcp",
core.ClientInfo{Name: "example", Version: "1.0"})
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()
var all []core.ToolDef
cursor := ""
for {
// One context for the whole walk, so cancelling it stops the paging.
page, err := c.ListToolsPage(ctx, cursor)
if err != nil {
log.Fatal(err)
}
all = append(all, page.Tools...)
if page.NextCursor == "" {
break
}
cursor = page.NextCursor
}
fmt.Println(len(all))
}
Output:
func (*Client) Listen ¶ added in v0.7.2
Listen opens a subscriptions/listen stream and returns once the server has acknowledged it, or with the server's error if it refused (for example -32602 for a malformed filter, or core.ErrCodeSubscriptionLimitExceeded at the server's stream cap).
Handlers run on the stream's reader goroutine, one at a time, in arrival order. They must not block, or later notifications wait behind them. Notifications on the stream also reach the client-wide callback set by WithNotificationCallback, as on any other stream.
The stream lasts until Stop, until ctx is cancelled, or until the server closes it. Done is closed when it has ended, and Err then says whether it ended in an error.
func (*Client) NotifyRootsChanged
deprecated
added in
v0.1.24
NotifyRootsChanged sends a notifications/roots/list_changed notification to the server. Call this after the client's filesystem roots have changed so the server can re-fetch the current list via a roots/list request.
Deprecated: per SEP-2577. Retained in 0.4; removal deferred to a future release (~2027 at the earliest, issue 850). See docs/SEP_2577_DEPRECATIONS.md.
Example ¶
Telling the server the client's filesystem roots changed. The server responds by re-issuing roots/list, so the notification carries no payload of its own. Registering a RootsHandler is what advertises the capability; without one the server never asks.
Deprecated: roots is deprecated per SEP-2577. See docs/SEP_2577_DEPRECATIONS.md.
This example is illustrative and does not run: it needs a live server.
package main
import (
"context"
"log"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func main() {
ctx := context.Background()
roots := []core.Root{{URI: "file:///home/me/project", Name: "project"}}
c := client.NewClient("http://localhost:8080/mcp",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithRootsHandler(func(context.Context) ([]core.Root, error) {
return roots, nil
}),
)
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()
roots = append(roots, core.Root{URI: "file:///tmp/scratch", Name: "scratch"})
if err := c.NotifyRootsChanged(ctx); err != nil {
log.Fatal(err)
}
}
Output:
func (*Client) Prompts ¶ added in v0.2.27
Prompts returns an iterator that yields all prompt definitions, automatically paginating through multiple pages.
func (*Client) ReadResource ¶
ReadResource reads a resource by URI and returns the first text content.
func (*Client) ReadResourceFull ¶ added in v0.2.47
ReadResourceFull reads a resource by URI and returns the full typed result — every content item plus the SEP-2549 ttlMs / cacheScope cache hints. The plain ReadResource helper drops that envelope and returns only the first text content; callers that want to cache a read response should use ReadResourceFull.
func (*Client) ReadResourceWith ¶ added in v0.8.6
func (c *Client) ReadResourceWith(ctx context.Context, uri string, opts ...CallOption) (*core.ResourceResult, error)
ReadResourceWith reads a resource like ReadResourceFull, with per-call options such as WithProgress.
func (*Client) ResourceTemplates ¶ added in v0.2.27
ResourceTemplates returns an iterator that yields all resource template definitions, automatically paginating through multiple pages.
func (*Client) Resources ¶ added in v0.2.27
Resources returns an iterator that yields all resource definitions, automatically paginating through multiple pages.
func (*Client) ServerExtensionCapability ¶ added in v0.2.47
func (c *Client) ServerExtensionCapability(id string) (core.ExtensionCapability, bool)
ServerExtensionCapability returns the cached ExtensionCapability the server advertised for id during initialize, or false when the server did not declare the extension.
The returned value is the extension's settings object, indexed directly by setting name (e.g. cap["directoryRead"] for SEP-2640). SEP-2133 puts the settings inline under the extension identifier, so there is no envelope to unwrap. It is decoded from the raw JSON captured at initialize time and reflects whatever the server emitted on the wire; callers should treat unknown keys permissively.
func (*Client) ServerSupportsExtension ¶
ServerSupportsExtension checks whether the server advertised support for the given extension ID in its initialize response. Call after Connect().
func (*Client) ServerSupportsUI ¶
ServerSupportsUI checks whether the server advertised MCP Apps (io.modelcontextprotocol/ui) support. Convenience wrapper around ServerSupportsExtension.
func (*Client) SetLogLevel ¶ added in v0.2.28
SetLogLevel sets the server's minimum log level for this session via logging/setLevel. The server will send notifications/message for log entries at or above this level. Use "debug" to see all logs.
func (*Client) SetTransport ¶
SetTransport sets the transport for the client. Use when the transport needs to reference the client (e.g., InProcessTransport with ServerRequestHandler that delegates to the client's sampling/elicitation handlers). Must be called before Connect().
func (*Client) SetURL ¶
SetURL updates the client's target URL. Used in reconnection tests to simulate DNS/load balancer changes.
func (*Client) SubscribeResource ¶
SubscribeResource subscribes to change notifications for a resource URI. The server will send notifications/resources/updated when the resource changes.
func (*Client) ToolCall ¶
ToolCall invokes a tool and returns the first text content.
If the tool's inputSchema (cached by a prior ListTools call) carries SEP-2243 x-mcp-header annotations on primitive-typed properties, the corresponding argument values are mirrored as Mcp-Param-{Name} HTTP headers on the outbound tools/call request (in addition to the JSON body). Header values are encoded plain-ASCII or =?base64?{...}?= per SEP-2243 §value-encoding. Tools without cached schemas or without x-mcp-header annotations send no extra headers (identical to pre- SEP-2243 behavior). Caller-side note: middleware registered via WithCallMiddleware does not run on the header-mirroring path.
func (*Client) ToolCallFull ¶ added in v0.2.6
ToolCallFull invokes a tool and returns the complete result including IsError, all content blocks, and the raw JSON. Unlike ToolCall, tool-level errors (isError: true) are returned in the result, not as Go errors. Only transport/protocol failures produce a Go error.
func (*Client) ToolCallWith ¶ added in v0.8.4
func (c *Client) ToolCallWith(ctx context.Context, name string, args any, opts ...CallOption) (*core.ToolResult, error)
ToolCallWith invokes a tool like ToolCallFull, with per-call options such as WithProgress. With no options it is ToolCallFull, except that it also sends the SEP-2243 Mcp-Param-* headers ToolCall derives from the tool's cached inputSchema. Like ToolCall, a call that carries such headers skips WithCallMiddleware.
func (*Client) Tools ¶ added in v0.2.27
Tools returns an iterator that yields all tool definitions, automatically paginating through multiple pages if the server uses cursor-based pagination.
Example:
for tool, err := range c.Tools(ctx) {
if err != nil { log.Fatal(err) }
fmt.Println(tool.Name)
}
func (*Client) UnsubscribeResource ¶
UnsubscribeResource removes a subscription for a resource URI.
func (*Client) UsingStatelessWire ¶ added in v0.2.47
UsingStatelessWire reports whether Connect() landed on the SEP-2575 stateless wire (server/discover) rather than the legacy initialize handshake. Always false before Connect(). True when the client was constructed with WithClientMode(ClientModeStateless), or when ClientModeAdaptive's probe succeeded against a server that speaks server/discover.
Use to display which wire a session is on (walkthroughs, debug tools) or to gate behavior that only makes sense on one wire.
type ClientAuthError ¶
type ClientAuthError = ssehttp.AuthRetryError
ClientAuthError is returned by the client transport when the server rejects a request with 401 or 403 and the transport has exhausted its retry budget. Find it with errors.As. On a 403 insufficient_scope challenge, RequiredScopes holds the scopes the server asked for and WWWAuthenticate the raw header, so a caller whose token source cannot step up (a static token) still learns what to request. Cause carries the client's own reason for giving up, e.g. that the source does not support step-up.
type ClientCallFunc ¶ added in v0.2.29
ClientCallFunc is the signature for the next handler in the client middleware chain.
type ClientMiddleware ¶ added in v0.2.29
type ClientMiddleware func(ctx context.Context, method string, params any, next ClientCallFunc) (*CallResult, error)
ClientMiddleware intercepts outgoing client calls before they reach the transport. Use for tracing, logging, metrics, or request transformation.
The middleware sees the method name (e.g., "tools/call", "tools/list", "logging/setLevel") and typed params. Call next to continue the chain, or return directly to short-circuit (e.g., cached responses, circuit breakers).
Example — tracing middleware:
client.WithClientMiddleware(func(ctx context.Context, method string, params any,
next client.ClientCallFunc) (*client.CallResult, error) {
start := time.Now()
result, err := next(ctx, method, params)
log.Printf("→ %s (%s)", method, time.Since(start))
return result, err
})
type ClientMode ¶ added in v0.2.47
type ClientMode int
ClientMode selects how the client negotiates the wire shape with the server during Connect.
const ( // ClientModeLegacyOnly performs the legacy initialize handshake at // Connect time. Never sends _meta envelopes or MCP-Protocol-Version // headers; pure pre-SEP-2575 behavior. Zero value (kept for the // strict-back-compat opt-out path). ClientModeLegacyOnly ClientMode = iota // ClientModeAdaptive probes server/discover first; on -32601, HTTP // 404, or a plain-text HTTP 400 from a session-only server, falls // back to the legacy initialize handshake. Once a server is // classified as stateless, subsequent calls carry the _meta envelope // and the MCP-Protocol-Version HTTP header. Default. ClientModeAdaptive // ClientModeStateless skips initialize entirely. Every call carries // the SEP-2575 _meta envelope and the MCP-Protocol-Version header. // server/discover may still be issued explicitly via DiscoverServer. ClientModeStateless )
func ParseClientMode ¶ added in v0.2.47
func ParseClientMode(s string) (ClientMode, bool)
ParseClientMode decodes a token (the inverse of String). Accepts "legacy", "adaptive", "stateless"; case-insensitive; empty maps to caller's fallback via ok=false.
func ResolveClientMode ¶ added in v0.2.47
func ResolveClientMode() ClientMode
ResolveClientMode seeds NewClient's mode from env var or DefaultClientMode. WithClientMode option clobbers when present (options run after seeding).
Precedence (high → low):
- client.WithClientMode(m)
- MCPKIT_CLIENT_MODE env var
- client.DefaultClientMode
func (ClientMode) String ¶ added in v0.2.47
func (m ClientMode) String() string
String returns the lowercase token used in env vars and structured logs.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithClientBearerToken ¶
func WithClientBearerToken(token string) ClientOption
WithClientBearerToken sets a static bearer token for all client requests.
func WithClientKeepalive ¶ added in v0.1.11
func WithClientKeepalive(interval time.Duration, maxFailures int) ClientOption
WithClientKeepalive enables application-level keepalive pings. The client periodically sends JSON-RPC ping requests to the server. If maxFailures consecutive pings fail (timeout or error), the client triggers reconnection (if retries are configured) or closes.
func WithClientLogging ¶
func WithClientLogging(logger *log.Logger) ClientOption
WithClientLogging enables debug logging of all client transport operations. Every connect, call, notify, and close is logged with method name, latency, and error details. Pass nil to use the default logger.
Example:
client := mcpkit.NewClient(url, info,
mcpkit.WithClientLogging(log.Default()),
)
func WithClientMiddleware ¶ added in v0.2.29
func WithClientMiddleware(mw ...ClientMiddleware) ClientOption
WithClientMiddleware registers middleware that wraps all outgoing client calls (ToolCall, ListTools, ReadResource, etc.). Middleware executes in registration order: first registered = outermost (runs first on request, last on response).
func WithClientMode ¶ added in v0.2.47
func WithClientMode(m ClientMode) ClientOption
WithClientMode pins the wire mode for this Client. Highest-precedence override — beats MCPKIT_CLIENT_MODE env and DefaultClientMode.
func WithCommandTransport ¶ added in v0.1.14
func WithCommandTransport(name string, args []string, opts ...CommandOption) ClientOption
WithCommandTransport configures the client to spawn a subprocess MCP server and communicate over stdin/stdout. A fresh process is started on each Connect() (and on each reconnection if WithMaxRetries is set).
Example:
c := client.NewClient("", info,
client.WithCommandTransport("python", []string{"my_server.py"},
client.WithEnv("DEBUG=1"),
),
)
err := c.Connect()
func WithConnectTimeout ¶ added in v0.1.16
func WithConnectTimeout(d time.Duration) ClientOption
WithConnectTimeout sets a deadline for Connect() to complete. This covers both the transport connection (subprocess start, SSE stream open) and the MCP initialize handshake. If the timeout expires, Connect() returns an error immediately instead of blocking indefinitely.
This is especially important for CommandTransport: if the subprocess starts but doesn't speak Content-Length framed JSON-RPC (e.g., wrong mode, missing env vars), Connect() would block forever without a timeout.
Default is 0 (no timeout).
func WithContentChunkHandler ¶ added in v0.1.17
func WithContentChunkHandler(fn func(chunk core.ContentChunk)) ClientOption
WithContentChunkHandler sets a callback for streaming tool content chunks. The callback is invoked for each content chunk notification received during tool execution (method matching the server's configured content chunk method, default "notifications/tools/content_chunk").
If not set, content chunk notifications are silently ignored and the client relies on the final ToolResult for the complete response.
func WithElicitationCompleteHandler ¶ added in v0.2.41
func WithElicitationCompleteHandler(h ElicitationCompleteHandler) ClientOption
WithElicitationCompleteHandler registers a handler for notifications/elicitation/complete notifications (SEP-1036).
func WithElicitationHandler ¶
func WithElicitationHandler(h ElicitationHandler) ClientOption
WithElicitationHandler registers a handler for server-to-client elicitation requests. When set, the client advertises form-mode elicitation capability during initialization. To also support URL-mode elicitation, combine with WithElicitationURLSupport.
Example (Defaults) ¶
SEP-1034 defaults. A schema property may declare a default; the client fills it in for any key the user left out, so a handler can return exactly what the user typed and stay unaware of defaults entirely.
The merge happens inside the elicitation/create dispatch path, after your handler returns and before the response goes back to the server.
package main
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func elicitRequest(schema string) *core.Request {
params, _ := json.Marshal(core.ElicitationRequest{
Message: "Configure the deploy",
RequestedSchema: json.RawMessage(schema),
})
return &core.Request{
JSONRPC: "2.0",
ID: json.RawMessage(`1`),
Method: "elicitation/create",
Params: core.NewRawJSON(params),
}
}
func elicitContent(resp *core.Response) string {
var out core.ElicitationResult
_ = resp.ResultAs(&out)
keys := make([]string, 0, len(out.Content))
for k := range out.Content {
keys = append(keys, k)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%v", k, out.Content[k]))
}
return fmt.Sprint(parts)
}
const deploySchema = `{
"type": "object",
"properties": {
"env": {"type": "string", "default": "dev"},
"replicas":{"type": "integer", "default": 2},
"verbose": {"type": "boolean", "default": false}
}
}`
func main() {
c := client.NewClient("http://example.invalid",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithElicitationHandler(
func(ctx context.Context, req core.ElicitationRequest) (core.ElicitationResult, error) {
// The user filled in one field and left the rest alone.
return core.ElicitationResult{
Action: "accept",
Content: map[string]any{"env": "prod"},
}, nil
},
),
)
resp := c.HandleServerRequestWithContext(context.Background(), elicitRequest(deploySchema))
fmt.Println(elicitContent(resp))
}
Output: [env=prod replicas=2 verbose=false]
Example (DefaultsDoNotOverwrite) ¶
A value the user supplied always wins. Defaults only fill absent keys, so this never overwrites input, including a value that happens to equal the zero value of its type.
package main
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func elicitRequest(schema string) *core.Request {
params, _ := json.Marshal(core.ElicitationRequest{
Message: "Configure the deploy",
RequestedSchema: json.RawMessage(schema),
})
return &core.Request{
JSONRPC: "2.0",
ID: json.RawMessage(`1`),
Method: "elicitation/create",
Params: core.NewRawJSON(params),
}
}
func elicitContent(resp *core.Response) string {
var out core.ElicitationResult
_ = resp.ResultAs(&out)
keys := make([]string, 0, len(out.Content))
for k := range out.Content {
keys = append(keys, k)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%v", k, out.Content[k]))
}
return fmt.Sprint(parts)
}
const deploySchema = `{
"type": "object",
"properties": {
"env": {"type": "string", "default": "dev"},
"replicas":{"type": "integer", "default": 2},
"verbose": {"type": "boolean", "default": false}
}
}`
func main() {
c := client.NewClient("http://example.invalid",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithElicitationHandler(
func(ctx context.Context, req core.ElicitationRequest) (core.ElicitationResult, error) {
return core.ElicitationResult{
Action: "accept",
Content: map[string]any{"env": "staging", "replicas": 0},
}, nil
},
),
)
resp := c.HandleServerRequestWithContext(context.Background(), elicitRequest(deploySchema))
fmt.Println(elicitContent(resp))
}
Output: [env=staging replicas=0 verbose=false]
Example (DefaultsSkippedOnDecline) ¶
Defaults apply only when the user accepted. On decline or cancel the result's content is undefined, so filling defaults would invent input the user never gave.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func elicitRequest(schema string) *core.Request {
params, _ := json.Marshal(core.ElicitationRequest{
Message: "Configure the deploy",
RequestedSchema: json.RawMessage(schema),
})
return &core.Request{
JSONRPC: "2.0",
ID: json.RawMessage(`1`),
Method: "elicitation/create",
Params: core.NewRawJSON(params),
}
}
const deploySchema = `{
"type": "object",
"properties": {
"env": {"type": "string", "default": "dev"},
"replicas":{"type": "integer", "default": 2},
"verbose": {"type": "boolean", "default": false}
}
}`
func main() {
c := client.NewClient("http://example.invalid",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithElicitationHandler(
func(ctx context.Context, req core.ElicitationRequest) (core.ElicitationResult, error) {
return core.ElicitationResult{Action: "decline"}, nil
},
),
)
resp := c.HandleServerRequestWithContext(context.Background(), elicitRequest(deploySchema))
var out core.ElicitationResult
_ = resp.ResultAs(&out)
fmt.Println(out.Action, len(out.Content))
}
Output: decline 0
Example (DefaultsTypeMismatch) ¶
A default whose type contradicts the schema is dropped rather than sent. Forwarding it would put wire-invalid data in front of the server, so the key is simply left absent and the server sees an omitted field.
package main
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func elicitRequest(schema string) *core.Request {
params, _ := json.Marshal(core.ElicitationRequest{
Message: "Configure the deploy",
RequestedSchema: json.RawMessage(schema),
})
return &core.Request{
JSONRPC: "2.0",
ID: json.RawMessage(`1`),
Method: "elicitation/create",
Params: core.NewRawJSON(params),
}
}
func elicitContent(resp *core.Response) string {
var out core.ElicitationResult
_ = resp.ResultAs(&out)
keys := make([]string, 0, len(out.Content))
for k := range out.Content {
keys = append(keys, k)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%v", k, out.Content[k]))
}
return fmt.Sprint(parts)
}
func main() {
const badSchema = `{
"type": "object",
"properties": {
"replicas": {"type": "integer", "default": "two"},
"env": {"type": "string", "default": "dev"}
}
}`
c := client.NewClient("http://example.invalid",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithElicitationHandler(
func(ctx context.Context, req core.ElicitationRequest) (core.ElicitationResult, error) {
return core.ElicitationResult{Action: "accept", Content: map[string]any{}}, nil
},
),
)
resp := c.HandleServerRequestWithContext(context.Background(), elicitRequest(badSchema))
fmt.Println(elicitContent(resp))
}
Output: [env=dev]
func WithElicitationURLSupport ¶ added in v0.2.41
func WithElicitationURLSupport() ClientOption
WithElicitationURLSupport enables URL-mode elicitation capability. The same ElicitationHandler receives both form and URL mode requests; it should branch on req.Mode. Must be combined with WithElicitationHandler.
func WithElicitationValidation ¶ added in v0.5.0
func WithElicitationValidation(enabled bool) ClientOption
WithElicitationValidation controls whether accepted elicitation responses are checked against the server's requestedSchema before being sent. Enabled by default, mirroring the server, which validates inbound tool and prompt arguments against their advertised schemas.
The client is the only party that sees both the schema and the user's input, so it is the only one that can catch a mismatch. When validation fails the request is answered with -32602 and a structured error list rather than forwarding content the server asked not to receive.
A malformed or uncompilable schema is never an error: the defect is the server's and the user answered in good faith, so validation is skipped.
Pass false to send whatever the handler produced:
c := client.NewClient(url, info, client.WithElicitationValidation(false))
func WithExtension ¶
func WithExtension(id string, cap core.ClientExtensionCap) ClientOption
WithExtension advertises support for an extension during the initialize handshake. The extension ID and capability are included in the client's capabilities.extensions map, allowing the server to detect client support via core.ClientSupportsExtension(ctx, id) in tool handlers.
func WithFileInputs ¶ added in v0.2.44
func WithFileInputs() ClientOption
WithFileInputs advertises SEP-2356 file-input support during the initialize handshake. Servers MUST NOT include `x-mcp-file` keywords in tool inputSchemas (or elicitation requestedSchemas) for clients that omit this capability — opt in here to receive the picker hints.
func WithGetSSEStream ¶
func WithGetSSEStream() ClientOption
WithGetSSEStream enables a background GET SSE stream on the Streamable HTTP endpoint after Connect(). The stream receives server-initiated notifications (list-changed, log messages, resource updates) that arrive outside POST request-response cycles. Only applies to Streamable HTTP transport; ignored for SSE and in-memory transports.
The notification callback (set via WithNotificationCallback) must be goroutine-safe when WithGetSSEStream is enabled, as notifications may arrive concurrently from both the GET SSE stream and POST SSE responses.
func WithIOTransport ¶ added in v0.2.27
func WithIOTransport(r io.Reader, w io.Writer, opts ...StdioTransportOption) ClientOption
WithIOTransport configures a Client to use stdio-framed JSON-RPC (newline-delimited unless opts say otherwise) over arbitrary reader/writer streams. This is the generic IO transport — use it for Unix sockets, named pipes, SSH tunnels, test pipe pairs, or any other stream-based transport.
Example (pipe pair for testing):
sr, cw := io.Pipe()
cr, sw := io.Pipe()
go srv.RunIO(ctx, sr, sw)
c := client.NewClient("", info, client.WithIOTransport(cr, cw))
func WithInspectResponse ¶ added in v0.2.47
func WithInspectResponse(fn func(*http.Response)) ClientOption
WithInspectResponse sets a callback that is invoked with every HTTP response the transport receives. Mirror of WithModifyRequest for the inbound direction.
Use it to observe response headers and status — the callback must NOT read or close the response body, which the transport owns. Typical uses: surfacing custom deployment headers, capturing rate-limit hints, reading tracing IDs the server stamps on responses.
Fires on every HTTP response the transport's http.Client returns, including pre-retry 401/403 responses that DoWithAuthRetry will retry and any non-2xx final response. Only network-level errors (no response received) skip the callback.
Only applies to HTTP transports (Streamable HTTP, SSE); ignored for stdio and in-process transports.
Example (logging an arbitrary deployment header):
c := client.NewClient(url, info,
client.WithInspectResponse(func(resp *http.Response) {
if v := resp.Header.Get("X-Some-Deployment-Header"); v != "" {
log.Printf("response header: %s", v)
}
}),
)
func WithMaxListPages ¶ added in v0.2.47
func WithMaxListPages(n int) ClientOption
WithMaxListPages overrides the per-client cap on auto-iterating list helpers. The cap applies uniformly to the Tools / Resources / ResourceTemplates / Prompts iterators and to the zero-arg legacy helpers (ListTools etc.) which drain those iterators internally.
When the iterator would issue page N+1 and N >= cap, it yields ErrListPaginationOverrun and stops. The items already yielded are safe to consume; what is uncertain is whether more pages legitimately existed beyond the cap or the server was wedged.
Pass 0 to disable the check entirely (unbounded iteration). The default is 1000 pages, comfortably above any realistic MCP catalog at the standard 100-items-per-page rate.
func WithMaxRetries ¶
func WithMaxRetries(n int) ClientOption
WithMaxRetries sets the maximum number of reconnection attempts on transient transport failure. Default 0 (reconnection disabled). Each retry includes a full reconnect + initialize handshake.
Example:
client := mcpkit.NewClient(url, info,
mcpkit.WithMaxRetries(3),
mcpkit.WithReconnectBackoff(time.Second),
)
func WithModifyRequest ¶ added in v0.1.14
func WithModifyRequest(fn func(*http.Request)) ClientOption
WithModifyRequest sets a callback that is invoked on every outgoing HTTP request before authentication headers are applied. Use it to add custom headers (API keys, tracing IDs, tenant identifiers) without needing a custom http.RoundTripper.
The callback must not modify the request body or URL. Only applies to HTTP transports (Streamable HTTP, SSE); ignored for stdio and in-process transports.
Example:
c := client.NewClient(url, info,
client.WithModifyRequest(func(req *http.Request) {
req.Header.Set("X-Tenant-ID", "acme")
req.Header.Set("X-Request-ID", uuid.New().String())
}),
)
func WithNotificationCallback ¶
func WithNotificationCallback(fn func(method string, params any)) ClientOption
WithNotificationCallback sets a callback for server-to-client notifications (logging, progress, resource updates). Works across all transports.
func WithReconnectBackoff ¶
func WithReconnectBackoff(d time.Duration) ClientOption
WithReconnectBackoff sets the base delay for exponential backoff between reconnection attempts. Default 1s. Actual delay is base * 2^attempt + jitter.
func WithReconnectWithoutReplay ¶ added in v0.8.16
func WithReconnectWithoutReplay() ClientOption
WithReconnectWithoutReplay changes what a transient failure does when WithMaxRetries is set: the client still reconnects (for a command transport, that starts a fresh process), but it returns the failure instead of sending the request again. A request that failed in flight may already have run on a server that then died, so replaying it can run a tool twice. Use this when the server's tools aren't safe to repeat, and let the caller decide whether to retry. Notifications are still replayed. Without WithMaxRetries it does nothing.
func WithRootsHandler
deprecated
added in
v0.1.24
func WithRootsHandler(h RootsHandler) ClientOption
WithRootsHandler registers a handler for server-to-client roots/list requests. When set, the client advertises the "roots" capability (with listChanged: true) during initialization, enabling the server to fetch the client's filesystem roots after receiving a notifications/roots/list_changed notification.
Deprecated: per SEP-2577. Retained in 0.4; removal deferred to a future release (~2027 at the earliest, issue 850). See docs/SEP_2577_DEPRECATIONS.md.
func WithSSEClient ¶
func WithSSEClient() ClientOption
WithSSEClient configures the client to use SSE transport instead of Streamable HTTP. The URL should point to the SSE endpoint (e.g., "http://localhost:8787/mcp/sse").
Example ¶
Connecting over the legacy SSE transport (MCP 2024-11-05). The default is Streamable HTTP; pass WithSSEClient only when talking to a server that has not moved off the two-endpoint SSE wire.
This example is illustrative and does not run: it needs a live server.
package main
import (
"context"
"fmt"
"log"
"github.com/panyam/mcpkit/client"
core "github.com/panyam/mcpkit/core"
)
func main() {
ctx := context.Background()
c := client.NewClient("http://localhost:8080/sse",
core.ClientInfo{Name: "example", Version: "1.0"},
client.WithSSEClient(),
)
// Connect's context bounds the handshake, not the session that follows.
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()
tools, err := c.ListTools(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(len(tools))
}
Output:
func WithSamplingHandler
deprecated
func WithSamplingHandler(h SamplingHandler) ClientOption
WithSamplingHandler registers a handler for server-to-client sampling requests. When set, the client advertises the "sampling" capability during initialization.
Deprecated: per SEP-2577. Retained in 0.4; removal deferred to a future release (~2027 at the earliest, issue 850). See docs/SEP_2577_DEPRECATIONS.md.
func WithStdioTransport ¶
func WithStdioTransport(r io.Reader, w io.Writer, opts ...StdioTransportOption) ClientOption
WithStdioTransport configures a Client to use stdio transport. This is equivalent to WithIOTransport with the given reader/writer pair — kept for backward compatibility and naming clarity when used with subprocess servers (where r is stdout and w is stdin of the child process).
func WithTasksExtension ¶ added in v0.2.41
func WithTasksExtension() ClientOption
WithTasksExtension advertises SEP-2663 Tasks support (io.modelcontextprotocol/tasks). v2 servers gate task creation and the tasks/* methods on this declaration — clients that omit it see synchronous tools/call responses and -32601 for tasks/get / tasks/cancel / tasks/update.
func WithTokenSource ¶
func WithTokenSource(ts core.TokenSource) ClientOption
WithTokenSource sets a dynamic token source for all client requests. Use this for OAuth flows where tokens are refreshed automatically.
func WithToolsListChangedHandler ¶ added in v0.4.0
func WithToolsListChangedHandler(fn func()) ClientOption
WithToolsListChangedHandler sets a dedicated callback for notifications/tools/list_changed. Exists so cache-invalidation plumbing (e.g. dropping a memoized tool index when the server registers or removes tools at runtime) composes with, rather than consumes, the single generic WithNotificationCallback: the notification is still delivered to the generic callback afterward. The callback carries no payload because the notification has none; re-list to observe the change.
func WithTracerProvider ¶ added in v0.2.47
func WithTracerProvider(tp core.TracerProvider) ClientOption
WithTracerProvider registers a core.TracerProvider that wraps every outbound Client.Call in a span and emits a wrap span around every inbound server-to-client request dispatch.
nil and core.NoopTracerProvider{} are treated identically: the trace middleware is not installed and no `_meta.traceparent` is stamped on outbound params. The default (no Option set) is the same "tracing disabled" behavior.
The trace middleware is positioned OUTERMOST in the user middleware chain, so user-registered ClientMiddleware (auth retry, header injection, custom logging) executes inside the span and contributes to the recorded latency. Symmetric with server.WithTracerProvider's outermost install.
Inbound (server-to-client) request resolution:
- `params._meta.traceparent` is extracted via core.ExtractTraceContextFromParams and attached to the handler's ctx via core.WithTraceContext. Handlers consume it through core.TraceContextFromContext(ctx) or pass ctx into a downstream OTel-instrumented call.
- A wrap span named after the request method (e.g. "sampling/createMessage") is emitted with the inbound trace context as parent. Handler-internal spans the user emits via their own tp are children of this wrap span.
Outbound (Client.Call): see traceMiddleware below for the full set of span attributes.
func WithTransport ¶
func WithTransport(t core.Transport) ClientOption
WithTransport sets a core.Transport for the client, bypassing the default HTTP transport creation. Use with server.NewInProcessTransport for testing or embedded scenarios.
Example:
transport := server.NewInProcessTransport(srv)
c := client.New("memory://", info, client.WithTransport(transport))
func WithUIExtension ¶
func WithUIExtension() ClientOption
WithUIExtension is a convenience wrapper that advertises MCP Apps (io.modelcontextprotocol/ui) support with the standard app MIME type.
type CommandOption ¶ added in v0.1.14
type CommandOption func(*commandOpts)
CommandOption configures a CommandTransport.
func WithCleanEnv ¶ added in v0.8.16
func WithCleanEnv() CommandOption
WithCleanEnv starts the subprocess with an empty environment, so it sees only the variables given with WithEnv rather than everything this process has. Use it to keep credentials and other secrets in the parent's environment away from a server that doesn't need them. Nothing is added implicitly, not even PATH or HOME, so pass what the server needs (an npx or uvx launcher usually needs both). The command name is resolved against this process's PATH as usual, before the child starts.
func WithCommandFraming ¶ added in v0.7.2
func WithCommandFraming(f StdioFraming) CommandOption
WithCommandFraming sets the stdio framing used with the subprocess. The default, FramingNewlineDelimited, is what MCP stdio servers speak; use FramingContentLength only for an mcpkit server built before #1517.
func WithDir ¶ added in v0.1.14
func WithDir(dir string) CommandOption
WithDir sets the working directory for the subprocess.
func WithEnv ¶ added in v0.1.14
func WithEnv(env ...string) CommandOption
WithEnv adds environment variables to the subprocess. Each value should be in KEY=VALUE format. These are appended to the current process environment.
func WithShutdownTimeout ¶ added in v0.1.14
func WithShutdownTimeout(d time.Duration) CommandOption
WithShutdownTimeout sets the duration to wait after sending SIGTERM before escalating to SIGKILL. Default is 5 seconds.
func WithStderr ¶ added in v0.1.14
func WithStderr(w io.Writer) CommandOption
WithStderr sets an additional writer for subprocess stderr output. Stderr is always captured in an internal buffer (for error messages on crash); this option tees it to an additional destination (e.g., os.Stderr, a logger).
type CommandTransport ¶ added in v0.1.14
type CommandTransport struct {
// contains filtered or unexported fields
}
CommandTransport implements core.Transport by spawning a subprocess and communicating via stdio. The subprocess is started on Connect() and gracefully shut down on Close().
func NewCommandTransport ¶ added in v0.1.14
func NewCommandTransport(name string, args []string, opts ...CommandOption) *CommandTransport
NewCommandTransport creates a CommandTransport that will spawn the given command with args when Connect() is called.
func (*CommandTransport) Close ¶ added in v0.1.14
func (ct *CommandTransport) Close() error
Close gracefully shuts down the subprocess. It first closes the stdin pipe (via StdioTransport) so well-behaved servers exit on EOF. If the process doesn't exit within a short grace period, it sends SIGTERM. If SIGTERM doesn't work within the shutdown timeout, it escalates to SIGKILL.
The grace period before SIGTERM handles servers that don't exit on stdin EOF (e.g., started in HTTP mode by mistake). Without this, StdioTransport.Close() would block forever waiting for readLoop to see EOF on a stdout pipe held open by a still-running process.
func (*CommandTransport) Connect ¶ added in v0.1.14
func (ct *CommandTransport) Connect(ctx context.Context) error
Connect starts the subprocess and establishes the stdio transport. The context is used only to bound the connect/handshake phase — the subprocess outlives it. If the context expires before the handshake completes, Connect returns an error and Close() should be called to clean up the process.
func (*CommandTransport) Notify ¶ added in v0.1.14
Notify delegates to the underlying StdioTransport.
func (*CommandTransport) SessionID ¶ added in v0.1.14
func (ct *CommandTransport) SessionID() string
SessionID returns "command" for the command transport.
func (*CommandTransport) Stderr ¶ added in v0.1.14
func (ct *CommandTransport) Stderr() string
Stderr returns the captured stderr output from the subprocess. Safe to call after Close(). Returns an empty string if no stderr was captured.
type ConnState ¶ added in v0.4.0
type ConnState int
ConnState is the connection state of a Group member. It is the client-layer half of the agent's per-server status (see docs/AGENT_SERVER_STATE.md): a consumer (agent host, a script, cmd/testclient, a dashboard) reacts to transitions to register/deregister capabilities and to render status.
const ( // StateDisabled is a member that has been added but not yet started. StateDisabled ConnState = iota // StateConnecting is an in-flight connection attempt (initial or a retry). StateConnecting // StateReady is connected and initialized — capabilities are usable. StateReady // StateFailed is a connect failure that will be retried with backoff // (network refusal, 5xx, EOF). Err holds the last failure. StateFailed // StateNeedsLogin is a 401/403 rejection: the member needs user action // (login) and is NOT auto-retried, so a retry loop can't hammer the server. StateNeedsLogin )
type DiscoverResult ¶ added in v0.2.47
type DiscoverResult struct {
SupportedVersions []string `json:"supportedVersions"`
Capabilities core.ServerCapabilities `json:"capabilities"`
// ServerInfo is the server's self-reported identity. Since spec
// PR 3002 it arrives in _meta[io.modelcontextprotocol/serverInfo]
// (see Meta); adaptiveProbe copies it here so callers keep one
// stable accessor. The json tag doubles as a decode fallback for
// pre-3002 servers that still emit it in the result body.
ServerInfo core.Implementation `json:"serverInfo"`
// Instructions is the server's guidance for the model, a top-level
// field of the 2026-07-28 DiscoverResult.
Instructions string `json:"instructions,omitempty"`
// Meta carries the result-side _meta envelope (spec PR 3002).
Meta core.ResultMeta `json:"_meta"`
}
DiscoverResult mirrors stateless.DiscoverResult shape. Defined locally so client/ does not import server/stateless/ (the package boundary runs the other direction; client only knows wire types in core/).
Surfaced through the public Client.Discover() helper for ad-hoc callers (CLI inspectors, conformance harnesses) that want to introspect a server's discovery payload without going through the Connect handshake.
type ElicitationCompleteHandler ¶ added in v0.2.41
type ElicitationCompleteHandler func(context.Context, core.ElicitationCompleteParams)
ElicitationCompleteHandler handles a notifications/elicitation/complete notification. Called when the server signals that an out-of-band URL-mode elicitation flow has been completed. The client can use this to retry the original request.
type ElicitationHandler ¶
type ElicitationHandler func(context.Context, core.ElicitationRequest) (core.ElicitationResult, error)
ElicitationHandler handles a server-to-client elicitation/create request. The client prompts the user for input and returns the result. For URL-mode requests (Mode == "url"), the handler should present the URL to the user and return once acknowledged. The actual completion is signaled separately via notifications/elicitation/complete.
type Group ¶ added in v0.4.0
type Group struct {
// contains filtered or unexported fields
}
Group manages the connection lifecycle of N clients: it connects them concurrently, tracks per-member state, retries failed members with backoff, and lets a caller block until the members it marked required are ready. It is host-agnostic — the connection concern lives in client/ so any consumer can hold a Group, while the reaction to a member becoming ready (registering its tools/skills/events) stays with the consumer via an observer.
A Group does not create the clients; the caller builds each Client and hands it to Add. Close stops the retry loops and closes the member clients (the Group owns their lifetime once Add is called).
func NewGroup ¶ added in v0.4.0
func NewGroup(opts ...GroupOption) *Group
NewGroup builds a Group. Add members, then Start.
func (*Group) Add ¶ added in v0.4.0
Add registers c under id. A required member blocks WaitRequired until it is ready. Add must be called before Start; a duplicate id is ignored.
func (*Group) Close ¶ added in v0.4.0
Close stops the connect/retry loops and closes the member clients. It waits for the per-member goroutines to exit. Safe to call more than once.
func (*Group) Reconnect ¶ added in v0.4.0
Reconnect forces a stuck member to attempt connecting again now: it wakes a failed member that is sleeping between backoff retries, and un-parks a needs-login member (the caller has since logged in) whose loop is otherwise idle. A ready member (nothing to reconnect — a live drop is handled by the client internally), an in-flight connecting member, an unknown id, and a not-yet-started or closing Group are all no-ops. Safe for concurrent and repeated use — a redundant wake is dropped.
func (*Group) Start ¶ added in v0.4.0
Start kicks off an async connect loop per member and returns immediately — the caller is usable before any member is ready. Idempotent.
func (*Group) State ¶ added in v0.4.0
State returns a member's current state and whether the id is known.
func (*Group) Status ¶ added in v0.4.0
func (g *Group) Status() []MemberStatus
Status returns an ordered snapshot of every member, for a /servers-style view.
func (*Group) WaitRequired ¶ added in v0.4.0
WaitRequired blocks until every required member has reached ready, the context is cancelled, or the required-timeout elapses (returning an error naming the members that did not make it). With no required members it returns immediately. A zero timeout waits indefinitely.
func (*Group) WaitSettled ¶ added in v0.4.0
WaitSettled blocks until every member's first connect attempt has completed (reached ready, failed, or needs-login), the context is cancelled, or the settle-timeout elapses. Unlike WaitRequired, a timeout is NOT an error: it just means some server is still on its first attempt, and it will register via the observer when it finishes. This is the bounded initial-connect round a caller waits for so reachable servers are ready-and-registered at boot while a down server (fast failure) does not block.
type GroupOption ¶ added in v0.4.0
type GroupOption func(*Group)
GroupOption configures a Group.
func WithGroupBackoff ¶ added in v0.4.0
func WithGroupBackoff(min, max time.Duration) GroupOption
WithGroupBackoff sets the exponential backoff bounds for retrying a failed member. Defaults: 500ms to 30s.
func WithObserver ¶ added in v0.4.0
func WithObserver(fn func(StateChange)) GroupOption
WithObserver registers a callback invoked on every member state transition. It is called from the member's own goroutine, so it may be invoked concurrently for different members and must be safe for concurrent use; keep it quick or hand off, since a slow observer stalls that member's loop.
func WithRequiredTimeout ¶ added in v0.4.0
func WithRequiredTimeout(d time.Duration) GroupOption
WithRequiredTimeout sets how long WaitRequired blocks before failing with the required members that never reached ready. Default 30s; 0 means wait indefinitely (the "hang until this server is up, however long it takes" case).
func WithSettleTimeout ¶ added in v0.4.0
func WithSettleTimeout(d time.Duration) GroupOption
WithSettleTimeout caps how long WaitSettled waits for the initial connect round. Default 10s; 0 waits indefinitely. Reachable servers settle in well under this (a ready connect or a fast refusal); only a server that accepts the socket but never completes the handshake hits the cap, and then it just wires in late instead of blocking boot.
type HTTPStatusError ¶
type HTTPStatusError = ssehttp.HTTPStatusError
HTTPStatusError is returned when the server responds with a non-2xx HTTP status code that is not 401/403 (those are handled by DoWithAuthRetry). This allows IsTransientError to classify 5xx responses as retriable.
type InputHandler ¶ added in v0.2.41
type InputHandler func(ctx context.Context, reqs core.InputRequests) (core.InputResponses, error)
InputHandler resolves an MRTR InputRequiredResult's inputRequests into the echoed inputResponses payload. Called once per retry round; returning an error aborts the loop.
The map shape mirrors the wire contract: keys are server-chosen identifiers that MUST round-trip verbatim; values are opaque JSON payloads matching the InputRequest.Method (ElicitResult, CreateMessageResult, ListRootsResult, etc.).
func DefaultInputHandler ¶ added in v0.2.41
func DefaultInputHandler(c *Client) InputHandler
DefaultInputHandler returns an InputHandler that resolves the standard MRTR inputRequest methods using the client's existing capability handlers:
- "elicitation/create" → samplingElicit via elicitationHandler
- "sampling/createMessage" → samplingHandler
- "roots/list" → rootsHandler
Unknown methods produce an error. Returns an error from the underlying handler too — CallToolWithInputs propagates it and aborts the loop.
This is a starting point. Wrap or replace it for custom inputRequest methods, alternative routing, or to inject non-default response payloads (e.g., declining elicitation requests, returning canned sampling output in tests).
type ListenOptions ¶ added in v0.7.2
type ListenOptions struct {
// Notifications is the filter sent as params.notifications. Build it
// with core.ListChangedFilter for boolean keys and Set for others.
// Nil is sent as {}, which a server acknowledges with nothing honored.
Notifications core.SubscriptionFilter
// Handlers routes a notification to a function by its method. params
// is the notification's params as received, _meta included.
Handlers map[string]func(params json.RawMessage)
// OnNotification receives any notification with no entry in Handlers.
// Optional.
OnNotification func(method string, params json.RawMessage)
}
ListenOptions configures a Listen call.
type Listener ¶ added in v0.7.2
type Listener struct {
// contains filtered or unexported fields
}
Listener is an open subscriptions/listen stream.
func (*Listener) Done ¶ added in v0.7.2
func (l *Listener) Done() <-chan struct{}
Done is closed once the stream has ended and no handler will run again.
func (*Listener) Err ¶ added in v0.7.2
Err reports why the stream ended, once Done is closed. It is nil for Stop, for cancellation of the ctx passed to Listen, and for a graceful close by the server.
func (*Listener) Honored ¶ added in v0.7.2
func (l *Listener) Honored() core.SubscriptionFilter
Honored is the filter the server agreed to, from its acknowledgement. A key the caller requested and the server declined or did not recognise is absent. Never nil.
func (*Listener) Stop ¶ added in v0.7.2
func (l *Listener) Stop()
Stop closes the stream. Safe to call more than once. Wait on Done to know the reader goroutine has exited.
func (*Listener) SubscriptionID ¶ added in v0.7.2
func (l *Listener) SubscriptionID() json.RawMessage
SubscriptionID is the id the server stamps on this stream's notifications: the JSON-RPC id of the listen request, as raw JSON.
type MRTROption ¶ added in v0.2.41
type MRTROption func(*mrtrConfig)
MRTROption tunes CallToolWithInputs behavior.
func WithMaxMRTRRounds ¶ added in v0.2.41
func WithMaxMRTRRounds(n int) MRTROption
WithMaxMRTRRounds caps how many times CallToolWithInputs will retry a tools/call when the server keeps returning InputRequiredResult. Default is 16 (enough for any sane workflow; high enough that hitting it suggests a bug). Zero or negative values fall back to the default.
type MemberStatus ¶ added in v0.4.0
MemberStatus is an ordered snapshot entry from Group.Status, for rendering a /servers-style view without racing the live state.
type RPCError ¶ added in v0.2.41
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
RPCError is a JSON-RPC error returned by the server. It preserves the error code, message, and optional data field for structured error handling.
Use errors.As to extract it from a Call/ToolCall error:
var rpcErr *client.RPCError
if errors.As(err, &rpcErr) {
fmt.Println(rpcErr.Code, rpcErr.Data)
}
type RootsHandler
deprecated
added in
v0.1.24
RootsHandler handles a server-to-client roots/list request. The client returns its current filesystem roots.
Deprecated: per SEP-2577. Retained in 0.4; removal deferred to a future release (~2027 at the earliest, issue 850). See docs/SEP_2577_DEPRECATIONS.md.
type SamplingHandler
deprecated
type SamplingHandler func(context.Context, core.CreateMessageRequest) (core.CreateMessageResult, error)
SamplingHandler handles a server-to-client sampling/createMessage request. The client performs LLM inference and returns the result.
Deprecated: per SEP-2577. Retained in 0.4; removal deferred to a future release (~2027 at the earliest, issue 850). See docs/SEP_2577_DEPRECATIONS.md.
type ServerCardCache ¶ added in v0.8.22
type ServerCardCache struct {
// contains filtered or unexported fields
}
ServerCardCache fetches Server Cards and keeps the last copy of each with its ETag. Every Fetch revalidates with If-None-Match, so a 304 costs one round trip and no body. It is safe for concurrent use.
func NewServerCardCache ¶ added in v0.8.22
func NewServerCardCache(opts ...CardFetchOption) *ServerCardCache
NewServerCardCache returns an empty cache that fetches with opts.
func (*ServerCardCache) Fetch ¶ added in v0.8.22
func (c *ServerCardCache) Fetch(ctx context.Context, mcpURL string) (*core.ServerCard, error)
Fetch returns the card for mcpURL. On a 304 it returns the cached copy. The returned card is shared with the cache, so treat it as read-only. Errors wrap ErrCardInsecure, ErrCardCrossOrigin, a non-2xx status or a decode failure. A failed fetch keeps the previously cached copy.
type StateChange ¶ added in v0.4.0
type StateChange struct {
ID string
State ConnState
Err error // set for StateFailed / StateNeedsLogin
}
StateChange is delivered to a Group observer on every member transition.
type StdioFraming ¶ added in v0.7.2
type StdioFraming = stdioframe.Framing
StdioFraming selects how a stdio transport delimits messages.
type StdioTransport ¶
type StdioTransport struct {
// contains filtered or unexported fields
}
StdioTransport implements core.Transport over stdio-framed JSON-RPC: one JSON message per line by default, or Content-Length headers with WithStdioFraming(FramingContentLength).
func NewStdioTransport ¶
func NewStdioTransport(r io.Reader, w io.Writer, opts ...StdioTransportOption) *StdioTransport
NewStdioTransport creates a client transport that communicates via newline-delimited JSON-RPC over the given reader/writer pair, or in the framing chosen with WithStdioFraming.
Typically the reader is connected to the server's stdout and the writer to the server's stdin (or pipe ends in tests).
Example:
cmd := exec.Command("my-mcp-server")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
cmd.Start()
transport := client.NewStdioTransport(stdout, stdin)
c := client.NewClient("stdio://", info, client.WithTransport(transport))
func (*StdioTransport) Close ¶
func (t *StdioTransport) Close() error
Close shuts down the transport and waits for the read loop to exit.
func (*StdioTransport) Connect ¶
func (t *StdioTransport) Connect(ctx context.Context) error
Connect starts the background read loop.
func (*StdioTransport) SessionID ¶
func (t *StdioTransport) SessionID() string
SessionID returns "stdio" for the stdio transport.
type StdioTransportOption ¶ added in v0.7.2
type StdioTransportOption func(*StdioTransport)
StdioTransportOption configures a StdioTransport.
func WithStdioFraming ¶ added in v0.7.2
func WithStdioFraming(f StdioFraming) StdioTransportOption
WithStdioFraming sets the framing the transport writes and expects back. The client speaks first, so it cannot detect the server's framing; the default is FramingNewlineDelimited.
type TaskCallOptionsV1 ¶ added in v0.2.41
type TaskCallOptionsV1 struct {
// TTL in milliseconds. 0 = server default.
TTL int
// PollInterval in milliseconds. 0 = server default.
PollInterval int
// ProgressToken is passed as _meta.progressToken so the server
// echoes it in notifications/progress. Nil = no token.
ProgressToken any
}
TaskCallOptionsV1 configures a ToolCallAsTaskV1 invocation. Nil means use server defaults for everything.
type ToolCallResult ¶ added in v0.2.41
type ToolCallResult struct {
// Sync is populated when the server ran the tool to completion in the
// same request and returned a ToolResult directly (resultType:
// "complete" or absent).
Sync *core.ToolResult
// Task is populated when the server elected to create a task (the
// resultType: "task" discriminator was present on the response).
Task *core.CreateTaskResult
// InputRequired is populated when the server returned an SEP-2322
// InputRequiredResult — it needs more input before it can produce a
// final result. Callers using the bare ToolCall must handle this
// themselves (resolve inputRequests, retry tools/call with
// inputResponses + requestState); CallToolWithInputs handles the
// loop automatically. Renamed from Incomplete in lockstep with
// SEP-2322. The `requestState` echo on retry remains valid for the
// MRTR surface even though the tasks-v2 wire no longer carries it.
InputRequired *core.InputRequiredResult
}
ToolCallResult is the discriminated union returned by ToolCall. Exactly one of Sync, Task, or InputRequired is non-nil — branch on which is set (or use the IsTask / IsInputRequired helpers).
func CallToolWithInputs ¶ added in v0.2.41
func CallToolWithInputs(ctx context.Context, c *Client, name string, args any, handler InputHandler, opts ...MRTROption) (*ToolCallResult, error)
CallToolWithInputs invokes a tool with automatic SEP-2322 MRTR retry. On InputRequiredResult, the handler is called to resolve the inputRequests; the call is then retried with inputResponses + the echoed requestState. The loop terminates as soon as the server returns a complete ToolResult, a CreateTaskResult, or an error. Returns ErrMRTRMaxRounds if the round cap (default 16) is hit.
Pass DefaultInputHandler(c) to handle the standard inputRequest methods using the client's existing capability handlers.
func ToolCall ¶ added in v0.2.41
ToolCall invokes a tool, transparently handling both the sync ToolResult and the SEP-2663 CreateTaskResult shapes. Branch on result.IsTask() (or the Sync / Task fields directly) to know which arrived.
Servers gate task creation on the io.modelcontextprotocol/tasks extension, so this only ever returns a Task result if the client declared the extension during initialize (or per-request via SEP-2575 _meta).
func (*ToolCallResult) IsInputRequired ¶ added in v0.2.46
func (r *ToolCallResult) IsInputRequired() bool
IsInputRequired reports whether the server returned an InputRequiredResult (SEP-2322 ephemeral MRTR). Callers needing the auto-retry loop should use CallToolWithInputs instead of inspecting this directly. Renamed from IsIncomplete to track the SEP-2322 wire-variant rename.
func (*ToolCallResult) IsTask ¶ added in v0.2.41
func (r *ToolCallResult) IsTask() bool
IsTask reports whether the result is the task-creation variant.
type UnsupportedDiscoverError ¶ added in v0.2.47
type UnsupportedDiscoverError struct {
// Source describes how the server signaled the gap, useful for
// diagnostics. Either "jsonrpc-32601" or "http-404".
Source string
}
UnsupportedDiscoverError is returned by Client.Discover() when the server reports it does not implement server/discover (either via JSON-RPC -32601 in a 200 body, which is what mcpkit's legacy dispatcher emits, or via HTTP 404 from a transport that does not route the method). Callers branch on this to fall back to the legacy initialize handshake when appropriate.
func (*UnsupportedDiscoverError) Error ¶ added in v0.2.47
func (e *UnsupportedDiscoverError) Error() string
type WaitOptions ¶ added in v0.2.41
type WaitOptions struct {
// PollInterval overrides the server's PollIntervalMs hint.
// 0 (the default) means: respect whatever the server returned, with a
// 1-second floor and a 30-second cap if the server didn't say.
PollInterval time.Duration
// OnStatus observes every polled snapshot (WaitForTask and
// WaitForTaskWithInput), including the input_required snapshot before
// any handler runs. Progress display, logging, and metrics hang off
// this; returning is the only control (observation, not steering).
// Nil skips.
OnStatus func(*core.DetailedTask)
}
WaitOptions configures WaitForTask.
Source Files
¶
- client.go
- client_logging.go
- client_reconnect.go
- command_transport.go
- elicitation_defaults.go
- elicitation_validation.go
- file_input.go
- group.go
- headers.go
- iterators.go
- listen.go
- middleware.go
- mrtr.go
- recover.go
- servercard.go
- stateless.go
- stateless_mode.go
- stdio_transport.go
- tasks.go
- tasks_v1.go
- trace_middleware.go
- version_negotiate.go