mcp

package
v0.0.113 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheTools added in v0.0.64

func CacheTools(serverName string, tools []ToolSpec)

CacheTools writes the tool list for a server to the cache file.

func DefaultConfigPath

func DefaultConfigPath() (string, error)

DefaultConfigPath returns the default path for mcp.json.

func GetBundledServersByCategory

func GetBundledServersByCategory() map[string][]BundledServer

GetBundledServersByCategory returns servers grouped by category.

func GetMCPToolSpecs

func GetMCPToolSpecs(manager *Manager) []llm.ToolSpec

GetMCPToolSpecs returns LLM tool specs for all running MCP tools.

func RegisterMCPTools

func RegisterMCPTools(manager *Manager, registry *llm.ToolRegistry)

RegisterMCPTools registers all MCP tools from the manager into the tool registry.

Types

type ArgumentInfo

type ArgumentInfo struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Type        string `json:"type"`   // positional, named
	Format      string `json:"format"` // string, number
	IsRequired  bool   `json:"isRequired"`
	Default     string `json:"default,omitempty"`
}

ArgumentInfo describes a command-line argument.

type BundledServer

type BundledServer struct {
	Name        string
	Description string
	Package     string // npm package name
	PackageType string // "npm" or "pypi"
	Category    string
	Official    bool   // true if official/reference implementation
	URL         string // repository or homepage URL
}

BundledServer represents a curated MCP server included in the binary. These are popular servers that may not always appear in registry searches.

func GetBundledServers

func GetBundledServers() []BundledServer

GetBundledServers returns all bundled servers.

func (*BundledServer) ToRegistryServer

func (b *BundledServer) ToRegistryServer() RegistryServer

ToRegistryServer converts a BundledServer to a RegistryServer for UI consistency.

func (*BundledServer) ToServerConfig

func (b *BundledServer) ToServerConfig() ServerConfig

ToServerConfig converts a BundledServer to a local ServerConfig.

type Client

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

Client wraps an MCP server connection.

func NewClient

func NewClient(name string, config ServerConfig) *Client

NewClient creates a new MCP client for the given server configuration.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, args json.RawMessage) (string, error)

CallTool invokes a tool on the MCP server.

func (*Client) IsRunning

func (c *Client) IsRunning() bool

IsRunning returns whether the client is connected.

func (*Client) Name

func (c *Client) Name() string

Name returns the server name.

func (*Client) SetSamplingHandler added in v0.0.39

func (c *Client) SetSamplingHandler(handler *SamplingHandler)

SetSamplingHandler sets the sampling handler for this client.

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

Start connects to the MCP server and initializes the session.

func (*Client) Stop

func (c *Client) Stop() error

Stop closes the MCP server connection.

func (*Client) Tools

func (c *Client) Tools() []ToolSpec

Tools returns the available tools from this server.

type Config

type Config struct {
	Servers map[string]ServerConfig `json:"servers"`
}

Config represents the mcp.json configuration file.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads the MCP configuration from the default path.

func LoadConfigFromPath

func LoadConfigFromPath(path string) (*Config, error)

LoadConfigFromPath loads the MCP configuration from a specific path.

func (*Config) AddServer

func (c *Config) AddServer(name string, cfg ServerConfig)

AddServer adds or updates a server configuration.

func (*Config) RemoveServer

func (c *Config) RemoveServer(name string) bool

RemoveServer removes a server configuration.

func (*Config) Save

func (c *Config) Save() error

Save saves the configuration to the default path.

func (*Config) SaveToPath

func (c *Config) SaveToPath(path string) error

SaveToPath saves the configuration to a specific path.

func (*Config) ServerNames

func (c *Config) ServerNames() []string

ServerNames returns a sorted list of configured server names.

type MCPTool

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

MCPTool wraps an MCP server tool as an llm.Tool.

func NewMCPTool

func NewMCPTool(manager *Manager, spec ToolSpec) *MCPTool

NewMCPTool creates a new MCP tool wrapper.

func (*MCPTool) Execute

func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (llm.ToolOutput, error)

Execute invokes the tool on the MCP server.

func (*MCPTool) Preview added in v0.0.25

func (t *MCPTool) Preview(args json.RawMessage) string

Preview returns empty string for MCP tools - the engine falls back to extractToolInfo().

func (*MCPTool) Spec

func (t *MCPTool) Spec() llm.ToolSpec

Spec returns the tool specification for the LLM.

type Manager

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

Manager handles MCP server lifecycle and provides tools to LLM.

func NewManager

func NewManager() *Manager

NewManager creates a new MCP manager.

func (*Manager) AllTools

func (m *Manager) AllTools() []ToolSpec

AllTools returns all tools from all running MCP servers. Tool names are prefixed with server name to avoid collisions.

func (*Manager) AvailableServers

func (m *Manager) AvailableServers() []string

AvailableServers returns the names of all configured servers.

