photoprism/internal/mcp/resources.go
Michael Mayer 60f4779565 Config: Refactor flag default reporting and harden Markdown rendering
- Rework CliFlag.Default() to read the documented default through
  cli.DocGenerationFlag.GetDefaultText() instead of GetValue(); urfave
  snapshots the source-code default in Apply() before reading the
  environment, so reports stay stable across deployments and no longer
  echo operator-supplied env values for runtime-mutated string flags.

- Add a Secret annotation on CliFlag for sensitive options. When set,
  Default() collapses to DocDefault (or "") so secrets like
  admin-password, oidc-secret, database-password, auth-secret,
  node-client-secret, join-token, download-token, preview-token, and
  vision-key never appear in `photoprism show config-options` or
  downstream consumers of the same report data.

- Convert index-workers from IntFlag to StringFlag with the new
  IndexWorkersAuto = "auto" sentinel, matching the convention already
  used by thumb-color, thumb-filter, and database-driver. Options.
  IndexWorkers becomes a string; Config.IndexWorkers() parses it once
  and keeps the existing CPU/SQLite caps. yaml.v2 silently coerces the
  legacy `IndexWorkers: 4` form into the string field, so existing
  operator config files load unchanged. Drops the dynamic
  cpuid.CPU.PhysicalCores/2 default and the previous DocDefault hack.
  Regenerated swagger.json picks up the new string type.

- Harden the Markdown report renderer (pkg/txt/report/markdown.go) by
  backslash-escaping `<` and `>` in row and header cells alongside the
  existing `|` and `* * *` escapes. Prevents arbitrary HTML from being
  emitted into downstream HTML pipelines that render the table source.

- Fix two pre-existing lint findings surfaced by the newer
  golangci-lint: misspell `marshalling` -> `marshaling` and replace a
  field-by-field struct literal with a same-shape type conversion
  (staticcheck S1016).

- Add coverage for every new code path: Default() Secret/env-override
  regression, GetDefaultText/strconv.Unquote round-trip, parseIndexWorkers
  branch matrix, ApplyCliContext index-workers string binding, the YAML
  round-trip across `auto`/quoted/bare-int/empty forms, and the Markdown
  angle-bracket escape.
2026-05-09 15:09:54 +00:00

59 lines
2 KiB
Go

package mcp
import (
"context"
"encoding/json"
sdkmcp "github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/photoprism/photoprism/pkg/http/header"
)
// registerResources registers every read-only resource exposed by the
// server against the shared *Dataset. The order matches ResourceURIs so
// the startup log and the SDK's resources/list response stay in sync.
func registerResources(server *sdkmcp.Server, data *Dataset) {
server.AddResource(&sdkmcp.Resource{
URI: configOptionsURI,
Name: "config-options",
Title: "PhotoPrism Config Options",
Description: "Read-only config options derived from the existing config report.",
MIMEType: header.ContentTypeJson,
}, func(_ context.Context, req *sdkmcp.ReadResourceRequest) (*sdkmcp.ReadResourceResult, error) {
return newResourceResult(req.Params.URI, ConfigOptionsResource{
Edition: data.CurrentEdition,
Items: data.ConfigOptions,
})
})
server.AddResource(&sdkmcp.Resource{
URI: searchFiltersURI,
Name: "search-filters",
Title: "PhotoPrism Search Filters",
Description: "Read-only search filter reference derived from the existing search report.",
MIMEType: header.ContentTypeJson,
}, func(_ context.Context, req *sdkmcp.ReadResourceRequest) (*sdkmcp.ReadResourceResult, error) {
return newResourceResult(req.Params.URI, SearchFiltersResource{
Edition: data.CurrentEdition,
Items: data.SearchFilters,
})
})
}
// newResourceResult marshals payload to indented JSON and wraps it in an
// MCP ReadResourceResult with the given URI and header.ContentTypeJson as
// the advertised MIME type. Returns an error if JSON marshaling fails.
func newResourceResult(uri string, payload any) (*sdkmcp.ReadResourceResult, error) {
body, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return nil, err
}
return &sdkmcp.ReadResourceResult{
Contents: []*sdkmcp.ResourceContents{{
URI: uri,
MIMEType: header.ContentTypeJson,
Text: string(body),
}},
}, nil
}