core

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package core provides the canonical types for MCP server configuration that can be converted to/from various AI assistant formats.

Index

Constants

View Source
const DefaultFileMode fs.FileMode = 0600

DefaultFileMode is the default permission mode for configuration files. This can be used by adapters or overridden with WriteFileWithMode.

Variables

View Source
var (
	// ErrNoCommandOrURL is returned when a server has neither command nor URL.
	ErrNoCommandOrURL = errors.New("server must have either command (stdio) or url (http/sse)")

	// ErrBothCommandAndURL is returned when a server has both command and URL.
	ErrBothCommandAndURL = errors.New("server cannot have both command and url")

	// ErrInvalidTransport is returned when a transport type is invalid.
	ErrInvalidTransport = errors.New("invalid transport type")

	// ErrServerNotFound is returned when a server is not found.
	ErrServerNotFound = errors.New("server not found")

	// ErrEmptyConfig is returned when configuration is empty.
	ErrEmptyConfig = errors.New("configuration is empty")
)

Common errors for MCP configuration.

View Source
var DefaultRegistry = NewAdapterRegistry()

DefaultRegistry is the global adapter registry.

Functions

func Convert

func Convert(data []byte, from, to string) ([]byte, error)

Convert converts between formats using the default registry.

func Register

func Register(adapter Adapter)

Register adds an adapter to the default registry.

Types

type Adapter

type Adapter interface {
	// Name returns the name of the tool/format (e.g., "claude", "vscode").
	Name() string

	// DefaultPaths returns the default configuration file paths for this tool.
	DefaultPaths() []string

	// Parse parses tool-specific data into the canonical Config format.
	Parse(data []byte) (*Config, error)

	// Marshal converts the canonical Config to tool-specific format.
	Marshal(cfg *Config) ([]byte, error)

	// ReadFile reads a tool-specific config file and returns canonical Config.
	ReadFile(path string) (*Config, error)

	// WriteFile writes the canonical Config to a tool-specific file.
	WriteFile(cfg *Config, path string) error
}

Adapter defines the interface for converting between the canonical Config format and tool-specific formats.

func GetAdapter

func GetAdapter(name string) (Adapter, bool)

GetAdapter returns an adapter from the default registry.

type AdapterRegistry

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

AdapterRegistry holds registered adapters for different tools.

func NewAdapterRegistry

func NewAdapterRegistry() *AdapterRegistry

NewAdapterRegistry creates a new adapter registry.

func (*AdapterRegistry) Convert

func (r *AdapterRegistry) Convert(data []byte, from, to string) ([]byte, error)

Convert converts a config from one format to another.

func (*AdapterRegistry) Get

func (r *AdapterRegistry) Get(name string) (Adapter, bool)

Get returns an adapter by name.

func (*AdapterRegistry) Names

func (r *AdapterRegistry) Names() []string

Names returns the names of all registered adapters.

func (*AdapterRegistry) Register

func (r *AdapterRegistry) Register(adapter Adapter)

Register adds an adapter to the registry.

type Config

type Config struct {
	// Servers is a map of server names to their configurations.
	Servers map[string]Server `json:"servers"`

	// Inputs defines input variables for sensitive data like API keys (VS Code feature).
	Inputs []InputVariable `json:"inputs,omitempty"`
}

Config represents the canonical MCP configuration that can be converted to/from various AI assistant formats.

func NewConfig

func NewConfig() *Config

NewConfig creates a new empty Config.

func ReadFile

func ReadFile(path string) (*Config, error)

ReadFile reads a config from a JSON file.

func (*Config) AddInput

func (c *Config) AddInput(input InputVariable)

AddInput adds an input variable to the configuration.

func (*Config) AddServer

func (c *Config) AddServer(name string, server Server)

AddServer adds a server to the configuration.

func (*Config) EnabledServers

func (c *Config) EnabledServers() map[string]Server

EnabledServers returns only the servers that are enabled.

func (*Config) GetInput

func (c *Config) GetInput(id string) (InputVariable, bool)

GetInput returns an input variable by ID and whether it exists.

func (*Config) GetServer

func (c *Config) GetServer(name string) (Server, bool)

GetServer returns a server by name and whether it exists.

func (*Config) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (*Config) Merge

func (c *Config) Merge(other *Config)

Merge combines another config into this one. Servers and inputs from the other config override those in this config with the same name/ID.

func (*Config) RemoteServers

func (c *Config) RemoteServers() map[string]Server

RemoteServers returns only the servers configured for HTTP/SSE transport.

func (*Config) RemoveServer

func (c *Config) RemoveServer(name string)

RemoveServer removes a server from the configuration.

func (*Config) ServerNames

func (c *Config) ServerNames() []string

ServerNames returns a slice of all server names.

func (*Config) StdioServers

func (c *Config) StdioServers() map[string]Server

StdioServers returns only the servers configured for stdio transport.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid.

func (*Config) WriteFile

func (c *Config) WriteFile(path string) error

WriteFile writes the config to a file in JSON format using DefaultFileMode.

func (*Config) WriteFileWithMode

func (c *Config) WriteFileWithMode(path string, mode fs.FileMode) error

WriteFileWithMode writes the config to a file in JSON format with the specified permission mode.

type InputVariable

