daemon

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAPIShutdownTimeout

func DefaultAPIShutdownTimeout() time.Duration

DefaultAPIShutdownTimeout is the default time allowed for API server graceful shutdown.

func DefaultCORSAllowCredentials

func DefaultCORSAllowCredentials() bool

DefaultCORSAllowCredentials returns the default CORS 'allow credentials' setting.

func DefaultCORSAllowHeaders

func DefaultCORSAllowHeaders() []string

DefaultCORSAllowHeaders returns standard headers required for API interaction.

func DefaultCORSAllowMethods

func DefaultCORSAllowMethods() []string

DefaultCORSAllowMethods returns standard HTTP methods for CORS.

func DefaultCORSMaxAge

func DefaultCORSMaxAge() time.Duration

DefaultCORSMaxAge returns the default CORS max age duration. Max age is the default time browsers can cache preflight responses.

func DefaultClientInitTimeout

func DefaultClientInitTimeout() time.Duration

DefaultClientInitTimeout is the default time to wait for MCP server initialization.

func DefaultClientShutdownTimeout

func DefaultClientShutdownTimeout() time.Duration

DefaultClientShutdownTimeout is the default time to wait for MCP clients to close.

func DefaultHealthCheckInterval

func DefaultHealthCheckInterval() time.Duration

DefaultHealthCheckInterval is the default interval for health checks.

func DefaultHealthCheckTimeout

func DefaultHealthCheckTimeout() time.Duration

DefaultHealthCheckTimeout is the default timeout for health check responses.

func DefaultMiddlewareProvider

func DefaultMiddlewareProvider() func(context.Context) (func(http.Handler) http.Handler, error)

DefaultMiddlewareProvider returns a provider that supplies no-op middleware. The no-op middleware passes requests through unchanged.

func IsValidAddr

func IsValidAddr(addr string) error

IsValidAddr returns an error if the address is not a valid "host:port" string.

Types

type APIDependencies

type APIDependencies struct {
	// Addr specifies the network address to bind (e.g., "0.0.0.0:8090").
	Addr string

	// ClientManager handles MCP client connections.
	ClientManager contracts.MCPClientAccessor

	// HealthTracker monitors server health status.
	HealthTracker contracts.MCPHealthMonitor

	// Logger for API server operations.
	Logger hclog.Logger
}

APIDependencies contains the required external dependencies for the API server. NewAPIDependencies should be used to create instances of APIDependencies.

func NewAPIDependencies

func NewAPIDependencies(
	logger hclog.Logger,
	clientManager contracts.MCPClientAccessor,
	healthTracker contracts.MCPHealthMonitor,
	addr string,
) (APIDependencies, error)

NewAPIDependencies creates and validates APIDependencies.

func (APIDependencies) Validate

func (d APIDependencies) Validate() error

Validate ensures all required dependencies are provided and valid.

type APIOption

type APIOption func(*APIOptions) error

APIOption defines a functional option for configuring APIOptions. Options are applied in order, with later options overriding earlier ones.

func WithCORSAllowCredentials

func WithCORSAllowCredentials(allowed bool) APIOption

WithCORSAllowCredentials sets whether credentials are allowed in CORS requests.

func WithCORSAllowHeaders

func WithCORSAllowHeaders(headers []string) APIOption

WithCORSAllowHeaders sets which additional request headers are safe for the client to send. See: https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_request_header for information on safelisted-headers that don't required explicit configuration.

func WithCORSAllowMethods

func WithCORSAllowMethods(methods []string) APIOption

WithCORSAllowMethods sets the allowed HTTP methods for CORS requests.

func WithCORSAllowOrigins

func WithCORSAllowOrigins(origins []string) APIOption

WithCORSAllowOrigins sets the allowed origins for CORS requests.

func WithCORSEnabled

func WithCORSEnabled(enabled bool) APIOption

WithCORSEnabled enables or disables CORS support.

func WithCORSExposeHeaders

func WithCORSExposeHeaders(headers []string) APIOption

WithCORSExposeHeaders sets which additional response headers are safe for the client to read. See: https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header for information on safelisted-headers that don't required explicit configuration.

func WithCORSMaxAge

func WithCORSMaxAge(maxAge time.Duration) APIOption

WithCORSMaxAge sets how long browsers can cache CORS preflight responses.

func WithMiddlewareProvider

func WithMiddlewareProvider(provider func(context.Context) (func(http.Handler) http.Handler, error)) APIOption

WithMiddlewareProvider configures a provider function that returns HTTP middleware. The provider is called during API server startup to lazily initialize middleware.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) APIOption

