mcp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultConnectTimeout bounds starting a server, the initialize
	// handshake and listing its tools, so a server that never answers cannot
	// hold up its caller.
	DefaultConnectTimeout = 30 * time.Second
	// DefaultCallTimeout bounds one tool call to a server, unless the
	// manager (WithCallTimeout) or the server (call_timeout_seconds) sets
	// another bound.
	DefaultCallTimeout = 5 * time.Minute
)

Variables

View Source
var ErrSessionLost = errors.New("the server lost its session during this call; the tool may or may not have run")

ErrSessionLost is the error of a tool call during which the server lost its session. The call is not sent again: the tool may already have run.

Functions

func WithProgress

func WithProgress(ctx context.Context, fn ProgressFunc) context.Context

WithProgress returns a context whose tool calls send the server's progress notifications to fn.

Types

type Manager

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

Manager manages multiple MCP server connections

func NewManager

func NewManager(opts ...ManagerOption) *Manager

NewManager creates a new MCP manager

func (*Manager) CallTimeout

func (m *Manager) CallTimeout(serverName string) time.Duration

CallTimeout returns how long a tool call to the named server may take: its call_timeout_seconds, or else the manager's call timeout.

func (*Manager) CallTool

func (m *Manager) CallTool(
	ctx context.Context,
	serverName, toolName string,
	arguments map[string]any,
) (*mcp.CallToolResult, error)

CallTool calls a tool on a specific server. Every call carries a progress token; the server's progress notifications go to the ProgressFunc of ctx (WithProgress).

A call during which the server lost its session is not sent again, since the tool may have run: CallTool reconnects for the calls that follow and returns ErrSessionLost. Only a tool that a trusted server marks read-only or idempotent is called once more, on the new session.

func (*Manager) Close

func (m *Manager) Close() error

Close closes all server connections. It waits a bounded time for calls in flight and for the servers to stop: a hung server cannot keep Compa from shutting down or reloading.

func (*Manager) ConnectServer

func (m *Manager) ConnectServer(
	ctx context.Context,
	name string,
	cfg config.MCPServerConfig,
) error

ConnectServer connects to a single MCP server. ctx bounds connecting only: the server, once connected, runs until Close, whatever happens to ctx -- callers such as a config reload pass contexts that end as soon as they return.

func (*Manager) GetAllTools

func (m *Manager) GetAllTools() map[string][]*mcp.Tool

GetAllTools returns all tools from all connected servers

func (*Manager) GetServer

func (m *Manager) GetServer(name string) (*ServerConnection, bool)

GetServer returns a specific server connection

func (*Manager) GetServers

func (m *Manager) GetServers() map[string]*ServerConnection

GetServers returns all connected servers

func (*Manager) LoadFromConfig

func (m *Manager) LoadFromConfig(ctx context.Context, cfg *config.Config) error

LoadFromConfig loads MCP servers from configuration

func (*Manager) LoadFromMCPConfig

func (m *Manager) LoadFromMCPConfig(
	ctx context.Context,
	mcpCfg config.MCPConfig,
	workspacePath string,
) error

LoadFromMCPConfig loads MCP servers from MCP configuration and workspace path. This is the minimal dependency version that doesn't require the full Config object. Relative env_file and cwd paths are taken from workspacePath, and a stdio server without a cwd starts in it.

func (*Manager) ServerTools

func (m *Manager) ServerTools(name string) []*mcp.Tool

ServerTools returns the tools a connected server lists.

type ManagerOption

type ManagerOption func(*Manager)

ManagerOption configures an MCP manager.

func WithCallTimeout

func WithCallTimeout(timeout time.Duration) ManagerOption

WithCallTimeout bounds one tool call to a server without a call_timeout_seconds of its own. 0 or less keeps DefaultCallTimeout.

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) ManagerOption

WithConnectTimeout bounds connecting to one server: starting it, the initialize handshake and listing its tools. 0 or less keeps DefaultConnectTimeout.

func WithRoots

func WithRoots(dirs ...string) ManagerOption

WithRoots has the servers told dirs as the client's roots: the folders they may work in.

func WithRuntimeEvents

func WithRuntimeEvents(eventBus runtimeevents.Bus) ManagerOption

WithRuntimeEvents injects the runtime event bus used for MCP observations.

func WithToolsChangedHandler

func WithToolsChangedHandler(handler ToolsChangedFunc) ManagerOption

WithToolsChangedHandler has handler told the tools of a server that announces its tool list changed (notifications/tools/list_changed), and of a server reconnected, which may list other tools than before.

type ProgressFunc

type ProgressFunc func(*mcp.ProgressNotificationParams)

ProgressFunc receives the progress notifications of one tool call.

type ServerConnection

type ServerConnection struct {
	Name    string
	Config  config.MCPServerConfig
	Client  *mcp.Client
	Session *mcp.ClientSession
	Tools   []*mcp.Tool
	// contains filtered or unexported fields
}

ServerConnection represents a connection to an MCP server

type ServerEventPayload

type ServerEventPayload struct {
	Server    string `json:"server"`
	Type      string `json:"type,omitempty"`
	URL       string `json:"url,omitempty"`
	Command   string `json:"command,omitempty"`
	Tool      string `json:"tool,omitempty"`
	ToolCount int    `json:"tool_count,omitempty"`
	Error     string `json:"error,omitempty"`
}

ServerEventPayload describes MCP server connection events.

type ToolsChangedFunc

type ToolsChangedFunc func(server string, tools []*mcp.Tool)

ToolsChangedFunc receives the tools of a server listed again: after it announced its tool list changed, or after it was reconnected.

Jump to

Keyboard shortcuts

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