Skip to content
Open
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
13 changes: 13 additions & 0 deletions docs/feature-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ as output formatting) won't appear here.
- `pullNumber`: The pull request number (number, required)
- `repo`: Repository name (string, required)

- **get_pull_request_agent_merge_enrollment** - Get Pull Request Agent Merge Enrollment
- **OAuth Challenge Scopes**: `repo`
- `owner`: Repository owner (username or organization) (string, required)
- `pullNumber`: The pull request number (integer, required)
- `repo`: Repository name (string, required)

- **hide_pull_request_review** - Hide Pull Request Review
- **OAuth Challenge Scopes**: `repo`
- `classifier`: The reason for hiding the comment (string, required)
Expand Down Expand Up @@ -490,6 +496,13 @@ as output formatting) won't appear here.
- **OAuth Challenge Scopes**: `repo`
- `threadID`: The node ID of the review thread to unresolve (e.g., PRRT_kwDOxxx) (string, required)

- **update_pull_request_agent_merge_enrollment** - Update Pull Request Agent Merge Enrollment
- **OAuth Challenge Scopes**: `repo`
- `allowedActions`: Actions Copilot may perform: fix_ci and/or resolve_merge_conflicts. Replaces the existing action selection. An empty array disables Agent Merge. (string[], required)
- `owner`: Repository owner (username or organization) (string, required)
- `pullNumber`: The pull request number (integer, required)
- `repo`: Repository name (string, required)

- **update_pull_request_body** - Update Pull Request Body
- **OAuth Challenge Scopes**: `repo`
- `body`: The new body content for the pull request (string, required)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"annotations": {
"idempotentHint": false,
"openWorldHint": true,
"readOnlyHint": true,
"title": "Get Pull Request Agent Merge Enrollment"
},
"description": "Get the enabled Agent Merge configuration and allowed actions for a pull request. Requires repository write access. A 404 can mean enrollment is disabled or absent, the pull request is missing, or access is masked; it does not conclusively establish that Agent Merge is disabled.",
"inputSchema": {
"properties": {
"owner": {
"description": "Repository owner (username or organization)",
"minLength": 1,
"type": "string"
},
"pullNumber": {
"description": "The pull request number",
"minimum": 1,
"type": "integer"
},
"repo": {
"description": "Repository name",
"minLength": 1,
"type": "string"
}
},
"required": [
"owner",
"repo",
"pullNumber"
],
"type": "object"
},
"name": "get_pull_request_agent_merge_enrollment"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"annotations": {
"destructiveHint": true,
"idempotentHint": true,
"openWorldHint": true,
"readOnlyHint": false,
"title": "Update Pull Request Agent Merge Enrollment"
},
"description": "Enable or update Agent Merge for a pull request by specifying allowedActions, or disable it with an empty array. Nonempty allowedActions replaces the permitted actions and authorizes Copilot to act asynchronously, not to merge immediately. Enabling requires a paid Copilot subscription, repository write access, Allow automations enabled, and a pull request that is not from a fork or part of a stack. Only the user who most recently enabled Agent Merge may update its allowed actions. Disabling requires repository write access but not enrollment eligibility.",
"inputSchema": {
"properties": {
"allowedActions": {
"description": "Actions Copilot may perform: fix_ci and/or resolve_merge_conflicts. Replaces the existing action selection. An empty array disables Agent Merge.",
"items": {
"enum": [
"fix_ci",
"resolve_merge_conflicts"
],
"type": "string"
},
"type": "array",
"uniqueItems": true
},
"owner": {
"description": "Repository owner (username or organization)",
"minLength": 1,
"type": "string"
},
"pullNumber": {
"description": "The pull request number",
"minimum": 1,
"type": "integer"
},
"repo": {
"description": "Repository name",
"minLength": 1,
"type": "string"
}
},
"required": [
"owner",
"repo",
"pullNumber",
"allowedActions"
],
"type": "object"
},
"name": "update_pull_request_agent_merge_enrollment"
}
4 changes: 4 additions & 0 deletions pkg/github/granular_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestGranularToolSnaps(t *testing.T) {
GranularUnhidePullRequestReviewComment,
GranularHidePullRequestReview,
GranularUnhidePullRequestReview,
GranularGetPullRequestAgentMergeEnrollment,
GranularUpdatePullRequestAgentMergeEnrollment,
}