WithShutdownTimeout configures how long to wait for graceful shutdown.

type APIOptions

type APIOptions struct {
	// CORS configuration for cross-origin requests.
	CORS CORSConfig

	// ShutdownTimeout specifies how long to wait for graceful shutdown.
	ShutdownTimeout time.Duration

	// MiddlewareProvider lazily provides HTTP middleware when called during API server startup.
	// This allows plugin initialization to be deferred until the server actually starts.
	MiddlewareProvider func(context.Context) (func(http.Handler) http.Handler, error)
}

APIOptions contains optional configuration for the API server. NewAPIOptions should be used to create instances of APIOptions.

func NewAPIOptions

func NewAPIOptions(opts ...APIOption) (APIOptions, error)

NewAPIOptions creates APIOptions with optional configurations applied. Starts with default values, then applies options in order with later options overriding earlier ones.

type APIServer

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

APIServer manages the HTTP API for the daemon. NewAPIServer should be used to create instances of APIServer.

func NewAPIServer

func NewAPIServer(deps APIDependencies, opt ...APIOption) (*APIServer, error)

NewAPIServer creates a new API server with the provided dependencies and options. Applies default options first, then user-provided options to ensure all fields have valid values.

func (*APIServer) Start

func (a *APIServer) Start(ctx context.Context) error

Start starts the API server and blocks until the context is canceled or an error occurs.

type CORSConfig

type CORSConfig struct {
	// Enabled determines whether CORS headers are added to responses.
	Enabled bool

	// AllowCredentials indicates whether the request can include credentials.
	// Must be false when AllowOrigins contains "*"
	AllowCredentials bool

	// AllowedHeaders specifies which headers the client can include in requests.
	AllowedHeaders []string

	// AllowMethods specifies which HTTP methods are permitted.
	// Using strings to match the go-chi/cors library API.
	AllowMethods []string

	// AllowOrigins specifies which origins can access the API.
	// Use ["*"] to allow all origins (not recommended for production).
	AllowOrigins []string

	// ExposedHeaders specifies which response headers are accessible to the client.
	ExposedHeaders []string

	// MaxAge specifies how long browsers can cache preflight responses.
	MaxAge time.Duration
}

CORSConfig defines Cross-Origin Resource Sharing settings for the API server.

type ClientManager

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

ClientManager holds active client connections and their associated tool lists. It is safe for concurrent use by multiple goroutines. NewClientManager should be used to create instances of ClientManager.

func NewClientManager

func NewClientManager() *ClientManager

NewClientManager creates an empty, concurrency-safe ClientManager.

func (*ClientManager) Add

func (cm *ClientManager) Add(name string, c client.MCPClient, tools []string)

Add registers a client and its tools by server name. The server name and tool names are normalized (lowercase, trimmed) for consistent lookups. This method is safe for concurrent use.

func (*ClientManager) Client

func (cm *ClientManager) Client(name string) (client.MCPClient, bool)

Client returns the client for the given server name. The server name is normalized for case-insensitive lookup. It returns a boolean to indicate whether the client was found. This method is safe for concurrent use.

func (*ClientManager) List

func (cm *ClientManager) List() []string

List returns all known server names. This method is safe for concurrent use.

func (*ClientManager) Remove

func (cm *ClientManager) Remove(name string)

Remove deletes the client and its tools by server name. The server name is normalized for case-insensitive lookup. This method is safe for concurrent use.

func (*ClientManager) Tools

func (cm *ClientManager) Tools(name string) ([]string, bool)

Tools returns the tools for the given server name. The server name is normalized for case-insensitive lookup. It returns a boolean to indicate whether the tools were found. This method is safe for concurrent use.

func (*ClientManager) UpdateTools

func (cm *ClientManager) UpdateTools(name string, tools []string) error

UpdateTools updates the tools list for an existing server without restarting the client. The server name and tool names are normalized for consistent lookups. Returns an error if the server is not found. This method is safe for concurrent use.

type Daemon

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

Daemon manages MCP server lifecycles, client connections, and health monitoring. It should only be created using NewDaemon to ensure proper initialization.

func NewDaemon

func NewDaemon(deps Dependencies, opt ...Option) (*Daemon, error)

NewDaemon creates a new Daemon instance with proper initialization. Use this function instead of directly creating a Daemon struct.

func (*Daemon) ReloadServers

func (d *Daemon) ReloadServers(ctx context.Context, newServers []runtime.Server) error

ReloadServers reloads the daemon's MCP servers based on a new configuration. It compares the current servers with the new configuration and: - Stops servers that have been removed - Starts servers that have been added - Updates tools for servers where only tools changed - Restarts servers with other configuration changes - Preserves servers that remain unchanged (keeping their client connections, tools, and health history intact)

