mcptools

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 27 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcptools exposes tools of configured MCP servers (MCP_SERVERS).

Index

Constants

View Source
const DefaultCallTimeout = 120 * time.Second

DefaultCallTimeout bounds one MCP tool call (MCP_CALL_TIMEOUT_SECONDS).

View Source
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

View Source
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).

View Source
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

func SafeCheckRedirect(req *http.Request, via []*http.Request) error

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

func SafeTransport() *http.Transport

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

func StdioCommand(s config.MCPServer) *exec.Cmd

StdioCommand prepares the child process of a stdio server with a reduced environment.

func ToolName

func ToolName(server, name string) string

ToolName is the exposed name of an MCP tool: {server}__{tool}, sanitized, ≤ 64 chars.

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 New

func New(servers []config.MCPServer, version string, maxResultChars int) *Provider

New returns nil when no servers are configured.

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.

func (*Provider) Describe

func (p *Provider) Describe(toolName string) string

Describe is the text the approval of a pending server's tool shows: the project, the server, its exact command and arguments and the names of its environment variables (never their values). "" when toolName names no pending server of this provider.

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) Tools

func (p *Provider) Tools(ctx context.Context, rc tools.RunContext) ([]ai.BaseTool, func(), error)

Tools implements tools.Provider. Failing servers are logged and skipped.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL