Documentation
¶
Overview ¶
Package mcp provides MCP (Model Context Protocol) client integration for connecting to external MCP servers and adapting their tools.
Index ¶
- Variables
- func AdaptTool(dt DiscoveredTool, conn *ServerConnection, maxOutputTokens int) *agent.Tool
- func AdaptTools(mgr *ServerManager, maxOutputTokens int) []*agent.Tool
- func BuildEnvSlice(extra map[string]string) []string
- func ExpandEnv(s string) string
- func ExpandEnvMap(m map[string]string) map[string]string
- func LoadMCPFile(path string) (map[string]config.MCPServerConfig, error)
- func MergedServers(cfg *config.MCPConfig) map[string]config.MCPServerConfig
- func SaveMCPFile(path string, servers map[string]config.MCPServerConfig) error
- type DiscoveredPrompt
- type DiscoveredResource
- type DiscoveredTool
- type ServerConnection
- func (sc *ServerConnection) Connect(ctx context.Context) error
- func (sc *ServerConnection) Disconnect(ctx context.Context) error
- func (sc *ServerConnection) Name() string
- func (sc *ServerConnection) Session() *sdkmcp.ClientSession
- func (sc *ServerConnection) SetEventBus(bus *eventbus.Bus)
- func (sc *ServerConnection) SetFailClosed(fc bool)
- func (sc *ServerConnection) SetOSIsolator(iso sandboxos.OSIsolator, workspacePath, dataRoot string)
- func (sc *ServerConnection) StartHealthCheck(ctx context.Context)
- func (sc *ServerConnection) State() ServerState
- func (sc *ServerConnection) Tools() []DiscoveredTool
- type ServerManager
- func (m *ServerManager) AllPrompts() []DiscoveredPrompt
- func (m *ServerManager) AllResources() []DiscoveredResource
- func (m *ServerManager) AllTools() []DiscoveredTool
- func (m *ServerManager) ConnectAll(ctx context.Context) map[string]error
- func (m *ServerManager) DisconnectAll(ctx context.Context) error
- func (m *ServerManager) GetConnection(name string) (*ServerConnection, bool)
- func (m *ServerManager) ServerCount() int
- func (m *ServerManager) ServerStatus() map[string]ServerState
- func (m *ServerManager) SetEventBus(bus *eventbus.Bus)
- func (m *ServerManager) SetFailClosed(fc bool)
- func (m *ServerManager) SetOSIsolator(iso sandboxos.OSIsolator, workspacePath, dataRoot string)
- type ServerState
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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:
- Profile config (cfg.Servers, lowest priority)
- User-level config (~/.lango/mcp.json)
- 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 ¶
DiscoveredPrompt holds an MCP prompt definition along with its source server.
type DiscoveredResource ¶
DiscoveredResource holds an MCP resource definition along with its source server.
type DiscoveredTool ¶
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.