Repository navigation
Add dune query results for the MCP's getLatestQueryResult #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| 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 <query-id>", | ||
| 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 <query-id>' 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 { | ||
|
rahuldotiyer marked this conversation as resolved.
Resolved
|
||
| // 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 | ||
| } | ||
| // 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) | ||
|
rahuldotiyer marked this conversation as resolved.
Resolved
|
||
| return nil | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 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") | ||
| assert.NotContains(t, out, "0 of 0 rows") | ||
| } | ||
|
|
||
| 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") | ||
|
|
||
| // -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) { | ||
| fake := &dunemcptest.Fake{} | ||
| _, err := runResults(fake, "abc") | ||
| require.Error(t, err) | ||
| assert.Empty(t, fake.Calls) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.