mcp

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TransportSSE        = "sse"
	TransportStreamable = "streamable"
	TransportStdio      = "stdio"
)

Transport type constants

View Source
const (
	ErrCodeConfigNotFound  = "MCP_CONFIG_NOT_FOUND"
	ErrCodeConnectFailed   = "MCP_CONNECT_FAILED"
	ErrCodeServerNotFound  = "MCP_SERVER_NOT_FOUND"
	ErrCodeMethodNotFound  = "MCP_METHOD_NOT_FOUND"
	ErrCodeMethodAmbiguous = "MCP_METHOD_AMBIGUOUS"
	ErrCodeCallFailed      = "MCP_CALL_FAILED"
	ErrCodeParamInvalid    = "MCP_PARAM_INVALID"
)

Error codes

Variables

This section is empty.

Functions

func ExpandHome

func ExpandHome(path string) string

ExpandHome expands ~ in paths to user's home directory

func FormatInputSchema

func FormatInputSchema(schema any) []string

FormatInputSchema converts JSON Schema to human-readable format Returns: ["key:type={value} // description", ...]

func GetConfigSearchPaths

func GetConfigSearchPaths() []string

GetConfigSearchPaths returns platform-specific config file search paths

func GetRequiredParams

func GetRequiredParams(schema any) []string

GetRequiredParams extracts required parameter names

func InferTransportType

func InferTransportType(cfg ServerConfig) string

InferTransportType infers transport type from server config

func IsMCPError

func IsMCPError(err error) bool

IsMCPError checks if an error is an MCPError

func IsPipedInput added in v0.4.0

func IsPipedInput() bool

IsPipedInput checks if stdin is a pipe (piped input)

func ParseKVArgs

func ParseKVArgs(args []string) (map[string]any, error)

ParseKVArgs parses "key=value" or "key:type=value" format arguments

func ParseYAML added in v0.4.0

func ParseYAML(yamlStr string) (map[string]any, error)

ParseYAML parses a YAML string and returns a map[string]any

func ReadStdinYAML added in v0.4.0

func ReadStdinYAML() (map[string]any, error)

ReadStdinYAML reads YAML from stdin and returns a map

func ReadYAMLFile added in v0.4.0

func ReadYAMLFile(path string) (map[string]any, error)

ReadYAMLFile reads and parses a YAML file

func SaveConfig added in v0.3.0

func SaveConfig(config *MCPConfig, path string) error

SaveConfig saves the config to a specific path

Types

type AuthConfig added in v0.2.8

type AuthConfig struct {
	// OAuth 2.1 + PKCE configuration
	OAuth *OAuthConfig `json:"oauth,omitempty"`
}

AuthConfig represents authentication configuration for MCP servers

type Client

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

Client represents an MCP server client

func NewClient

func NewClient(name string, config ServerConfig) *Client

NewClient creates a new MCP client for a server

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, params map[string]any) (*mcp.CallToolResult, error)

CallTool calls a tool with the given name and parameters

func (*Client) Close

func (c *Client) Close()

Close closes the client connection

func (*Client) Connect

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

Connect establishes connection to the MCP server

func (*Client) GetConfig

func (c *Client) GetConfig() ServerConfig

GetConfig returns the server config

func (*Client) GetTransport

func (c *Client) GetTransport() string

GetTransport returns the transport type

func (*Client) ListTools

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

ListTools returns all available tools from the server

type Dispatcher

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

Dispatcher manages multiple MCP server clients

func NewDispatcher

func NewDispatcher(config *MCPConfig, loadedPaths []string) *Dispatcher

NewDispatcher creates a new Dispatcher

func (*Dispatcher) CallTool

func (d *Dispatcher) CallTool(ctx context.Context, toolName, serverName string, params map[string]any) (string, any, error)

CallTool calls a tool on a server with retry on network failures

func (*Dispatcher) Close added in v0.3.0

func (d *Dispatcher) Close()

Close closes all client connections

func (*Dispatcher) ConfigPaths

func (d *Dispatcher) ConfigPaths() []string

ConfigPaths returns the loaded config file paths

func (*Dispatcher) FindTool

func (d *Dispatcher) FindTool(ctx context.Context, toolName string) ([]ToolMatch, error)

FindTool searches for a tool across all servers

func (*Dispatcher) GetClient added in v0.3.0

func (d *Dispatcher) GetClient(serverName string) (*Client, error)

GetClient returns a client for the given server name, creating one if needed

func (*Dispatcher) GetConfig

func (d *Dispatcher) GetConfig() *MCPConfig

GetConfig returns the underlying config

func (*Dispatcher) GetServerInfo

func (d *Dispatcher) GetServerInfo(ctx context.Context, serverName string) (*ServerInfo, error)

GetServerInfo returns info for a specific server

func (*Dispatcher) GetToolInfo

func (d *Dispatcher) GetToolInfo(ctx context.Context, serverName, toolName string) (*ToolMatch, error)

GetToolInfo returns info for a specific tool on a server

func (*Dispatcher) ListAllServers

func (d *Dispatcher) ListAllServers(ctx context.Context) []ServerInfo

ListAllServers returns server info with tools from all servers

func (*Dispatcher) ListServersConfig

func (d *Dispatcher) ListServersConfig() []ServerInfo

ListServersConfig returns server info from config without connecting

func (*Dispatcher) SearchTools added in v0.3.0

func (d *Dispatcher) SearchTools(ctx context.Context, query string) []ToolMatch

