From c001d492aa9d6f16302ee6d8284a4738ab749c77 Mon Sep 17 00:00:00 2001 From: Rahul Iyer Date: Mon, 30 Nov 2026 08:06:17 +0000 Subject: [PATCH 1/2] Add dune query results for the MCP's getLatestQueryResult core#8349/#8443 added getLatestQueryResult to the Dune MCP server: a saved query's latest stored result, without running it. 'dune query results ' calls it, shows when the run happened and its parameters, and prints the tool's notice when no result is stored or rows are withheld for licensed datasets. It goes through the MCP, not the REST results endpoint, so the MCP's licensed-data withholding applies. Co-authored-by: Amp Amp-Thread-ID: https://ampcode.com/threads/T-01a2147b-3c2e-76b3-aeaf-0aafbe53d879 --- README.md | 1 + cmd/query/query.go | 2 + cmd/query/results.go | 88 +++++++++++++++++++++++++++++++++++++++ cmd/query/results_test.go | 61 +++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 cmd/query/results.go create mode 100644 cmd/query/results_test.go diff --git a/README.md b/README.md index cb6e956..308ce0d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Manage and execute Dune queries. | `query archive ` | Archive a saved query | | `query run [--param key=value] [--performance small\|medium\|large] [--limit] [--timeout] [--no-wait]` | Execute a saved query and display results | | `query run-sql --sql [--param key=value] [--performance small\|medium\|large] [--limit] [--timeout] [--no-wait]` | Execute raw SQL directly | +| `query results [--limit] [--offset]` | Show a saved query's latest stored result without running it | | `query alert --cron --performance small\|medium\|large --email ... \| --slack-webhook \| --discord-webhook \| --webhook ` | Schedule a query and get notified each time a run returns rows | ### `dune matview` (alias: `mv`) diff --git a/cmd/query/query.go b/cmd/query/query.go index b07a0c9..bf63bf5 100644 --- a/cmd/query/query.go +++ b/cmd/query/query.go @@ -17,6 +17,7 @@ func NewQueryCmd() *cobra.Command { " archive - Hide a query from the library (still retrievable by ID)\n" + " run - Execute a saved query by ID and display results\n" + " run-sql - Execute raw DuneSQL inline without saving a query\n" + + " results - Show a saved query's latest stored result without running it\n" + " alert - Schedule a query and get notified when a run returns rows", } cmd.AddCommand(newCreateCmd()) @@ -27,6 +28,7 @@ func NewQueryCmd() *cobra.Command { cmd.AddCommand(newArchiveCmd()) cmd.AddCommand(newRunCmd()) cmd.AddCommand(newRunSQLCmd()) + cmd.AddCommand(newResultsCmd()) cmd.AddCommand(newAlertCmd()) return cmd } diff --git a/cmd/query/results.go b/cmd/query/results.go new file mode 100644 index 0000000..c94450f --- /dev/null +++ b/cmd/query/results.go @@ -0,0 +1,88 @@ +package query + +import ( + "fmt" + "strconv" + + "github.com/duneanalytics/cli/cmdutil" + "github.com/duneanalytics/cli/output" + "github.com/spf13/cobra" +) + +func newResultsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "results ", + Short: "Show a saved query's latest stored result without running it", + Long: `Show the rows of the latest completed run of a saved query's current SQL, +whoever started it (you, a schedule, or the API), and when that run happened. + +This starts no execution and uses no execution credits, but the result can be +old: check "Executed at". When no run is stored for the current SQL, or the +stored result has expired, the command says so; run the query with +'dune query run ' to get a fresh result. + +For a query with parameters, the stored run may have used other values than the +defaults. They are shown when they are the defaults or you started the run. + +Dune withholds rows from enterprise datasets that are not available to AI tools; +the command then prints the reason and a dune.com link instead. + +Examples: + dune query results 12345 + dune query results 12345 --limit 1000 --offset 1000 -o json`, + Args: cobra.ExactArgs(1), + RunE: runResults, + } + cmd.Flags().Int("limit", 0, "maximum rows to return, up to 32000 (default 100)") + cmd.Flags().Int("offset", 0, "rows to skip, for the next page") + output.AddFormatFlag(cmd, "text") + return cmd +} + +// latestResult is what the Dune MCP's getLatestQueryResult tool returns. +type latestResult struct { + executionResult + QueryURL string `json:"queryUrl"` + ExecutedAt string `json:"executedAt"` + Parameters []struct { + Key string `json:"key"` + Value any `json:"value"` + } `json:"parameters"` +} + +func runResults(cmd *cobra.Command, args []string) error { + queryID, err := strconv.Atoi(args[0]) + if err != nil || queryID <= 0 { + return fmt.Errorf("invalid query ID %q: must be a positive integer", args[0]) + } + toolArgs := cmdutil.ToolArgs(cmd, map[string]string{"limit": "limit", "offset": "offset"}) + toolArgs["query_id"] = queryID + + var res latestResult + raw, err := cmdutil.CallTool(cmd, "getLatestQueryResult", toolArgs, &res) + if err != nil { + return err + } + w := cmd.OutOrStdout() + if output.FormatFromCmd(cmd) == output.FormatJSON { + return output.PrintRawJSON(w, raw) + } + if err := res.Err(); err != nil { + return err + } + // No stored run: the tool returns only the query and a notice. + if res.State == "" { + fmt.Fprintf(w, "%s\n%s\n", res.Notice, res.QueryURL) + return nil + } + fmt.Fprintf(w, "Query: %s\n", res.QueryURL) + if res.ExecutedAt != "" { + fmt.Fprintf(w, "Executed at: %s\n", res.ExecutedAt) + } + for _, p := range res.Parameters { + fmt.Fprintf(w, "Parameter: %s = %v\n", p.Key, p.Value) + } + fmt.Fprintln(w) + printExecutionResult(w, res.executionResult) + return nil +} diff --git a/cmd/query/results_test.go b/cmd/query/results_test.go new file mode 100644 index 0000000..d426e72 --- /dev/null +++ b/cmd/query/results_test.go @@ -0,0 +1,61 @@ +package query_test + +import ( + "testing" + + "github.com/duneanalytics/cli/dunemcp/dunemcptest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func runResults(fake *dunemcptest.Fake, args ...string) (string, error) { + root, buf := newMCPRoot(fake) + root.SetArgs(append([]string{"query", "results"}, args...)) + err := root.Execute() + return buf.String(), err +} + +func TestResultsShowsStoredRunWithItsAge(t *testing.T) { + fake := &dunemcptest.Fake{Results: map[string]string{"getLatestQueryResult": `{"queryId":42, + "queryUrl":"https://dune.com/queries/42","executedAt":"2026-11-29T08:00:00Z", + "parameters":[{"key":"chain","value":"base"}],"executionId":"01E","state":"COMPLETED", + "resultMetadata":{"columns":[{"name":"n"}],"totalRowCount":3},"data":{"rows":[{"n":7}]}}`}} + out, err := runResults(fake, "42", "--limit", "1") + require.NoError(t, err) + // Only the flags the user set are sent, so the server's defaults apply to the rest. + assert.JSONEq(t, `{"query_id":42,"limit":1}`, fake.Last().Args) + assert.Contains(t, out, "Executed at: 2026-11-29T08:00:00Z") + assert.Contains(t, out, "Parameter: chain = base") + assert.Contains(t, out, "1 of 3 rows") +} + +func TestResultsWithoutStoredRunPrintsTheNotice(t *testing.T) { + fake := &dunemcptest.Fake{Results: map[string]string{"getLatestQueryResult": `{"queryId":42, + "queryUrl":"https://dune.com/queries/42","notice":"This query has no stored result for its current SQL."}`}} + out, err := runResults(fake, "42") + require.NoError(t, err) + assert.Equal(t, "This query has no stored result for its current SQL.\nhttps://dune.com/queries/42\n", out) +} + +func TestResultsWithheldRowsShowTheNotice(t *testing.T) { + fake := &dunemcptest.Fake{Results: map[string]string{"getLatestQueryResult": `{"queryId":42, + "queryUrl":"https://dune.com/queries/42","executedAt":"2026-11-29T08:00:00Z","executionId":"01E", + "state":"COMPLETED","notice":"Rows withheld: licensed dataset. See https://dune.com/queries/42"}`}} + out, err := runResults(fake, "42") + require.NoError(t, err) + assert.Contains(t, out, "Rows withheld: licensed dataset") +} + +func TestResultsFailedRunIsAnError(t *testing.T) { + fake := &dunemcptest.Fake{Results: map[string]string{"getLatestQueryResult": `{"queryId":42, + "executionId":"01E","state":"FAILED","errorMessage":"line 1: syntax error"}`}} + _, err := runResults(fake, "42") + assert.EqualError(t, err, "execution 01E failed: line 1: syntax error") +} + +func TestResultsRejectsBadQueryID(t *testing.T) { + fake := &dunemcptest.Fake{} + _, err := runResults(fake, "abc") + require.Error(t, err) + assert.Empty(t, fake.Calls) +} From da1b13c9952b9478d6d2632257530f104a066926 Mon Sep 17 00:00:00 2001 From: rahul iyer Date: Mon, 30 Nov 2026 07:15:16 -0500 Subject: [PATCH 2/2] query results: fail -o json on a failed run, skip the empty table when rows are withheld (#86) Amp-Thread-ID: https://ampcode.com/threads/T-01a2147b-3c2e-76b3-aeaf-0aafbe53d879 Co-authored-by: Amp --- cmd/query/result_preview.go | 6 ++++++ cmd/query/results.go | 6 +++++- cmd/query/results_test.go | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/query/result_preview.go b/cmd/query/result_preview.go index 357355e..00287f0 100644 --- a/cmd/query/result_preview.go +++ b/cmd/query/result_preview.go @@ -48,6 +48,12 @@ func printExecutionResult(w io.Writer, r executionResult) { fmt.Fprintf(w, "State: %s\n", r.State) return } + // Withheld rows come back as a notice without data; a table would read as + // an empty result. + if r.Data == nil && r.Notice != "" { + fmt.Fprintln(w, r.Notice) + return + } var columns []string total := 0 credits := "" diff --git a/cmd/query/results.go b/cmd/query/results.go index c94450f..f2f6c48 100644 --- a/cmd/query/results.go +++ b/cmd/query/results.go @@ -65,7 +65,11 @@ func runResults(cmd *cobra.Command, args []string) error { } w := cmd.OutOrStdout() if output.FormatFromCmd(cmd) == output.FormatJSON { - return output.PrintRawJSON(w, raw) + // A failed stored run still fails the command, so scripts see it. + if err := output.PrintRawJSON(w, raw); err != nil { + return err + } + return res.Err() } if err := res.Err(); err != nil { return err diff --git a/cmd/query/results_test.go b/cmd/query/results_test.go index d426e72..03eda7d 100644 --- a/cmd/query/results_test.go +++ b/cmd/query/results_test.go @@ -44,6 +44,7 @@ func TestResultsWithheldRowsShowTheNotice(t *testing.T) { out, err := runResults(fake, "42") require.NoError(t, err) assert.Contains(t, out, "Rows withheld: licensed dataset") + assert.NotContains(t, out, "0 of 0 rows") } func TestResultsFailedRunIsAnError(t *testing.T) { @@ -51,6 +52,11 @@ func TestResultsFailedRunIsAnError(t *testing.T) { "executionId":"01E","state":"FAILED","errorMessage":"line 1: syntax error"}`}} _, err := runResults(fake, "42") assert.EqualError(t, err, "execution 01E failed: line 1: syntax error") + + // -o json prints the result and still fails, so scripts see the failure. + out, err := runResults(fake, "42", "-o", "json") + assert.EqualError(t, err, "execution 01E failed: line 1: syntax error") + assert.Contains(t, out, `"state": "FAILED"`) } func TestResultsRejectsBadQueryID(t *testing.T) {