// SPDX-License-Identifier: EUPL-1.2 package agentic import ( "context" "os/exec" core "dappco.re/go/core" "dappco.re/go/core/forge" forge_types "dappco.re/go/core/forge/types" "github.com/modelcontextprotocol/go-sdk/mcp" ) // --- agentic_create_pr --- // CreatePRInput is the input for agentic_create_pr. // // input := agentic.CreatePRInput{Workspace: "go-io-1773581873", Title: "Fix watcher panic"} type CreatePRInput struct { Workspace string `json:"workspace"` // workspace name (e.g. "mcp-1773581873") Title string `json:"title,omitempty"` // PR title (default: task description) Body string `json:"body,omitempty"` // PR body (default: auto-generated) Base string `json:"base,omitempty"` // base branch (default: "main") DryRun bool `json:"dry_run,omitempty"` // preview without creating } // CreatePROutput is the output for agentic_create_pr. // // out := agentic.CreatePROutput{Success: true, PRURL: "https://forge.example/core/go-io/pulls/12", PRNum: 12} type CreatePROutput struct { Success bool `json:"success"` PRURL string `json:"pr_url,omitempty"` PRNum int `json:"pr_number,omitempty"` Title string `json:"title"` Branch string `json:"branch"` Repo string `json:"repo"` Pushed bool `json:"pushed"` } func (s *PrepSubsystem) registerCreatePRTool(server *mcp.Server) { mcp.AddTool(server, &mcp.Tool{ Name: "agentic_create_pr", Description: "Create a pull request from an agent workspace. Pushes the branch to Forge and opens a PR. Links to the source issue if one was tracked.", }, s.createPR) } func (s *PrepSubsystem) createPR(ctx context.Context, _ *mcp.CallToolRequest, input CreatePRInput) (*mcp.CallToolResult, CreatePROutput, error) { if input.Workspace == "" { return nil, CreatePROutput{}, core.E("createPR", "workspace is required", nil) } if s.forgeToken == "" { return nil, CreatePROutput{}, core.E("createPR", "no Forge token configured", nil) } wsDir := core.JoinPath(WorkspaceRoot(), input.Workspace) repoDir := core.JoinPath(wsDir, "repo") if !fs.IsDir(core.JoinPath(repoDir, ".git")) { return nil, CreatePROutput{}, core.E("createPR", "workspace not found: "+input.Workspace, nil) } // Read workspace status for repo, branch, issue context st, err := ReadStatus(wsDir) if err != nil { return nil, CreatePROutput{}, core.E("createPR", "no status.json", err) } if st.Branch == "" { // Detect branch from git branchCmd := exec.CommandContext(ctx, "git", "rev-parse", "--abbrev-ref", "HEAD") branchCmd.Dir = repoDir out, err := branchCmd.Output() if err != nil { return nil, CreatePROutput{}, core.E("createPR", "failed to detect branch", err) } st.Branch = core.Trim(string(out)) } org := st.Org if org == "" { org = "core" } base := input.Base if base == "" { base = "dev" } // Build PR title title := input.Title if title == "" { title = st.Task } if title == "" { title = core.Sprintf("Agent work on %s", st.Branch) } // Build PR body body := input.Body if body == "" { body = s.buildPRBody(st) } if input.DryRun { return nil, CreatePROutput{ Success: true, Title: title, Branch: st.Branch, Repo: st.Repo, }, nil } // Push branch to Forge (origin is the local clone, not Forge) forgeRemote := core.Sprintf("ssh://git@forge.lthn.ai:2223/%s/%s.git", org, st.Repo) pushCmd := exec.CommandContext(ctx, "git", "push", forgeRemote, st.Branch) pushCmd.Dir = repoDir pushOut, err := pushCmd.CombinedOutput() if err != nil { return nil, CreatePROutput{}, core.E("createPR", "git push failed: "+string(pushOut), err) } // Create PR via Forge API prURL, prNum, err := s.forgeCreatePR(ctx, org, st.Repo, st.Branch, base, title, body) if err != nil { return nil, CreatePROutput{}, core.E("createPR", "failed to create PR", err) } // Update status with PR URL st.PRURL = prURL writeStatus(wsDir, st) // Comment on issue if tracked if st.Issue > 0 { comment := core.Sprintf("Pull request created: %s", prURL) s.commentOnIssue(ctx, org, st.Repo, st.Issue, comment) } return nil, CreatePROutput{ Success: true, PRURL: prURL, PRNum: prNum, Title: title, Branch: st.Branch, Repo: st.Repo, Pushed: true, }, nil } func (s *PrepSubsystem) buildPRBody(st *WorkspaceStatus) string { b := core.NewBuilder() b.WriteString("## Summary\n\n") if st.Task != "" { b.WriteString(st.Task) b.WriteString("\n\n") } if st.Issue > 0 { b.WriteString(core.Sprintf("Closes #%d\n\n", st.Issue)) } b.WriteString(core.Sprintf("**Agent:** %s\n", st.Agent)) b.WriteString(core.Sprintf("**Runs:** %d\n", st.Runs)) b.WriteString("\n---\n*Created by agentic dispatch*\n") return b.String() } func (s *PrepSubsystem) forgeCreatePR(ctx context.Context, org, repo, head, base, title, body string) (string, int, error) { pr, err := s.forge.Pulls.Create(ctx, forge.Params{"owner": org, "repo": repo}, &forge_types.CreatePullRequestOption{ Title: title, Body: body, Head: head, Base: base, }) if err != nil { return "", 0, core.E("forgeCreatePR", "create PR failed", err) } return pr.HTMLURL, int(pr.Index), nil } func (s *PrepSubsystem) commentOnIssue(ctx context.Context, org, repo string, issue int, comment string) { s.forge.Issues.CreateComment(ctx, org, repo, int64(issue), comment) } // --- agentic_list_prs --- // ListPRsInput is the input for agentic_list_prs. // // input := agentic.ListPRsInput{Org: "core", Repo: "go-io", State: "open", Limit: 10} type ListPRsInput struct { Org string `json:"org,omitempty"` // forge org (default "core") Repo string `json:"repo,omitempty"` // specific repo, or empty for all State string `json:"state,omitempty"` // "open" (default), "closed", "all" Limit int `json:"limit,omitempty"` // max results (default 20) } // ListPRsOutput is the output for agentic_list_prs. // // out := agentic.ListPRsOutput{Success: true, Count: 2, PRs: []agentic.PRInfo{{Repo: "go-io", Number: 12}}} type ListPRsOutput struct { Success bool `json:"success"` Count int `json:"count"` PRs []PRInfo `json:"prs"` } // PRInfo represents a pull request. // // pr := agentic.PRInfo{Repo: "go-io", Number: 12, Title: "Migrate pkg/fs", Branch: "agent/migrate-fs"} type PRInfo struct { Repo string `json:"repo"` Number int `json:"number"` Title string `json:"title"` State string `json:"state"` Author string `json:"author"` Branch string `json:"branch"` Base string `json:"base"` Labels []string `json:"labels,omitempty"` Mergeable bool `json:"mergeable"` URL string `json:"url"` } func (s *PrepSubsystem) registerListPRsTool(server *mcp.Server) { mcp.AddTool(server, &mcp.Tool{ Name: "agentic_list_prs", Description: "List pull requests across Forge repos. Filter by org, repo, and state (open/closed/all).", }, s.listPRs) } func (s *PrepSubsystem) listPRs(ctx context.Context, _ *mcp.CallToolRequest, input ListPRsInput) (*mcp.CallToolResult, ListPRsOutput, error) { if s.forgeToken == "" { return nil, ListPRsOutput{}, core.E("listPRs", "no Forge token configured", nil) } if input.Org == "" { input.Org = "core" } if input.State == "" { input.State = "open" } if input.Limit == 0 { input.Limit = 20 } var repos []string if input.Repo != "" { repos = []string{input.Repo} } else { var err error repos, err = s.listOrgRepos(ctx, input.Org) if err != nil { return nil, ListPRsOutput{}, err } } var allPRs []PRInfo for _, repo := range repos { prs, err := s.listRepoPRs(ctx, input.Org, repo, input.State) if err != nil { continue } allPRs = append(allPRs, prs...) if len(allPRs) >= input.Limit { break } } if len(allPRs) > input.Limit { allPRs = allPRs[:input.Limit] } return nil, ListPRsOutput{ Success: true, Count: len(allPRs), PRs: allPRs, }, nil } func (s *PrepSubsystem) listRepoPRs(ctx context.Context, org, repo, state string) ([]PRInfo, error) { prs, err := s.forge.Pulls.ListAll(ctx, forge.Params{"owner": org, "repo": repo}) if err != nil { return nil, core.E("listRepoPRs", "failed to list PRs for "+repo, err) } var result []PRInfo for _, pr := range prs { if state != "" && state != "all" && string(pr.State) != state { continue } var labels []string for _, l := range pr.Labels { labels = append(labels, l.Name) } author := "" if pr.User != nil { author = pr.User.UserName } result = append(result, PRInfo{ Repo: repo, Number: int(pr.Index), Title: pr.Title, State: string(pr.State), Author: author, Branch: pr.Head.Ref, Base: pr.Base.Ref, Labels: labels, Mergeable: pr.Mergeable, URL: pr.HTMLURL, }) } return result, nil }