Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
13a186c60a
|
|||
|
e23e86b06c
|
7
.gitignore
vendored
7
.gitignore
vendored
@@ -39,3 +39,10 @@ Thumbs.db
|
|||||||
|
|
||||||
# ── Generated framework index (rebuilt by cmd/indexer; placeholder is committed) ─
|
# ── Generated framework index (rebuilt by cmd/indexer; placeholder is committed) ─
|
||||||
data/index.json
|
data/index.json
|
||||||
|
|
||||||
|
# ── Local deployment artifacts (systemd units, Hestia templates, deploy scripts) ─
|
||||||
|
# Operator-specific by design. Different users will deploy on k8s, Fly.io, Render,
|
||||||
|
# Cloud Run, bare metal — our HestiaCP + systemd + unix-socket layout is one shape
|
||||||
|
# among many and shouldn't be presented as the canonical path in a public repo.
|
||||||
|
# The Dockerfile at the module root is the portable artifact.
|
||||||
|
/deploy/
|
||||||
|
|||||||
47
CHANGELOG.md
47
CHANGELOG.md
@@ -6,6 +6,53 @@ This module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## [0.2.0] — 2026-06-10
|
||||||
|
|
||||||
|
Minor release. The indexer now captures the *members* of composite types, closing a gap where `get_symbol` and `search_symbols` could name a struct or interface but not describe its shape — most painfully, struct tags (env-var keys, json names) were invisible.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Struct fields in the index.** Each struct-type symbol now carries a `fields` array — field name, type, raw struct tag (backticks stripped), doc comment, and an `embedded` marker. `get_symbol` returns it; previously the signature was truncated to the bare `type X struct` header, so field names, types, and tags (e.g. `env:"EINHERJAR_PG_HOST"`) were dropped entirely.
|
||||||
|
- **Interface method sets in the index.** Each interface-type symbol now carries a `methods` array — method name, signature (without the leading `func`), and doc comment, including embedded interfaces. Consumers can now see what a port like `db-postgres` `Provider` actually requires.
|
||||||
|
- **Search by field/tag/method.** `search_symbols` now also matches a query against struct field names, field types, and struct tags, and against interface method names/signatures — so an env-var key or a method name resolves to the type that declares it.
|
||||||
|
- **`internal/index` test suite** covering field, tag, embedded-field, interface-method, and search-by-tag capture (the package previously had no tests).
|
||||||
|
|
||||||
|
### Notes
|
||||||
|
|
||||||
|
- The index schema gains two optional (`omitempty`) fields; `SchemaVersion` is unchanged because the change is additive and older consumers parse the new index unchanged.
|
||||||
|
- This is a pure indexer/schema change. The live server picks it up on its next image build, which re-runs `cmd/indexer` (see `Dockerfile`). A deployment that has not been rebuilt will still serve the older, member-less index.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## [0.1.1] — 2026-05-29
|
||||||
|
|
||||||
|
Patch release. Two changes to `cmd/server` make the binary cleaner to run behind a unix socket on a reverse-proxied host, plus two repository-hygiene changes that follow from the same deployment exercise.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Systemd socket activation** in `cmd/server`. The binary inherits the listener from `LISTEN_FDS` via `github.com/coreos/go-systemd/v22/activation` when present, falling back transparently to TCP `-addr` binding otherwise. Startup log records `"mode":"socket-activated"` or `"mode":"tcp"`. Same binary, no flag or env var to toggle.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- **Health probe path** moved from `/healthz` to `<path>/healthz` (default `/mcp/healthz`). Lets a reverse proxy expose the entire MCP service through one location prefix. v0.1.0 consumers hitting the old `/healthz` route receive 404; update to `/mcp/healthz` (or whatever path matches your `-path` flag).
|
||||||
|
- **`README.md` deployment section** rewritten to be hosting-agnostic. Points at the `Dockerfile` and systemd socket activation as supported binary modes without prescribing one operator's setup. Adds the SSE-buffering caveat once: any reverse proxy must disable response buffering on the `/mcp` location, otherwise Server-Sent Events get batched and streamable MCP sessions break.
|
||||||
|
- **`/deploy/`** is now `.gitignored`. Local deployment artefacts (systemd units, reverse-proxy templates, per-release scripts) are operator-specific by design and live outside the public repository. The `Dockerfile` at the module root remains the only portable, public-facing build artefact.
|
||||||
|
|
||||||
|
### Dependencies
|
||||||
|
|
||||||
|
- **Added:** `github.com/coreos/go-systemd/v22 v22.7.0` — used by `cmd/server` to detect and use a systemd-passed listener.
|
||||||
|
|
||||||
|
### Upgrade notes
|
||||||
|
|
||||||
|
| If you… | Action |
|
||||||
|
|---|---|
|
||||||
|
| Consume the MCP service from an MCP client (Claude, Cursor, Zed, etc.) | None — `/mcp` is unchanged |
|
||||||
|
| Monitor the service via the healthz probe | Update the probe URL from `/healthz` to `/mcp/healthz` |
|
||||||
|
| Run the binary directly (no reverse proxy) | None — `-addr` TCP binding still works the same way |
|
||||||
|
| Run the binary under systemd with a socket unit | The same binary now picks up the inherited listener automatically |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## [0.1.0] — 2026-05-29
|
## [0.1.0] — 2026-05-29
|
||||||
|
|
||||||
Initial release. The `mcp` module hosts the **Einherjar Model Context Protocol server** — a remote, streamable-HTTP service that teaches AI assistants about every other module of the framework.
|
Initial release. The `mcp` module hosts the **Einherjar Model Context Protocol server** — a remote, streamable-HTTP service that teaches AI assistants about every other module of the framework.
|
||||||
|
|||||||
35
README.md
35
README.md
@@ -45,8 +45,8 @@ Cursor, Zed, and anything else that speaks MCP):
|
|||||||
|---|---|
|
|---|---|
|
||||||
| `list_modules` | Enumerate every Einherjar module with its purpose and sub-packages |
|
| `list_modules` | Enumerate every Einherjar module with its purpose and sub-packages |
|
||||||
| `get_module` | Package doc, dependencies, sub-packages, key types, compliance counts; optional README |
|
| `get_module` | Package doc, dependencies, sub-packages, key types, compliance counts; optional README |
|
||||||
| `search_symbols` | Find a type, function, or interface by name, doc text, sub-package, or module |
|
| `search_symbols` | Find a type, function, or interface by name, doc text, sub-package, module — or by a struct field/tag or an interface method |
|
||||||
| `get_symbol` | Full signature, doc comment, and source location for one symbol |
|
| `get_symbol` | Full signature, doc comment, and source location for one symbol — plus struct fields (with tags) and interface method sets |
|
||||||
| `list_adrs` | List architectural decision records, optionally restricted to one module |
|
| `list_adrs` | List architectural decision records, optionally restricted to one module |
|
||||||
| `get_adr` | Fetch a single ADR's markdown body |
|
| `get_adr` | Fetch a single ADR's markdown body |
|
||||||
| `get_example` | Canonical usage snippet — pulled from module READMEs and from the synthetic `wire` conventions |
|
| `get_example` | Canonical usage snippet — pulled from module READMEs and from the synthetic `wire` conventions |
|
||||||
@@ -159,6 +159,37 @@ All four commands must produce clean output before a PR will be reviewed.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
The server is a single self-contained static binary. There is no canonical
|
||||||
|
hosting shape — pick whichever matches the rest of your infrastructure. Two
|
||||||
|
patterns cover most cases:
|
||||||
|
|
||||||
|
- **Container.** The [`Dockerfile`](Dockerfile) at the module root produces a
|
||||||
|
distroless runtime image. Build from the einherjar repository root so the
|
||||||
|
indexer can reach every sibling module at image-build time:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -f mcp/Dockerfile -t einherjar-mcp:0.1.0 .
|
||||||
|
docker run --rm -p 8080:8080 einherjar-mcp:0.1.0
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Systemd / socket-activated binary.** The server detects an inherited
|
||||||
|
listener via `github.com/coreos/go-systemd/v22/activation` and uses it when
|
||||||
|
present, falling back to `-addr` TCP binding otherwise. Same binary works in
|
||||||
|
both modes — no flag, no env var. Drop the binary into `/opt/<somewhere>/`
|
||||||
|
and write a `.socket` + `.service` pair that matches your conventions.
|
||||||
|
|
||||||
|
Whatever shape you pick, the public-facing reverse proxy must keep response
|
||||||
|
buffering **off** on the `/mcp` location. Streamable MCP delivers tool results
|
||||||
|
via Server-Sent Events; default nginx, Envoy, or Caddy buffering batches the
|
||||||
|
stream and breaks Claude's session before the first event arrives. For nginx
|
||||||
|
that means `proxy_buffering off; proxy_cache off; proxy_request_buffering off;
|
||||||
|
chunked_transfer_encoding on;` plus an extended `proxy_read_timeout` for
|
||||||
|
long-lived sessions.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Architecture Decisions
|
## Architecture Decisions
|
||||||
|
|
||||||
No ADRs at `v0.1.0`. The structural decisions in this release (synthetic `wire`
|
No ADRs at `v0.1.0`. The structural decisions in this release (synthetic `wire`
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ import (
|
|||||||
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
||||||
"code.nochebuena.dev/einherjar/mcp/internal/tools"
|
"code.nochebuena.dev/einherjar/mcp/internal/tools"
|
||||||
|
|
||||||
|
"github.com/coreos/go-systemd/v22/activation"
|
||||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,17 +52,39 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle(*path, handler)
|
mux.Handle(*path, handler)
|
||||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
|
mux.HandleFunc(*path+"/healthz", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
fmt.Fprintln(w, "ok")
|
fmt.Fprintln(w, "ok")
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Info("listening", "addr", *addr, "path", *path)
|
ln, mode, err := chooseListener(*addr)
|
||||||
if err := http.ListenAndServe(*addr, mux); err != nil {
|
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)
|
log.Error("server", "err", err)
|
||||||
os.Exit(1)
|
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 {
|
func envOr(key, def string) string {
|
||||||
if v := os.Getenv(key); v != "" {
|
if v := os.Getenv(key); v != "" {
|
||||||
return v
|
return v
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -2,7 +2,10 @@ module code.nochebuena.dev/einherjar/mcp
|
|||||||
|
|
||||||
go 1.26
|
go 1.26
|
||||||
|
|
||||||
require github.com/modelcontextprotocol/go-sdk v1.0.0
|
require (
|
||||||
|
github.com/coreos/go-systemd/v22 v22.7.0
|
||||||
|
github.com/modelcontextprotocol/go-sdk v1.0.0
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/jsonschema-go v0.3.0 // indirect
|
github.com/google/jsonschema-go v0.3.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
|
github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
|
||||||
|
|||||||
@@ -154,7 +154,14 @@ func collectSymbols(m *Module, sub, modDir string, fset *token.FileSet, p *doc.P
|
|||||||
if isInterface(t.Decl) {
|
if isInterface(t.Decl) {
|
||||||
kind = "interface"
|
kind = "interface"
|
||||||
}
|
}
|
||||||
m.Symbols = append(m.Symbols, newSymbol(m.Name, sub, kind, t.Name, t.Doc, t.Decl, fset, modDir))
|
sym := newSymbol(m.Name, sub, kind, t.Name, t.Doc, t.Decl, fset, modDir)
|
||||||
|
switch underlying := typeSpecType(t.Decl).(type) {
|
||||||
|
case *ast.StructType:
|
||||||
|
sym.Fields = extractFields(fset, underlying)
|
||||||
|
case *ast.InterfaceType:
|
||||||
|
sym.Methods = extractIfaceMethods(fset, underlying)
|
||||||
|
}
|
||||||
|
m.Symbols = append(m.Symbols, sym)
|
||||||
for _, f := range t.Funcs {
|
for _, f := range t.Funcs {
|
||||||
m.Symbols = append(m.Symbols, newSymbol(m.Name, sub, "func", f.Name, f.Doc, f.Decl, fset, modDir))
|
m.Symbols = append(m.Symbols, newSymbol(m.Name, sub, "func", f.Name, f.Doc, f.Decl, fset, modDir))
|
||||||
}
|
}
|
||||||
@@ -221,6 +228,95 @@ func isInterface(decl *ast.GenDecl) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// typeSpecType returns the underlying type expression of a single-type GenDecl
|
||||||
|
// (the RHS of `type X = <expr>`), or nil when the declaration is not a lone
|
||||||
|
// type spec. Used to tell struct/interface declarations apart from aliases and
|
||||||
|
// defined primitives so their members can be extracted.
|
||||||
|
func typeSpecType(decl *ast.GenDecl) ast.Expr {
|
||||||
|
if decl == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, spec := range decl.Specs {
|
||||||
|
if ts, ok := spec.(*ast.TypeSpec); ok {
|
||||||
|
return ts.Type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractFields flattens a struct's field list into index Fields, preserving
|
||||||
|
// declaration order, struct tags (backticks stripped), embedded markers, and
|
||||||
|
// per-field doc/line comments. A `x, y int` group yields one Field per name.
|
||||||
|
func extractFields(fset *token.FileSet, st *ast.StructType) []Field {
|
||||||
|
if st == nil || st.Fields == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var out []Field
|
||||||
|
for _, f := range st.Fields.List {
|
||||||
|
typeStr := nodeString(fset, f.Type)
|
||||||
|
tag := ""
|
||||||
|
if f.Tag != nil {
|
||||||
|
tag = strings.Trim(f.Tag.Value, "`")
|
||||||
|
}
|
||||||
|
fdoc := fieldDoc(f)
|
||||||
|
if len(f.Names) == 0 {
|
||||||
|
// Embedded field: the type name is also the field name.
|
||||||
|
out = append(out, Field{Type: typeStr, Tag: tag, Doc: fdoc, Embedded: true})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, n := range f.Names {
|
||||||
|
out = append(out, Field{Name: n.Name, Type: typeStr, Tag: tag, Doc: fdoc})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractIfaceMethods flattens an interface's method set into index Methods.
|
||||||
|
// Explicit methods carry their name and the signature without the leading
|
||||||
|
// `func`; embedded interfaces carry an empty name and the embedded type's name
|
||||||
|
// as the signature.
|
||||||
|
func extractIfaceMethods(fset *token.FileSet, it *ast.InterfaceType) []Method {
|
||||||
|
if it == nil || it.Methods == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var out []Method
|
||||||
|
for _, f := range it.Methods.List {
|
||||||
|
mdoc := fieldDoc(f)
|
||||||
|
if len(f.Names) == 0 {
|
||||||
|
// Embedded interface (or type constraint element).
|
||||||
|
out = append(out, Method{Signature: nodeString(fset, f.Type), Doc: mdoc})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sig := strings.TrimPrefix(nodeString(fset, f.Type), "func")
|
||||||
|
for _, n := range f.Names {
|
||||||
|
out = append(out, Method{Name: n.Name, Signature: n.Name + sig, Doc: mdoc})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func fieldDoc(f *ast.Field) string {
|
||||||
|
switch {
|
||||||
|
case f.Doc != nil:
|
||||||
|
return strings.TrimSpace(f.Doc.Text())
|
||||||
|
case f.Comment != nil:
|
||||||
|
return strings.TrimSpace(f.Comment.Text())
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// nodeString prints an AST node verbatim with no truncation — unlike
|
||||||
|
// formatNode, which deliberately strips type/func bodies for one-line
|
||||||
|
// signatures. Used for field types, tags, and interface method signatures.
|
||||||
|
func nodeString(fset *token.FileSet, node ast.Node) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
cfg := printer.Config{Mode: printer.UseSpaces, Tabwidth: 4}
|
||||||
|
if err := cfg.Fprint(&buf, fset, node); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modulePathRe = regexp.MustCompile(`(?m)^module\s+(\S+)`)
|
modulePathRe = regexp.MustCompile(`(?m)^module\s+(\S+)`)
|
||||||
goVersionRe = regexp.MustCompile(`(?m)^go\s+(\S+)`)
|
goVersionRe = regexp.MustCompile(`(?m)^go\s+(\S+)`)
|
||||||
|
|||||||
149
internal/index/builder_test.go
Normal file
149
internal/index/builder_test.go
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package index
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// writeFixture lays down a single-module Einherjar-like repo under a temp root
|
||||||
|
// and returns the root path for Build to walk.
|
||||||
|
func writeFixture(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
root := t.TempDir()
|
||||||
|
modDir := filepath.Join(root, "demo")
|
||||||
|
if err := os.MkdirAll(modDir, 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
write := func(name, body string) {
|
||||||
|
if err := os.WriteFile(filepath.Join(modDir, name), []byte(body), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write("go.mod", "module example.com/demo\n\ngo 1.26\n")
|
||||||
|
write("demo.go", `// Package demo is a build fixture.
|
||||||
|
package demo
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
// Config holds settings.
|
||||||
|
type Config struct {
|
||||||
|
// Host is the bind address.
|
||||||
|
Host string `+"`"+`env:"DEMO_HOST" envDefault:"0.0.0.0"`+"`"+`
|
||||||
|
Port int `+"`"+`env:"DEMO_PORT"`+"`"+`
|
||||||
|
Inner
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inner is embedded into Config.
|
||||||
|
type Inner struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store reads values.
|
||||||
|
type Store interface {
|
||||||
|
// Get fetches one value.
|
||||||
|
Get(ctx context.Context, id string) (string, error)
|
||||||
|
context.Context
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
func findSymbol(t *testing.T, idx *Index, mod, name string) Symbol {
|
||||||
|
t.Helper()
|
||||||
|
m := idx.FindModule(mod)
|
||||||
|
if m == nil {
|
||||||
|
t.Fatalf("module %q not found", mod)
|
||||||
|
}
|
||||||
|
for _, s := range m.Symbols {
|
||||||
|
if s.Name == name {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatalf("symbol %q not found in module %q", name, mod)
|
||||||
|
return Symbol{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildCapturesStructFields(t *testing.T) {
|
||||||
|
idx, err := Build(writeFixture(t))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := findSymbol(t, idx, "demo", "Config")
|
||||||
|
if cfg.Kind != "type" {
|
||||||
|
t.Errorf("Config kind = %q, want type", cfg.Kind)
|
||||||
|
}
|
||||||
|
if len(cfg.Fields) != 3 {
|
||||||
|
t.Fatalf("Config has %d fields, want 3: %+v", len(cfg.Fields), cfg.Fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
host := cfg.Fields[0]
|
||||||
|
if host.Name != "Host" || host.Type != "string" {
|
||||||
|
t.Errorf("field[0] = %+v, want Host string", host)
|
||||||
|
}
|
||||||
|
if host.Tag != `env:"DEMO_HOST" envDefault:"0.0.0.0"` {
|
||||||
|
t.Errorf("Host tag = %q, want the env/envDefault tag (backticks stripped)", host.Tag)
|
||||||
|
}
|
||||||
|
if host.Doc != "Host is the bind address." {
|
||||||
|
t.Errorf("Host doc = %q", host.Doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
if port := cfg.Fields[1]; port.Name != "Port" || port.Tag != `env:"DEMO_PORT"` {
|
||||||
|
t.Errorf("field[1] = %+v, want Port with env tag", port)
|
||||||
|
}
|
||||||
|
|
||||||
|
if inner := cfg.Fields[2]; !inner.Embedded || inner.Name != "" || inner.Type != "Inner" {
|
||||||
|
t.Errorf("field[2] = %+v, want embedded Inner", inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildCapturesInterfaceMethods(t *testing.T) {
|
||||||
|
idx, err := Build(writeFixture(t))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
store := findSymbol(t, idx, "demo", "Store")
|
||||||
|
if store.Kind != "interface" {
|
||||||
|
t.Errorf("Store kind = %q, want interface", store.Kind)
|
||||||
|
}
|
||||||
|
if len(store.Methods) != 2 {
|
||||||
|
t.Fatalf("Store has %d methods, want 2: %+v", len(store.Methods), store.Methods)
|
||||||
|
}
|
||||||
|
|
||||||
|
get := store.Methods[0]
|
||||||
|
if get.Name != "Get" {
|
||||||
|
t.Errorf("method[0] name = %q, want Get", get.Name)
|
||||||
|
}
|
||||||
|
if get.Signature != "Get(ctx context.Context, id string) (string, error)" {
|
||||||
|
t.Errorf("Get signature = %q", get.Signature)
|
||||||
|
}
|
||||||
|
if get.Doc != "Get fetches one value." {
|
||||||
|
t.Errorf("Get doc = %q", get.Doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
if emb := store.Methods[1]; emb.Name != "" || emb.Signature != "context.Context" {
|
||||||
|
t.Errorf("method[1] = %+v, want embedded context.Context", emb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A struct must be discoverable by one of its struct tags — the failure mode
|
||||||
|
// that motivated capturing fields in the first place.
|
||||||
|
func TestSearchSymbolsByStructTag(t *testing.T) {
|
||||||
|
idx, err := Build(writeFixture(t))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := idx.SearchSymbols("DEMO_HOST", 10)
|
||||||
|
found := false
|
||||||
|
for _, s := range got {
|
||||||
|
if s.Name == "Config" {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("searching a struct tag did not surface its type; got %d results", len(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -77,15 +77,46 @@ type SubPackage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Symbol is one exported declaration (type, func, interface, const, var).
|
// Symbol is one exported declaration (type, func, interface, const, var).
|
||||||
|
//
|
||||||
|
// For struct types Fields carries the field set (the Signature is only the
|
||||||
|
// one-line `type X struct` header, so without Fields the field names, types,
|
||||||
|
// and — crucially — struct tags would be lost). For interface types Methods
|
||||||
|
// carries the method set for the same reason. Both are empty for every other
|
||||||
|
// kind.
|
||||||
type Symbol struct {
|
type Symbol struct {
|
||||||
Module string `json:"module"`
|
Module string `json:"module"`
|
||||||
SubPackage string `json:"subPackage"`
|
SubPackage string `json:"subPackage"`
|
||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
Doc string `json:"doc"`
|
Doc string `json:"doc"`
|
||||||
File string `json:"file"`
|
File string `json:"file"`
|
||||||
Line int `json:"line"`
|
Line int `json:"line"`
|
||||||
|
Fields []Field `json:"fields,omitempty"`
|
||||||
|
Methods []Method `json:"methods,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Field is one field of a struct-type Symbol. Name is empty for an embedded
|
||||||
|
// field (its type then appears in Type, with Embedded true). Tag is the raw
|
||||||
|
// struct tag with the surrounding backticks stripped (e.g.
|
||||||
|
// `env:"EINHERJAR_PG_HOST" envDefault:"postgres"`) — the single most useful
|
||||||
|
// thing a config consumer needs and the thing the old index dropped.
|
||||||
|
type Field struct {
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Tag string `json:"tag,omitempty"`
|
||||||
|
Doc string `json:"doc,omitempty"`
|
||||||
|
Embedded bool `json:"embedded,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method is one method of an interface-type Symbol. Signature is the method
|
||||||
|
// as written without the leading func keyword (e.g.
|
||||||
|
// `GetExecutor(ctx context.Context) Executor`). For an embedded interface
|
||||||
|
// Name is empty and Signature is the embedded interface's name.
|
||||||
|
type Method struct {
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
Doc string `json:"doc,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ADR is one architectural decision record.
|
// ADR is one architectural decision record.
|
||||||
@@ -157,8 +188,27 @@ func matches(s Symbol, needle string) bool {
|
|||||||
if needle == "" {
|
if needle == "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return strings.Contains(strings.ToLower(s.Name), needle) ||
|
if strings.Contains(strings.ToLower(s.Name), needle) ||
|
||||||
strings.Contains(strings.ToLower(s.Doc), needle) ||
|
strings.Contains(strings.ToLower(s.Doc), needle) ||
|
||||||
strings.Contains(strings.ToLower(s.SubPackage), needle) ||
|
strings.Contains(strings.ToLower(s.SubPackage), needle) ||
|
||||||
strings.Contains(strings.ToLower(s.Module), needle)
|
strings.Contains(strings.ToLower(s.Module), needle) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// A struct is also findable by a field name, type, or tag — so a query
|
||||||
|
// like an env-var key or json field resolves to the type that declares it.
|
||||||
|
for _, f := range s.Fields {
|
||||||
|
if strings.Contains(strings.ToLower(f.Name), needle) ||
|
||||||
|
strings.Contains(strings.ToLower(f.Type), needle) ||
|
||||||
|
strings.Contains(strings.ToLower(f.Tag), needle) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// An interface is findable by a method it requires.
|
||||||
|
for _, mth := range s.Methods {
|
||||||
|
if strings.Contains(strings.ToLower(mth.Name), needle) ||
|
||||||
|
strings.Contains(strings.ToLower(mth.Signature), needle) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type getSymbolOutput struct {
|
|||||||
func registerGetSymbol(s *mcp.Server, idx *index.Index) {
|
func registerGetSymbol(s *mcp.Server, idx *index.Index) {
|
||||||
mcp.AddTool(s, &mcp.Tool{
|
mcp.AddTool(s, &mcp.Tool{
|
||||||
Name: "get_symbol",
|
Name: "get_symbol",
|
||||||
Description: "Fetch full signature, doc comment, and source location for one symbol. Returns every match across sub-packages — use the subPackage filter when ambiguous.",
|
Description: "Fetch full signature, doc comment, and source location for one symbol. For a struct type the result includes its fields (name, type, struct tag, doc); for an interface type it includes its method set. Returns every match across sub-packages — use the subPackage filter when ambiguous.",
|
||||||
}, func(ctx context.Context, req *mcp.CallToolRequest, args getSymbolInput) (*mcp.CallToolResult, getSymbolOutput, error) {
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args getSymbolInput) (*mcp.CallToolResult, getSymbolOutput, error) {
|
||||||
m := idx.FindModule(args.Module)
|
m := idx.FindModule(args.Module)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type searchSymbolsOutput struct {
|
|||||||
func registerSearchSymbols(s *mcp.Server, idx *index.Index) {
|
func registerSearchSymbols(s *mcp.Server, idx *index.Index) {
|
||||||
mcp.AddTool(s, &mcp.Tool{
|
mcp.AddTool(s, &mcp.Tool{
|
||||||
Name: "search_symbols",
|
Name: "search_symbols",
|
||||||
Description: "Search Einherjar's exported symbols (types, interfaces, funcs, methods, consts, vars) by name or doc text. Optionally filter by module or kind. Use when you need to find where a type or function lives.",
|
Description: "Search Einherjar's exported symbols (types, interfaces, funcs, methods, consts, vars) by name or doc text. Structs also match on a field name, field type, or struct tag (e.g. an env-var key); interfaces also match on a required method. Optionally filter by module or kind. Use when you need to find where a type, function, field, or tag lives.",
|
||||||
}, func(ctx context.Context, req *mcp.CallToolRequest, args searchSymbolsInput) (*mcp.CallToolResult, searchSymbolsOutput, error) {
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args searchSymbolsInput) (*mcp.CallToolResult, searchSymbolsOutput, error) {
|
||||||
all := idx.SearchSymbols(args.Query, args.Limit*4+25)
|
all := idx.SearchSymbols(args.Query, args.Limit*4+25)
|
||||||
filtered := all[:0]
|
filtered := all[:0]
|
||||||
|
|||||||
Reference in New Issue
Block a user