mcp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 14 Imported by: 0 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 mcp exposes the tools of an MCP (Model Context Protocol) server as a toolset; add a Client to Agent.Toolsets to use it.

It is built on the official Go SDK, which negotiates the protocol version (the stateless 2026-07-28 revision, falling back to the initialize handshake of older servers). Only tools are supported: resources, prompts, sampling, elicitation and roots are not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotations

type Annotations struct {
	// Title is a human-readable name of the tool.
	Title string
	// ReadOnly reports that the tool does not modify its environment.
	ReadOnly bool
	// Destructive reports that the tool may perform destructive updates; meaningful only when ReadOnly is false.
	Destructive bool
	// Idempotent reports that repeating a call with the same arguments has no additional effect.
	Idempotent bool
	// OpenWorld reports that the tool interacts with external entities.
	OpenWorld bool
}

Annotations are the behavior hints a server declares for a tool, with the protocol defaults filled in. They are untrusted unless the server is trusted, so the framework does not act on them.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client connects to one MCP server and provides its tools as a tool.Toolset. One Client can be shared by any number of sessions and runs; Close it when it is no longer needed.

The connection is made on the first run and checked at the start of every run: when it has dropped, the Client reconnects. A server that cannot be reached does not fail the run: it contributes no tools, and the system prompt tells the model it is unavailable.

Tools are named mcp__{server}__{tool}, with characters other than letters, digits, '_' and '-' replaced by '_'. A tool whose name is still invalid (longer than 64 characters, or colliding with another tool of the server) is skipped and reported in the system prompt. The tool list is cached until the server reports a change or, on servers of the 2026-07-28 revision, the cache TTL it gives expires.

func New

func New(name string, transport Transport, opts ...Option) *Client

New creates a Client for the server reached through transport. name identifies the server in tool names and the system prompt; it should be short and unique among the servers of an Agent. New does not connect. An empty name is a programming error and panics.

func (*Client) Close

func (c *Client) Close() error

Close disconnects from the server, stopping the subprocess of a Stdio transport. The Client cannot be used afterwards.

func (*Client) Instructions

func (c *Client) Instructions(context.Context, tool.RunInfo) (string, error)

Instructions returns the server's instructions together with notes on skipped tools, or explains why the server is unavailable.

func (*Client) Tools

func (c *Client) Tools(ctx context.Context, _ tool.RunInfo) ([]tool.Tool, error)

Tools connects when needed and returns the server's tools. When the server cannot be reached it returns no tools and no error.

type Option

type Option func(*Client)

Option configures a Client.

func WithFilter

func WithFilter(keep func(name string) bool) Option

WithFilter exposes only the tools for which keep returns true; it receives the tool's name on the server.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the timeout of every call to this server's tools, taking precedence over Agent.ToolTimeout.

func WithoutInstructions

func WithoutInstructions() Option

WithoutInstructions stops the server's instructions from being appended to the system prompt.

type Tool

type Tool struct {
	// contains filtered or unexported fields
}

Tool is a tool of an MCP server. Middlewares can type-assert a tool.Tool to *Tool to see which server it belongs to and the behavior hints the server declared.

func (*Tool) Annotations

func (t *Tool) Annotations() Annotations

Annotations returns the behavior hints the server declared for the tool.

func (*Tool) Def

func (t *Tool) Def() chat.ToolDef

func (*Tool) Execute

func (t *Tool) Execute(ctx *tool.Context, args json.RawMessage) (*tool.Result, error)

Execute calls the tool on the server. A protocol or connection error is returned as an error, which the Agent gives to the model.

func (*Tool) RemoteName

func (t *Tool) RemoteName() string

RemoteName returns the tool's name on the server.

func (*Tool) Server

func (t *Tool) Server() string

Server returns the name of the server the tool belongs to, as given to New.

func (*Tool) Timeout

func (t *Tool) Timeout() time.Duration

Timeout implements tool.Timeouter; zero leaves the Agent's default in place.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is how a Client reaches its server. A new connection is made from it every time the Client connects.

func HTTP

func HTTP(endpoint string, client *http.Client) Transport

HTTP connects to a remote server over the Streamable HTTP transport. A nil client uses http.DefaultClient; credentials such as a bearer token are added by the client's RoundTripper.

func Stdio

func Stdio(newCmd func() *exec.Cmd) Transport

Stdio runs the server as a local subprocess and talks to it over stdin and stdout. newCmd is called on every connection, because an exec.Cmd can only be started once.

Jump to

Keyboard shortcuts

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