mcp

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package mcp implements a client for the Model Context Protocol. It supports connecting to MCP servers via stdio (subprocess) transport, discovering tools, and invoking them on behalf of the primary model.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestConnection added in v1.19.0

func TestConnection(ctx context.Context, cfg config.MCPServerConfig) (int, error)

TestConnection creates a temporary connection to an MCP server using the given config, performs the initialize handshake, discovers tools, and tears down the connection. Returns the tool count or an error. Does not modify any MCPClient state — safe to call from the wizard with staged config.

Types

type EnvVarMeta added in v1.20.0

type EnvVarMeta struct {
	Name        string
	Description string
	IsSecret    bool
	IsRequired  bool
}

EnvVarMeta holds metadata about an environment variable needed by an MCP server. Used by CLI/TUI to decide masking and keyring storage.

func ToMCPServerConfig added in v1.20.0

func ToMCPServerConfig(server RegistryServer) (config.MCPServerConfig, []EnvVarMeta)

ToMCPServerConfig maps a registry server to an MCPServerConfig and returns env var metadata for input masking and keyring decisions.

type MCPClient

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

MCPClient manages connections to one or more MCP servers.

func NewMCPClient

func NewMCPClient(configs []config.MCPServerConfig, debug bool) *MCPClient

NewMCPClient creates a new MCP client from the given server configurations. No connections are made until Connect is called.

func (*MCPClient) CallCount added in v1.19.0

func (c *MCPClient) CallCount() int64

CallCount returns the total number of MCP tool calls made.

func (*MCPClient) CallTool

func (c *MCPClient) CallTool(ctx context.Context, serverName, toolName string, args json.RawMessage) (string, error)

CallTool invokes a tool on the specified MCP server and returns the text result. A per-call timeout is applied from the server's Timeout config (default 30s). If the server process has died, a reconnect is attempted automatically.

func (*MCPClient) Close

func (c *MCPClient) Close() error

Close shuts down all MCP server connections.

func (*MCPClient) Connect

func (c *MCPClient) Connect(ctx context.Context) error

Connect starts all configured MCP servers and discovers their tools. Individual server failures are isolated — other servers continue to work. Returns a non-nil error summarizing any failures, but the client remains usable for servers that connected successfully.

func (*MCPClient) DisconnectServer added in v1.19.0

func (c *MCPClient) DisconnectServer(serverName string)

DisconnectServer removes and closes a single server connection and all its metadata.

func (*MCPClient) IsServerReadOnly added in v1.19.0

func (c *MCPClient) IsServerReadOnly(serverName string) bool

IsServerReadOnly returns true if the named server is configured as read-only.

func (*MCPClient) Prompts added in v1.19.0

func (c *MCPClient) Prompts() []MCPPrompt

Prompts returns all prompts discovered from connected MCP servers.

func (*MCPClient) ReadOnlyToolDefinitions added in v1.19.0

func (c *MCPClient) ReadOnlyToolDefinitions() []provider.ToolDefinition

ReadOnlyToolDefinitions returns tool definitions for MCP tools that are marked as read-only (via server config or tool annotation). These are safe to include in fan-out queries to non-primary providers.

func (*MCPClient) Reconfigure added in v1.19.0

func (c *MCPClient) Reconfigure(ctx context.Context, newConfigs []config.MCPServerConfig) error

Reconfigure diffs the current config against newConfigs and applies changes: - New servers are connected - Removed servers are disconnected - Changed servers are reconnected Updates c.configs so Status() and Reconnect() use the new set.

func (*MCPClient) Reconnect added in v1.19.0

func (c *MCPClient) Reconnect(ctx context.Context, serverName string) error

Reconnect forces a reconnection to a specific server by name. Works for both currently-connected and initially-failed servers.

func (*MCPClient) ResolveToolCall added in v1.19.0

func (c *MCPClient) ResolveToolCall(prefixedName string) (serverName, toolName string, ok bool)

ResolveToolCall takes a prefixed tool name (e.g. "mcp_filesystem_read_file") and returns the (serverName, toolName) pair via the lookup map built during tool discovery. This avoids ambiguous underscore-based string parsing.

func (*MCPClient) Resources added in v1.19.0

func (c *MCPClient) Resources() []MCPResource

