Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cmd/query/create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package query

import (
"errors"
"fmt"
"strings"

"github.com/duneanalytics/cli/cmdutil"
"github.com/duneanalytics/cli/output"
Expand Down Expand Up @@ -48,6 +50,9 @@ func addCreateFlags(cmd *cobra.Command) {
cmd.Flags().String("folder-name", "",
"save the query into the folder with this name; fails if several folders share it")
cmd.Flags().String("parameters", "", parametersHelp)
cmd.Flags().Bool("public-if-private-limit-reached", false,
"if --private is not set, the team's queries are private by default, and its private query limit is used up, "+
"create the query public instead of failing")
cmd.MarkFlagsMutuallyExclusive("folder-id", "folder-name")
_ = cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("sql")
Expand All @@ -58,6 +63,16 @@ const parametersHelp = `query parameters as a JSON array, @path, or @-: ` +
`[{"key":"days","type":"number","value":"30"}]; ` +
`type is text, number, date, or enum (with "enumOptions"); reference them in SQL as {{key}}`

// privateLimitError replaces the MCP's error at a team's private query limit,
// which tells an AI assistant to ask the user, with the CLI flag that agrees.
func privateLimitError(err error) error {
if err != nil && strings.Contains(err.Error(), "public_if_private_limit_reached") {
return errors.New("the team has used all of its private queries, so this query would be public; " +
"nothing was created. To create it public, run the command again with --public-if-private-limit-reached")
}
return err
}

// createArgs returns the createDuneQuery arguments for the flags of query create
// and create-and-run.
func createArgs(cmd *cobra.Command) (map[string]any, error) {
Expand All @@ -67,6 +82,8 @@ func createArgs(cmd *cobra.Command) (map[string]any, error) {
"private": "is_private",
"folder-id": "folder_id",
"folder-name": "folder_name",

"public-if-private-limit-reached": "public_if_private_limit_reached",
})
sql, err := cmdutil.FileFlag(cmd, "sql")
if err != nil {
Expand All @@ -92,7 +109,7 @@ func runCreate(cmd *cobra.Command, _ []string) error {
}
var resp models.CreateQueryResponse
if _, err := cmdutil.CallTool(cmd, "createDuneQuery", args, &resp); err != nil {
return err
return privateLimitError(err)
}

w := cmd.OutOrStdout()
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/create_and_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runCreateAndRun(cmd *cobra.Command, _ []string) error {
}
raw, err := cmdutil.CallTool(cmd, "createAndExecuteQuery", args, &res)
if err != nil {
return err
return privateLimitError(err)
}

var errs []error
Expand Down
29 changes: 29 additions & 0 deletions cmd/query/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ func TestCreateToolError(t *testing.T) {
assert.EqualError(t, root.Execute(), "folder_ambiguous: pick a folder_id")
}

func TestCreateAtThePrivateLimit(t *testing.T) {
limit := errors.New("The team has used all of its private queries, so this query would be public. Nothing was " +
"created. Tell the user it will be public and ask whether that is OK; only if they agree, create it again " +
"with public_if_private_limit_reached: true.")
for _, tool := range []string{"createDuneQuery", "createAndExecuteQuery"} {
sub := map[string]string{"createDuneQuery": "create", "createAndExecuteQuery": "create-and-run"}[tool]
t.Run(sub, func(t *testing.T) {
// Without the flag, the error names the CLI flag instead of the MCP argument.
fake := &dunemcptest.Fake{Errors: map[string]error{tool: limit}}
root, _ := newMCPRoot(fake)
root.SetArgs([]string{"query", sub, "--name", "T", "--sql", "S"})
err := root.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "run the command again with --public-if-private-limit-reached")
assert.NotContains(t, err.Error(), "public_if_private_limit_reached")
assert.NotContains(t, fake.Last().Args, "public_if_private_limit_reached")

// With it, the consent is passed on.
fake = &dunemcptest.Fake{Results: map[string]string{tool: createAndRunResult, "createDuneQuery": `{"query_id":1}`}}
root, _ = newMCPRoot(fake)
root.SetArgs([]string{"query", sub, "--name", "T", "--sql", "S", "--public-if-private-limit-reached"})
require.NoError(t, root.Execute())
var args map[string]any
require.NoError(t, json.Unmarshal([]byte(fake.Last().Args), &args))
assert.Equal(t, true, args["public_if_private_limit_reached"])
})
}
}

const createAndRunResult = `{
"query":{"query_id":11,"url":"https://dune.com/queries/11"},
"execution":{"execution_id":"01EXEC","state":"COMPLETED","engine_used":"small"},
Expand Down
Loading