61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package tools
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"code.nochebuena.dev/einherjar/mcp/internal/envspec"
|
||
|
|
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
||
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||
|
|
)
|
||
|
|
|
||
|
|
type getConfigEnvInput struct {
|
||
|
|
Module string `json:"module,omitempty" jsonschema:"restrict to one module's env vars, e.g. db-postgres or web. Omit to return every framework env var."`
|
||
|
|
}
|
||
|
|
|
||
|
|
type getConfigEnvOutput struct {
|
||
|
|
Vars []envspec.Var `json:"vars"`
|
||
|
|
Count int `json:"count"`
|
||
|
|
Summary string `json:"summary"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func registerGetConfigEnv(s *mcp.Server, idx *index.Index) {
|
||
|
|
mcp.AddTool(s, &mcp.Tool{
|
||
|
|
Name: "get_config_env",
|
||
|
|
Description: "List the REAL environment variables an Einherjar component config reads, derived from the framework's struct tags (never hand-maintained). " +
|
||
|
|
"Each entry gives the var name, the declaring module/struct/field, whether it is required, and its default. " +
|
||
|
|
"Use this before composing a global Config or writing a .env — it is the source of truth for which EINHERJAR_* vars exist. " +
|
||
|
|
"Pass a module (e.g. db-postgres, web, core, storage-minio) to scope it; omit to get all.",
|
||
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args getConfigEnvInput) (*mcp.CallToolResult, getConfigEnvOutput, error) {
|
||
|
|
module := strings.TrimSpace(args.Module)
|
||
|
|
var vars []envspec.Var
|
||
|
|
if module == "" {
|
||
|
|
vars = envspec.All(idx)
|
||
|
|
} else {
|
||
|
|
if idx.FindModule(module) == nil {
|
||
|
|
return errorResult("module not found: " + module), getConfigEnvOutput{}, nil
|
||
|
|
}
|
||
|
|
vars = envspec.ForModule(idx, module)
|
||
|
|
}
|
||
|
|
if vars == nil {
|
||
|
|
vars = []envspec.Var{}
|
||
|
|
}
|
||
|
|
out := getConfigEnvOutput{Vars: vars, Count: len(vars), Summary: summariseEnv(vars, module)}
|
||
|
|
return jsonText(out), out, nil
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func summariseEnv(vars []envspec.Var, module string) string {
|
||
|
|
var required int
|
||
|
|
for _, v := range vars {
|
||
|
|
if v.Required {
|
||
|
|
required++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
scope := "the framework"
|
||
|
|
if module != "" {
|
||
|
|
scope = module
|
||
|
|
}
|
||
|
|
return itoa(len(vars)) + " env var(s) for " + scope + ", " + itoa(required) + " required."
|
||
|
|
}
|