mcp

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 17 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) 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.

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