Files
mcp/cmd/server/main.go
T
Rene Nochebuena 262eade93d feat(mcp): scaffold tool, config/env conventions, and scaffold-hygiene rules
Major release to v1.0.0, aligned with the v1.0.0 framework. The MCP documented
the wiring conventions but not the config half of a project, and its example
main.go omitted the godotenv autoload — so an assistant starting a service from
zero still hand-rolled main.go and the launcher, and got config wrong. This adds
a first-class scaffold, completes the config/.env.example conventions, and adds
rules that catch the "mess in main" pattern.

internal/tools:
- New get_scaffold: returns the canonical minimum application scaffold as
  ready-to-write files (main.go with godotenv autoload + wire.Run(), wire.go,
  a composed config.go, a health hook, .env.example), with import paths filled
  from a `module` argument. Registered in tools.go.

internal/rules:
- Three new validate_snippet rules, appended in scaffold_rules.go: main.dirty
  (launcher/components built in main instead of internal/wire),
  main.no-godotenv-autoload (a wire-convention main that never loads .env), and
  config.raw-getenv (an EINHERJAR_* var read via os.Getenv instead of composing
  the component Config; EINHERJAR_LOG_* stays with logz.direct-env-read).
- scaffold_rules_test.go — internal/rules had no tests; asserts each new rule
  fires and that a clean main is not flagged.

internal/index (builtins):
- The synthetic wire module gains a Config section (compose the framework's
  component configs, load with caarlos0/env, APP_* app fields / EINHERJAR_*
  framework fields) and a Config & .env.example discipline (every env var the
  config reads is documented in .env.example, kept in lock-step).
- main.go now shows the `_ "github.com/joho/godotenv/autoload"` blank import,
  previously omitted. The assembly file is renamed launcher.go -> wire.go.
- Migrations and seeding removed from the documented scaffold — developer
  choices, not framework conventions. Re-synced against iron-dough-api / pei-api.

Version:
- Badge and serverVersion const were stale at v0.1.0; both now v1.0.0.

Docs:
- README (eleven tools, eleven validation rules) and CHANGELOG updated.

No new dependencies. The wire conventions are embedded at build time
(//go:embed builtins/README.md) and the new tool and rules are compiled in, so a
deployment must be rebuilt to serve them; a server still running the v0.2.0
binary keeps serving the old conventions until redeployed.
2026-08-07 12:21:00 -06:00

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.0.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
}