SearchTools searches for tools across all servers by name or description

func (*Dispatcher) SuggestTools added in v0.3.0

func (d *Dispatcher) SuggestTools(ctx context.Context, task string) []SuggestMatch

SuggestTools suggests tools based on a task description using keyword matching

type MCPConfig

type MCPConfig struct {
	MCPServers map[string]ServerConfig `json:"mcpServers"`
}

MCPConfig represents the full MCP configuration

func LoadConfig

func LoadConfig() (*MCPConfig, []string, error)

LoadConfig loads and merges all config files from search paths

func LoadConfigFromPaths

func LoadConfigFromPaths(paths []string) (*MCPConfig, []string, error)

LoadConfigFromPaths loads and merges config files from specified paths

func (*MCPConfig) GetServerConfig

func (c *MCPConfig) GetServerConfig(name string) (ServerConfig, bool)

GetServerConfig returns config for a specific server

type MCPError

type MCPError struct {
	Code    string
	Message string
	Details map[string]any
}

MCPError represents an MCP-specific error

func CallErrors

func CallErrors(toolName, serverName string, err error) *MCPError

CallErrors returns a CallFailed error

func ConfigErrors

func ConfigErrors(message string) *MCPError

ConfigErrors returns a ConfigNotFound error

func ConnectErrors

func ConnectErrors(serverName, message string) *MCPError

ConnectErrors returns a ConnectFailed error

func GetMCPError

func GetMCPError(err error) *MCPError

GetMCPError extracts MCPError from an error

func MethodNotFoundErrors

func MethodNotFoundErrors(toolName, serverName string) *MCPError

MethodNotFoundErrors returns a MethodNotFound error

func NewMCPError

func NewMCPError(code, message string) *MCPError

NewMCPError creates a new MCPError

func NewMCPErrorWithDetails

func NewMCPErrorWithDetails(code, message string, details map[string]any) *MCPError

NewMCPErrorWithDetails creates a new MCPError with details

func ParamErrors

func ParamErrors(message string) *MCPError

ParamErrors returns a ParamInvalid error

func ServerNotFoundErrors

func ServerNotFoundErrors(serverName string) *MCPError

ServerNotFoundErrors returns a ServerNotFound error

func (*MCPError) Error

func (e *MCPError) Error() string

type OAuthConfig added in v0.2.8

type OAuthConfig struct {
	// AccessToken is a static access token to use for authentication.
	// If provided, it will be used directly as a Bearer token.
	AccessToken string `json:"accessToken,omitempty"`
	// ClientID is the OAuth client ID (for full OAuth flow).
	ClientID string `json:"clientId,omitempty"`
	// ClientSecret is the OAuth client secret (for confidential clients).
	ClientSecret string `json:"clientSecret,omitempty"`
	// AuthorizationURL is the authorization server's authorization endpoint.
	AuthorizationURL string `json:"authorizationURL,omitempty"`
	// TokenURL is the authorization server's token endpoint.
	TokenURL string `json:"tokenURL,omitempty"`
	// Scopes are the OAuth scopes to request.
	Scopes string `json:"scopes,omitempty"`
	// RedirectURL is the callback URL for OAuth (defaults to http://localhost:7777/callback).
	RedirectURL string `json:"redirectURL,omitempty"`
	// ClientIDMetadataURL is the URL for Client ID Metadata Document (MCP spec).
	ClientIDMetadataURL string `json:"clientIdMetadataUrl,omitempty"`
}

OAuthConfig holds OAuth 2.1 authentication parameters

type ParamInfo

type ParamInfo struct {
	Name        string `json:"name"`
	Type        string `json:"type"`
	Required    bool   `json:"required"`
	Description string `json:"description,omitempty"`
}

ParamInfo represents structured parameter information

func GetParamInfoList

func GetParamInfoList(schema any) []ParamInfo

GetParamInfoList extracts structured parameter information

type ServerConfig

type ServerConfig struct {
	Transport string            `json:"transport,omitempty"`
	Type      string            `json:"type,omitempty"`
	URL       string            `json:"url,omitempty"`
	Command   string            `json:"command,omitempty"`
	Args      []string          `json:"args,omitempty"`
	Env       map[string]string `json:"env,omitempty"`
	Timeout   int               `json:"timeout,omitempty"`
	Headers   map[string]string `json:"headers,omitempty"`
	Auth      AuthConfig        `json:"auth,omitempty"`
}

ServerConfig represents a server configuration

type ServerInfo

type ServerInfo struct {
	Name      string   `json:"name"`
	Transport string   `json:"transport"`
	URL       string   `json:"url,omitempty"`
	Command   string   `json:"command,omitempty"`
	Tools     []string `json:"tools,omitempty"`
	Error     string   `json:"error,omitempty"`
}

ServerInfo represents information about an MCP server

type SuggestMatch added in v0.3.0

type SuggestMatch struct {
	ServerName string
	Tool       *ToolInfo
	Reason     string
}

SuggestMatch represents a suggested tool with reasoning

type ToolInfo

type ToolInfo struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	InputSchema any    `json:"inputSchema,omitempty"`
}

ToolInfo represents information about a tool

func FormatToolForOutput

func FormatToolForOutput(tool *mcp.Tool) *ToolInfo

FormatToolForOutput formats a tool for JSON output

type ToolMatch

type ToolMatch struct {
	ServerName string
	Tool       *ToolInfo
}

ToolMatch represents a matched tool with its server

Jump to

Keyboard shortcuts

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