func (*Manager) CallTool

func (m *Manager) CallTool(ctx context.Context, fullName string, args json.RawMessage) (string, error)

CallTool routes a tool call to the appropriate MCP server. Tool names should be prefixed with "servername__".

func (*Manager) Config

func (m *Manager) Config() *Config

Config returns the current configuration.

func (*Manager) Disable

func (m *Manager) Disable(name string) error

Disable stops an MCP server.

func (*Manager) Enable

func (m *Manager) Enable(ctx context.Context, name string) error

Enable starts an MCP server in the background (non-blocking).

func (*Manager) EnabledServers

func (m *Manager) EnabledServers() []string

EnabledServers returns the names of currently enabled (running or starting) servers.

func (*Manager) GetAllStates

func (m *Manager) GetAllStates() []ServerState

GetAllStates returns the current state of all servers (for UI display).

func (*Manager) GetSamplingHandler added in v0.0.39

func (m *Manager) GetSamplingHandler() *SamplingHandler

GetSamplingHandler returns the current sampling handler.

func (*Manager) LoadConfig

func (m *Manager) LoadConfig() error

LoadConfig loads the MCP configuration.

func (*Manager) Restart

func (m *Manager) Restart(ctx context.Context, name string) error

Restart stops and restarts an MCP server.

func (*Manager) ServerStatus

func (m *Manager) ServerStatus(name string) (ServerStatus, error)

ServerStatus returns the current status of a server.

func (*Manager) SetSamplingProvider added in v0.0.39

func (m *Manager) SetSamplingProvider(provider llm.Provider, model string, yoloMode bool)

SetSamplingProvider configures the provider and model for MCP sampling requests. If yoloMode is true, sampling requests are auto-approved without prompting.

func (*Manager) SetStatusChannel

func (m *Manager) SetStatusChannel(ch chan StatusUpdate)

SetStatusChannel sets a channel to receive status updates.

func (*Manager) StopAll

func (m *Manager) StopAll()

StopAll stops all running MCP servers.

type OfficialMeta

type OfficialMeta struct {
	Status      string `json:"status"` // active, deleted
	PublishedAt string `json:"publishedAt"`
	UpdatedAt   string `json:"updatedAt"`
	IsLatest    bool   `json:"isLatest"`
}

OfficialMeta contains official registry metadata.

type PackageInfo

type PackageInfo struct {
	RegistryType    string         `json:"registryType"` // npm, pypi, oci
	RegistryBaseURL string         `json:"registryBaseUrl,omitempty"`
	Identifier      string         `json:"identifier"`
	Version         string         `json:"version"`
	Transport       *TransportInfo `json:"transport,omitempty"`
	Arguments       []ArgumentInfo `json:"packageArguments,omitempty"`
	RuntimeArgs     []ArgumentInfo `json:"runtimeArguments,omitempty"`
}

PackageInfo describes how to install/run a server.

type RegistryClient

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

RegistryClient queries the official MCP registry.

func NewRegistryClient

func NewRegistryClient() *RegistryClient

NewRegistryClient creates a new registry client.

func (*RegistryClient) GetServer

func (r *RegistryClient) GetServer(ctx context.Context, name string) (*RegistryServer, error)

GetServer fetches a specific server by name.

func (*RegistryClient) Search

func (r *RegistryClient) Search(ctx context.Context, opts SearchOptions) (*SearchResult, error)

Search queries the registry for MCP servers.

type RegistryMeta

type RegistryMeta struct {
	Official *OfficialMeta `json:"io.modelcontextprotocol.registry/official,omitempty"`
}

RegistryMeta contains registry-specific metadata.

type RegistryServer

type RegistryServer struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	Version     string          `json:"version"`
	Repository  *RepositoryInfo `json:"repository,omitempty"`
	Packages    []PackageInfo   `json:"packages,omitempty"`
	Meta        *RegistryMeta   `json:"_meta,omitempty"`
}

RegistryServer represents a server from the registry.

func (*RegistryServer) DisplayName

func (s *RegistryServer) DisplayName() string

DisplayName returns a user-friendly display name for the server.

func (*RegistryServer) ToServerConfig

func (s *RegistryServer) ToServerConfig() (ServerConfig, bool)

ToServerConfig converts a registry server to a local configuration. Returns the config and a flag indicating if user input is needed.

type RegistryServerWrapper

type RegistryServerWrapper struct {
	Server RegistryServer `json:"server"`
	Meta   *RegistryMeta  `json:"_meta,omitempty"`
}

RegistryServerWrapper wraps server with metadata.

func GetBundledAsRegistryWrappers

func GetBundledAsRegistryWrappers() []RegistryServerWrapper

GetBundledAsRegistryWrappers returns bundled servers as RegistryServerWrappers for UI.

type RepositoryInfo

