From 716c5864a796bb7b931f1b1fbf1e3d5297e75d99 Mon Sep 17 00:00:00 2001 From: Tobias Herber <22559657+herber@users.noreply.github.com> Date: Tue, 10 Nov 2026 14:53:11 +0100 Subject: [PATCH 1/2] Agent improvements run 1 --- generator/src/join.ts | 6 +- generator/src/manifest.ts | 1 + internal/cli/admin_cli_test.go | 62 ++++++ internal/commands/onboarding/command.go | 64 ++++--- internal/commands/onboarding/emailhint.go | 121 ------------ .../commands/onboarding/emailhint_test.go | 179 ------------------ .../commands/onboarding/onboarding_test.go | 24 +++ internal/commands/onboarding/presenter.go | 27 +-- internal/commands/onboarding/region.go | 44 ++--- internal/commands/onboarding/region_test.go | 28 +-- .../commands/resources/integration_setup.go | 125 ++++++++++++ internal/commands/resources/provider_auth.go | 80 ++++++++ internal/commands/resources/public.go | 19 +- internal/config/config.go | 4 +- internal/generated/admin/catalog.go | 73 ++----- manifest/admin.yaml | 11 +- 16 files changed, 409 insertions(+), 459 deletions(-) delete mode 100644 internal/commands/onboarding/emailhint.go delete mode 100644 internal/commands/onboarding/emailhint_test.go create mode 100644 internal/commands/resources/integration_setup.go create mode 100644 internal/commands/resources/provider_auth.go diff --git a/generator/src/join.ts b/generator/src/join.ts index 2dabe1c..13a9f44 100644 --- a/generator/src/join.ts +++ b/generator/src/join.ts @@ -102,12 +102,14 @@ export let joinManifest = (input: { throw new Error(`manifest command ${entry.path.join(' ')} is missing resource`); } - let wanted = normalizeSdkPath(entry.resource); + let defaultResource = entry.resource; let matches = publicEndpoints.filter(endpoint => { let { resource, method } = splitResourceMethod(normalizeSdkPath(endpoint.sdkPath)); + let methodName = camelToKebab(method); + let wanted = normalizeSdkPath(entry.methodResources?.[methodName] ?? defaultResource); if (resource !== wanted) return false; if (!entry.include?.length) return true; - return entry.include.some(name => camelToKebab(name) === camelToKebab(method)); + return entry.include.some(name => camelToKebab(name) === methodName); }); if (matches.length === 0) { diff --git a/generator/src/manifest.ts b/generator/src/manifest.ts index c038b1a..f30fc1f 100644 --- a/generator/src/manifest.ts +++ b/generator/src/manifest.ts @@ -18,6 +18,7 @@ export type CommandEntry = { resource?: string; group?: boolean; include?: string[]; + methodResources?: Record; aliases?: string[]; short?: string; long?: string; diff --git a/internal/cli/admin_cli_test.go b/internal/cli/admin_cli_test.go index bbb0fd2..c562b4c 100644 --- a/internal/cli/admin_cli_test.go +++ b/internal/cli/admin_cli_test.go @@ -3,6 +3,9 @@ package cli import ( "bytes" "encoding/json" + "io" + "net/http" + "net/http/httptest" "strings" "testing" "time" @@ -167,3 +170,62 @@ func TestAdminCLIProfileHintsUseAdminPath(t *testing.T) { t.Fatalf("list output missing admin hint: %q", rendered) } } + +func TestAdminProviderDiscoveryUsesCatalog(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("METORIAL_SKIP_UPDATE_CHECK", "1") + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/provider-listings" || r.URL.Query().Get("search") != "github" { + http.Error(w, "unexpected request", http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"items":[{"id":"plg_1","provider":{"id":"pro_1"}}]}`) + })) + defer server.Close() + + output, err := executeAdmin(t, "--api-host", server.URL, "--api-key", "metorial_sk_test", + "--format", "json", "providers", "list", "--search", "github") + var result struct { + Items []struct { + Provider struct{ ID string } `json:"provider"` + } + } + if err != nil || json.Unmarshal([]byte(output), &result) != nil || + len(result.Items) != 1 || result.Items[0].Provider.ID != "pro_1" { + t.Fatalf("providers list = %q, %v", output, err) + } +} + +func TestAdminIntegrationSetupResumesByID(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("METORIAL_SKIP_UPDATE_CHECK", "1") + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodPost && r.URL.Path == "/integration-setup-sessions": + _, _ = io.WriteString(w, `{"id":"iss_1","status":"pending","url":"https://example.test/setup"}`) + case r.Method == http.MethodGet && r.URL.Path == "/integration-setup-sessions/iss_1": + _, _ = io.WriteString(w, `{"id":"iss_1","status":"successful","integration_instance":{"id":"ini_1"}}`) + default: + http.Error(w, "unexpected request", http.StatusBadRequest) + } + })) + defer server.Close() + + base := []string{"--api-host", server.URL, "--api-key", "metorial_sk_test", "--format", "json", "integrations", "setup"} + for _, step := range []struct { + args []string + status string + }{ + {[]string{"--integration-id", "int_1", "--name", "GitHub", "--no-browser"}, "pending"}, + {[]string{"status", "iss_1"}, "successful"}, + {[]string{"wait", "iss_1", "--timeout", "1s"}, "successful"}, + } { + output, err := executeAdmin(t, append(base, step.args...)...) + var result struct{ ID, Status string } + if err != nil || json.Unmarshal([]byte(output), &result) != nil || result.ID != "iss_1" || result.Status != step.status { + t.Fatalf("setup %v = %q, %v", step.args, output, err) + } + } +} diff --git a/internal/commands/onboarding/command.go b/internal/commands/onboarding/command.go index 52649b0..c958000 100644 --- a/internal/commands/onboarding/command.go +++ b/internal/commands/onboarding/command.go @@ -1,6 +1,7 @@ package onboarding import ( + "encoding/json" "errors" "fmt" "net/url" @@ -13,6 +14,8 @@ import ( authcmd "github.com/metorial/cli/internal/commands/auth" "github.com/metorial/cli/internal/commandutil" "github.com/metorial/cli/internal/config" + "github.com/metorial/cli/internal/fetch" + "github.com/metorial/cli/internal/output" "github.com/metorial/cli/internal/terminal" "github.com/spf13/cobra" ) @@ -28,8 +31,8 @@ Create a new, unclaimed Metorial organization named "'s workspace" (using this computer's user name) and save a profile for it on this machine. The organization can be used right away. Open the printed claim link (or run "metorial-admin claim ") to attach it to a Metorial account. -The organization is created in a data region. You are asked to pick one, with the closest -region recommended. Pass --region to skip the question. +Pass --region to choose where the organization is created. Run signup without it +to list the available regions and the recommendation. `), Args: cobra.NoArgs, RunE: func(command *cobra.Command, args []string) error { @@ -66,6 +69,10 @@ reminds it daily until the organization is claimed. } func runSignup(command *cobra.Command, ctx commandutil.Context, region string) error { + format, err := output.ParseFormat(ctx.Options.Format) + if err != nil { + return err + } store, err := config.OpenStore() if err != nil { return err @@ -83,14 +90,14 @@ func runSignup(command *cobra.Command, ctx commandutil.Context, region string) e } stdoutFeatures := ctx.App.StdoutFeatures() - isAgent, agentName := agent.IsAiAgent() + isAgent, _ := agent.IsAiAgent() - region, err = resolveSignupRegion(command, ctx, apiHostURL, region, isAgent) + region, err = resolveSignupRegion(command, ctx, apiHostURL, region) if err != nil { return err } - spinner := terminal.NewSpinner(command.OutOrStdout(), stdoutFeatures, "Creating your organization...") + spinner := terminal.NewSpinner(command.ErrOrStderr(), ctx.App.StderrFeatures(), "Creating your organization...") spinner.Start() response, err := auth.NewClient(apiHostURL).CLISignup(workspaceName(), region) spinner.Stop() @@ -111,18 +118,30 @@ func runSignup(command *cobra.Command, ctx commandutil.Context, region string) e profile.InstanceID = response.Instance.ID profile.ClaimCheckedAt = &now - if isAgent { - if hint := detectEmailHint(agentName); hint != nil { - profile.ClaimEmailHint = hint.Email - profile.ClaimEmailHintSource = hint.Source - } - } - if err := store.UpsertProfile(*profile, true); err != nil { return err } - renderSignupSuccess(command.OutOrStdout(), stdoutFeatures, response, isAgent, *profile, command.Root().Name()) + if format == output.FormatStructured { + renderSignupSuccess(command.OutOrStdout(), stdoutFeatures, response, isAgent, *profile, command.Root().Name()) + } else { + body, err := json.Marshal(map[string]string{ + "organization_id": response.Organization.ID, + "organization_slug": response.Organization.Slug, + "project_id": response.Project.ID, + "instance_id": response.Instance.ID, + "profile_id": profile.ID, + "region": commandutil.FirstNonEmpty(response.Region, region), + "claim_status": "unclaimed", + "claim_url": response.Claim.URL, + }) + if err != nil { + return err + } + if err := output.WriteResponse(command.OutOrStdout(), &fetch.Response{Body: body}, output.RenderOptions{Format: format}); err != nil { + return err + } + } if browser.Supported() { _ = browser.Open(response.Claim.URL) @@ -131,29 +150,20 @@ func runSignup(command *cobra.Command, ctx commandutil.Context, region string) e return nil } -func resolveSignupRegion(command *cobra.Command, ctx commandutil.Context, apiHost *url.URL, requested string, isAgent bool) (string, error) { +func resolveSignupRegion(command *cobra.Command, ctx commandutil.Context, apiHost *url.URL, requested string) (string, error) { if requested = strings.TrimSpace(requested); requested != "" { return requested, nil } - features := ctx.App.StdoutFeatures() - interactive := !isAgent && features.IsTTY && stdinIsTerminal(ctx.App) - - spinner := terminal.NewSpinner(command.OutOrStdout(), features, "Finding the closest region...") + spinner := terminal.NewSpinner(command.ErrOrStderr(), ctx.App.StderrFeatures(), "Finding the closest region...") spinner.Start() choice := discoverRegions(apiHost) spinner.Stop() - switch { - case choice == nil: - return "", nil - case isAgent: - return "", errors.New(choice.agentInstructions(command.Root().Name())) - case interactive: - return promptRegion(ctx.App, choice) - default: - return choice.Recommended, nil + if choice == nil { + return "", fmt.Errorf("metorial: --region is required. Could not list regions for %s. Retry or run %s signup --region ", apiHost, command.Root().Name()) } + return "", errors.New(choice.regionInstructions(command.Root().Name())) } func runClaim(command *cobra.Command, ctx commandutil.Context, email string) error { diff --git a/internal/commands/onboarding/emailhint.go b/internal/commands/onboarding/emailhint.go deleted file mode 100644 index 0cc0b89..0000000 --- a/internal/commands/onboarding/emailhint.go +++ /dev/null @@ -1,121 +0,0 @@ -package onboarding - -import ( - "context" - "net/mail" - "os/exec" - "regexp" - "strings" - "time" -) - -type emailHint struct { - Email string - Source string -} - -const emailHintTimeout = 2 * time.Second - -var emailPattern = regexp.MustCompile(`[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}`) - -var runHintCommand = func(name string, args ...string) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), emailHintTimeout) - defer cancel() - - output, err := exec.CommandContext(ctx, name, args...).Output() - return string(output), err -} - -var maskingEmailDomains = []string{ - "privaterelay.appleid.com", - "mozmail.com", - "relay.firefox.com", - "duck.com", - "simplelogin.com", - "simplelogin.co", - "simplelogin.fr", - "simplelogin.io", - "slmail.me", - "aleeas.com", - "8alias.com", - "anonaddy.com", - "anonaddy.me", - "addy.io", - "passmail.net", - "passmail.com", - "passinbox.com", - "passfwd.com", - "cloaked.id", - "spamgourmet.com", - "33mail.com", -} - -func isMaskedEmail(email string) bool { - lower := strings.ToLower(email) - if strings.Contains(lower, "noreply") || strings.Contains(lower, "no-reply") { - return true - } - - at := strings.LastIndex(lower, "@") - if at < 0 { - return true - } - - domain := lower[at+1:] - for _, masked := range maskingEmailDomains { - if domain == masked || strings.HasSuffix(domain, "."+masked) { - return true - } - } - return false -} - -func usableHintEmail(candidate string) (string, bool) { - address, err := mail.ParseAddress(strings.TrimSpace(candidate)) - if err != nil { - return "", false - } - - if isMaskedEmail(address.Address) { - return "", false - } - - return address.Address, true -} - -func emailFromGit() *emailHint { - output, err := runHintCommand("git", "config", "--get", "user.email") - if err != nil { - return nil - } - - if email, ok := usableHintEmail(output); ok { - return &emailHint{Email: email, Source: "git config user.email"} - } - return nil -} - -func emailFromClaudeCode() *emailHint { - output, err := runHintCommand("claude", "auth", "status") - if err != nil { - return nil - } - - if email, ok := usableHintEmail(emailPattern.FindString(output)); ok { - return &emailHint{Email: email, Source: "`claude auth status`"} - } - return nil -} - -// The result is only a suggestion for the agent to confirm with the user; it is never sent to Metorial. -func detectEmailHint(agentName string) *emailHint { - if hint := emailFromGit(); hint != nil { - return hint - } - - if agentName == "claude_code" { - return emailFromClaudeCode() - } - - return nil -} diff --git a/internal/commands/onboarding/emailhint_test.go b/internal/commands/onboarding/emailhint_test.go deleted file mode 100644 index b2d1303..0000000 --- a/internal/commands/onboarding/emailhint_test.go +++ /dev/null @@ -1,179 +0,0 @@ -package onboarding - -import ( - "bytes" - "errors" - "strings" - "testing" - - "github.com/metorial/cli/internal/claimlink" - "github.com/metorial/cli/internal/config" -) - -func stubHintCommands(t *testing.T, outputs map[string]string) *[]string { - t.Helper() - var calls []string - original := runHintCommand - runHintCommand = func(name string, args ...string) (string, error) { - key := name + " " + strings.Join(args, " ") - calls = append(calls, key) - if output, ok := outputs[key]; ok { - return output, nil - } - return "", errors.New("not available") - } - t.Cleanup(func() { runHintCommand = original }) - return &calls -} - -func TestDetectEmailHintPrefersGit(t *testing.T) { - calls := stubHintCommands(t, map[string]string{ - "git config --get user.email": "ada@example.com\n", - "claude auth status": "Logged in as other@example.com", - }) - - hint := detectEmailHint("claude_code") - if hint == nil || hint.Email != "ada@example.com" || hint.Source != "git config user.email" { - t.Fatalf("hint = %+v", hint) - } - if len(*calls) != 1 { - t.Fatalf("expected only git to be queried, got %v", *calls) - } -} - -func TestDetectEmailHintFallsBackToClaudeCode(t *testing.T) { - stubHintCommands(t, map[string]string{ - "git config --get user.email": "12345+ada@users.noreply.github.com", - "claude auth status": "Login method: Claude Account\nEmail: Ada@Example.com\nOrg: Example", - }) - - hint := detectEmailHint("claude_code") - if hint == nil || hint.Email != "Ada@Example.com" { - t.Fatalf("hint = %+v", hint) - } -} - -func TestDetectEmailHintIgnoresClaudeForOtherAgents(t *testing.T) { - calls := stubHintCommands(t, map[string]string{ - "claude auth status": "Email: ada@example.com", - }) - - if hint := detectEmailHint("cursor"); hint != nil { - t.Fatalf("expected no hint, got %+v", hint) - } - for _, call := range *calls { - if strings.HasPrefix(call, "claude") { - t.Fatalf("claude must not be queried for other agents: %v", *calls) - } - } -} - -func TestDetectEmailHintRejectsInvalidOutput(t *testing.T) { - stubHintCommands(t, map[string]string{ - "git config --get user.email": "not an email", - "claude auth status": "Not logged in", - }) - - if hint := detectEmailHint("claude_code"); hint != nil { - t.Fatalf("expected no hint, got %+v", hint) - } -} - -func TestWriteAgentInstructions(t *testing.T) { - base := unclaimedProfile() - - t.Run("asks the user and suggests a detected email", func(t *testing.T) { - profile := base - profile.ClaimEmailHint = "ada@example.com" - profile.ClaimEmailHintSource = "git config user.email" - - var out bytes.Buffer - writeAgentInstructions(&out, "metorial-admin", profile) - - for _, want := range []string{ - "Ask them for the email address", - "ada@example.com (found in git config user.email)", - "confirm it with the user first", - "metorial-admin claim ", - claimlink.Identifier(base.ClaimToken), - } { - if !strings.Contains(out.String(), want) { - t.Fatalf("instructions missing %q:\n%s", want, out.String()) - } - } - }) - - t.Run("works without a hint", func(t *testing.T) { - var out bytes.Buffer - writeAgentInstructions(&out, "metorial-admin", base) - - if strings.Contains(out.String(), "likely address") { - t.Fatalf("unexpected hint line:\n%s", out.String()) - } - if !strings.Contains(out.String(), "metorial-admin claim ") { - t.Fatalf("missing claim command:\n%s", out.String()) - } - }) - - t.Run("stops asking for an email once the link was sent", func(t *testing.T) { - profile := base - profile.ClaimEmail = "ada@example.com" - - var out bytes.Buffer - writeAgentInstructions(&out, "metorial-admin", profile) - - if !strings.Contains(out.String(), "A claim link was emailed to ada@example.com") { - t.Fatalf("missing sent notice:\n%s", out.String()) - } - if strings.Contains(out.String(), "Ask them for the email address") { - t.Fatalf("should not ask for an email again:\n%s", out.String()) - } - }) -} - -func TestSaveClaimEmailPersists(t *testing.T) { - profile := unclaimedProfile() - store := setupStore(t, profile) - - saveClaimEmail(store, profile.ID, "ada@example.com") - - reopened, _ := config.OpenStore() - got, ok := reopened.ProfileByID(profile.ID) - if !ok || got.ClaimEmail != "ada@example.com" { - t.Fatalf("claim email not persisted: %+v", got) - } -} - -func TestUsableHintEmailRejectsMaskedAddresses(t *testing.T) { - masked := []string{ - "12345+ada@users.noreply.github.com", - "ada@noreply.github.com", - "ada@users.noreply.gitlab.com", - "ada@no-reply.example.com", - "abc123@privaterelay.appleid.com", - "ada.abc@mozmail.com", - "ada@relay.firefox.com", - "ada_at_example_com_xyz@duck.com", - "ada.abc@simplelogin.com", - "ada@slmail.me", - "ada.abc@aleeas.com", - "ada@mail.anonaddy.com", - "ada@abc.anonaddy.me", - "ada@addy.io", - "ada@passmail.net", - "ada@passinbox.com", - "Ada ", - } - for _, candidate := range masked { - if email, ok := usableHintEmail(candidate); ok { - t.Errorf("%q should be rejected, got %q", candidate, email) - } - } - - real := []string{"ada@example.com", "ada@gmail.com", "ada@notduck.com", "ada@icloud.com"} - for _, candidate := range real { - if _, ok := usableHintEmail(candidate); !ok { - t.Errorf("%q should be accepted", candidate) - } - } -} diff --git a/internal/commands/onboarding/onboarding_test.go b/internal/commands/onboarding/onboarding_test.go index 8c630cc..d25cd11 100644 --- a/internal/commands/onboarding/onboarding_test.go +++ b/internal/commands/onboarding/onboarding_test.go @@ -366,3 +366,27 @@ func TestClaimLinkFallsBackToTheStoredLink(t *testing.T) { t.Fatalf("ClaimLink() = %q, want %q", got, profile.ClaimURL) } } + +func TestWriteAgentInstructions(t *testing.T) { + var out bytes.Buffer + profile := unclaimedProfile() + writeAgentInstructions(&out, "metorial-admin", profile) + for _, want := range []string{"continue setup now", "final reply", claimlink.Identifier(profile.ClaimToken)} { + if !strings.Contains(out.String(), want) { + t.Fatalf("instructions missing %q:\n%s", want, out.String()) + } + } +} + +func TestSaveClaimEmailPersists(t *testing.T) { + profile := unclaimedProfile() + store := setupStore(t, profile) + + saveClaimEmail(store, profile.ID, "ada@example.com") + + reopened, _ := config.OpenStore() + got, ok := reopened.ProfileByID(profile.ID) + if !ok || got.ClaimEmail != "ada@example.com" { + t.Fatalf("claim email not persisted: %+v", got) + } +} diff --git a/internal/commands/onboarding/presenter.go b/internal/commands/onboarding/presenter.go index c7fca76..07a7d5d 100644 --- a/internal/commands/onboarding/presenter.go +++ b/internal/commands/onboarding/presenter.go @@ -29,14 +29,14 @@ func renderSignupSuccess(writer io.Writer, features terminal.Features, response _ = output.RenderBox(writer, details, boxOptions) _, _ = fmt.Fprintln(writer) - _, _ = fmt.Fprintln(writer, colors.Notice("This organization is not claimed yet. Claim it to keep it and invite your team.")) - _, _ = fmt.Fprintln(writer, colors.Muted("Open this link and sign in to keep it and invite your team:")) - _, _ = fmt.Fprintln(writer, response.Claim.URL) - if isAgent { - _, _ = fmt.Fprintln(writer) writeAgentInstructions(writer, rootName, profile) + return } + + _, _ = fmt.Fprintln(writer, colors.Notice("This organization is not claimed yet. Claim it to keep it and invite your team.")) + _, _ = fmt.Fprintln(writer, colors.Muted("Open this link and sign in to keep it and invite your team:")) + _, _ = fmt.Fprintln(writer, response.Claim.URL) } func renderClaimEmailSent(writer io.Writer, features terminal.Features, profile config.Profile, email string) { @@ -75,18 +75,7 @@ func writeAgentInstructions(writer io.Writer, rootName string, profile config.Pr _, _ = fmt.Fprintf(writer, "[metorial] This CLI profile uses an UNCLAIMED Metorial organization (%s).\n", orgRef) - if profile.ClaimEmail != "" { - _, _ = fmt.Fprintf(writer, "A claim link was emailed to %s. Ask the user to open it and sign in, or give them this link directly:\n", profile.ClaimEmail) - _, _ = fmt.Fprintf(writer, "%s\n", profile.ClaimLink(time.Now())) - _, _ = fmt.Fprintf(writer, "To send it to a different address, run `%s claim `.\n", rootName) - return - } - - _, _ = fmt.Fprintln(writer, "Before continuing, ask the user to claim it. Ask them for the email address they want to use with Metorial:") - if profile.ClaimEmailHint != "" { - source := commandutil.FirstNonEmpty(profile.ClaimEmailHintSource, "this machine") - _, _ = fmt.Fprintf(writer, "- A likely address is %s (found in %s). Do not assume it is right: confirm it with the user first.\n", profile.ClaimEmailHint, source) - } - _, _ = fmt.Fprintf(writer, "- Once the user gave you their email, run `%s claim ` to send them the claim link.\n", rootName) - _, _ = fmt.Fprintf(writer, "- If they would rather not share it, give them this link to claim the organization themselves: %s\n", profile.ClaimLink(time.Now())) + _, _ = fmt.Fprintln(writer, "You can continue setup now. Include the claim link in your final reply so the user can take ownership:") + _, _ = fmt.Fprintln(writer, profile.ClaimLink(time.Now())) + _, _ = fmt.Fprintf(writer, "If the user explicitly requests email delivery, run `%s claim `.\n", rootName) } diff --git a/internal/commands/onboarding/region.go b/internal/commands/onboarding/region.go index c5230d3..3a354a4 100644 --- a/internal/commands/onboarding/region.go +++ b/internal/commands/onboarding/region.go @@ -5,15 +5,13 @@ import ( "io" "net/http" "net/url" - "os" "sort" "strings" "sync" "time" - "github.com/metorial/cli/internal/app" "github.com/metorial/cli/internal/auth" - "golang.org/x/term" + "github.com/metorial/cli/internal/config" ) const ( @@ -59,12 +57,17 @@ var measureRegionLatency = func(traceURL string) (time.Duration, error) { return best, nil } -// Hosts without regions (self-hosted, older servers) simply have nothing to choose, so any -// failure here means "no choice" rather than an error. func discoverRegions(apiHost *url.URL) *regionChoice { response, err := fetchSignupRegions(apiHost) - if err != nil || len(response.Regions) < 2 { - return nil + if err != nil || len(response.Regions) == 0 { + if apiHost.Hostname() != config.DefaultAPIHost { + return nil + } + // Older Metorial Cloud deployments may not expose region discovery yet. + response = &auth.SignupRegionsResponse{Regions: []auth.SignupRegion{ + {ID: "us1", Name: "United States", TraceURL: "https://api-us1.metorial.com/.metorial/trace-region"}, + {ID: "eu1", Name: "European Union", TraceURL: "https://api-eu1.metorial.com/.metorial/trace-region"}, + }} } choice := ®ionChoice{} @@ -147,10 +150,10 @@ func (c *regionChoice) label(option regionOption) string { return label } -func (c *regionChoice) agentInstructions(rootName string) string { +func (c *regionChoice) regionInstructions(rootName string) string { var builder strings.Builder - builder.WriteString("metorial: choose a region for the new organization first. Available regions:\n") + builder.WriteString("metorial: --region is required to create a new organization. Available regions:\n") for _, option := range c.recommendedFirst() { builder.WriteString("- " + c.label(option) + "\n") } @@ -167,30 +170,9 @@ func (c *regionChoice) agentInstructions(rootName string) string { if c.Recommended != "" { builder.WriteString("Pick the recommended region unless the user asked for a different one (for example for data residency), ") } else { - builder.WriteString("Pick the region closest to the user, or ask them, ") + builder.WriteString("Choose the region that meets the user’s location or data residency needs, ") } builder.WriteString(fmt.Sprintf("then run `%s signup --region `.", rootName)) return builder.String() } - -func stdinIsTerminal(application *app.App) bool { - file, ok := application.Stdin.(*os.File) - return ok && term.IsTerminal(int(file.Fd())) -} - -func promptRegion(application *app.App, choice *regionChoice) (string, error) { - options := choice.recommendedFirst() - - labels := make([]string, 0, len(options)) - for _, option := range options { - labels = append(labels, choice.label(option)) - } - - index, err := application.PromptSelect("Where should your organization be created?", labels) - if err != nil { - return "", fmt.Errorf("metorial: region selection was cancelled. Use --region to choose one") - } - - return options[index].ID, nil -} diff --git a/internal/commands/onboarding/region_test.go b/internal/commands/onboarding/region_test.go index 76c4a9c..fe121c1 100644 --- a/internal/commands/onboarding/region_test.go +++ b/internal/commands/onboarding/region_test.go @@ -102,15 +102,15 @@ func TestDiscoverRegionsWithoutAnyRecommendation(t *testing.T) { } } -func TestDiscoverRegionsSkipsHostsWithoutAChoice(t *testing.T) { +func TestDiscoverRegionsHandlesFailureAndSingleRegion(t *testing.T) { stubRegions(t, nil, nil) if choice := discoverRegions(hostURL(t)); choice != nil { t.Fatalf("expected no choice when the lookup fails, got %+v", choice) } stubRegions(t, &auth.SignupRegionsResponse{Regions: testRegions[:1]}, nil) - if choice := discoverRegions(hostURL(t)); choice != nil { - t.Fatalf("expected no choice for a single region, got %+v", choice) + if choice := discoverRegions(hostURL(t)); choice == nil || len(choice.Options) != 1 || choice.Options[0].ID != "us1" { + t.Fatalf("expected the single region to be listed, got %+v", choice) } } @@ -124,7 +124,7 @@ func TestAgentInstructionsListRecommendedRegionFirst(t *testing.T) { RecommendedBy: "latency", } - text := choice.agentInstructions("metorial-admin") + text := choice.regionInstructions("metorial-admin") if strings.Index(text, "European Union (eu1) - 35ms - recommended") > strings.Index(text, "United States (us1)") { t.Fatalf("recommended region should be listed first:\n%s", text) @@ -139,8 +139,8 @@ func TestAgentInstructionsListRecommendedRegionFirst(t *testing.T) { func TestAgentInstructionsWithoutRecommendationAskToPick(t *testing.T) { choice := ®ionChoice{Options: []regionOption{{SignupRegion: testRegions[0]}, {SignupRegion: testRegions[1]}}} - text := choice.agentInstructions("metorial-admin") - if !strings.Contains(text, "No region could be recommended") || !strings.Contains(text, "closest to the user") { + text := choice.regionInstructions("metorial-admin") + if !strings.Contains(text, "No region could be recommended") || !strings.Contains(text, "data residency needs") { t.Fatalf("unexpected instructions:\n%s", text) } } @@ -166,7 +166,7 @@ func TestResolveSignupRegionPrefersTheFlag(t *testing.T) { stubRegions(t, nil, nil) command, ctx := signupContext(t, "CLAUDECODE") - region, err := resolveSignupRegion(command, ctx, hostURL(t), " us1 ", true) + region, err := resolveSignupRegion(command, ctx, hostURL(t), " us1 ") if err != nil || region != "us1" { t.Fatalf("region = %q, err = %v", region, err) } @@ -176,28 +176,28 @@ func TestResolveSignupRegionAgentMustChoose(t *testing.T) { stubRegions(t, &auth.SignupRegionsResponse{Regions: testRegions, RecommendedRegion: "eu1"}, nil) command, ctx := signupContext(t, "CURSOR_AGENT") - _, err := resolveSignupRegion(command, ctx, hostURL(t), "", true) + _, err := resolveSignupRegion(command, ctx, hostURL(t), "") if err == nil || !strings.Contains(err.Error(), "signup --region ") || !strings.Contains(err.Error(), "recommended") { t.Fatalf("expected region instructions, got %v", err) } } -func TestResolveSignupRegionNonInteractiveUsesRecommendation(t *testing.T) { +func TestResolveSignupRegionNonInteractiveMustChoose(t *testing.T) { stubRegions(t, &auth.SignupRegionsResponse{Regions: testRegions, RecommendedRegion: "us1"}, nil) command, ctx := signupContext(t, "") - region, err := resolveSignupRegion(command, ctx, hostURL(t), "", false) - if err != nil || region != "us1" { + region, err := resolveSignupRegion(command, ctx, hostURL(t), "") + if err == nil || region != "" || !strings.Contains(err.Error(), "United States (us1) - recommended") { t.Fatalf("region = %q, err = %v", region, err) } } -func TestResolveSignupRegionWithoutRegionsLetsTheServerDecide(t *testing.T) { +func TestResolveSignupRegionWithoutRegionsRequiresFlag(t *testing.T) { stubRegions(t, nil, nil) command, ctx := signupContext(t, "CLAUDECODE") - region, err := resolveSignupRegion(command, ctx, hostURL(t), "", true) - if err != nil || region != "" { + region, err := resolveSignupRegion(command, ctx, hostURL(t), "") + if err == nil || region != "" || !strings.Contains(err.Error(), "--region is required") { t.Fatalf("region = %q, err = %v", region, err) } } diff --git a/internal/commands/resources/integration_setup.go b/internal/commands/resources/integration_setup.go new file mode 100644 index 0000000..8682872 --- /dev/null +++ b/internal/commands/resources/integration_setup.go @@ -0,0 +1,125 @@ +package resources + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/metorial/cli/internal/browser" + "github.com/metorial/cli/internal/commandutil" + "github.com/metorial/cli/internal/config" + "github.com/metorial/cli/internal/fetch" + "github.com/metorial/cli/internal/output" + "github.com/spf13/cobra" +) + +type integrationSetupSession struct { + ID string `json:"id"` + URL string `json:"url"` + Status string `json:"status"` +} + +func presentIntegrationSetup(command *cobra.Command, response *fetch.Response) { + var session integrationSetupSession + if err := json.Unmarshal(response.Body, &session); err != nil || session.ID == "" { + return + } + + fmt.Fprintf(command.ErrOrStderr(), "Check status with %s status %s or wait with %s wait %s. Reuse the same profile and instance options.\n", + command.CommandPath(), session.ID, command.CommandPath(), session.ID) + + noBrowser, _ := command.Flags().GetBool("no-browser") + link, err := url.Parse(session.URL) + if !noBrowser && !commandutil.BrowserShellEnabled() && err == nil && link.Host != "" && + link.User == nil && (link.Scheme == "https" || link.Scheme == "http") && browser.Supported() { + _ = browser.Open(session.URL) + } +} + +func newIntegrationSetupWait(cli commandutil.Context) *cobra.Command { + var timeout time.Duration + command := &cobra.Command{ + Use: "wait ", + Short: "Wait for an existing integration setup session to finish", + Args: cobra.ExactArgs(1), + RunE: func(command *cobra.Command, args []string) error { + id := args[0] + if id == "" || id == "." || id == ".." || strings.Contains(id, "/") { + return fmt.Errorf("metorial: invalid integration setup session ID") + } + if timeout <= 0 { + return fmt.Errorf("metorial: --timeout must be positive") + } + format, err := output.ParseFormat(cli.Options.Format) + if err != nil { + return err + } + + runtime, err := cli.ResolveRuntime() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(command.Context(), timeout) + defer cancel() + + for { + response, err := fetchIntegrationSetup(ctx, runtime, id) + if err != nil { + return fmt.Errorf("metorial: could not wait for integration setup session %s: %w", id, err) + } + var session integrationSetupSession + if err := json.Unmarshal(response.Body, &session); err != nil || session.Status == "" { + return fmt.Errorf("metorial: could not read status for integration setup session %s", id) + } + if session.Status != "pending" { + if err := output.WriteResponse(command.OutOrStdout(), response, output.RenderOptions{ + Format: format, Colors: cli.App.StdoutFeatures(), + }); err != nil { + return err + } + if session.Status == "successful" { + return nil + } + return fmt.Errorf("metorial: integration setup session %s is %s", id, session.Status) + } + + timer := time.NewTimer(2 * time.Second) + select { + case <-ctx.Done(): + timer.Stop() + return fmt.Errorf("metorial: stopped waiting for integration setup session %s: %w", id, ctx.Err()) + case <-timer.C: + } + } + }, + } + command.Flags().DurationVar(&timeout, "timeout", time.Minute, "Maximum time to wait for authorization") + commandutil.ConfigureCommand(command) + return command +} + +// The SDK bounds HTTP requests but its raw request API does not accept a context. +func fetchIntegrationSetup(ctx context.Context, runtime config.Runtime, id string) (*fetch.Response, error) { + type result struct { + response *fetch.Response + err error + } + done := make(chan result, 1) + go func() { + response, err := fetch.Do(runtime, fetch.Request{ + Method: http.MethodGet, + Target: "/integration-setup-sessions/" + url.PathEscape(id), + }) + done <- result{response: response, err: err} + }() + select { + case <-ctx.Done(): + return nil, ctx.Err() + case response := <-done: + return response.response, response.err + } +} diff --git a/internal/commands/resources/provider_auth.go b/internal/commands/resources/provider_auth.go new file mode 100644 index 0000000..d15efc6 --- /dev/null +++ b/internal/commands/resources/provider_auth.go @@ -0,0 +1,80 @@ +package resources + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + + "github.com/metorial/cli/internal/apiclient" + "github.com/metorial/cli/internal/commandutil" + "github.com/metorial/cli/internal/fetch" + "github.com/metorial/cli/internal/output" + "github.com/spf13/cobra" +) + +func newProviderAuthCommand(cli commandutil.Context) *cobra.Command { + command := &cobra.Command{ + Use: "auth ", + Short: "Show a provider's auth methods and available credentials", + Long: "Inspect the provider's current auth methods and available managed or custom credentials. " + + "Pass the selected method as --provider-auth-method-id and a compatible OAuth app as --provider-auth-credentials-id. " + + "OAuth auto-registration describes creating a new app.", + Args: cobra.ExactArgs(1), + RunE: func(command *cobra.Command, args []string) error { + format, err := output.ParseFormat(cli.Options.Format) + if err != nil { + return err + } + runtime, err := cli.ResolveRuntime() + if err != nil { + return err + } + + client := apiclient.Client{Runtime: runtime} + var provider struct { + ID string `json:"id"` + Name string `json:"name"` + OAuth json.RawMessage `json:"oauth"` + CurrentVersion struct { + ID string `json:"id"` + } `json:"current_version"` + } + if err := client.Do(http.MethodGet, "/providers/"+apiclient.PathSegment(args[0]), nil, &provider); err != nil { + return err + } + if provider.ID == "" { + return fmt.Errorf("metorial: provider response is missing an ID") + } + + var methods, credentials json.RawMessage + if provider.CurrentVersion.ID != "" { + query := url.Values{"provider_version_id": {provider.CurrentVersion.ID}} + if err := client.Do(http.MethodGet, "/provider-auth-methods?"+query.Encode(), nil, &methods); err != nil { + return err + } + } + query := url.Values{"provider_id": {provider.ID}} + if err := client.Do(http.MethodGet, "/provider-auth-credentials?"+query.Encode(), nil, &credentials); err != nil { + return err + } + + body, err := json.Marshal(map[string]any{ + "provider_id": provider.ID, + "name": provider.Name, + "provider_version_id": provider.CurrentVersion.ID, + "oauth": provider.OAuth, + "auth_methods": methods, + "auth_credentials": credentials, + }) + if err != nil { + return err + } + return output.WriteResponse(command.OutOrStdout(), &fetch.Response{Body: body}, output.RenderOptions{ + Format: format, Colors: cli.App.StdoutFeatures(), + }) + }, + } + commandutil.ConfigureCommand(command) + return command +} diff --git a/internal/commands/resources/public.go b/internal/commands/resources/public.go index 7681048..4bed7ba 100644 --- a/internal/commands/resources/public.go +++ b/internal/commands/resources/public.go @@ -92,6 +92,15 @@ func AddGeneratedCommands( } } + if providers := resourcecmd.CommandByName(root, "providers"); providers != nil { + providers.AddCommand(newProviderAuthCommand(ctx)) + } + if integrations := resourcecmd.CommandByName(root, "integrations"); integrations != nil { + if setup := resourcecmd.CommandByName(integrations, "setup"); setup != nil { + setup.AddCommand(newIntegrationSetupWait(ctx)) + } + } + return nil } @@ -132,6 +141,8 @@ func newPublicResourceAction( short = fmt.Sprintf("%s %s", operation.Name, resource.Plural) } + isIntegrationSetup := operation.SDKMapping == "integrations.setupSessions.create" && operation.Name == "setup" + command := &cobra.Command{ Use: use, Short: short, @@ -176,9 +187,15 @@ func newPublicResourceAction( } } + if isIntegrationSetup && requestErr == nil && response != nil { + presentIntegrationSetup(command, response) + } return requestErr }, } + if isIntegrationSetup { + command.Flags().Bool("no-browser", false, "Print the setup link without opening a browser") + } commandutil.ConfigureCommand(command) addOperationFlags(command, operation) @@ -768,7 +785,7 @@ func readJSONFlagValue(command *cobra.Command, flag resourcecmd.FlagSpec) (any, } func transformResourceResponse(response *fetch.Response, resource resourcecmd.ResourceSpec, operation resourcecmd.OperationSpec) error { - if response == nil || resource.Plural != "providers" { + if response == nil || resource.Plural != "providers" || operation.Path != "" { return nil } diff --git a/internal/config/config.go b/internal/config/config.go index 04dea13..1f02650 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -83,9 +83,7 @@ type Profile struct { OrgSlug string `mapstructure:"org_slug" json:"org_slug,omitempty"` ClaimCheckedAt *time.Time `mapstructure:"claim_checked_at" json:"claim_checked_at,omitempty"` - ClaimEmail string `mapstructure:"claim_email" json:"claim_email,omitempty"` - ClaimEmailHint string `mapstructure:"claim_email_hint" json:"claim_email_hint,omitempty"` - ClaimEmailHintSource string `mapstructure:"claim_email_hint_source" json:"claim_email_hint_source,omitempty"` + ClaimEmail string `mapstructure:"claim_email" json:"claim_email,omitempty"` CreatedAt time.Time `mapstructure:"created_at" json:"created_at"` UpdatedAt time.Time `mapstructure:"updated_at" json:"updated_at"` diff --git a/internal/generated/admin/catalog.go b/internal/generated/admin/catalog.go index 962cc12..392e9b7 100644 --- a/internal/generated/admin/catalog.go +++ b/internal/generated/admin/catalog.go @@ -54,71 +54,33 @@ func Catalog() []resourcecmd.ResourceSpec { { Name: "list", Method: "GET", - Path: "/providers", - Short: "List providers", - Long: "Returns a paginated list of providers.", - SDKMapping: "providers.list", + Path: "/provider-listings", + Short: "List provider listings", + Long: "Returns a paginated list of provider listings.", + SDKMapping: "providerListings.list", Flags: []resourcecmd.FlagSpec{ {Name: "limit", Type: resourcecmd.FlagFloat, Target: "query.limit", Usage: "limit", Required: false, Repeated: false}, {Name: "after", Type: resourcecmd.FlagString, Target: "query.after", Usage: "after", Required: false, Repeated: false}, {Name: "before", Type: resourcecmd.FlagString, Target: "query.before", Usage: "before", Required: false, Repeated: false}, {Name: "cursor", Type: resourcecmd.FlagString, Target: "query.cursor", Usage: "cursor", Required: false, Repeated: false}, {Name: "order", Type: resourcecmd.FlagString, Target: "query.order", Usage: "order", Required: false, Repeated: false}, - {Name: "id", Type: resourcecmd.FlagStringSlice, Target: "query.id", Usage: "Filter by provider ID(s)", Required: false, Repeated: true}, - {Name: "search", Type: resourcecmd.FlagString, Target: "query.search", Usage: "Search providers by name, description, or readme", Required: false, Repeated: false}, - {Name: "auth-method", Type: resourcecmd.FlagStringSlice, Target: "query.auth_method", Usage: "Filter by auth method — matches an auth method ID, auth method global ID, key, name, or type (oauth, token, service_account, custom)", Required: false, Repeated: true}, - {Name: "auth-setup", Type: resourcecmd.FlagStringSlice, Target: "query.auth_setup", Usage: "Filter by auth setup status. \"configured\" matches providers with a token or custom auth method, or with auth credentials already configured. \"not_configured\" matches providers with only OAuth auth methods and no auth credentials configured.", Required: false, Repeated: true}, + {Name: "search", Type: resourcecmd.FlagString, Target: "query.search", Usage: "search", Required: false, Repeated: false}, + {Name: "id", Type: resourcecmd.FlagStringSlice, Target: "query.id", Usage: "id", Required: false, Repeated: true}, + {Name: "provider-category-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_category_id", Usage: "provider_category_id", Required: false, Repeated: true}, + {Name: "provider-collection-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_collection_id", Usage: "provider_collection_id", Required: false, Repeated: true}, + {Name: "provider-group-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_group_id", Usage: "provider_group_id", Required: false, Repeated: true}, + {Name: "publisher-id", Type: resourcecmd.FlagStringSlice, Target: "query.publisher_id", Usage: "publisher_id", Required: false, Repeated: true}, + {Name: "is-owner", Type: resourcecmd.FlagBool, Target: "query.is_owner", Usage: "is_owner", Required: false, Repeated: false}, + {Name: "is-public", Type: resourcecmd.FlagBool, Target: "query.is_public", Usage: "is_public", Required: false, Repeated: false}, + {Name: "is-verified", Type: resourcecmd.FlagBool, Target: "query.is_verified", Usage: "is_verified", Required: false, Repeated: false}, + {Name: "is-official", Type: resourcecmd.FlagBool, Target: "query.is_official", Usage: "is_official", Required: false, Repeated: false}, + {Name: "is-metorial", Type: resourcecmd.FlagBool, Target: "query.is_metorial", Usage: "is_metorial", Required: false, Repeated: false}, + {Name: "created-at", Type: resourcecmd.FlagJSON, Target: "query.created_at", Usage: "Filter provider listing creation time by date range", Required: false, Repeated: false}, + {Name: "updated-at", Type: resourcecmd.FlagJSON, Target: "query.updated_at", Usage: "Filter provider listing last update time by date range", Required: false, Repeated: false}, }, }, }, Children: []resourcecmd.ResourceSpec{ - { - Plural: "listings", - Singular: "listings", - Short: "Provider Listings", - Long: "A listing is a provider enriched with marketplace metadata.", - Operations: []resourcecmd.OperationSpec{ - { - Name: "get", - Method: "GET", - Path: "/provider-listings/:providerListingId", - Short: "Get provider listing", - Long: "Retrieves a specific provider listing by ID.", - SDKMapping: "providerListings.get", - Args: []resourcecmd.ArgumentSpec{ - {Name: "provider-listing-id", Target: "path.providerListingId", Required: true, Description: "provider listing id"}, - }, - }, - { - Name: "list", - Method: "GET", - Path: "/provider-listings", - Short: "List provider listings", - Long: "Returns a paginated list of provider listings.", - SDKMapping: "providerListings.list", - Flags: []resourcecmd.FlagSpec{ - {Name: "limit", Type: resourcecmd.FlagFloat, Target: "query.limit", Usage: "limit", Required: false, Repeated: false}, - {Name: "after", Type: resourcecmd.FlagString, Target: "query.after", Usage: "after", Required: false, Repeated: false}, - {Name: "before", Type: resourcecmd.FlagString, Target: "query.before", Usage: "before", Required: false, Repeated: false}, - {Name: "cursor", Type: resourcecmd.FlagString, Target: "query.cursor", Usage: "cursor", Required: false, Repeated: false}, - {Name: "order", Type: resourcecmd.FlagString, Target: "query.order", Usage: "order", Required: false, Repeated: false}, - {Name: "search", Type: resourcecmd.FlagString, Target: "query.search", Usage: "search", Required: false, Repeated: false}, - {Name: "id", Type: resourcecmd.FlagStringSlice, Target: "query.id", Usage: "id", Required: false, Repeated: true}, - {Name: "provider-category-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_category_id", Usage: "provider_category_id", Required: false, Repeated: true}, - {Name: "provider-collection-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_collection_id", Usage: "provider_collection_id", Required: false, Repeated: true}, - {Name: "provider-group-id", Type: resourcecmd.FlagStringSlice, Target: "query.provider_group_id", Usage: "provider_group_id", Required: false, Repeated: true}, - {Name: "publisher-id", Type: resourcecmd.FlagStringSlice, Target: "query.publisher_id", Usage: "publisher_id", Required: false, Repeated: true}, - {Name: "is-owner", Type: resourcecmd.FlagBool, Target: "query.is_owner", Usage: "is_owner", Required: false, Repeated: false}, - {Name: "is-public", Type: resourcecmd.FlagBool, Target: "query.is_public", Usage: "is_public", Required: false, Repeated: false}, - {Name: "is-verified", Type: resourcecmd.FlagBool, Target: "query.is_verified", Usage: "is_verified", Required: false, Repeated: false}, - {Name: "is-official", Type: resourcecmd.FlagBool, Target: "query.is_official", Usage: "is_official", Required: false, Repeated: false}, - {Name: "is-metorial", Type: resourcecmd.FlagBool, Target: "query.is_metorial", Usage: "is_metorial", Required: false, Repeated: false}, - {Name: "created-at", Type: resourcecmd.FlagJSON, Target: "query.created_at", Usage: "Filter provider listing creation time by date range", Required: false, Repeated: false}, - {Name: "updated-at", Type: resourcecmd.FlagJSON, Target: "query.updated_at", Usage: "Filter provider listing last update time by date range", Required: false, Repeated: false}, - }, - }, - }, - }, { Plural: "versions", Singular: "versions", @@ -8211,6 +8173,7 @@ func Catalog() []resourcecmd.ResourceSpec { }, Shortcuts: []resourcecmd.ShortcutSpec{ {Path: []string{"integrations", "setup"}, Method: "create"}, + {Path: []string{"integrations", "setup", "status"}, Method: "get"}, }, }, }, diff --git a/manifest/admin.yaml b/manifest/admin.yaml index 3b3d4dd..da81627 100644 --- a/manifest/admin.yaml +++ b/manifest/admin.yaml @@ -22,13 +22,8 @@ commands: - path: [providers] resource: providers include: [list, get] - args: - list: - search: { flag: search } - auth_method: { flag: auth-method } - - - path: [providers, listings] - resource: providerListings + methodResources: + list: providerListings - path: [providers, versions] resource: providers.versions @@ -481,6 +476,8 @@ commands: shortcuts: - path: [integrations, setup] method: create + - path: [integrations, setup, status] + method: get - path: [api-keys] resource: apiKeys From ddeaeb3bafefda686daf85a17dacafc164d96ee0 Mon Sep 17 00:00:00 2001 From: Tobias Herber <22559657+herber@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:30:57 +0100 Subject: [PATCH 2/2] Add consumer magic mcp access --- README.md | 18 ++ internal/cli/admin.go | 5 + internal/cli/admin_cli_test.go | 81 +++++++++ internal/commands/consumer/common.go | 7 +- internal/commands/consumer/mcp_magic.go | 30 +++- internal/commands/consumer/member.go | 208 ++++++++++++++++++++++++ internal/commands/consumer/setup.go | 50 +++++- internal/commands/consumer/tools.go | 4 +- internal/consumerapi/client.go | 36 +++- 9 files changed, 422 insertions(+), 17 deletions(-) create mode 100644 internal/commands/consumer/member.go diff --git a/README.md b/README.md index 0860d07..13e1540 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,24 @@ metorial-admin files upload ./logo.png --purpose organization_image metorial-admin files upload ./build-context --purpose sandbox_image_build_context ``` +### Personal integrations + +A signed-in member of a claimed organization can connect an existing integration +and use its Magic MCP tools as their own consumer: + +```sh +metorial-admin integrations personal setup +metorial-admin integrations personal status +metorial-admin integrations personal wait +metorial-admin integrations personal finish +metorial-admin integrations personal tools +``` + +Setup opens the browser and returns the IDs for resuming. Finish deploys and +verifies the connection. The CLI uses its existing sign-in and keeps the +short-lived consumer credential in memory. + + ## Skills Both CLIs can edit Metorial skills. `metorial` works with the skills you can access in your portal; `metorial-admin skills` works with every skill in the instance. diff --git a/internal/cli/admin.go b/internal/cli/admin.go index 4c6c640..10fc4c7 100644 --- a/internal/cli/admin.go +++ b/internal/cli/admin.go @@ -5,6 +5,7 @@ import ( authcmd "github.com/metorial/cli/internal/commands/auth" clicmd "github.com/metorial/cli/internal/commands/cli" completioncmd "github.com/metorial/cli/internal/commands/completion" + consumercmd "github.com/metorial/cli/internal/commands/consumer" examplecmd "github.com/metorial/cli/internal/commands/example" fetchcmd "github.com/metorial/cli/internal/commands/fetch" filescmd "github.com/metorial/cli/internal/commands/files" @@ -16,6 +17,7 @@ import ( systemcmd "github.com/metorial/cli/internal/commands/system" "github.com/metorial/cli/internal/commandutil" "github.com/metorial/cli/internal/config" + "github.com/metorial/cli/internal/resourcecmd" "github.com/spf13/cobra" ) @@ -57,6 +59,9 @@ func newAdminRoot(application *app.App) (*cobra.Command, error) { return nil, err } if !commandutil.BrowserShellEnabled() { + if integrations := resourcecmd.CommandByName(command, "integrations"); integrations != nil { + integrations.AddCommand(consumercmd.NewMemberCommand(ctx)) + } if err := filescmd.Attach(command, ctx); err != nil { return nil, err } diff --git a/internal/cli/admin_cli_test.go b/internal/cli/admin_cli_test.go index c562b4c..f709f30 100644 --- a/internal/cli/admin_cli_test.go +++ b/internal/cli/admin_cli_test.go @@ -2,7 +2,9 @@ package cli import ( "bytes" + "context" "encoding/json" + "fmt" "io" "net/http" "net/http/httptest" @@ -12,6 +14,7 @@ import ( "github.com/metorial/cli/internal/app" "github.com/metorial/cli/internal/config" + "github.com/modelcontextprotocol/go-sdk/mcp" ) func executeAdmin(t *testing.T, args ...string) (string, error) { @@ -229,3 +232,81 @@ func TestAdminIntegrationSetupResumesByID(t *testing.T) { } } } + +func TestAdminPersonalIntegrationReusesConsumerSetupAndMCP(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("METORIAL_SKIP_UPDATE_CHECK", "1") + + protocol := mcp.NewServer(&mcp.Implementation{Name: "test", Version: "1"}, nil) + protocol.AddTool(&mcp.Tool{Name: "example", InputSchema: json.RawMessage(`{"type":"object"}`)}, + func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) { + t.Error("verification must not call tools") + return nil, nil + }) + mcpHandler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return protocol }, nil) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + authorization := r.Header.Get("Authorization") + if r.URL.Path == "/member-consumer-session" { + if r.Method != http.MethodPost || authorization != "Bearer admin-token" || + r.Header.Get("metorial-instance-id") != "ins_1" { + http.Error(w, "invalid exchange request", http.StatusUnauthorized) + return + } + var body struct { + IntegrationID string `json:"integration_id"` + } + if json.NewDecoder(r.Body).Decode(&body) != nil { + http.Error(w, "invalid exchange body", http.StatusBadRequest) + return + } + response := map[string]string{"access_token": "ephemeral-consumer-token"} + if body.IntegrationID == "int_1" { + response["catalog_item_id"] = "ptb_1" + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(response) + return + } + + if authorization != "Bearer ephemeral-consumer-token" { + http.Error(w, "consumer credential required", http.StatusUnauthorized) + return + } + if r.URL.Path == "/connect/magic/example" { + mcpHandler.ServeHTTP(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + switch r.Method + " " + r.URL.Path { + case "GET /consumer/providers/ptb_1": + _, _ = io.WriteString(w, `{"id":"cat_1","type":"provider_template","availability":"available_now"}`) + case "POST /consumer/providers/cat_1/setup": + _, _ = io.WriteString(w, `{"id":"iss_1","status":"pending","url":"https://example.test/setup"}`) + case "GET /consumer/providers/cat_1/setup/iss_1": + _, _ = io.WriteString(w, `{"id":"iss_1","status":"successful"}`) + case "POST /consumer/providers/cat_1/deploy", "GET /magic-mcp-servers/mgsr_1": + _, _ = fmt.Fprintf(w, `{"id":"mgsr_1","endpoints":[{"url":"http://%s/connect/magic/example"}]}`, r.Host) + default: + http.Error(w, "unexpected endpoint", http.StatusNotFound) + } + })) + defer server.Close() + + base := []string{"--api-host", server.URL, "--api-key", "admin-token", "--instance", "ins_1", + "--format", "json", "integrations", "personal"} + for _, step := range []struct { + args []string + want string + }{ + {[]string{"setup", "int_1", "--no-browser"}, `"status": "pending"`}, + {[]string{"status", "cat_1", "iss_1"}, `"status": "successful"`}, + {[]string{"wait", "cat_1", "iss_1"}, `"status": "successful"`}, + {[]string{"finish", "cat_1", "iss_1"}, `"tool_count": 1`}, + {[]string{"tools", "mgsr_1"}, `"name": "example"`}, + } { + output, err := executeAdmin(t, append(base, step.args...)...) + if err != nil || !strings.Contains(output, step.want) || strings.Contains(output, "ephemeral-consumer-token") { + t.Fatalf("personal %v = %q, %v", step.args, output, err) + } + } +} diff --git a/internal/commands/consumer/common.go b/internal/commands/consumer/common.go index ce686fc..5a3ba79 100644 --- a/internal/commands/consumer/common.go +++ b/internal/commands/consumer/common.go @@ -16,7 +16,8 @@ import ( const defaultPageLimit = 15 type commandEnv struct { - ctx commandutil.Context + ctx commandutil.Context + member bool } func (e commandEnv) app() *app.App { @@ -33,6 +34,10 @@ func (e commandEnv) client() (*consumerapi.Client, error) { return nil, err } + if e.member { + client, _, err := consumerapi.NewMember(runtime, "") + return client, err + } return consumerapi.New(runtime) } diff --git a/internal/commands/consumer/mcp_magic.go b/internal/commands/consumer/mcp_magic.go index 92bdced..f67f47d 100644 --- a/internal/commands/consumer/mcp_magic.go +++ b/internal/commands/consumer/mcp_magic.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "strings" "time" @@ -17,11 +18,11 @@ type magicMCPClient struct { session *mcp.ClientSession } -func connectMagicMCP(ctx context.Context, accessToken string, server *consumerapi.MagicMcpServer) (*magicMCPClient, error) { +func connectMagicMCP(ctx context.Context, clientAuth *consumerapi.Client, server *consumerapi.MagicMcpServer) (*magicMCPClient, error) { if server == nil { return nil, fmt.Errorf("metorial: integration is required") } - if strings.TrimSpace(accessToken) == "" { + if clientAuth.AccessToken() == "" { return nil, fmt.Errorf("metorial: an access token is required to connect to an integration") } @@ -43,8 +44,9 @@ func connectMagicMCP(ctx context.Context, accessToken string, server *consumerap DisableStandaloneSSE: true, HTTPClient: &http.Client{ Transport: &magicMCPAuthTransport{ - base: http.DefaultTransport, - bearerToken: accessToken, + base: http.DefaultTransport, + bearerToken: clientAuth.AccessToken(), + allowedOrigin: clientAuth.MCPOrigin(), }, }, }, nil) @@ -151,11 +153,16 @@ func mcpContentToValues(contents []mcp.Content) []any { } type magicMCPAuthTransport struct { - base http.RoundTripper - bearerToken string + base http.RoundTripper + bearerToken string + allowedOrigin *url.URL } func (t *magicMCPAuthTransport) RoundTrip(request *http.Request) (*http.Response, error) { + if t.allowedOrigin != nil && !isTrustedMCPOrigin(t.allowedOrigin, request.URL) { + return nil, fmt.Errorf("metorial: integration endpoint is outside the configured API origin") + } + base := t.base if base == nil { base = http.DefaultTransport @@ -172,3 +179,14 @@ func (t *magicMCPAuthTransport) RoundTrip(request *http.Request) (*http.Response return base.RoundTrip(cloned) } + +func isTrustedMCPOrigin(api, endpoint *url.URL) bool { + if endpoint.User != nil || !strings.EqualFold(api.Scheme, endpoint.Scheme) { + return false + } + if strings.EqualFold(api.Host, endpoint.Host) { + return true + } + return api.Scheme == "https" && endpoint.Host == "api.metorial.com" && + (api.Host == "api-us1.metorial.com" || api.Host == "api-eu1.metorial.com") +} diff --git a/internal/commands/consumer/member.go b/internal/commands/consumer/member.go new file mode 100644 index 0000000..0647391 --- /dev/null +++ b/internal/commands/consumer/member.go @@ -0,0 +1,208 @@ +package consumer + +import ( + "context" + "fmt" + "io" + "net/url" + "time" + + "github.com/metorial/cli/internal/browser" + "github.com/metorial/cli/internal/commandutil" + "github.com/metorial/cli/internal/consumerapi" + "github.com/metorial/cli/internal/output" + "github.com/metorial/cli/internal/terminal" + "github.com/spf13/cobra" +) + +// NewMemberCommand exposes the consumer setup and MCP transport using an admin CLI login. +func NewMemberCommand(ctx commandutil.Context) *cobra.Command { + env := commandEnv{ctx: ctx, member: true} + command := &cobra.Command{Use: "personal", Short: "Connect integrations as your member consumer"} + command.AddCommand( + newMemberSetupCommand(env), + newMemberSetupStatusCommand(env, false), + newMemberSetupStatusCommand(env, true), + newMemberFinishCommand(env), + newToolsCommand(env), + ) + return command +} + +func newMemberSetupCommand(env commandEnv) *cobra.Command { + var authMethod string + var noBrowser bool + command := &cobra.Command{ + Use: "setup ", + Short: "Open setup for a personal connection to an existing integration", + Args: cobra.ExactArgs(1), + RunE: func(command *cobra.Command, args []string) error { + if _, err := env.ctx.Structured(); err != nil { + return err + } + runtime, err := env.ctx.ResolveRuntime() + if err != nil { + return err + } + client, catalogItemID, err := consumerapi.NewMember(runtime, args[0]) + if err != nil { + return err + } + if catalogItemID == "" { + return fmt.Errorf("metorial: the integration has no personal catalog item") + } + item, err := client.GetProvider(catalogItemID) + if err != nil { + return err + } + if err := requireConfigurable(item); err != nil { + return err + } + session, err := startSetupSession(client, item, authMethod) + if err != nil { + return err + } + + resume := command.Parent().CommandPath() + " " + ids := commandutil.ShellQuote(item.ID) + " " + commandutil.ShellQuote(session.ID) + fmt.Fprintf(command.ErrOrStderr(), "Check with %sstatus %s or %swait %s. When ready, run %sfinish %s. Reuse the same profile and instance.\n", + resume, ids, resume, ids, resume, ids) + if err := writeMemberSetup(command, env, item.ID, session); err != nil { + return err + } + + link, err := url.Parse(session.URL) + if !noBrowser && session.Status == consumerapi.SetupStatusPending && err == nil && + link.Host != "" && link.User == nil && (link.Scheme == "https" || link.Scheme == "http") && browser.Supported() { + _ = browser.Open(session.URL) + } + return nil + }, + } + command.Flags().StringVar(&authMethod, "auth-method", "", "Auth method ID to use (defaults to OAuth when available)") + command.Flags().BoolVar(&noBrowser, "no-browser", false, "Print the setup link without opening a browser") + return command +} + +func newMemberSetupStatusCommand(env commandEnv, wait bool) *cobra.Command { + name := "status" + short := "Read a personal setup session" + if wait { + name = "wait" + short = "Wait for a personal setup session to finish" + } + var timeout time.Duration + command := &cobra.Command{ + Use: name + " ", + Short: short, + Args: cobra.ExactArgs(2), + RunE: func(command *cobra.Command, args []string) error { + if wait && timeout <= 0 { + return fmt.Errorf("metorial: --timeout must be positive") + } + client, err := env.client() + if err != nil { + return err + } + + ctx := commandContext(command) + if wait { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, timeout) + defer cancel() + } + session, err := getSetupWithContext(ctx, client, args[0], args[1]) + if err != nil { + return err + } + if wait && session.Status == consumerapi.SetupStatusPending { + session, err = waitForSetupSessionUntil(ctx, command.ErrOrStderr(), env.app().StderrFeatures(), client, args[0], session) + if err != nil { + return err + } + } + if wait && session.Status != consumerapi.SetupStatusSuccessful { + return fmt.Errorf("metorial: setup session %s is %s", session.ID, session.Status) + } + return writeMemberSetup(command, env, args[0], session) + }, + } + if wait { + command.Flags().DurationVar(&timeout, "timeout", time.Minute, "Maximum time to wait for authorization") + } + return command +} + +func writeMemberSetup(command *cobra.Command, env commandEnv, catalogItemID string, session *consumerapi.SetupSession) error { + return env.ctx.Write(command, map[string]any{ + "catalog_item_id": catalogItemID, + "setup_session": session, + }, func(writer io.Writer, features terminal.Features) error { + return output.RenderDataList(writer, []output.DataListItem{ + {Label: "Catalog item", Value: catalogItemID}, + {Label: "Setup session", Value: session.ID}, + {Label: "Status", Value: string(session.Status)}, + {Label: "Setup URL", Value: session.URL}, + }) + }) +} + +func newMemberFinishCommand(env commandEnv) *cobra.Command { + return &cobra.Command{ + Use: "finish ", + Short: "Deploy a completed personal integration and verify its tools", + Args: cobra.ExactArgs(2), + RunE: func(command *cobra.Command, args []string) error { + if _, err := env.ctx.Structured(); err != nil { + return err + } + client, err := env.client() + if err != nil { + return err + } + + session, err := client.GetSetup(args[0], args[1]) + if err != nil { + return err + } + if session.Status != consumerapi.SetupStatusSuccessful { + return fmt.Errorf("metorial: setup session %s is %s", session.ID, session.Status) + } + server, err := client.Deploy(args[0], consumerapi.DeployInput{IntegrationSetupSessionID: session.ID}) + if err != nil { + return err + } + + ctx := commandContext(command) + count, verifyErr := verifyMemberIntegration(ctx, client, server) + result := map[string]any{"integration": server, "verified": verifyErr == nil} + if verifyErr == nil { + result["tool_count"] = count + } + if err := env.ctx.Write(command, result, func(writer io.Writer, features terminal.Features) error { + commandutil.RenderTitle(writer, features, integrationTitle(server), server.ID) + if verifyErr == nil { + fmt.Fprintf(writer, "Verified %d tools.\n", count) + } + return renderServerEndpoints(writer, features, server) + }); err != nil { + return err + } + if verifyErr != nil { + return fmt.Errorf("metorial: server %s is ready but verification failed. Retry with %s tools %s: %w", + server.ID, command.Parent().CommandPath(), server.ID, verifyErr) + } + return nil + }, + } +} + +func verifyMemberIntegration(ctx context.Context, client *consumerapi.Client, server *consumerapi.MagicMcpServer) (int, error) { + mcpClient, err := connectMagicMCP(ctx, client, server) + if err != nil { + return 0, err + } + defer mcpClient.Close() + tools, err := mcpClient.ListTools(ctx) + return len(tools), err +} diff --git a/internal/commands/consumer/setup.go b/internal/commands/consumer/setup.go index 087371f..11f9ac6 100644 --- a/internal/commands/consumer/setup.go +++ b/internal/commands/consumer/setup.go @@ -1,6 +1,7 @@ package consumer import ( + "context" "fmt" "io" "strings" @@ -190,11 +191,7 @@ func resolveReconnectServer(client *consumerapi.Client, item *consumerapi.Provid } func runSetupSession(env commandEnv, command *cobra.Command, client *consumerapi.Client, item *consumerapi.Provider, authMethodID string) (*consumerapi.SetupSession, error) { - if strings.TrimSpace(authMethodID) == "" { - authMethodID = preferredAuthMethodID(item) - } - - session, err := client.StartSetup(item.ID, authMethodID) + session, err := startSetupSession(client, item, authMethodID) if err != nil { return nil, err } @@ -223,6 +220,13 @@ func runSetupSession(env commandEnv, command *cobra.Command, client *consumerapi return waitForSetupSession(writer, features, client, item.ID, session) } +func startSetupSession(client *consumerapi.Client, item *consumerapi.Provider, authMethodID string) (*consumerapi.SetupSession, error) { + if strings.TrimSpace(authMethodID) == "" { + authMethodID = preferredAuthMethodID(item) + } + return client.StartSetup(item.ID, authMethodID) +} + func preferredAuthMethodID(item *consumerapi.Provider) string { for _, method := range item.AuthMethods { if method.Type == "oauth" { @@ -264,6 +268,10 @@ func renderSetupPrompt(writer io.Writer, features terminal.Features, item *consu } func waitForSetupSession(writer io.Writer, features terminal.Features, client *consumerapi.Client, catalogItemID string, session *consumerapi.SetupSession) (*consumerapi.SetupSession, error) { + return waitForSetupSessionUntil(context.Background(), writer, features, client, catalogItemID, session) +} + +func waitForSetupSessionUntil(ctx context.Context, writer io.Writer, features terminal.Features, client *consumerapi.Client, catalogItemID string, session *consumerapi.SetupSession) (*consumerapi.SetupSession, error) { deadline := session.ExpiresAt if deadline.IsZero() { deadline = time.Now().Add(setupFallbackTimeout) @@ -274,7 +282,7 @@ func waitForSetupSession(writer io.Writer, features terminal.Features, client *c defer spinner.Stop() for { - current, err := client.GetSetup(catalogItemID, session.ID) + current, err := getSetupWithContext(ctx, client, catalogItemID, session.ID) if err != nil { return nil, err } @@ -292,7 +300,13 @@ func waitForSetupSession(writer io.Writer, features terminal.Features, client *c return nil, fmt.Errorf("metorial: setup session %s expired before it was completed", current.ID) } - time.Sleep(setupPollInterval) + timer := time.NewTimer(setupPollInterval) + select { + case <-ctx.Done(): + timer.Stop() + return nil, fmt.Errorf("metorial: stopped waiting for setup session %s: %w", session.ID, ctx.Err()) + case <-timer.C: + } } } @@ -322,3 +336,25 @@ func writeSetupResult(env commandEnv, command *cobra.Command, title string, item return commandutil.RenderTips(writer, features, tips) }) } + +// The existing REST client has its own request timeout. Bound the command's wait as well. +func getSetupWithContext(ctx context.Context, client *consumerapi.Client, catalogItemID, setupID string) (*consumerapi.SetupSession, error) { + if ctx.Done() == nil { + return client.GetSetup(catalogItemID, setupID) + } + type result struct { + session *consumerapi.SetupSession + err error + } + done := make(chan result, 1) + go func() { + session, err := client.GetSetup(catalogItemID, setupID) + done <- result{session: session, err: err} + }() + select { + case <-ctx.Done(): + return nil, fmt.Errorf("metorial: stopped waiting for setup session %s: %w", setupID, ctx.Err()) + case result := <-done: + return result.session, result.err + } +} diff --git a/internal/commands/consumer/tools.go b/internal/commands/consumer/tools.go index c31aaad..7e894da 100644 --- a/internal/commands/consumer/tools.go +++ b/internal/commands/consumer/tools.go @@ -46,7 +46,7 @@ func newToolsCommand(env commandEnv) *cobra.Command { } tips := []string{} - if len(liveTools) > 0 { + if len(liveTools) > 0 && !env.member { identifier := integrationIdentifier(server) toolKey := liveTools[0].Name tips = append(tips, @@ -160,7 +160,7 @@ func connectIntegration(ctx context.Context, env commandEnv, integrationID strin return nil, nil, err } - mcpClient, err := connectMagicMCP(ctx, client.AccessToken(), server) + mcpClient, err := connectMagicMCP(ctx, client, server) if err != nil { return nil, nil, err } diff --git a/internal/consumerapi/client.go b/internal/consumerapi/client.go index c3f544e..efaf524 100644 --- a/internal/consumerapi/client.go +++ b/internal/consumerapi/client.go @@ -14,6 +14,7 @@ import ( type Client struct { runtime config.Runtime + member bool } func New(runtime config.Runtime) (*Client, error) { @@ -24,6 +25,35 @@ func New(runtime config.Runtime) (*Client, error) { return &Client{runtime: runtime}, nil } +// NewMember exchanges the selected admin login for a disposable consumer credential. +func NewMember(runtime config.Runtime, integrationID string) (*Client, string, error) { + var session struct { + AccessToken string `json:"access_token"` + CatalogItemID string `json:"catalog_item_id"` + } + body := map[string]string{} + if integrationID != "" { + body["integration_id"] = integrationID + } + if err := (apiclient.Client{Runtime: runtime}).Do(http.MethodPost, "/member-consumer-session", body, &session); err != nil { + return nil, "", err + } + if strings.TrimSpace(session.AccessToken) == "" { + return nil, "", fmt.Errorf("metorial: member consumer session did not return a credential") + } + runtime.APIKey = session.AccessToken + runtime.InstanceID = "" + runtime.Refresh = nil + return &Client{runtime: runtime, member: true}, session.CatalogItemID, nil +} + +func (c *Client) MCPOrigin() *url.URL { + if c.member { + return c.runtime.APIHostURL + } + return nil +} + func (c *Client) AccessToken() string { return strings.TrimSpace(c.runtime.APIKey) } @@ -150,7 +180,11 @@ func (c *Client) DeleteMagicMcpToken(magicMcpTokenID string) (*MagicMcpToken, er } func (c *Client) do(method string, target string, body any, output any) error { - return apiclient.Client{Runtime: c.runtime, Describe: describeError}.Do(method, target, body, output) + client := apiclient.Client{Runtime: c.runtime} + if !c.member { + client.Describe = describeError + } + return client.Do(method, target, body, output) } func describeError(target string, err error) error {