mcp

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package mcp provides MCP (Model Context Protocol) client integration for connecting to external MCP servers and adapting their tools.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrServerNotFound indicates the named MCP server is not configured.
	ErrServerNotFound = errors.New("mcp: server not found")

	// ErrConnectionFailed indicates a connection attempt failed.
	ErrConnectionFailed = errors.New("mcp: connection failed")

	// ErrToolCallFailed indicates a tool call returned an error.
	ErrToolCallFailed = errors.New("mcp: tool call failed")

	// ErrNotConnected indicates the server is not connected.
	ErrNotConnected = errors.New("mcp: not connected")

	// ErrInvalidTransport indicates an unsupported transport type.
	ErrInvalidTransport = errors.New("mcp: invalid transport type")
)

Functions

func AdaptTool

func AdaptTool(dt DiscoveredTool, conn *ServerConnection, maxOutputTokens int) *agent.Tool

AdaptTool converts a single discovered MCP tool into an agent.Tool.

func AdaptTools

func AdaptTools(mgr *ServerManager, maxOutputTokens int) []*agent.Tool

AdaptTools converts all discovered MCP tools from the manager into agent.Tool instances. Tool naming follows the convention: mcp__{serverName}__{toolName}

func BuildEnvSlice

func BuildEnvSlice(extra map[string]string) []string

BuildEnvSlice converts a map of env vars to a slice of "KEY=VALUE" strings suitable for os/exec.Cmd.Env, inheriting the current process environment.

func ExpandEnv

func ExpandEnv(s string) string

ExpandEnv replaces ${VAR} and ${VAR:-default} patterns with environment variable values. If the variable is not set and no default is provided, the original pattern is kept.

func ExpandEnvMap

func ExpandEnvMap(m map[string]string) map[string]string

ExpandEnvMap applies ExpandEnv to all values in a map, returning a new map.

func LoadMCPFile

func LoadMCPFile(path string) (map[string]config.MCPServerConfig, error)

LoadMCPFile reads an MCP config file and returns the server map (exported).

func MergedServers

func MergedServers(cfg *config.MCPConfig) map[string]config.MCPServerConfig

MergedServers loads and merges MCP server configs from multiple scopes:

  1. Profile config (cfg.Servers, lowest priority)
  2. User-level config (~/.lango/mcp.json)
  3. Project-level config (.lango-mcp.json, highest priority)

Later scopes override earlier ones on a per-server-name basis.

func SaveMCPFile

func SaveMCPFile(path string, servers map[string]config.MCPServerConfig) error

SaveMCPFile writes MCP server configs to a JSON file.

Types

type DiscoveredPrompt

type DiscoveredPrompt struct {
	ServerName string
	Prompt     *sdkmcp.Prompt
}

DiscoveredPrompt holds an MCP prompt definition along with its source server.

type DiscoveredResource

type DiscoveredResource struct {
	ServerName string
	Resource   *sdkmcp.Resource
}

DiscoveredResource holds an MCP resource definition along with its source server.

type DiscoveredTool

type DiscoveredTool struct {
	ServerName string
	Tool       *sdkmcp.Tool
}

DiscoveredTool holds an MCP tool definition along with its source server.

type ServerConnection

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

ServerConnection manages the lifecycle of a single MCP server.

func NewServerConnection

func NewServerConnection(name string, cfg config.MCPServerConfig, global config.MCPConfig) *ServerConnection

NewServerConnection creates a new server connection manager.

func (*ServerConnection) Connect

func (sc *ServerConnection) Connect(ctx context.Context) error

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

func (*ServerConnection) Disconnect

func (sc *ServerConnection) Disconnect(ctx context.Context) error

Disconnect closes the connection to the MCP server.

func (*ServerConnection) Name

func (sc *ServerConnection) Name() string

Name returns the server name.

func (*ServerConnection) Session

func (sc *ServerConnection) Session() *sdkmcp.ClientSession

Session returns the active client session, or nil if not connected.

func (*ServerConnection) SetEventBus added in v0.7.0

func (sc *ServerConnection) SetEventBus(bus *eventbus.Bus)

