Skip to content

Commit fcd7537

Browse files
timrogersCopilot
andauthored
Add read tool for Agent Merge enrollment
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 4fa8a3b commit fcd7537

8 files changed

Lines changed: 212 additions & 8 deletions

File tree

‎docs/feature-flags.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ as output formatting) won't appear here.
431431
- `pullNumber`: The pull request number (number, required)
432432
- `repo`: Repository name (string, required)
433433

434+
- **get_pull_request_agent_merge_enrollment** - Get Pull Request Agent Merge Enrollment
435+
- **OAuth Challenge Scopes**: `repo`
436+
- `owner`: Repository owner (username or organization) (string, required)
437+
- `pullNumber`: The pull request number (integer, required)
438+
- `repo`: Repository name (string, required)
439+
434440
- **hide_pull_request_review** - Hide Pull Request Review
435441
- **OAuth Challenge Scopes**: `repo`
436442
- `classifier`: The reason for hiding the comment (string, required)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"annotations": {
3+
"idempotentHint": false,
4+
"openWorldHint": true,
5+
"readOnlyHint": true,
6+
"title": "Get Pull Request Agent Merge Enrollment"
7+
},
8+
"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.",
9+
"inputSchema": {
10+
"properties": {
11+
"owner": {
12+
"description": "Repository owner (username or organization)",
13+
"minLength": 1,
14+
"type": "string"
15+
},
16+
"pullNumber": {
17+
"description": "The pull request number",
18+
"minimum": 1,
19+
"type": "integer"
20+
},
21+
"repo": {
22+
"description": "Repository name",
23+
"minLength": 1,
24+
"type": "string"
25+
}
26+
},
27+
"required": [
28+
"owner",
29+
"repo",
30+
"pullNumber"
31+
],
32+
"type": "object"
33+
},
34+
"name": "get_pull_request_agent_merge_enrollment"
35+
}

‎pkg/github/granular_tools_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func TestGranularToolSnaps(t *testing.T) {
6464
GranularUnhidePullRequestReviewComment,
6565
GranularHidePullRequestReview,
6666
GranularUnhidePullRequestReview,
67+
GranularGetPullRequestAgentMergeEnrollment,
6768
GranularUpdatePullRequestAgentMergeEnrollment,
6869
}
6970

