Minor to v1.1.0. Make the whole environment-variable surface derive from the
indexed component-config tags instead of a hand-maintained list that drifts.
Also folds in the scaffold-symbol fixes staged as v1.0.1 (never tagged); the
generated scaffold compiles clean against einherjar v1.0.0.
internal/envspec (new):
- Parse index struct tags into env vars {name, module, struct, field, required,
default}. One source of truth: ParseTag, ForModule, FindStruct, All, KnownNames.
internal/tools:
- get_config_env: list the real env vars a component config reads (or all).
- check_env: flag unknown EINHERJAR_* names, required vars missing for the
composed modules, and dead vars (set for an uncomposed module).
- get_scaffold: .env.example is now DERIVED from the index — required vars
uncommented with a dev value, defaulted vars commented with their default.
The scaffold now composes logz.Config (Log logz.Config) so EINHERJAR_LOG_*
are live and documented, not hardcoded/ignored (log format is env-driven).
- validate_snippet: inject the real env-var name set into the rules package.
internal/index/builtins (wire conventions):
- Route the incremental "compose a component later" flow to get_config_env /
check_env; distinguish framework EINHERJAR_* from app-owned APP_* (JWT).
- Compose logz.Config in the config + Run() examples, to match the scaffold.
internal/rules:
- config.unknown-env-var (twelfth rule): reject an env:"EINHERJAR_*" struct tag
the framework doesn't declare. No-op until the server injects the name set, so
it never fires on incomplete knowledge.
Fixed (was v1.0.1): scaffold health hook + wire builtin used logz.Logger (real:
contracts/logging.Logger) and postgres.Component (hooks take Provider); env tags
were EINHERJAR_SERVER_ADDR / EINHERJAR_PG_DATABASE (real: _HOST/_PORT / _PG_NAME).
Tests: envspec unit tests; env tools against the real data/index.json; the rule.
Verified by generating the scaffold, building it against local einherjar v1.0.0
(exit 0), and runtime-loading the composed logz.Config (EINHERJAR_LOG_LEVEL=DEBUG
-> slog.LevelDebug, EINHERJAR_LOG_JSON=true). Version bumped to v1.1.0 (badge +
serverVersion). No dependency changes.
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
// Command server is the Einherjar MCP server: a streamable-HTTP service
|
|
// exposing framework knowledge tools to AI assistants.
|
|
//
|
|
// The framework index is embedded into the binary at build time by
|
|
// cmd/indexer. The server itself only reads it.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
|
|
mcpmod "code.nochebuena.dev/einherjar/mcp"
|
|
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
|
"code.nochebuena.dev/einherjar/mcp/internal/tools"
|
|
|
|
"github.com/coreos/go-systemd/v22/activation"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
const (
|
|
serverName = "einherjar-mcp"
|
|
serverVersion = "v1.1.0"
|
|
)
|
|
|
|
func main() {
|
|
addr := flag.String("addr", envOr("EINHERJAR_MCP_ADDR", ":8080"), "listen address")
|
|
path := flag.String("path", envOr("EINHERJAR_MCP_PATH", "/mcp"), "HTTP path for the MCP streamable endpoint")
|
|
flag.Parse()
|
|
|
|
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
|
|
|
idx, err := index.Load(mcpmod.IndexJSON)
|
|
if err != nil {
|
|
log.Error("load index", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
log.Info("index loaded", "modules", len(idx.Modules), "builtAt", idx.BuiltAt)
|
|
|
|
server := mcp.NewServer(&mcp.Implementation{
|
|
Name: serverName,
|
|
Version: serverVersion,
|
|
}, nil)
|
|
tools.Register(server, idx)
|
|
|
|
handler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
|
|
return server
|
|
}, nil)
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle(*path, handler)
|
|
mux.HandleFunc(*path+"/healthz", func(w http.ResponseWriter, _ *http.Request) {
|
|
fmt.Fprintln(w, "ok")
|
|
})
|
|
|
|
ln, mode, err := chooseListener(*addr)
|
|
if err != nil {
|
|
log.Error("listen", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
log.Info("listening", "mode", mode, "addr", ln.Addr().String(), "path", *path)
|
|
|
|
srv := &http.Server{Handler: mux}
|
|
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
|
|
log.Error("server", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// chooseListener returns a socket-activated listener when systemd inherited
|
|
// one, falling back to a plain TCP listener on addr. The mode string is
|
|
// "socket-activated" or "tcp" for logging.
|
|
func chooseListener(addr string) (net.Listener, string, error) {
|
|
listeners, err := activation.Listeners()
|
|
if err == nil && len(listeners) > 0 {
|
|
return listeners[0], "socket-activated", nil
|
|
}
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return ln, "tcp", nil
|
|
}
|
|
|
|
func envOr(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|