SetEventBus attaches an event bus for SandboxDecisionEvent publishing.

func (*ServerConnection) SetFailClosed added in v0.7.0

func (sc *ServerConnection) SetFailClosed(fc bool)

SetFailClosed sets whether this connection blocks stdio transport creation when no sandbox is available.

func (*ServerConnection) SetOSIsolator added in v0.7.0

func (sc *ServerConnection) SetOSIsolator(iso sandboxos.OSIsolator, workspacePath, dataRoot string)

SetOSIsolator sets the OS-level sandbox isolator for this connection. workspacePath is recorded so MCPServerPolicy can walk up to find the repository `.git` and apply the same baseline deny as DefaultToolPolicy. dataRoot is recorded so the policy applied at transport creation time can deny the lango control-plane to the spawned MCP server child process. workspacePath also becomes the MCP child's cmd.Dir so policy discovery and execution share the same git context.

func (*ServerConnection) StartHealthCheck

func (sc *ServerConnection) StartHealthCheck(ctx context.Context)

StartHealthCheck starts a background goroutine that periodically pings the server.

func (*ServerConnection) State

func (sc *ServerConnection) State() ServerState

State returns the current connection state.

func (*ServerConnection) Tools

func (sc *ServerConnection) Tools() []DiscoveredTool

Tools returns the discovered tools from this server.

type ServerManager

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

ServerManager manages multiple MCP server connections.

func NewServerManager

func NewServerManager(cfg config.MCPConfig) *ServerManager

NewServerManager creates a new manager for the given config.

func (*ServerManager) AllPrompts

func (m *ServerManager) AllPrompts() []DiscoveredPrompt

AllPrompts returns all discovered prompts from all connected servers.

func (*ServerManager) AllResources

func (m *ServerManager) AllResources() []DiscoveredResource

AllResources returns all discovered resources from all connected servers.

func (*ServerManager) AllTools

func (m *ServerManager) AllTools() []DiscoveredTool

AllTools returns all discovered tools from all connected servers.

func (*ServerManager) ConnectAll

func (m *ServerManager) ConnectAll(ctx context.Context) map[string]error

ConnectAll connects to all configured and enabled servers. Returns a map of server names to errors for any that failed.

func (*ServerManager) DisconnectAll

func (m *ServerManager) DisconnectAll(ctx context.Context) error

DisconnectAll disconnects all managed servers.

func (*ServerManager) GetConnection

func (m *ServerManager) GetConnection(name string) (*ServerConnection, bool)

GetConnection returns the named server connection.

func (*ServerManager) ServerCount

func (m *ServerManager) ServerCount() int

ServerCount returns the number of managed servers.

func (*ServerManager) ServerStatus

func (m *ServerManager) ServerStatus() map[string]ServerState

ServerStatus returns the state of each managed server.

func (*ServerManager) SetEventBus added in v0.7.0

func (m *ServerManager) SetEventBus(bus *eventbus.Bus)

SetEventBus attaches an event bus for SandboxDecisionEvent publishing on all current and future connections.

func (*ServerManager) SetFailClosed added in v0.7.0

func (m *ServerManager) SetFailClosed(fc bool)

SetFailClosed enables or disables fail-closed semantics for all current and future connections. When true, stdio MCP transport creation is blocked if no OS sandbox can be applied.

func (*ServerManager) SetOSIsolator added in v0.7.0

func (m *ServerManager) SetOSIsolator(iso sandboxos.OSIsolator, workspacePath, dataRoot string)

SetOSIsolator sets the OS-level sandbox isolator for all current and future connections. workspacePath is forwarded so each connection's MCPServerPolicy can walk up to the repo `.git` and apply the same baseline deny as DefaultToolPolicy. dataRoot is forwarded so each connection's policy denies the lango control-plane to the spawned MCP child.

type ServerState

type ServerState int

ServerState represents the lifecycle state of an MCP server connection.

const (
	StateDisconnected ServerState = iota
	StateConnecting
	StateConnected
	StateFailed
	StateStopped
)

func (ServerState) String

func (s ServerState) String() string

String returns a human-readable state name.

Jump to

Keyboard shortcuts

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