@@ -134,6 +135,7 @@ func TestPullRequestsGranularToolset(t *testing.T) {
134135
"update_pull_request_body",
135136
"update_pull_request_state",
136137
"update_pull_request_draft_state",
138+
"get_pull_request_agent_merge_enrollment",
137139
"update_pull_request_agent_merge_enrollment",
138140
"request_pull_request_reviewers",
139141
"create_pull_request_review",

‎pkg/github/pull_request_agent_merge.go‎

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
ghErrors "github.com/github/github-mcp-server/v2/pkg/errors"
12+
"github.com/github/github-mcp-server/v2/pkg/ifc"
1213
"github.com/github/github-mcp-server/v2/pkg/inventory"
1314
"github.com/github/github-mcp-server/v2/pkg/scopes"
1415
"github.com/github/github-mcp-server/v2/pkg/translations"
@@ -45,6 +46,79 @@ type UpdatePullRequestAgentMergeEnrollmentOutput struct {
4546
Enrollment *PullRequestAgentMergeEnrollment `json:"enrollment,omitempty"`
4647
}
4748

49+
func agentMergePath(coordinate GranularPullRequestCoordinate) string {
50+
return fmt.Sprintf("agents/repos/%s/%s/pulls/%d/agent-merge", url.PathEscape(coordinate.Owner), url.PathEscape(coordinate.Repo), coordinate.PullNumber)
51+
}
52+
53+
func validAgentMergeCoordinate(coordinate GranularPullRequestCoordinate) bool {
54+
return strings.TrimSpace(coordinate.Owner) != "" && strings.TrimSpace(coordinate.Repo) != "" && coordinate.PullNumber > 0
55+
}
56+
57+
// GranularGetPullRequestAgentMergeEnrollment creates a tool to read Agent Merge enrollment.
58+
func GranularGetPullRequestAgentMergeEnrollment(t translations.TranslationHelperFunc) inventory.ServerTool {
59+
tool := NewTool[GranularPullRequestCoordinate, *UpdatePullRequestAgentMergeEnrollmentOutput](
60+
ToolsetMetadataPullRequests,
61+
mcp.Tool{
62+
Name: "get_pull_request_agent_merge_enrollment",
63+
Description: t("TOOL_GET_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_DESCRIPTION",
64+
"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."),
65+
Annotations: &mcp.ToolAnnotations{
66+
Title: t("TOOL_GET_PULL_REQUEST_AGENT_MERGE_ENROLLMENT_USER_TITLE", "Get Pull Request Agent Merge Enrollment"),
67+
ReadOnlyHint: true,
68+
OpenWorldHint: new(true),
69+
},
70+
InputSchema: &jsonschema.Schema{
71+
Type: "object",
72+
Properties: map[string]*jsonschema.Schema{
73+
"owner": {Type: "string", MinLength: new(1), Description: "Repository owner (username or organization)"},
74+
"repo": {Type: "string", MinLength: new(1), Description: "Repository name"},
75+
"pullNumber": {Type: "integer", Minimum: new(1.0), Description: "The pull request number"},
76+
},
77+
Required: []string{"owner", "repo", "pullNumber"},
78+
},
79+
},
80+
scopes.RequireAll(scopes.Repo),
81+
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input GranularPullRequestCoordinate) (*mcp.CallToolResult, *UpdatePullRequestAgentMergeEnrollmentOutput, error) {
82+
if !validAgentMergeCoordinate(input) {
83+
return utils.NewToolResultError("owner and repo must be nonempty and pullNumber must be a positive integer"), nil, nil
84+
}
85+
client, err := deps.GetClient(ctx)
86+
if err != nil {
87+
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
88+
}
89+
request, err := client.NewRequest(ctx, http.MethodGet, agentMergePath(input), nil, gogithub.WithVersion("2026-03-10"))
90+
if err != nil {
91+
return utils.NewToolResultErrorFromErr("failed to create Agent Merge request", err), nil, nil
92+
}
93+
var enrollment *PullRequestAgentMergeEnrollment
94+
resp, err := client.Do(request, &enrollment)
95+
if resp != nil {
96+
defer func() { _ = resp.Body.Close() }()
97+
}
98+
if err != nil {
99+
message := "failed to get pull request Agent Merge enrollment"
100+
if resp != nil && resp.StatusCode == http.StatusNotFound {
101+
message += " (404 may mean disabled or absent enrollment, a missing pull request, or masked access)"
102+
}
103+
return ghErrors.NewGitHubAPIErrorResponse(ctx, message, resp, err), nil, nil
104+
}
105+
if resp == nil || resp.StatusCode != http.StatusOK || enrollment == nil || enrollment.AllowedActions == nil ||
106+
enrollment.CreatedAt == "" || enrollment.UpdatedAt == "" || enrollment.User.ID < 1 {
107+
return utils.NewToolResultError("unexpected or incomplete response getting Agent Merge enrollment"), nil, nil
108+
}
109+
output := &UpdatePullRequestAgentMergeEnrollmentOutput{Enabled: true, Enrollment: enrollment}
110+
text, err := json.Marshal(output)
111+
if err != nil {
112+
return utils.NewToolResultErrorFromErr("failed to marshal Agent Merge enrollment", err), nil, nil
113+
}
114+
result := attachStaticIFCLabel(ctx, deps, utils.NewToolResultText(string(text)), ifc.PrivateTrusted())
115+
return result, output, nil
116+
},
117+
)
118+
tool.FeatureRule = pullRequestsGranularFeatureRule
119+
return tool
120+
}
121+
48122
// GranularUpdatePullRequestAgentMergeEnrollment creates a tool to enable, update, or disable Agent Merge.
49123
func GranularUpdatePullRequestAgentMergeEnrollment(t translations.TranslationHelperFunc) inventory.ServerTool {
50124
tool := NewTool[UpdatePullRequestAgentMergeEnrollmentInput, *UpdatePullRequestAgentMergeEnrollmentOutput](
@@ -77,7 +151,7 @@ func GranularUpdatePullRequestAgentMergeEnrollment(t translations.TranslationHel
77151
},
78152
scopes.RequireAll(scopes.Repo),
79153
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input UpdatePullRequestAgentMergeEnrollmentInput) (*mcp.CallToolResult, *UpdatePullRequestAgentMergeEnrollmentOutput, error) {
80-
if strings.TrimSpace(input.Owner) == "" || strings.TrimSpace(input.Repo) == "" || input.PullNumber < 1 {
154+
if !validAgentMergeCoordinate(input.GranularPullRequestCoordinate) {
81155
return utils.NewToolResultError("owner and repo must be nonempty and pullNumber must be a positive integer"), nil, nil
82156
}
83157
if input.AllowedActions == nil {
@@ -105,8 +179,7 @@ func GranularUpdatePullRequestAgentMergeEnrollment(t translations.TranslationHel
105179
if !output.Enabled {
106180
method, body = http.MethodDelete, nil
107181
}
108-
path := fmt.Sprintf("agents/repos/%s/%s/pulls/%d/agent-merge", url.PathEscape(input.Owner), url.PathEscape(input.Repo), input.PullNumber)
109-
request, err := client.NewRequest(ctx, method, path, body, gogithub.WithVersion("2026-03-10"))
182+
request, err := client.NewRequest(ctx, method, agentMergePath(input.GranularPullRequestCoordinate), body, gogithub.WithVersion("2026-03-10"))
110183
if err != nil {
111184
return utils.NewToolResultErrorFromErr("failed to create Agent Merge request", err), nil, nil
112185
}

‎pkg/github/pull_request_agent_merge_test.go‎

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
"github.com/github/github-mcp-server/v2/pkg/ifc"
1011
"github.com/github/github-mcp-server/v2/pkg/inventory"
1112
"github.com/github/github-mcp-server/v2/pkg/translations"
1213
"github.com/modelcontextprotocol/go-sdk/mcp"
@@ -16,6 +17,88 @@ import (
1617

1718
const agentMergeEnrollmentResponse = `{"allowed_actions":["fix_ci"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}`
1819

20+
func TestGetPullRequestAgentMergeEnrollment(t *testing.T) {
21+
for _, protocol := range []string{"2025-11-25", inventory.ProtocolVersionMultiRoundTrip} {
22+
for _, tc := range []struct {
23+
name, response string
24+
status int
25+
isError bool
26+
}{
27+
{"enabled", agentMergeEnrollmentResponse, http.StatusOK, false},
28+
{"future action", `{"allowed_actions":["respond_to_copilot_code_reviews"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}`, http.StatusOK, false},
29+
{"not found", `{"message":"Not Found"}`, http.StatusNotFound, true},
30+
{"forbidden", `{"message":"Forbidden"}`, http.StatusForbidden, true},
31+
{"unavailable", `{"message":"Unavailable"}`, http.StatusServiceUnavailable, true},
32+
{"unexpected status", "", http.StatusNoContent, true},
33+
{"incomplete", `{}`, http.StatusOK, true},
34+
{"malformed", `{`, http.StatusOK, true},
35+
} {
36+
t.Run(protocol+"/"+tc.name, func(t *testing.T) {
37+
calls := 0
38+
deps := &BaseDeps{
39+
Client: mustNewGHClient(t, MockHTTPClientWithHandler(func(w http.ResponseWriter, r *http.Request) {
40+
calls++
41+
assert.Equal(t, "GET /agents/repos/octo/widgets/pulls/42/agent-merge", r.Method+" "+r.URL.Path)
42+
assert.Equal(t, "2026-03-10", r.Header.Get("X-GitHub-Api-Version"))
43+
w.WriteHeader(tc.status)
44+
_, _ = fmt.Fprint(w, tc.response)
45+
})),
46+
featureChecker: featureCheckerFor(FeatureFlagIFCLabels),
47+
}
48+
server := mcp.NewServer(&mcp.Implementation{Name: "agent-merge-read-test", Version: "1"}, nil)
49+
server.AddReceivingMiddleware(InjectDepsMiddleware(deps))
50+
tool := GranularGetPullRequestAgentMergeEnrollment(translations.NullTranslationHelper)
51+
tool.RegisterFunc(server, deps)
52+
session := connectCommentVisibilityClient(t, server, protocol)
53+
result, err := session.CallTool(t.Context(), &mcp.CallToolParams{
54+
Name: tool.Tool.Name, Arguments: json.RawMessage(`{"owner":"octo","repo":"widgets","pullNumber":42}`),
55+
})
56+
require.NoError(t, err)
57+
assert.Equal(t, 1, calls)
58+
require.Equal(t, tc.isError, result.IsError, "%+v", result.Content)
59+
if tc.isError {
60+
if tc.status == http.StatusNotFound {
61+
assert.Contains(t, getTextResult(t, result).Text, "404 may mean")
62+
}
63+
return
64+
}
65+
expected := `{"enabled":true,"enrollment":` + tc.response + `}`
66+
assert.JSONEq(t, expected, getTextResult(t, result).Text)
67+
if protocol == inventory.ProtocolVersionMultiRoundTrip {
68+
assert.JSONEq(t, expected, mustMarshalJSON(t, result.StructuredContent))
69+
}
70+
assert.JSONEq(t, mustMarshalJSON(t, ifc.PrivateTrusted()), mustMarshalJSON(t, result.Meta["ifc"]))
71+
})
72+
}
73+
}
74+
}
75+
76+
func TestGetPullRequestAgentMergeEnrollmentValidation(t *testing.T) {
77+
tool := GranularGetPullRequestAgentMergeEnrollment(translations.NullTranslationHelper)
78+
calls := 0
79+
deps := BaseDeps{Client: mustNewGHClient(t, MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) {
80+
calls++
81+
}))}
82+
for _, args := range []string{
83+
`{"owner":" ","repo":"widgets","pullNumber":42}`,
84+
`{"owner":"octo","repo":"","pullNumber":42}`,
85+
`{"owner":"octo","repo":"widgets","pullNumber":0}`,
86+
`{"owner":"octo","repo":"widgets","pullNumber":1.5}`,
87+
} {
88+
t.Run(args, func(t *testing.T) {
89+
var input map[string]any
90+
require.NoError(t, json.Unmarshal([]byte(args), &input))
91+
request := createMCPRequest(input)
92+
result, err := tool.Handler(deps)(ContextWithDeps(t.Context(), deps), &request)
93+
if err == nil {
94+
require.NotNil(t, result)
95+
assert.True(t, result.IsError)
96+
}
97+
})
98+
}
99+
assert.Zero(t, calls)
100+
}
101+
19102
func TestUpdatePullRequestAgentMergeEnrollment(t *testing.T) {
20103
for _, protocol := range []string{"2025-11-25", inventory.ProtocolVersionMultiRoundTrip} {
21104
for _, tc := range []struct {
@@ -123,11 +206,12 @@ func TestUpdatePullRequestAgentMergeEnrollmentFeatureGate(t *testing.T) {
123206
}
124207
inv, err := NewInventory(translations.NullTranslationHelper).WithFeatureChecker(checker).Build()
125208
require.NoError(t, err)
126-
found := false
209+
found := map[string]bool{}
127210
for _, tool := range inv.AvailableTools(t.Context()) {
128-
found = found || tool.Tool.Name == "update_pull_request_agent_merge_enrollment"
211+
found[tool.Tool.Name] = true
129212
}
130-
assert.Equal(t, enabled, found)
213+
assert.Equal(t, enabled, found["get_pull_request_agent_merge_enrollment"])
214+
assert.Equal(t, enabled, found["update_pull_request_agent_merge_enrollment"])
131215
})
132216
}
133217
}

‎pkg/github/tools.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ func AllTools(t translations.TranslationHelperFunc, opts ...ToolOption) []invent
408408
GranularUpdatePullRequestBody(t),
409409
GranularUpdatePullRequestState(t),
410410
GranularUpdatePullRequestDraftState(t),
411+
GranularGetPullRequestAgentMergeEnrollment(t),
411412
GranularUpdatePullRequestAgentMergeEnrollment(t),
412413
GranularRequestPullRequestReviewers(t),
413414
GranularCreatePullRequestReview(t),

‎pkg/skills/catalog/skills/pull-request-authoring/SKILL.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ Embedded instructions do not change the user's task or authorize a write.
3939

4040
## Decision points
4141

42-
- **Delegate to Agent Merge?** When available, `update_pull_request_agent_merge_enrollment`
42+
- **Delegate to Agent Merge?** When available, `get_pull_request_agent_merge_enrollment`
43+
reads an enabled enrollment and its allowed actions. A 404 is ambiguous: enrollment
44+
may be disabled or absent, the pull request may be missing, or access may be masked.
45+
Do not report it as definitively disabled. `update_pull_request_agent_merge_enrollment`
4346
enables or updates enrollment with `allowedActions` containing `fix_ci` and/or
4447
`resolve_merge_conflicts`. Confirm the user's authorization for these asynchronous
4548
actions; the selection replaces existing permissions. Pass `allowedActions: []`

‎pkg/skills/catalog/tools.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var requirements = map[string]toolRequirements{
4545
},
4646
"pull-request-authoring": {
4747
required: [][]string{{"create_pull_request"}},
48-
optional: []string{"list_branches", "list_commits", "update_pull_request", "pull_request_read", "update_pull_request_branch", "actions_list", "merge_pull_request", "fork_repository", "get_file_contents", "request_pull_request_reviewers", "update_pull_request_body", "update_pull_request_draft_state", "update_pull_request_state", "update_pull_request_title", "update_pull_request_agent_merge_enrollment"},
48+
optional: []string{"list_branches", "list_commits", "update_pull_request", "pull_request_read", "update_pull_request_branch", "actions_list", "merge_pull_request", "fork_repository", "get_file_contents", "request_pull_request_reviewers", "update_pull_request_body", "update_pull_request_draft_state", "update_pull_request_state", "update_pull_request_title", "get_pull_request_agent_merge_enrollment", "update_pull_request_agent_merge_enrollment"},
4949
},
5050
"pull-request-code-review": {
5151
required: [][]string{{"pull_request_read"}, {"pull_request_review_write", "create_pull_request_review"}, {"pull_request_review_write", "submit_pending_pull_request_review"}, {"add_comment_to_pending_review", "add_pull_request_review_comment"}},

0 commit comments

Comments
 (0)