Documentation
¶
Overview ¶
Package mcptool provides integration with the Model Context Protocol (MCP). It allows agents to connect to external MCP servers via stdio (subprocess) or HTTP (SSE / streamable HTTP) and expose their tools as tool.Tool / tool.FuncTool instances.
Index ¶
- func AddTool(src *mcp.Server, tl tool.FuncTool)
- func Connect(ctx context.Context, transport mcp.Transport) (*mcp.ClientSession, error)
- func ListTools(ctx context.Context, session *mcp.ClientSession) ([]tool.Tool, error)
- func ListToolsWithTasks(ctx context.Context, session *mcp.ClientSession, options TaskOptions) ([]tool.Tool, error)
- type TaskInputHandler
- type TaskOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTool ¶
AddTool registers a tool.FuncTool on the given mcp.Server so it is exposed to MCP clients. Native mcp.Content and []mcp.Content results are returned as MCP content blocks; they must contain content types valid in a tool response. Nil content entries are represented as the text "null". A *mcp.CallToolResult is returned as-is. Array return schemas and their structured results are wrapped in an object with a "result" property so structured output also works with older MCP clients. The text content retains the original JSON for existing clients. Explicit MCP results and MCP/framework content bypass this wrapping. When an output schema is advertised, callers returning these types must provide structured content matching that schema for successful results.
func Connect ¶
Connect dials an MCP server over the given transport and returns a client session with task-extension support.
func ListTools ¶
ListTools enumerates all pages of the remote server's tools and wraps each as a tool.Tool.
func ListToolsWithTasks ¶ added in v0.3.1
func ListToolsWithTasks(ctx context.Context, session *mcp.ClientSession, options TaskOptions) ([]tool.Tool, error)
ListToolsWithTasks lists the server's tools as task-aware functions. Calls to tools that return an MCP task are polled until completion; servers that do not create tasks can still return results inline. The session must be created with Connect so the task RPC methods are registered. Zero-valued options use the defaults documented on TaskOptions. The options and their capability declarations are copied before listing; subsequent changes do not affect the returned tools. Task input handling requires both a handler and explicit input capabilities in options.
See https://modelcontextprotocol.io/extensions/tasks/overview.
Types ¶
type TaskInputHandler ¶ added in v0.3.1
type TaskInputHandler func(context.Context, mcp.InputRequest) (mcp.InputResponse, error)
TaskInputHandler resolves an input request from a running MCP task. It must support the capabilities declared in TaskOptions and be safe for concurrent calls when tools are invoked concurrently.
type TaskOptions ¶ added in v0.3.1
type TaskOptions struct {
// DisableCancelRemoteTaskOnLocalCancellation leaves the remote task running
// when the local invocation is canceled. When false, the tool makes a
// best-effort attempt to cancel the remote task.
DisableCancelRemoteTaskOnLocalCancellation bool
// MaxConsecutiveStuckPolls limits consecutive input_required polls
// without new input requests. Zero defaults to 60.
MaxConsecutiveStuckPolls int
// MaxTotalInputRequests limits unique input requests per invocation.
// Zero defaults to 100.
MaxTotalInputRequests int
// RemoteCancellationTimeout bounds a remote cancellation attempt.
// Zero defaults to 5 seconds.
RemoteCancellationTimeout time.Duration
// MinimumPollingInterval is the shortest accepted server polling interval.
// Zero defaults to 10 milliseconds.
MinimumPollingInterval time.Duration
// MaximumPollingInterval is the longest accepted server polling interval.
// Zero uses the largest whole-millisecond [time.Duration].
MaximumPollingInterval time.Duration
// InputHandler resolves task input requests. It must be configured together
// with ElicitationCapabilities, SamplingCapabilities, or both.
InputHandler TaskInputHandler
// ElicitationCapabilities declares the elicitation modes supported by
// InputHandler. Nil declares no support; an empty value declares form
// elicitation. The declaration is sent only on task-aware requests.
ElicitationCapabilities *mcp.ElicitationCapabilities
// SamplingCapabilities declares the sampling features supported by
// InputHandler. Nil declares no support; an empty value declares basic
// sampling without context or tools. The declaration is sent only on
// task-aware requests.
SamplingCapabilities *mcp.SamplingCapabilities //nolint:staticcheck // MCP sampling remains supported during the SEP-2577 deprecation window.
}
TaskOptions configures the task lifecycle for tools returned by ListToolsWithTasks. The zero value uses the default lifecycle settings. Options and capability declarations are copied when tools are listed.