func (*Daemon) StartAndManage

func (d *Daemon) StartAndManage(ctx context.Context) error

StartAndManage is a long-running method that starts configured MCP servers, and the API. It launches regular health checks on the MCP servers, with statuses visible via API routes.

type Dependencies

type Dependencies struct {
	// APIAddr specifies the network address for the APIServer to bind (e.g., "0.0.0.0:8090").
	APIAddr string

	// Logger for daemon and subcomponent (API server) operations.
	Logger hclog.Logger

	// RuntimeServers contains the aggregated runtime servers (config + secrets).
	RuntimeServers []runtime.Server
}

Dependencies contains required dependencies for the Daemon. NewDependencies should be used to create instances of Dependencies.

func NewDependencies

func NewDependencies(
	logger hclog.Logger,
	apiAddr string,
	runtimeServers []runtime.Server,
) (Dependencies, error)

NewDependencies creates Dependencies with processed runtime servers.

func (Dependencies) Validate

func (d Dependencies) Validate() error

Validate ensures all required dependencies are provided and valid.

type HealthTracker

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

HealthTracker is used to track the health of registered MCP servers. NewHealthTracker should be used to initialize this type.

func NewHealthTracker

func NewHealthTracker(serverNames []string) *HealthTracker

NewHealthTracker creates a HealthTracker which tracks the specified MCP server names.

func (*HealthTracker) Add

func (h *HealthTracker) Add(name string)

Add registers a new server for health tracking. If the server is already being tracked, this is a no-op.

func (*HealthTracker) List

func (h *HealthTracker) List() []domain.ServerHealth

List returns a copy of all known server health records.

func (*HealthTracker) Remove

func (h *HealthTracker) Remove(name string)

Remove stops tracking health for a server and removes its health data.

func (*HealthTracker) Status

func (h *HealthTracker) Status(name string) (domain.ServerHealth, error)

Status returns the health status for a single tracked server.

func (*HealthTracker) Update

func (h *HealthTracker) Update(name string, status domain.HealthStatus, latency *time.Duration) error

Update records a health check for a tracked server. The current time is recorded as LastChecked, and LastSuccessful is updated only if status is HealthStatusOK. Latency can be nil if the ping failed or was not measured.

type Option

type Option func(*Options) error

Option defines a functional option for configuring Options. Options are applied in order, with later options overriding earlier ones.

func WithAPIOptions

func WithAPIOptions(apiOpts ...APIOption) Option

WithAPIOptions configures API server options. Replaces all previous API configuration including CORS settings.

func WithMCPServerHealthCheckInterval

func WithMCPServerHealthCheckInterval(interval time.Duration) Option

WithMCPServerHealthCheckInterval configures how often to ping MCP servers for health checks.

func WithMCPServerHealthCheckTimeout

func WithMCPServerHealthCheckTimeout(timeout time.Duration) Option

WithMCPServerHealthCheckTimeout configures maximum time to wait for MCP server health check responses.

func WithMCPServerInitTimeout

func WithMCPServerInitTimeout(timeout time.Duration) Option

WithMCPServerInitTimeout configures how long to wait for MCP servers to initialize.

func WithMCPServerShutdownTimeout

func WithMCPServerShutdownTimeout(timeout time.Duration) Option

WithMCPServerShutdownTimeout configures how long to wait for MCP servers to shut down.

func WithPluginConfig

func WithPluginConfig(cfg *config.PluginConfig) Option

WithPluginConfig configures the plugin system with the specified configuration.

type Options

type Options struct {
	// APIOptions contains functional options for the API server.
	APIOptions []APIOption

	// ClientInitTimeout specifies how long to wait for MCP server initialization.
	ClientInitTimeout time.Duration

	// ClientHealthCheckInterval specifies how often to ping MCP servers for health checks.
	ClientHealthCheckInterval time.Duration

	// ClientHealthCheckTimeout specifies maximum time to wait for health check responses.
	ClientHealthCheckTimeout time.Duration

	// ClientShutdownTimeout specifies how long to wait for MCP clients to close.
	ClientShutdownTimeout time.Duration

	// PluginConfig specifies the configuration for plugins.
	PluginConfig *config.PluginConfig
}

Options contains optional configuration for the daemon. NewOptions should be used to create instances of Options.

func NewOptions

func NewOptions(opts ...Option) (Options, error)

NewOptions creates Options with optional configurations applied. Starts with default values, then applies options in order with later options overriding earlier ones.

Jump to

Keyboard shortcuts

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