type RepositoryInfo struct {
	URL       string `json:"url"`
	Source    string `json:"source"`
	Subfolder string `json:"subfolder,omitempty"`
}

RepositoryInfo contains source repository information.

type SamplingApprovalChoice added in v0.0.39

type SamplingApprovalChoice int

SamplingApprovalChoice represents a user's approval selection for sampling.

const (
	SamplingChoiceDeny      SamplingApprovalChoice = iota // Deny the request
	SamplingChoiceAllow                                   // Allow for this session
	SamplingChoiceCancelled                               // User cancelled
)

type SamplingConfig added in v0.0.39

type SamplingConfig struct {
	// Enabled controls whether sampling is allowed for this server (default: true)
	Enabled *bool `json:"enabled,omitempty"`
	// AutoApprove skips the approval prompt for this server
	AutoApprove bool `json:"auto_approve,omitempty"`
	// Provider overrides the provider used for sampling (e.g., "zen", "anthropic")
	Provider string `json:"provider,omitempty"`
	// Model overrides the model used for sampling
	Model string `json:"model,omitempty"`
	// MaxTokens limits the maximum tokens per sampling request
	MaxTokens int `json:"max_tokens,omitempty"`
}

SamplingConfig configures MCP sampling behavior for a server.

func (*SamplingConfig) IsSamplingEnabled added in v0.0.39

func (c *SamplingConfig) IsSamplingEnabled() bool

IsSamplingEnabled returns whether sampling is enabled for this server.

type SamplingHandler added in v0.0.39

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

SamplingHandler handles sampling/createMessage requests from MCP servers.

func NewSamplingHandler added in v0.0.39

func NewSamplingHandler(provider llm.Provider, model string) *SamplingHandler

NewSamplingHandler creates a new sampling handler.

func (*SamplingHandler) Handle added in v0.0.39

Handle processes a sampling/createMessage request from an MCP server.

func (*SamplingHandler) SetServerConfig added in v0.0.39

func (h *SamplingHandler) SetServerConfig(name string, config ServerConfig)

SetServerConfig sets the configuration for a specific server.

func (*SamplingHandler) SetYoloMode added in v0.0.39

func (h *SamplingHandler) SetYoloMode(enabled bool)

SetYoloMode enables or disables yolo mode (auto-approve all sampling requests).

type SearchMetadata

type SearchMetadata struct {
	Count      int    `json:"count"`
	NextCursor string `json:"nextCursor,omitempty"`
}

SearchMetadata contains pagination info.

type SearchOptions

type SearchOptions struct {
	Query  string
	Limit  int
	Cursor string
}

SearchOptions configures a registry search.

type SearchResult

type SearchResult struct {
	Servers  []RegistryServerWrapper `json:"servers"`
	Metadata SearchMetadata          `json:"metadata"`
}

SearchResult contains the response from a registry search.

type ServerConfig

type ServerConfig struct {
	// Type discriminator: "stdio" (default if command present) or "http"
	Type string `json:"type,omitempty"`

	// Stdio transport fields
	Command string   `json:"command,omitempty"`
	Args    []string `json:"args,omitempty"`

	// HTTP transport fields
	URL     string            `json:"url,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`

	// Shared fields
	Env map[string]string `json:"env,omitempty"`

	// Sampling configuration
	Sampling *SamplingConfig `json:"sampling,omitempty"`
}

ServerConfig represents a configured MCP server. Supports both stdio transport (Command/Args) and HTTP transport (URL).

func (*ServerConfig) TransportType added in v0.0.23

func (c *ServerConfig) TransportType() string

TransportType returns the effective transport type for this server.

func (*ServerConfig) Validate added in v0.0.23

func (c *ServerConfig) Validate() error

Validate checks that the server configuration is valid.

type ServerState

type ServerState struct {
	Name   string
	Status ServerStatus
	Error  error
	Client *Client
}

ServerState holds the state of a managed MCP server.

type ServerStatus

type ServerStatus string

ServerStatus represents the current state of an MCP server.

const (
	StatusStopped  ServerStatus = "stopped"
	StatusStarting ServerStatus = "starting"
	StatusReady    ServerStatus = "ready"
	StatusFailed   ServerStatus = "failed"
)

type StatusUpdate

type StatusUpdate struct {
	Name   string
	Status ServerStatus
	Error  error
}

StatusUpdate is sent when a server's status changes.

type ToolSpec

type ToolSpec struct {
	Name        string
	Description string
	Schema      map[string]any
}

ToolSpec describes a tool available from an MCP server.

func LoadCachedTools added in v0.0.64

func LoadCachedTools(serverName string) []ToolSpec

LoadCachedTools returns the cached tool list for a server, or nil if not cached.

type TransportInfo

type TransportInfo struct {
	Type string `json:"type"` // stdio, sse, streamable-http
	URL  string `json:"url,omitempty"`
}

TransportInfo describes the transport type.

Jump to

Keyboard shortcuts

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