mcp

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 10 Imported by: 0

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

View Source
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"
)
View Source
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

func Initialize(cwd string) error

Initialize initializes the global MCP registry with the given working directory

func IsMCPTool

func IsMCPTool(name string) bool

IsMCPTool returns true if the tool name is an MCP tool

func ParseMCPToolName

func ParseMCPToolName(name string) (serverName, toolName string, ok bool)

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) Connect

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

Connect establishes a connection to the MCP server

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect closes the connection to the MCP server

func (*Client) GetCachedPrompts

func (c *Client) GetCachedPrompts() []MCPPrompt

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

func (c *Client) GetCachedTools() []MCPTool

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

func (c *Client) IsConnected() bool

IsConnected returns true if the client is connected

func (*Client) ListPrompts

func (c *Client) ListPrompts(ctx context.Context) ([]MCPPrompt, error)

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) ListTools

func (c *Client) ListTools(ctx context.Context) ([]MCPTool, error)

ListTools returns the tools available from the server

func (*Client) MarshalJSON

func (c *Client) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for debugging

func (*Client) Ping

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

Ping sends a ping to check if the server is responsive

func (*Client) ReadResource

func (c *Client) ReadResource(ctx context.Context, uri string) ([]ResourceContent, error)

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

func (*Client) ToServer

func (c *Client) ToServer() Server

ToServer converts the client state to a Server struct for display

type ClientCapabilities

type ClientCapabilities struct {
}

ClientCapabilities represents the capabilities of the MCP client

type ClientInfo

type ClientInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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):

  1. ~/.gen/mcp.json (user scope)
  2. ./.gen/mcp.json (project scope)
  3. ./.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

type PromptMessageContent struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

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

func NewRegistry(cwd string) (*Registry, error)

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) Connect

func (r *Registry) Connect(ctx context.Context, name string) error

Connect connects to an MCP server

func (*Registry) Disconnect

func (r *Registry) Disconnect(name string) error

Disconnect disconnects from an MCP server

func (*Registry) DisconnectAll

func (r *Registry) DisconnectAll()

DisconnectAll disconnects from all MCP servers

func (*Registry) GetClient

func (r *Registry) GetClient(name string) (*Client, bool)

GetClient returns a client by name

func (*Registry) GetConfig

func (r *Registry) GetConfig(name string) (ServerConfig, bool)

GetConfig returns a server config by name

func (*Registry) GetToolSchemas

func (r *Registry) GetToolSchemas() []provider.Tool

GetToolSchemas returns provider.Tool schemas for all connected MCP servers

func (*Registry) List

func (r *Registry) List() []Server

List returns all configured servers with their current status

func (*Registry) Reload

func (r *Registry) Reload() error

Reload reloads configurations from disk

func (*Registry) RemoveServer

func (r *Registry) RemoveServer(name string) error

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 Scope

type Scope string

Scope defines where the MCP configuration is stored

const (
	ScopeUser    Scope = "user"    // ~/.gen/mcp.json (global)
	ScopeProject Scope = "project" // ./.gen/mcp.json (team shared)
	ScopeLocal   Scope = "local"   // ./.gen/mcp.local.json (personal, git-ignored)
)

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

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

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"
)

Directories

Path Synopsis
Package transport provides transport implementations for MCP protocol.
Package transport provides transport implementations for MCP protocol.

Jump to

Keyboard shortcuts

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