Resources returns all resources discovered from connected MCP servers.

func (*MCPClient) SetNotificationHandler added in v1.19.0

func (c *MCPClient) SetNotificationHandler(handler func(serverName, method string, params json.RawMessage))

SetNotificationHandler sets a callback that is invoked when any MCP server sends a notification. This is used to handle notifications/tools/list_changed. Must be called before Connect().

func (*MCPClient) Status added in v1.19.0

func (c *MCPClient) Status() []ServerStatus

Status returns the connection status of all configured MCP servers.

func (*MCPClient) ToToolDefinitions

func (c *MCPClient) ToToolDefinitions() []provider.ToolDefinition

ToToolDefinitions converts discovered MCP tools to provider.ToolDefinition format so they can be sent to the primary model alongside built-in tools. Tool names are prefixed with "mcp_{serverName}_" to avoid collisions.

func (*MCPClient) Tools

func (c *MCPClient) Tools() []MCPTool

Tools returns all tools discovered from connected MCP servers.

type MCPPrompt added in v1.19.0

type MCPPrompt struct {
	Name        string
	Description string
	Arguments   []MCPPromptArgument
	ServerName  string
}

MCPPrompt represents a prompt template exposed by an MCP server.

type MCPPromptArgument added in v1.19.0

type MCPPromptArgument struct {
	Name        string
	Description string
	Required    bool
}

MCPPromptArgument describes an argument for a prompt template.

type MCPResource added in v1.19.0

type MCPResource struct {
	URI         string
	Name        string
	Description string
	MimeType    string
	ServerName  string
}

MCPResource represents a resource exposed by an MCP server.

type MCPTool

type MCPTool struct {
	Name        string
	Description string
	InputSchema map[string]any
	ServerName  string // which server provides this tool
	ReadOnly    bool   // true if server or tool annotation marks it read-only
}

MCPTool represents a tool discovered from an MCP server.

type RegistryClient added in v1.20.0

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

RegistryClient queries the official MCP Registry API.

func NewRegistryClient added in v1.20.0

func NewRegistryClient() *RegistryClient

NewRegistryClient creates a client for the official MCP Registry.

func (*RegistryClient) Search added in v1.20.0

func (rc *RegistryClient) Search(ctx context.Context, query string, limit int) ([]RegistryServer, string, error)

Search queries the registry for servers matching the given query. Returns servers, a cursor for pagination (empty if no more), and any error.

func (*RegistryClient) SearchNext added in v1.20.0

func (rc *RegistryClient) SearchNext(ctx context.Context, query string, limit int, cursor string) ([]RegistryServer, string, error)

SearchNext fetches the next page of results using the cursor from a previous search.

type RegistryEnvVar added in v1.20.0

type RegistryEnvVar struct {
	Name        string
	Description string
	IsRequired  bool
	IsSecret    bool
}

RegistryEnvVar represents an environment variable required by a package.

type RegistryPackage added in v1.20.0

type RegistryPackage struct {
	RegistryType string // "npm", "pip", "oci"
	Identifier   string // e.g., "@modelcontextprotocol/server-github"
	Transport    string // "stdio", "streamable-http"
	EnvVars      []RegistryEnvVar
}

RegistryPackage represents an installable package for a registry server.

type RegistryRemote added in v1.20.0

type RegistryRemote struct {
	Type string // "streamable-http"
	URL  string
}

RegistryRemote represents a hosted HTTP endpoint for a registry server.

type RegistryServer added in v1.20.0

type RegistryServer struct {
	Name        string
	Description string
	Version     string
	Packages    []RegistryPackage
	Remotes     []RegistryRemote
	Repository  string // GitHub URL
}

RegistryServer represents a server from the MCP Registry API.

func (RegistryServer) PackageIdentifier added in v1.20.0

func (rs RegistryServer) PackageIdentifier() string

PackageIdentifier returns the first package identifier, or the remote URL.

func (RegistryServer) TransportLabel added in v1.20.0

func (rs RegistryServer) TransportLabel() string

TransportLabel returns a human-readable transport label for display.

type ServerStatus added in v1.19.0

type ServerStatus struct {
	Name      string
	Connected bool
	Error     error
	ToolCount int
}

ServerStatus reports the connection status of a single MCP server.

Jump to

Keyboard shortcuts

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