mcp

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotConnected is returned when a tool call is attempted on a disconnected server.
	ErrNotConnected = errors.New("mcp server not connected")

	// ErrAlreadyConnected is returned when Connect is called on an already-connected server.
	ErrAlreadyConnected = errors.New("mcp server already connected")

	// ErrInvalidConfig is returned when the server configuration is invalid.
	ErrInvalidConfig = errors.New("invalid mcp configuration")
)

Functions

This section is empty.

Types

type HTTPTransportConfig

type HTTPTransportConfig struct {
	// URL is the MCP server endpoint.
	// When URLFunc is also set, URL is used only for initial tool discovery
	// at startup; all subsequent tool calls route through URLFunc.
	URL string

	// URLFunc dynamically resolves the MCP server URL per tool execution.
	// It receives the tool execution context which carries run variables
	// (tenant_id, user_id, etc.) accessible via tool.GetVariable().
	// When set, the MCPServer pools and reuses sessions per resolved URL.
	// If nil, all tool calls use the static URL.
	URLFunc func(ctx context.Context) (string, error)

	// HTTPClient allows full control over the HTTP client used for requests.
	// Users can configure TLS, timeouts, and inject auth via custom
	// RoundTripper middleware.
	// If nil, a default client is used.
	HTTPClient *http.Client

	// Headers are additional HTTP headers sent with every request.
	// Use for static auth tokens: {"Authorization": "Bearer xxx"}.
	Headers map[string]string

	// HeaderFunc is called before each request to produce dynamic headers.
	// This takes precedence over static Headers for the same key.
	// Useful for OAuth tokens that rotate.
	HeaderFunc func() (map[string]string, error)
}

HTTPTransportConfig configures a Streamable HTTP MCP server connection.

type MCPServer

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

MCPServer represents a connection to a single MCP server. It manages the connection lifecycle and exposes discovered tools as tool.Tool implementations.

func NewMCPServer

func NewMCPServer(config *MCPServerConfig) (*MCPServer, error)

NewMCPServer creates a new MCP server connection manager. Does not connect yet — call Connect() to establish the connection, or use RegisterServer() for convenience.

func RegisterServer

func RegisterServer(ctx context.Context, registrar ToolRegistrar, config *MCPServerConfig) (*MCPServer, error)

RegisterServer connects to an MCP server, discovers tools, and registers them with the given registrar (typically *agentpg.Client). Returns the MCPServer for lifecycle management. The caller must call Close() when the server is no longer needed (typically via defer).

Must be called before client.Start().

func RegisterServerLazy added in v0.3.3

func RegisterServerLazy(registrar ToolRegistrar, config *MCPServerConfig) (*MCPServer, error)

RegisterServerLazy creates an MCPServer without connecting. Tools are discovered and registered lazily on the first tool call (or via an explicit EnsureConnected call). This is useful for multi-tenant setups where the MCP URL is only known at request time via URLFunc.

func (*MCPServer) Close

func (s *MCPServer) Close() error

Close shuts down the MCP server connection and all pooled sessions.

func (*MCPServer) Connect

func (s *MCPServer) Connect(ctx context.Context) error

Connect establishes a connection to the MCP server and discovers tools.

func (*MCPServer) EnsureConnected added in v0.3.3

func (s *MCPServer) EnsureConnected(ctx context.Context) error

EnsureConnected connects to the MCP server if not already connected. Idempotent and thread-safe — safe to call from multiple goroutines. When URLFunc is set and URL is empty, the first call resolves the discovery URL from URLFunc(ctx).

func (*MCPServer) Name

func (s *MCPServer) Name() string

Name returns the server name from the configuration.

func (*MCPServer) Tools

func (s *MCPServer) Tools() []tool.Tool

Tools returns all discovered tools as tool.Tool implementations.

type MCPServerConfig

type MCPServerConfig struct {
	// Name is the unique identifier for this MCP server.
	// Used as namespace prefix for tools: "{Name}__{tool_name}".
	// Must be non-empty.
	Name string

	// Transport: exactly one of Stdio or HTTP must be set.
	Stdio *StdioTransportConfig
	HTTP  *HTTPTransportConfig

	// DisableToolPrefix disables tool name prefixing.
	// When false (default), tools are registered as "{Name}__{tool_name}".
	// When true, tools use their original MCP names (risk of collision).
	DisableToolPrefix bool

	// ToolFilter optionally filters which tools to expose from this server.
	// Receives the original MCP tool name (without prefix).
	// Return true to include the tool, false to exclude it.
	// If nil, all tools are exposed.
	ToolFilter func(toolName string) bool

	// Reconnect configures automatic reconnection behavior.
	// If nil, no automatic reconnection is performed.
	Reconnect *ReconnectConfig
}

MCPServerConfig configures a connection to an MCP server.

type MCPTool

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

MCPTool bridges an MCP server tool to the agentpg tool.Tool interface. It is created by MCPServer during tool discovery.

func (*MCPTool) Description

func (t *MCPTool) Description() string

Description returns the tool description from the MCP server.

func (*MCPTool) Execute

func (t *MCPTool) Execute(ctx context.Context, input json.RawMessage) (string, error)

Execute calls the MCP server's tool and returns the text result.

func (*MCPTool) InputSchema

func (t *MCPTool) InputSchema() tool.ToolSchema

InputSchema returns the tool's input schema converted to agentpg format.

func (*MCPTool) Name

func (t *MCPTool) Name() string

Name returns the namespaced tool name.

type ReconnectConfig

type ReconnectConfig struct {
	// MaxRetries is the maximum number of reconnection attempts.
	// 0 means unlimited retries.
	// Default: 0 (unlimited)
	MaxRetries int

	// InitialDelay is the delay before the first reconnection attempt.
	// Default: 1 second
	InitialDelay time.Duration

	// MaxDelay caps the exponential backoff.
	// Default: 30 seconds
	MaxDelay time.Duration
}

ReconnectConfig controls automatic reconnection to MCP servers.

type StdioTransportConfig

type StdioTransportConfig struct {
	// Command is the path to the MCP server executable.
	Command string

	// Args are the command-line arguments.
	Args []string

	// Env are additional environment variables for the subprocess.
	// These are appended to os.Environ().
	// Use this for passing API keys, tokens, etc.
	Env []string

	// Dir is the working directory for the subprocess.
	// If empty, the current working directory is used.
	Dir string
}

StdioTransportConfig configures a stdio (subprocess) MCP server connection.

type ToolRegistrar

type ToolRegistrar interface {
	RegisterTool(t tool.Tool) error
}

ToolRegistrar is the minimal interface for registering tools. Satisfied by *agentpg.Client[TTx].

Jump to

Keyboard shortcuts

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