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.
Reviewed-on: #3
Co-authored-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
Co-committed-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.nochebuena.dev/einherjar/mcp/internal/envspec"
|
|
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
|
"code.nochebuena.dev/einherjar/mcp/internal/rules"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
type validateSnippetInput struct {
|
|
Code string `json:"code" jsonschema:"Go source code to validate against Einherjar conventions. A full file is preferred; a partial body will be wrapped automatically."`
|
|
}
|
|
|
|
type validateSnippetOutput struct {
|
|
Findings []rules.Finding `json:"findings"`
|
|
Summary string `json:"summary"`
|
|
}
|
|
|
|
func registerValidateSnippet(s *mcp.Server, idx *index.Index) {
|
|
// Inject the real framework env-var names so config.unknown-env-var can reject
|
|
// invented/misspelled EINHERJAR_* tags instead of guessing.
|
|
rules.SetKnownEnvVars(envspec.KnownNames(idx))
|
|
mcp.AddTool(s, &mcp.Tool{
|
|
Name: "validate_snippet",
|
|
Description: "Validate a Go snippet against Einherjar wiring conventions: lifecycle setup, logger configuration, env-var handling, server registration. Findings are advisory, not a substitute for go vet or the project's tests.",
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args validateSnippetInput) (*mcp.CallToolResult, validateSnippetOutput, error) {
|
|
findings := rules.Run(args.Code)
|
|
if findings == nil {
|
|
findings = []rules.Finding{}
|
|
}
|
|
summary := summarise(findings)
|
|
out := validateSnippetOutput{Findings: findings, Summary: summary}
|
|
return jsonText(out), out, nil
|
|
})
|
|
}
|
|
|
|
func summarise(fs []rules.Finding) string {
|
|
if len(fs) == 0 {
|
|
return "No issues found — snippet follows Einherjar conventions."
|
|
}
|
|
var errs, warns, infos int
|
|
for _, f := range fs {
|
|
switch f.Severity {
|
|
case rules.SeverityError:
|
|
errs++
|
|
case rules.SeverityWarning:
|
|
warns++
|
|
case rules.SeverityInfo:
|
|
infos++
|
|
}
|
|
}
|
|
return pluralise(errs, "error", "errors") + ", " +
|
|
pluralise(warns, "warning", "warnings") + ", " +
|
|
pluralise(infos, "note", "notes")
|
|
}
|
|
|
|
func pluralise(n int, singular, plural string) string {
|
|
if n == 1 {
|
|
return "1 " + singular
|
|
}
|
|
return itoa(n) + " " + plural
|
|
}
|
|
|
|
func itoa(n int) string {
|
|
if n == 0 {
|
|
return "0"
|
|
}
|
|
neg := n < 0
|
|
if neg {
|
|
n = -n
|
|
}
|
|
var buf [20]byte
|
|
i := len(buf)
|
|
for n > 0 {
|
|
i--
|
|
buf[i] = byte('0' + n%10)
|
|
n /= 10
|
|
}
|
|
if neg {
|
|
i--
|
|
buf[i] = '-'
|
|
}
|
|
return string(buf[i:])
|
|
}
|