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 ¶
- func TestConnection(ctx context.Context, cfg config.MCPServerConfig) (int, error)
- type EnvVarMeta
- type MCPClient
- func (c *MCPClient) CallCount() int64
- func (c *MCPClient) CallTool(ctx context.Context, serverName, toolName string, args json.RawMessage) (string, error)
- func (c *MCPClient) Close() error
- func (c *MCPClient) Connect(ctx context.Context) error
- func (c *MCPClient) DisconnectServer(serverName string)
- func (c *MCPClient) IsServerReadOnly(serverName string) bool
- func (c *MCPClient) Prompts() []MCPPrompt
- func (c *MCPClient) ReadOnlyToolDefinitions() []provider.ToolDefinition
- func (c *MCPClient) Reconfigure(ctx context.Context, newConfigs []config.MCPServerConfig) error
- func (c *MCPClient) Reconnect(ctx context.Context, serverName string) error
- func (c *MCPClient) ResolveToolCall(prefixedName string) (serverName, toolName string, ok bool)
- func (c *MCPClient) Resources() []MCPResource
- func (c *MCPClient) SetNotificationHandler(handler func(serverName, method string, params json.RawMessage))
- func (c *MCPClient) Status() []ServerStatus
- func (c *MCPClient) ToToolDefinitions() []provider.ToolDefinition
- func (c *MCPClient) Tools() []MCPTool
- type MCPPrompt
- type MCPPromptArgument
- type MCPResource
- type MCPTool
- type RegistryClient
- type RegistryEnvVar
- type RegistryPackage
- type RegistryRemote
- type RegistryServer
- type ServerStatus
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TestConnection ¶ added in v1.19.0
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
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
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) Connect ¶
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
DisconnectServer removes and closes a single server connection and all its metadata.
func (*MCPClient) IsServerReadOnly ¶ added in v1.19.0
IsServerReadOnly returns true if the named server is configured as read-only.
func (*MCPClient) Prompts ¶ added in v1.19.0
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
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
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
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.
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
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
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
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.