Documentation
¶
Overview ¶
Package mcptools exposes tools of configured MCP servers (MCP_SERVERS).
Index ¶
- Constants
- Variables
- func Connect(ctx context.Context, s Server, version string, connectTimeout time.Duration, ...) ([]ai.BaseTool, *mcp.ClientSession, error)
- func SafeCheckRedirect(req *http.Request, via []*http.Request) error
- func SafeTransport() *http.Transport
- func StdioCommand(s config.MCPServer) *exec.Cmd
- func ToolName(server, name string) string
- type Provider
- type Server
- type ToolInfo
Constants ¶
const DefaultCallTimeout = 120 * time.Second
DefaultCallTimeout bounds one MCP tool call (MCP_CALL_TIMEOUT_SECONDS).
const MaxResultChars = 1 << 20
MaxResultChars is the result cap that leaves large results to the agent's spill (harness v3 §2: the full output goes to a file, nothing is cut silently).
Variables ¶
var ErrNotApproved = errors.New("the project's stdio MCP server is not approved: approve it when asked, or run `harness trust mcp <name>`")
ErrNotApproved means a project's stdio server was not spawned: its command has no approval (or a changed one).
var ErrRejected = errors.New("the server rejected the credentials")
ErrRejected is wrapped by the error of ListTools when the server answered 401 or 403: the credentials were not accepted.
Functions ¶
func Connect ¶
func Connect(ctx context.Context, s Server, version string, connectTimeout time.Duration, maxResultChars int) ([]ai.BaseTool, *mcp.ClientSession, error)
Connect opens a session to s and returns its allowed tools, exposed as ToolName(server, tool) with results capped at maxResultChars. connectTimeout bounds connect + list tools; the session (closed by the caller) outlives it.
func SafeCheckRedirect ¶ added in v0.5.0
SafeCheckRedirect is the http.Client.CheckRedirect of a guarded client: at most 3 redirects, never from https to http. The transport refuses a redirect to a blocked address when it dials.
func SafeTransport ¶ added in v0.5.0
SafeTransport is the transport for a server a user supplied: it refuses to connect to a loopback, private, link-local (cloud metadata included), CGNAT, multicast or unspecified address, or to an IPv6 form that embeds an IPv4 one. It ignores the proxy environment (a proxy would connect for us and hide the destination from the check) and is never http.DefaultTransport.
func StdioCommand ¶
StdioCommand prepares the child process of a stdio server with a reduced environment.
Types ¶
type Provider ¶
type Provider struct {
// ConnectTimeout bounds connect + list tools per server.
ConnectTimeout time.Duration
// MaxResultChars caps a tool result (officialmcp result policy).
MaxResultChars int
// CallTimeout bounds one tool call (0: DefaultCallTimeout).
CallTimeout time.Duration
// TrustDir is the user directory holding trust.json. A stdio server a project defines
// (config.MCPServer.ProjectRoot) is only spawned when the user approved its exact command there
// (config.MCPApproved); config.Load already withholds the others, this is the check at the spawn.
TrustDir string
// Pending are the project stdio servers that wait for the user's approval (config.Layers.MCPPending).
// Ask offers the model a tool that asks for it (approve.go); set it only when the run's approval gate
// pauses such a call and refuses it where nobody can answer (approvals.Gate). History reads a thread's
// messages so a refused question is not asked again on the thread.
Pending []config.MCPServer
Ask bool
History func(ctx context.Context, threadID string) ([]*store.Message, error)
// Dynamic, when set, supplies more servers for each run (the marketplace: servers from the database for the
// run's project or user). Every server it returns is treated as Untrusted. A failure is logged and the run
// goes on with the other servers; a server whose tool prefix is already taken is skipped.
Dynamic func(ctx context.Context, threadID, login string) ([]config.MCPServer, error)
// contains filtered or unexported fields
}
Provider connects every configured MCP server per run (identity is per user).
func NewDynamic ¶ added in v0.5.0
func NewDynamic(dynamic func(ctx context.Context, threadID, login string) ([]config.MCPServer, error), version string, maxResultChars int) *Provider
NewDynamic is a provider with no configured server, only the ones dynamic returns for each run.
func NewWithPending ¶
func NewWithPending(servers, pending []config.MCPServer, version string, maxResultChars int) *Provider
NewWithPending is New for a configuration that also has project stdio servers waiting for approval; it returns nil when there is neither.
type Server ¶
type Server struct {
Name string
URL string
// Tools is the allow-list of the server's tools (empty: all of them).
Tools []string
// Transport adds the server's auth headers (nil: http.DefaultTransport).
Transport http.RoundTripper
// CallTimeout bounds one tool call (0: DefaultCallTimeout).
CallTimeout time.Duration
// Untrusted says the server was supplied by a user: redirects are limited (SafeCheckRedirect) and, without a
// Tools allow-list, only the tools annotated readOnlyHint are offered. The caller supplies the guarded Transport.
Untrusted bool
// SSE connects with the legacy HTTP+SSE transport instead of streamable HTTP.
SSE bool
// ClientTransport, when set, replaces the HTTP transports (a child process, an in-memory pipe in tests).
ClientTransport mcp.Transport
// contains filtered or unexported fields
}
Server is an MCP server connected for one run.
type ToolInfo ¶ added in v0.5.0
type ToolInfo struct {
Name string
Description string
// ReadOnly is the server's own readOnlyHint annotation.
ReadOnly bool
}
ToolInfo describes one tool a server offers.
func ListTools ¶ added in v0.5.0
func ListTools(ctx context.Context, s config.MCPServer, version string, timeout time.Duration) ([]ToolInfo, error)
ListTools connects to a database-supplied server (guarded transport, no stdio; s.Type "sse" or streamable HTTP, s.Headers sent) and lists ALL its tools, ignoring s.Tools. The session is closed before it returns. An authentication failure wraps ErrRejected.