feat(mcpgen,mcpserver): templated @mcpTool selections#34
Merged
Conversation
Static @mcptool(selection: "...") strings cannot express selection sets that depend on per-call argument values — for example, a signals query where the caller names a list of {signal, aggregation} pairs and each pair becomes its own sub-field `name(agg: AGG)`. Tools of that shape today return useless data because the generated query hard-codes a selection that matches none of the requested signals. Support Go text/template syntax inside the `selection` argument. When `selection` contains `{{`, mcpgen: - skips the static field-name validation (rendered fields are dynamic) - emits the query with a `SelectionPlaceholder` marker where the selection would normally go - stores the raw template on `ToolDefinition.SelectionTemplate` At registration time mcpserver parses each template once (failing fast on syntax errors) and on every tool call renders it against the call's argument map, splicing the result into the query. Non-templated selections take the existing code path unchanged. Example: signals(...): [SignalAggregations!] @mcptool( name: "get_signals_time_series" description: "..." selection: "timestamp {{range .signalRequests}} {{.name}}(agg: {{.agg}}) {{end}}" ) The generated tool exposes `signalRequests` as an argument; the model passes `[{name:\"speed\",agg:AVG}, ...]`; mcpserver renders the selection set at call time and executes the correctly-shaped GraphQL query.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@mcpTool(selection: "...")now accepts Gotext/templatesyntax. The rendered selection is spliced into the query at call time with the call's argument map as template data.ToolDefinition.SelectionTemplateand theSelectionPlaceholdermarker that mcpgen puts intoQuerywhen the selection is templated.Why
Static selection strings cannot express selection sets that depend on per-call argument values. For consumers like telemetry-api's
signalsquery, where each{name, agg}pair needs to become its own sub-fieldname(agg: AGG), the current shortcut tool's selectiontimestampreturns empty buckets and forces models to stuff signal names into unrelated arguments likefilter. Fixing this at the mcpgen layer means every consumer repo gets dynamic selections by annotation alone — no schema reshaping, no resolver rewrites, no breaking changes in consumer schemas.Example
A call with
signalRequests: [{name: \"speed\", agg: AVG}, {name: \"rpm\", agg: MAX}]renders to:Changes
pkg/mcpserver/mcpserver.go:ToolDefinition.SelectionTemplate, exportedSelectionPlaceholder, error propagation fromregisterShortcutToolsthroughNew.pkg/mcpserver/tools.go: parse eachSelectionTemplateonce at registration; render + splice per call; fail-fast on bad templates or missing placeholder.cmd/mcpgen/condensed.go: detect{{in selection; emitSelectionPlaceholderin the generatedQuery; skip static validation for templated selections.cmd/mcpgen/generate.go:isTemplatedSelectionhelper; Go file template emitsSelectionTemplatewhen non-empty.cmd/mcpgen:TestTemplatedSelection,TestTemplatedSelectionGeneratedOutput+testdata/templated_selection.graphqls.pkg/mcpserver:TestSelectionTemplateRendering,TestSelectionTemplateInvalidRejectedAtRegistration,TestSelectionTemplateMissingPlaceholderRejected.Test plan
make lintcleango test ./...(all existing + new tests pass)signals/signalsLatest@mcpToolannotations to templated selections, confirm the/mcptools return correctly-shaped data end-to-end