mcp

package
v0.0.0-...-a80ce91 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package mcp connects an agent to Model Context Protocol (MCP) servers and exposes their tools through the SDK's tools.Tool interface. Servers are declared in configuration (never chosen by the model) and connected at load time over either the stdio or streamable-HTTP transport, using the official github.com/modelcontextprotocol/go-sdk client.

Index

Constants

View Source
const (
	TransportStdio = "stdio"
	TransportHTTP  = "http"
)

Transport names.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a connected MCP session and its adapted tools.

func Connect

func Connect(ctx context.Context, cfg ServerConfig, logger *slog.Logger) (*Client, error)

Connect dials the server described by cfg and lists its tools. A nil logger disables logging.

func (*Client) Close

func (c *Client) Close() error

Close terminates the session (and, for stdio, the subprocess).

func (*Client) Tools

func (c *Client) Tools() []tools.Tool

Tools returns the server's tools adapted to the tools.Tool interface.

type Manager

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

Manager holds connections to multiple MCP servers and aggregates their tools.

func ConnectAll

func ConnectAll(ctx context.Context, cfgs []ServerConfig, logger *slog.Logger) (*Manager, error)

ConnectAll connects to every server in cfgs. If any connection fails, all already-opened connections are closed and the error is returned. A nil logger disables logging.

func (*Manager) Close

func (m *Manager) Close() error

Close closes all server connections, returning the first error encountered.

func (*Manager) Tools

func (m *Manager) Tools() []tools.Tool

Tools returns the combined tools from every connected server.

type ServerConfig

type ServerConfig struct {
	// Name namespaces the server's tools (exposed as "<name>_<tool>").
	Name string `yaml:"name"`
	// Transport is "stdio" or "http".
	Transport string `yaml:"transport"`

	// Command is the subprocess to run for the stdio transport, e.g.
	// ["npx", "-y", "@modelcontextprotocol/server-github"].
	Command []string `yaml:"command"`
	// Env sets extra environment variables for a stdio server (in addition to
	// the inherited process environment). Values are expanded with $VAR syntax.
	Env map[string]string `yaml:"env"`

	// URL is the endpoint for the http (streamable-HTTP) transport.
	URL string `yaml:"url"`
	// Headers are sent on every HTTP request. Values are expanded with $VAR.
	Headers map[string]string `yaml:"headers"`

	// Tools optionally restricts which of the server's tools are exposed
	// (by raw tool name). Empty means expose all.
	Tools []string `yaml:"tools"`
}

ServerConfig declares a single MCP server.

Jump to

Keyboard shortcuts

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