config.go

 1// Package config holds configuration for the SilverBullet MCP server,
 2// loaded from environment variables and command-line flags.
 3package config
 4
 5import (
 6	"fmt"
 7	"net/url"
 8	"os"
 9	"strconv"
10)
11
12// Config holds all configuration for the sb-mcp server.
13type Config struct {
14	// SBURL is the base URL of the SilverBullet instance (required).
15	SBURL string
16	// SBUser and SBPass are Basic Auth credentials (optional).
17	SBUser string
18	SBPass string
19	// SBToken is a Bearer token for authentication (optional).
20	SBToken string
21	// HTTPAddr is the address for streamable HTTP transport.
22	// Empty string means stdio transport.
23	HTTPAddr string
24	// DefaultTimeout is the default Lua execution timeout in seconds.
25	DefaultTimeout int
26}
27
28// Load reads configuration from environment variables.
29// SB_URL (required), SB_USER, SB_PASS, SB_TOKEN (optional).
30func Load() (*Config, error) {
31	sbURL := os.Getenv("SB_URL")
32	if sbURL == "" {
33		return nil, fmt.Errorf("SB_URL environment variable is required")
34	}
35
36	u, err := url.Parse(sbURL)
37	if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
38		return nil, fmt.Errorf("SB_URL must be a valid HTTP(S) URL, got: %s", sbURL)
39	}
40
41	cfg := &Config{
42		SBURL:          sbURL,
43		SBUser:         os.Getenv("SB_USER"),
44		SBPass:         os.Getenv("SB_PASS"),
45		SBToken:        os.Getenv("SB_TOKEN"),
46		DefaultTimeout: parseDefaultTimeout(),
47	}
48
49	return cfg, nil
50}
51
52// HasBasicAuth returns true if both SB_USER and SB_PASS are set.
53func (c *Config) HasBasicAuth() bool {
54	return c.SBUser != "" && c.SBPass != ""
55}
56
57// HasBearerToken returns true if SB_TOKEN is set.
58func (c *Config) HasBearerToken() bool {
59	return c.SBToken != ""
60}
61
62// parseDefaultTimeout reads the default Lua execution timeout from
63// the SB_TIMEOUT environment variable (in seconds), defaulting to 120.
64func parseDefaultTimeout() int {
65	s := os.Getenv("SB_TIMEOUT")
66	if s == "" {
67		return 120
68	}
69	n, err := strconv.Atoi(s)
70	if err != nil || n < 1 {
71		return 120
72	}
73	return n
74}