for _, constructor := range toolConstructors {
Expand Down Expand Up @@ -133,6 +135,8 @@ func TestPullRequestsGranularToolset(t *testing.T) {
"update_pull_request_body",
"update_pull_request_state",
"update_pull_request_draft_state",
"get_pull_request_agent_merge_enrollment",
"update_pull_request_agent_merge_enrollment",
"request_pull_request_reviewers",
"create_pull_request_review",
"submit_pending_pull_request_review",
Expand Down
214 changes: 214 additions & 0 deletions pkg/github/pull_request_agent_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

ghErrors "github.com/github/github-mcp-server/v2/pkg/errors"
"github.com/github/github-mcp-server/v2/pkg/ifc"
"github.com/github/github-mcp-server/v2/pkg/inventory"
"github.com/github/github-mcp-server/v2/pkg/scopes"
"github.com/github/github-mcp-server/v2/pkg/translations"
"github.com/github/github-mcp-server/v2/pkg/utils"
gogithub "github.com/google/go-github/v92/github"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
)

type PullRequestAgentMergeAction string

func (PullRequestAgentMergeAction) JSONSchema() *jsonschema.Schema {
return granularPullRequestEnumSchema([]string{"fix_ci", "resolve_merge_conflicts"})
}

type UpdatePullRequestAgentMergeEnrollmentInput struct {
GranularPullRequestCoordinate
AllowedActions []PullRequestAgentMergeAction `json:"allowedActions"`
}

type PullRequestAgentMergeEnrollment struct {
AllowedActions []string `json:"allowed_actions"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
User PullRequestAgentMergeUser `json:"user"`
}

type PullRequestAgentMergeUser struct {
ID int64 `json:"id"`
}

type UpdatePullRequestAgentMergeEnrollmentOutput struct {
Enabled bool `json:"enabled"`
Enrollment *PullRequestAgentMergeEnrollment `json:"enrollment,omitempty"`
}

func agentMergePath(coordinate GranularPullRequestCoordinate) string {
return fmt.Sprintf("agents/repos/%s/%s/pulls/%d/agent-merge", url.PathEscape(coordinate.Owner), url.PathEscape(coordinate.Repo), coordinate.PullNumber)
}

func validAgentMergeCoordinate(coordinate GranularPullRequestCoordinate) bool {
return strings.TrimSpace(coordinate.Owner) != "" && strings.TrimSpace(coordinate.Repo) != "" && coordinate.PullNumber > 0
}

// GranularGetPullRequestAgentMergeEnrollment creates a tool to read Agent Merge enrollment.
func GranularGetPullRequestAgentMergeEnrollment(t translations.TranslationHelperFunc) inventory.ServerTool {
tool := NewTool[GranularPullRequestCoordinate, *UpdatePullRequestAgentMergeEnrollmentOutput](
ToolsetMetadataPullRequests,
mcp.Tool{
Name: "get_pull_request_agent_merge_enrollment",
Description: t("TOOL_GET_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_DESCRIPTION",
"Get the enabled Agent Merge configuration and allowed actions for a pull request. Requires repository write access. A 404 can mean enrollment is disabled or absent, the pull request is missing, or access is masked; it does not conclusively establish that Agent Merge is disabled."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_GET_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_USER_TITLE", "Get Pull Request Agent Merge Enrollment"),
ReadOnlyHint: true,
OpenWorldHint: new(true),
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {Type: "string", MinLength: new(1), Description: "Repository owner (username or organization)"},
"repo": {Type: "string", MinLength: new(1), Description: "Repository name"},
"pullNumber": {Type: "integer", Minimum: new(1.0), Description: "The pull request number"},
},
Required: []string{"owner", "repo", "pullNumber"},
},
},
scopes.RequireAll(scopes.Repo),
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input GranularPullRequestCoordinate) (*mcp.CallToolResult, *UpdatePullRequestAgentMergeEnrollmentOutput, error) {
if !validAgentMergeCoordinate(input) {
return utils.NewToolResultError("owner and repo must be nonempty and pullNumber must be a positive integer"), nil, nil
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
request, err := client.NewRequest(ctx, http.MethodGet, agentMergePath(input), nil, gogithub.WithVersion("2026-03-10"))
if err != nil {
return utils.NewToolResultErrorFromErr("failed to create Agent Merge request", err), nil, nil
}
var enrollment *PullRequestAgentMergeEnrollment
resp, err := client.Do(request, &enrollment)
if resp != nil {
defer func() { _ = resp.Body.Close() }()
}
if err != nil {
message := "failed to get pull request Agent Merge enrollment"
if resp != nil && resp.StatusCode == http.StatusNotFound {
message += " (404 may mean disabled or absent enrollment, a missing pull request, or masked access)"
}
return ghErrors.NewGitHubAPIErrorResponse(ctx, message, resp, err), nil, nil
}
if resp == nil || resp.StatusCode != http.StatusOK || enrollment == nil || enrollment.AllowedActions == nil ||
enrollment.CreatedAt == "" || enrollment.UpdatedAt == "" || enrollment.User.ID < 1 {
return utils.NewToolResultError("unexpected or incomplete response getting Agent Merge enrollment"), nil, nil
}
output := &UpdatePullRequestAgentMergeEnrollmentOutput{Enabled: true, Enrollment: enrollment}
text, err := json.Marshal(output)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal Agent Merge enrollment", err), nil, nil
}
result := attachStaticIFCLabel(ctx, deps, utils.NewToolResultText(string(text)), ifc.PrivateTrusted())
return result, output, nil
},
)
tool.FeatureRule = pullRequestsGranularFeatureRule
return tool
}

// GranularUpdatePullRequestAgentMergeEnrollment creates a tool to enable, update, or disable Agent Merge.
func GranularUpdatePullRequestAgentMergeEnrollment(t translations.TranslationHelperFunc) inventory.ServerTool {
tool := NewTool[UpdatePullRequestAgentMergeEnrollmentInput, *UpdatePullRequestAgentMergeEnrollmentOutput](
ToolsetMetadataPullRequests,
mcp.Tool{
Name: "update_pull_request_agent_merge_enrollment",
Description: t("TOOL_UPDATE_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_DESCRIPTION",
"Enable or update Agent Merge for a pull request by specifying allowedActions, or disable it with an empty array. Nonempty allowedActions replaces the permitted actions and authorizes Copilot to act asynchronously, not to merge immediately. Enabling requires a paid Copilot subscription, repository write access, Allow automations enabled, and a pull request that is not from a fork or part of a stack. Only the user who most recently enabled Agent Merge may update its allowed actions. Disabling requires repository write access but not enrollment eligibility."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_UPDATE_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_USER_TITLE", "Update Pull Request Agent Merge Enrollment"),
ReadOnlyHint: false,
DestructiveHint: new(true),
IdempotentHint: true,
OpenWorldHint: new(true),
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {Type: "string", MinLength: new(1), Description: "Repository owner (username or organization)"},
"repo": {Type: "string", MinLength: new(1), Description: "Repository name"},
"pullNumber": {Type: "integer", Minimum: new(1.0), Description: "The pull request number"},
"allowedActions": {
Type: "array", UniqueItems: true,
Items: PullRequestAgentMergeAction("").JSONSchema(),
Description: "Actions Copilot may perform: fix_ci and/or resolve_merge_conflicts. Replaces the existing action selection. An empty array disables Agent Merge.",
},
},
Required: []string{"owner", "repo", "pullNumber", "allowedActions"},
},
},
scopes.RequireAll(scopes.Repo),
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input UpdatePullRequestAgentMergeEnrollmentInput) (*mcp.CallToolResult, *UpdatePullRequestAgentMergeEnrollmentOutput, error) {
if !validAgentMergeCoordinate(input.GranularPullRequestCoordinate) {
return utils.NewToolResultError("owner and repo must be nonempty and pullNumber must be a positive integer"), nil, nil
}
if input.AllowedActions == nil {
return utils.NewToolResultError("allowedActions must be an array; use [] to disable Agent Merge"), nil, nil
}
seen := make(map[PullRequestAgentMergeAction]bool, len(input.AllowedActions))
for _, action := range input.AllowedActions {
if action != "fix_ci" && action != "resolve_merge_conflicts" {
return utils.NewToolResultError(fmt.Sprintf("unsupported Agent Merge action: %q", action)), nil, nil
}
if seen[action] {
return utils.NewToolResultError(fmt.Sprintf("duplicate Agent Merge action: %q", action)), nil, nil
}
seen[action] = true
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
method := http.MethodPut
var body any = struct {
AllowedActions []PullRequestAgentMergeAction `json:"allowed_actions"`
}{input.AllowedActions}
output := &UpdatePullRequestAgentMergeEnrollmentOutput{Enabled: len(input.AllowedActions) > 0}
if !output.Enabled {
method, body = http.MethodDelete, nil
}
request, err := client.NewRequest(ctx, method, agentMergePath(input.GranularPullRequestCoordinate), body, gogithub.WithVersion("2026-03-10"))
if err != nil {
return utils.NewToolResultErrorFromErr("failed to create Agent Merge request", err), nil, nil
}
var target any
if output.Enabled {
target = &output.Enrollment
}
resp, err := client.Do(request, target)
if resp != nil {
defer func() { _ = resp.Body.Close() }()
}
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to update pull request Agent Merge enrollment", resp, err), nil, nil
}
if resp == nil || (output.Enabled && resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated) ||
(!output.Enabled && resp.StatusCode != http.StatusNoContent) {
return utils.NewToolResultError("unexpected response updating Agent Merge enrollment; verify its status before retrying"), nil, nil
}
if output.Enabled && (output.Enrollment == nil || len(output.Enrollment.AllowedActions) == 0 ||
output.Enrollment.CreatedAt == "" || output.Enrollment.UpdatedAt == "" || output.Enrollment.User.ID < 1) {
return utils.NewToolResultError("incomplete response updating Agent Merge enrollment; the operation may have succeeded. Verify its status before retrying"), nil, nil
}
text, err := json.Marshal(output)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal Agent Merge enrollment", err), nil, nil
}
return utils.NewToolResultText(string(text)), output, nil
},
)
tool.FeatureRule = pullRequestsGranularFeatureRule
return tool
}
Loading