feat(mcp): index struct fields and interface method sets (#1)

Minor release. The indexer named composite types but could not describe
their shape: every declaration was truncated at the first brace, so a
struct symbol carried only its `type X struct` header and an interface
symbol only its `type X interface` header. Field names, field types, and
— most painfully — struct tags such as `env:"EINHERJAR_PG_HOST"` were
dropped, as were the method sets of every port interface. An assistant
could be told that `db-postgres` has a `Config` and a `Provider`, but not
what env vars configure the one or what methods the other requires. This
change captures both.

internal/index (schema):
- Symbol gains two optional fields. `fields` ([]Field) carries a struct's
  field set — name, type, raw struct tag (surrounding backticks stripped),
  doc comment, and an `embedded` marker. `methods` ([]Method) carries an
  interface's method set — name, signature without the leading `func`, and
  doc comment, including embedded interfaces. Both are omitempty and absent
  for every other kind.
- SchemaVersion is deliberately unchanged. The two additions are additive
  and omitempty, so an older consumer parses the new index unchanged; per
  the existing rule the constant only bumps on a breaking format change.

internal/index (builder):
- collectSymbols now inspects each type's TypeSpec and, for a *ast.StructType
  or *ast.InterfaceType, fills the new Symbol members. A grouped field
  declaration (`x, y int`) yields one Field per name; an embedded field or
  interface yields an entry with an empty name.
- New helpers: typeSpecType (underlying type expr of a lone type spec),
  extractFields, extractIfaceMethods, fieldDoc (doc comment or trailing
  line comment), and nodeString — a non-truncating printer used for field
  types, tags, and method signatures, distinct from formatNode which keeps
  truncating to produce the one-line header.

internal/index (search):
- matches() now also tests the query against struct field names, field
  types, and struct tags, and against interface method names and
  signatures. A query like an env-var key or a method name now resolves to
  the type that declares it.

internal/tools:
- get_symbol and search_symbols descriptions updated to advertise the new
  struct-field and interface-method coverage. No input/output schema change
  beyond the additive Symbol fields, which get_symbol already returns whole.

internal/index (tests):
- New builder_test.go — the package previously had no tests. Builds a
  temporary module fixture and asserts capture of struct fields (with tags
  and docs), embedded fields, interface methods (with signatures and docs),
  embedded interfaces, and discovery of a struct by one of its struct tags
  (the failure mode that motivated the change).

Docs:
- CHANGELOG.md gains an [Unreleased] entry; README.md tool table updated to
  state that get_symbol returns struct fields and interface methods and
  that search_symbols matches fields, tags, and methods.

No new dependencies. The committed data/index.json placeholder is untouched
— the index is regenerated at image build (Dockerfile runs cmd/indexer),
so a deployment must be rebuilt to serve the richer index; a server still
running the prior image keeps serving the older, member-less one.

Reviewed-on: #1
Co-authored-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
Co-committed-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
This commit was merged in pull request #1.
This commit is contained in:
2026-06-10 10:38:30 -06:00
committed by NOCHEBUENADEV
parent e23e86b06c
commit 13a186c60a
7 changed files with 328 additions and 15 deletions

View File

@@ -77,15 +77,46 @@ type SubPackage struct {
}
// 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 {
Module string `json:"module"`
SubPackage string `json:"subPackage"`
Kind string `json:"kind"`
Name string `json:"name"`
Signature string `json:"signature"`
Doc string `json:"doc"`
File string `json:"file"`
Line int `json:"line"`
Module string `json:"module"`
SubPackage string `json:"subPackage"`
Kind string `json:"kind"`
Name string `json:"name"`
Signature string `json:"signature"`
Doc string `json:"doc"`
File string `json:"file"`
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.
@@ -157,8 +188,27 @@ func matches(s Symbol, needle string) bool {
if needle == "" {
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.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
}