Documentation
¶
Overview ¶
Package mcp implements Model Context Protocol (MCP) client functionality. It provides support for connecting to MCP servers via STDIO, HTTP, and SSE transports.
Index ¶
- Constants
- func Initialize(cwd string) error
- func IsMCPTool(name string) bool
- func ParseMCPToolName(name string) (serverName, toolName string, ok bool)
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, arguments map[string]any) (*ToolResult, error)
- func (c *Client) Config() ServerConfig
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Disconnect() error
- func (c *Client) GetCachedPrompts() []MCPPrompt
- func (c *Client) GetCachedResources() []MCPResource
- func (c *Client) GetCachedTools() []MCPTool
- func (c *Client) GetCapabilities() ServerCapabilities
- func (c *Client) GetPrompt(ctx context.Context, name string, arguments map[string]string) (*PromptResult, error)
- func (c *Client) GetServerInfo() ServerInfo
- func (c *Client) IsConnected() bool
- func (c *Client) ListPrompts(ctx context.Context) ([]MCPPrompt, error)
- func (c *Client) ListResources(ctx context.Context) ([]MCPResource, error)
- func (c *Client) ListTools(ctx context.Context) ([]MCPTool, error)
- func (c *Client) MarshalJSON() ([]byte, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) ReadResource(ctx context.Context, uri string) ([]ResourceContent, error)
- func (c *Client) SetOnToolsChanged(callback func())
- func (c *Client) ToServer() Server
- type ClientCapabilities
- type ClientInfo
- type ConfigLoader
- func (l *ConfigLoader) GetFilePath(scope Scope) string
- func (l *ConfigLoader) GetProjectDir() string
- func (l *ConfigLoader) GetUserDir() string
- func (l *ConfigLoader) LoadAll() (map[string]ServerConfig, error)
- func (l *ConfigLoader) RemoveServer(name string, scope Scope) error
- func (l *ConfigLoader) RemoveServerFromAll(name string) error
- func (l *ConfigLoader) SaveServer(name string, config ServerConfig, scope Scope) error
- type InitializeParams
- type InitializeResult
- type MCPConfig
- type MCPPrompt
- type MCPPromptArgument
- type MCPResource
- type MCPTool
- type PromptMessage
- type PromptMessageContent
- type PromptResult
- type PromptsCapability
- type PromptsGetParams
- type PromptsGetResult
- type PromptsListResult
- type Registry
- func (r *Registry) AddServer(name string, config ServerConfig, scope Scope) error
- func (r *Registry) CallTool(ctx context.Context, fullName string, arguments map[string]any) (*ToolResult, error)
- func (r *Registry) Connect(ctx context.Context, name string) error
- func (r *Registry) Disconnect(name string) error
- func (r *Registry) DisconnectAll()
- func (r *Registry) GetClient(name string) (*Client, bool)
- func (r *Registry) GetConfig(name string) (ServerConfig, bool)
- func (r *Registry) GetToolSchemas() []provider.Tool
- func (r *Registry) List() []Server
- func (r *Registry) Reload() error
- func (r *Registry) RemoveServer(name string) error
- func (r *Registry) SetOnToolsChanged(callback func())
- type ResourceContent
- type ResourcesCapability
- type ResourcesListResult
- type ResourcesReadParams
- type ResourcesReadResult
- type Scope
- type Server
- type ServerCapabilities
- type ServerConfig
- type ServerInfo
- type ServerStatus
- type ToolResult
- type ToolResultContent
- type ToolsCallParams
- type ToolsCallResult
- type ToolsCapability
- type ToolsListResult
- type TransportType
Constants ¶
const ( // ProtocolVersion is the MCP protocol version this client supports ProtocolVersion = "2024-11-05" // ClientName is the name of this MCP client ClientName = "gencode" // ClientVersion is the version of this MCP client ClientVersion = "1.0.0" )
const ( MethodInitialize = "initialize" MethodInitialized = "notifications/initialized" MethodToolsList = "tools/list" MethodToolsCall = "tools/call" MethodResourcesList = "resources/list" MethodResourcesRead = "resources/read" MethodPromptsList = "prompts/list" MethodPromptsGet = "prompts/get" MethodPing = "ping" MethodToolsListChanged = "notifications/tools/list_changed" )
MCP-specific methods
Variables ¶
This section is empty.
Functions ¶
func Initialize ¶
Initialize initializes the global MCP registry with the given working directory
func ParseMCPToolName ¶
ParseMCPToolName parses a tool name in the format mcp__<server>__<tool>
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an MCP client that connects to a single MCP server
func NewClient ¶
func NewClient(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, arguments map[string]any) (*ToolResult, error)
CallTool calls a tool on the MCP server
func (*Client) Config ¶
func (c *Client) Config() ServerConfig
Config returns the server configuration
func (*Client) Disconnect ¶
Disconnect closes the connection to the MCP server
func (*Client) GetCachedPrompts ¶
GetCachedPrompts returns the cached prompts without making an API call
func (*Client) GetCachedResources ¶
func (c *Client) GetCachedResources() []MCPResource
GetCachedResources returns the cached resources without making an API call
func (*Client) GetCachedTools ¶
GetCachedTools returns the cached tools without making an API call
func (*Client) GetCapabilities ¶
func (c *Client) GetCapabilities() ServerCapabilities
GetCapabilities returns the server's capabilities
func (*Client) GetPrompt ¶
func (c *Client) GetPrompt(ctx context.Context, name string, arguments map[string]string) (*PromptResult, error)
GetPrompt retrieves a specific prompt with the given arguments
func (*Client) GetServerInfo ¶
func (c *Client) GetServerInfo() ServerInfo
GetServerInfo returns information about the connected server
func (*Client) IsConnected ¶
IsConnected returns true if the client is connected
func (*Client) ListPrompts ¶
ListPrompts returns the prompts available from the server
func (*Client) ListResources ¶
func (c *Client) ListResources(ctx context.Context) ([]MCPResource, error)
ListResources returns the resources available from the server
func (*Client) MarshalJSON ¶
MarshalJSON implements json.Marshaler for debugging
func (*Client) ReadResource ¶
ReadResource reads a resource from the MCP server
func (*Client) SetOnToolsChanged ¶
func (c *Client) SetOnToolsChanged(callback func())
SetOnToolsChanged sets a callback for when tools list changes
type ClientCapabilities ¶
type ClientCapabilities struct {
}
ClientCapabilities represents the capabilities of the MCP client
type ClientInfo ¶
ClientInfo represents information about the MCP client
type ConfigLoader ¶
type ConfigLoader struct {
// contains filtered or unexported fields
}
ConfigLoader handles loading MCP configuration from multiple sources
func NewConfigLoader ¶
func NewConfigLoader(cwd string) *ConfigLoader
NewConfigLoader creates a new configuration loader
func (*ConfigLoader) GetFilePath ¶
func (l *ConfigLoader) GetFilePath(scope Scope) string
GetFilePath returns the file path for a given scope
func (*ConfigLoader) GetProjectDir ¶
func (l *ConfigLoader) GetProjectDir() string
GetProjectDir returns the project config directory
func (*ConfigLoader) GetUserDir ¶
func (l *ConfigLoader) GetUserDir() string
GetUserDir returns the user config directory
func (*ConfigLoader) LoadAll ¶
func (l *ConfigLoader) LoadAll() (map[string]ServerConfig, error)
LoadAll loads and merges MCP configurations from all sources. Priority (lowest to highest):
- ~/.gen/mcp.json (user scope)
- ./.gen/mcp.json (project scope)
- ./.gen/mcp.local.json (local scope)
func (*ConfigLoader) RemoveServer ¶
func (l *ConfigLoader) RemoveServer(name string, scope Scope) error
RemoveServer removes a server configuration from the specified scope
func (*ConfigLoader) RemoveServerFromAll ¶
func (l *ConfigLoader) RemoveServerFromAll(name string) error
RemoveServerFromAll removes a server from all config files where it exists
func (*ConfigLoader) SaveServer ¶
func (l *ConfigLoader) SaveServer(name string, config ServerConfig, scope Scope) error
SaveServer saves a server configuration to the specified scope
type InitializeParams ¶
type InitializeParams struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities ClientCapabilities `json:"capabilities"`
ClientInfo ClientInfo `json:"clientInfo"`
}
InitializeParams represents parameters for the initialize request
type InitializeResult ¶
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities ServerCapabilities `json:"capabilities"`
ServerInfo ServerInfo `json:"serverInfo"`
}
InitializeResult represents the result of an initialize request
type MCPConfig ¶
type MCPConfig struct {
MCPServers map[string]ServerConfig `json:"mcpServers"`
}
MCPConfig represents the mcp.json configuration file format
type MCPPrompt ¶
type MCPPrompt struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Arguments []MCPPromptArgument `json:"arguments,omitempty"`
}
MCPPrompt represents a prompt template exposed by an MCP server
type MCPPromptArgument ¶
type MCPPromptArgument struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
}
MCPPromptArgument represents an argument for an MCP prompt
type MCPResource ¶
type MCPResource struct {
URI string `json:"uri"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
MCPResource represents a resource exposed by an MCP server
type MCPTool ¶
type MCPTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema json.RawMessage `json:"inputSchema,omitempty"`
}
MCPTool represents a tool exposed by an MCP server
type PromptMessage ¶
type PromptMessage struct {
Role string `json:"role"`
Content PromptMessageContent `json:"content"`
}
PromptMessage represents a message in a prompt result
type PromptMessageContent ¶
PromptMessageContent represents the content of a prompt message
type PromptResult ¶
type PromptResult struct {
Description string `json:"description,omitempty"`
Messages []PromptMessage `json:"messages,omitempty"`
}
PromptResult represents the result of getting an MCP prompt
type PromptsCapability ¶
type PromptsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
PromptsCapability represents prompt-related capabilities
type PromptsGetParams ¶
type PromptsGetParams struct {
Name string `json:"name"`
Arguments map[string]string `json:"arguments,omitempty"`
}
PromptsGetParams represents parameters for prompts/get
type PromptsGetResult ¶
type PromptsGetResult = PromptResult
PromptsGetResult is an alias for PromptResult (same structure)
type PromptsListResult ¶
type PromptsListResult struct {
Prompts []MCPPrompt `json:"prompts"`
}
PromptsListResult represents the result of prompts/list
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages multiple MCP server connections
var DefaultRegistry *Registry
DefaultRegistry is the global MCP registry
func NewRegistry ¶
NewRegistry creates a new MCP registry
func (*Registry) AddServer ¶
func (r *Registry) AddServer(name string, config ServerConfig, scope Scope) error
AddServer adds a new server configuration
func (*Registry) CallTool ¶
func (r *Registry) CallTool(ctx context.Context, fullName string, arguments map[string]any) (*ToolResult, error)
CallTool calls a tool on an MCP server The tool name should be in the format: mcp__<server>__<tool>
func (*Registry) Disconnect ¶
Disconnect disconnects from an MCP server
func (*Registry) DisconnectAll ¶
func (r *Registry) DisconnectAll()
DisconnectAll disconnects from all MCP servers
func (*Registry) GetConfig ¶
func (r *Registry) GetConfig(name string) (ServerConfig, bool)
GetConfig returns a server config by name
func (*Registry) GetToolSchemas ¶
GetToolSchemas returns provider.Tool schemas for all connected MCP servers
func (*Registry) RemoveServer ¶
RemoveServer removes a server configuration
func (*Registry) SetOnToolsChanged ¶
func (r *Registry) SetOnToolsChanged(callback func())
SetOnToolsChanged sets a callback for when tools change
type ResourceContent ¶
type ResourceContent struct {
URI string `json:"uri"`
MimeType string `json:"mimeType,omitempty"`
Text string `json:"text,omitempty"`
Blob string `json:"blob,omitempty"` // base64 encoded binary
}
ResourceContent represents the content of a resource
type ResourcesCapability ¶
type ResourcesCapability struct {
Subscribe bool `json:"subscribe,omitempty"`
ListChanged bool `json:"listChanged,omitempty"`
}
ResourcesCapability represents resource-related capabilities
type ResourcesListResult ¶
type ResourcesListResult struct {
Resources []MCPResource `json:"resources"`
}
ResourcesListResult represents the result of resources/list
type ResourcesReadParams ¶
type ResourcesReadParams struct {
URI string `json:"uri"`
}
ResourcesReadParams represents parameters for resources/read
type ResourcesReadResult ¶
type ResourcesReadResult struct {
Contents []ResourceContent `json:"contents"`
}
ResourcesReadResult represents the result of resources/read
type Server ¶
type Server struct {
Config ServerConfig `json:"config"`
Status ServerStatus `json:"status"`
Capabilities ServerCapabilities `json:"capabilities,omitempty"`
ServerInfo ServerInfo `json:"serverInfo,omitempty"`
Error string `json:"error,omitempty"`
Tools []MCPTool `json:"tools,omitempty"`
Resources []MCPResource `json:"resources,omitempty"`
Prompts []MCPPrompt `json:"prompts,omitempty"`
}
Server represents a connected MCP server with its current state
type ServerCapabilities ¶
type ServerCapabilities struct {
Tools *ToolsCapability `json:"tools,omitempty"`
Resources *ResourcesCapability `json:"resources,omitempty"`
Prompts *PromptsCapability `json:"prompts,omitempty"`
}
ServerCapabilities represents the capabilities of an MCP server
type ServerConfig ¶
type ServerConfig struct {
// Name is the unique identifier for this server
Name string `json:"name,omitempty"`
// Type is the transport type (stdio, http, sse). Default is stdio.
Type TransportType `json:"type,omitempty"`
// STDIO transport fields
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Env map[string]string `json:"env,omitempty"`
// HTTP/SSE transport fields
URL string `json:"url,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
// Scope indicates where this config was loaded from
Scope Scope `json:"-"`
}
ServerConfig represents an MCP server configuration
func (*ServerConfig) GetType ¶
func (c *ServerConfig) GetType() TransportType
GetType returns the transport type, defaulting to stdio if not set
type ServerInfo ¶
ServerInfo represents information about an MCP server
type ServerStatus ¶
type ServerStatus string
ServerStatus represents the current status of an MCP server connection
const ( StatusDisconnected ServerStatus = "disconnected" StatusConnecting ServerStatus = "connecting" StatusConnected ServerStatus = "connected" StatusError ServerStatus = "error" )
type ToolResult ¶
type ToolResult struct {
Content []ToolResultContent `json:"content,omitempty"`
IsError bool `json:"isError,omitempty"`
}
ToolResult represents the result of calling an MCP tool
type ToolResultContent ¶
type ToolResultContent struct {
Type string `json:"type"` // "text", "image", "resource"
Text string `json:"text,omitempty"`
// Additional fields for other content types
Data string `json:"data,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
ToolResultContent represents a content item in a tool result
type ToolsCallParams ¶
type ToolsCallParams struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments,omitempty"`
}
ToolsCallParams represents parameters for tools/call
type ToolsCallResult ¶
type ToolsCallResult = ToolResult
ToolsCallResult is an alias for ToolResult (same structure)
type ToolsCapability ¶
type ToolsCapability struct {
ListChanged bool `json:"listChanged,omitempty"` // Server supports tools/list_changed notification
}
ToolsCapability represents tool-related capabilities
type ToolsListResult ¶
type ToolsListResult struct {
Tools []MCPTool `json:"tools"`
}
ToolsListResult represents the result of tools/list
type TransportType ¶
type TransportType string
TransportType defines the type of MCP transport
const ( TransportSTDIO TransportType = "stdio" TransportHTTP TransportType = "http" TransportSSE TransportType = "sse" )