type InputVariable struct {
	// Type is the input type, typically "promptString".
	Type string `json:"type"`

	// ID is the unique identifier referenced in server config as ${input:id}.
	ID string `json:"id"`

	// Description is the user-friendly prompt text.
	Description string `json:"description"`

	// Password hides typed input when true (for API keys and passwords).
	Password bool `json:"password,omitempty"`
}

InputVariable represents a placeholder for sensitive configuration values. When referenced using ${input:variable-id}, the user is prompted for the value.

type ParseError

type ParseError struct {
	Format string
	Path   string
	Err    error
}

ParseError represents an error parsing a configuration file.

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type Server

type Server struct {
	// Transport specifies the communication protocol (stdio, http, sse).
	// If empty, it will be inferred from Command (stdio) or URL (http).
	Transport TransportType `json:"transport,omitempty"`

	// Command is the executable to launch for stdio servers.
	Command string `json:"command,omitempty"`

	// Args are the command-line arguments passed to the command.
	Args []string `json:"args,omitempty"`

	// Env contains environment variables for the server process.
	Env map[string]string `json:"env,omitempty"`

	// EnvFile is the path to an environment file to load (VS Code feature).
	EnvFile string `json:"envFile,omitempty"`

	// Cwd is the working directory for the server process (Codex feature).
	Cwd string `json:"cwd,omitempty"`

	// URL is the endpoint for HTTP/SSE servers.
	URL string `json:"url,omitempty"`

	// Headers contains HTTP headers for authentication or configuration.
	Headers map[string]string `json:"headers,omitempty"`

	// BearerTokenEnvVar is the name of an env var containing a bearer token (Codex feature).
	BearerTokenEnvVar string `json:"bearerTokenEnvVar,omitempty"`

	// EnabledTools is an allow-list of tools to expose (Codex/Cline feature).
	EnabledTools []string `json:"enabledTools,omitempty"`

	// DisabledTools is a deny-list of tools to hide (Codex/Cline feature).
	DisabledTools []string `json:"disabledTools,omitempty"`

	// AlwaysAllow lists tools that don't require user approval (Cline feature).
	AlwaysAllow []string `json:"alwaysAllow,omitempty"`

	// Enabled indicates whether the server is active. Defaults to true.
	Enabled *bool `json:"enabled,omitempty"`

	// StartupTimeoutSec is the timeout for server startup in seconds (Codex feature).
	StartupTimeoutSec int `json:"startupTimeoutSec,omitempty"`

	// ToolTimeoutSec is the timeout for tool execution in seconds (Codex feature).
	ToolTimeoutSec int `json:"toolTimeoutSec,omitempty"`

	// NetworkTimeoutSec is the timeout for network operations (Cline feature).
	NetworkTimeoutSec int `json:"networkTimeoutSec,omitempty"`
}

Server represents a canonical MCP server configuration that can be converted to/from various AI assistant formats (Claude, Cursor, VS Code, etc.).

func (*Server) InferTransport

func (s *Server) InferTransport() TransportType

InferTransport returns the inferred transport type based on configuration.

func (*Server) IsEnabled

func (s *Server) IsEnabled() bool

IsEnabled returns whether the server is enabled. Defaults to true if not set.

func (*Server) IsHTTP

func (s *Server) IsHTTP() bool

IsHTTP returns true if the server is configured for HTTP transport.

func (*Server) IsRemote

func (s *Server) IsRemote() bool

IsRemote returns true if the server is accessed over the network.

func (*Server) IsSSE

func (s *Server) IsSSE() bool

IsSSE returns true if the server is configured for SSE transport.

func (*Server) IsStdio

func (s *Server) IsStdio() bool

IsStdio returns true if the server is configured for stdio transport.

func (*Server) SetEnabled

func (s *Server) SetEnabled(enabled bool)

SetEnabled sets the enabled state of the server.

func (*Server) Validate

func (s *Server) Validate() error

Validate checks if the server configuration is valid.

type ServerValidationError

type ServerValidationError struct {
	Name string
	Err  error
}

ServerValidationError wraps a validation error with the server name.

func (*ServerValidationError) Error

func (e *ServerValidationError) Error() string

func (*ServerValidationError) Unwrap

func (e *ServerValidationError) Unwrap() error

type TransportType

type TransportType string

TransportType represents the communication protocol for an MCP server.

const (
	// TransportStdio represents local servers using standard input/output streams.
	TransportStdio TransportType = "stdio"

	// TransportHTTP represents remote servers using HTTP/Streamable HTTP transport.
	TransportHTTP TransportType = "http"

	// TransportSSE represents remote servers using Server-Sent Events (legacy).
	TransportSSE TransportType = "sse"
)

func (TransportType) IsLocal

func (t TransportType) IsLocal() bool

IsLocal returns true if the transport type runs locally (stdio).

func (TransportType) IsRemote

func (t TransportType) IsRemote() bool

IsRemote returns true if the transport type is network-based.

func (TransportType) String

func (t TransportType) String() string

String returns the string representation of the transport type.

func (TransportType) Valid

func (t TransportType) Valid() bool

Valid returns true if the transport type is a known valid type.

type WriteError

type WriteError struct {
	Format string
	Path   string
	Err    error
}

WriteError represents an error writing a configuration file.

func (*WriteError) Error

func (e *WriteError) Error() string

func (*WriteError) Unwrap

func (e *WriteError) Unwrap() error

Jump to

Keyboard shortcuts

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