From 0c67326851f9c4842310a390db83aa9d662c23aa Mon Sep 17 00:00:00 2001 From: Andrew Georgiou Date: Mon, 16 Nov 2026 13:08:51 +0000 Subject: [PATCH 1/2] Keep automation confirmation on delete only and tighten tool schemas Only delete_automation keeps the multi-round-trip confirmation, so create, update and run stay available to clients without form elicitation. Reject unknown automation arguments, return the revisions next_cursor from the Link header, and mark create and update as non-destructive. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f921ed41-2ec0-4075-a572-7692f9529b7e --- .../__toolsnaps__/create_automation.snap | 4 +- .../__toolsnaps__/delete_automation.snap | 1 + pkg/github/__toolsnaps__/get_automation.snap | 1 + .../get_automation_revision.snap | 1 + ...omations_configuration_for_repository.snap | 1 + .../list_automation_revisions.snap | 1 + .../__toolsnaps__/list_automations.snap | 1 + .../list_available_automation_tools.snap | 1 + .../list_available_automation_triggers.snap | 1 + pkg/github/__toolsnaps__/run_automation.snap | 3 +- .../__toolsnaps__/update_automation.snap | 4 +- pkg/github/agent_automations.go | 81 +++--- pkg/github/agent_automations_test.go | 250 ++++++------------ pkg/http/handler.go | 10 +- pkg/http/handler_test.go | 14 +- pkg/http/server.go | 6 +- pkg/http/server_test.go | 4 +- 17 files changed, 150 insertions(+), 234 deletions(-) diff --git a/pkg/github/__toolsnaps__/create_automation.snap b/pkg/github/__toolsnaps__/create_automation.snap index 4c0171ff7c..693b611ccd 100644 --- a/pkg/github/__toolsnaps__/create_automation.snap +++ b/pkg/github/__toolsnaps__/create_automation.snap @@ -1,11 +1,13 @@ { "annotations": { + "destructiveHint": false, "idempotentHint": false, "readOnlyHint": false, "title": "Create a repository automation" }, - "description": "Create a Copilot automation in a repository after explicit user confirmation.", + "description": "Create a Copilot automation in a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "continuous_history": { "description": "Whether the automation keeps a continuous conversation history.", diff --git a/pkg/github/__toolsnaps__/delete_automation.snap b/pkg/github/__toolsnaps__/delete_automation.snap index 13a78a67d5..b87d0e77d3 100644 --- a/pkg/github/__toolsnaps__/delete_automation.snap +++ b/pkg/github/__toolsnaps__/delete_automation.snap @@ -7,6 +7,7 @@ }, "description": "Delete a Copilot automation from a repository after explicit user confirmation.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/__toolsnaps__/get_automation.snap b/pkg/github/__toolsnaps__/get_automation.snap index 225e12b86a..e359bd982e 100644 --- a/pkg/github/__toolsnaps__/get_automation.snap +++ b/pkg/github/__toolsnaps__/get_automation.snap @@ -6,6 +6,7 @@ }, "description": "Get the configuration of a Copilot automation in a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/__toolsnaps__/get_automation_revision.snap b/pkg/github/__toolsnaps__/get_automation_revision.snap index e95fee3627..bdb7ee6fc7 100644 --- a/pkg/github/__toolsnaps__/get_automation_revision.snap +++ b/pkg/github/__toolsnaps__/get_automation_revision.snap @@ -6,6 +6,7 @@ }, "description": "Get a specific saved revision of a Copilot automation.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/__toolsnaps__/get_automations_configuration_for_repository.snap b/pkg/github/__toolsnaps__/get_automations_configuration_for_repository.snap index 31a35d395d..569e16ef5a 100644 --- a/pkg/github/__toolsnaps__/get_automations_configuration_for_repository.snap +++ b/pkg/github/__toolsnaps__/get_automations_configuration_for_repository.snap @@ -6,6 +6,7 @@ }, "description": "Get whether automations are enabled, whether you can manage them, and the repository's automation trigger and write-scope policies.", "inputSchema": { + "additionalProperties": false, "properties": { "owner": { "description": "Repository owner", diff --git a/pkg/github/__toolsnaps__/list_automation_revisions.snap b/pkg/github/__toolsnaps__/list_automation_revisions.snap index d236f02c06..cde788946f 100644 --- a/pkg/github/__toolsnaps__/list_automation_revisions.snap +++ b/pkg/github/__toolsnaps__/list_automation_revisions.snap @@ -6,6 +6,7 @@ }, "description": "List the saved revisions of a Copilot automation.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/__toolsnaps__/list_automations.snap b/pkg/github/__toolsnaps__/list_automations.snap index 0af1e21492..f4cccdbddb 100644 --- a/pkg/github/__toolsnaps__/list_automations.snap +++ b/pkg/github/__toolsnaps__/list_automations.snap @@ -6,6 +6,7 @@ }, "description": "List Copilot automations for a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "owner": { "description": "Repository owner", diff --git a/pkg/github/__toolsnaps__/list_available_automation_tools.snap b/pkg/github/__toolsnaps__/list_available_automation_tools.snap index 3308b48076..3757617786 100644 --- a/pkg/github/__toolsnaps__/list_available_automation_tools.snap +++ b/pkg/github/__toolsnaps__/list_available_automation_tools.snap @@ -6,6 +6,7 @@ }, "description": "List the tools that can be used by automations in a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "owner": { "description": "Repository owner", diff --git a/pkg/github/__toolsnaps__/list_available_automation_triggers.snap b/pkg/github/__toolsnaps__/list_available_automation_triggers.snap index ed6b211e1c..c620b56676 100644 --- a/pkg/github/__toolsnaps__/list_available_automation_triggers.snap +++ b/pkg/github/__toolsnaps__/list_available_automation_triggers.snap @@ -6,6 +6,7 @@ }, "description": "List the trigger types that can be used by automations in a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "owner": { "description": "Repository owner", diff --git a/pkg/github/__toolsnaps__/run_automation.snap b/pkg/github/__toolsnaps__/run_automation.snap index 5be2b1761e..d358ecd8a9 100644 --- a/pkg/github/__toolsnaps__/run_automation.snap +++ b/pkg/github/__toolsnaps__/run_automation.snap @@ -5,8 +5,9 @@ "readOnlyHint": false, "title": "Run a repository automation" }, - "description": "Start a manual run of a Copilot automation in a repository after explicit user confirmation.", + "description": "Start a manual run of a Copilot automation in a repository.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/__toolsnaps__/update_automation.snap b/pkg/github/__toolsnaps__/update_automation.snap index 6e1d312823..5ca7e67d43 100644 --- a/pkg/github/__toolsnaps__/update_automation.snap +++ b/pkg/github/__toolsnaps__/update_automation.snap @@ -1,11 +1,13 @@ { "annotations": { + "destructiveHint": false, "idempotentHint": false, "readOnlyHint": false, "title": "Update a repository automation" }, - "description": "Update a Copilot automation in a repository after explicit user confirmation. Only the provided fields are changed.", + "description": "Update a Copilot automation in a repository. Only the provided fields are changed.", "inputSchema": { + "additionalProperties": false, "properties": { "automation_id": { "description": "The automation identifier.", diff --git a/pkg/github/agent_automations.go b/pkg/github/agent_automations.go index df0bdcd478..25415e84ab 100644 --- a/pkg/github/agent_automations.go +++ b/pkg/github/agent_automations.go @@ -9,7 +9,6 @@ import ( "fmt" "net/http" "net/url" - "sort" "strconv" "strings" "time" @@ -225,6 +224,12 @@ func newAgentAutomationTool[Input any]( } return utils.NewToolResultError("GitHub agent automations returned an empty response"), nil, nil } + if httpResponse.Cursor != "" { + response, err = withAutomationNextCursor(response, httpResponse.Cursor) + if err != nil { + return utils.NewToolResultErrorFromErr("failed to add the next page cursor", err), nil, nil + } + } sanitizedResponse, err := sanitizeAutomationNames(response) if err != nil { return utils.NewToolResultErrorFromErr("failed to sanitize GitHub agent automation response", err), nil, nil @@ -349,20 +354,6 @@ func agentAutomationConfirmationRequired(tool inventory.ServerTool) inventory.Se return tool } -func agentAutomationMutationConfirmation(action, target string, body map[string]any) *agentAutomationConfirmation { - fields := make([]string, 0, len(body)) - for field := range body { - fields = append(fields, field) - } - sort.Strings(fields) - value := strings.ToUpper(action) + " " + target - return &agentAutomationConfirmation{ - id: agentAutomationConfirmationIDPrefix + action, - value: value, - message: fmt.Sprintf("Review the proposed automation %s for %s. The request changes these fields: %s. Values are not shown here. Type %q to confirm.", action, target, strings.Join(fields, ", "), value), - } -} - func agentAutomationTarget(owner, repo, automationID string) string { target := url.PathEscape(owner) + "/" + url.PathEscape(repo) if automationID != "" { @@ -399,7 +390,12 @@ func agentAutomationConfigurationOutputSchema() json.RawMessage { } func agentAutomationInputSchema(properties map[string]*jsonschema.Schema, required ...string) *jsonschema.Schema { - return &jsonschema.Schema{Type: "object", Properties: properties, Required: required} + return &jsonschema.Schema{ + Type: "object", + Properties: properties, + Required: required, + AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}}, + } } func automationStringProperty(description string) *jsonschema.Schema { @@ -491,6 +487,15 @@ func sanitizeAutomationNames(response json.RawMessage) (json.RawMessage, error) return sanitizedResponse, nil } +func withAutomationNextCursor(response json.RawMessage, cursor string) (json.RawMessage, error) { + var payload map[string]any + if err := json.Unmarshal(response, &payload); err != nil || payload == nil { + return response, err + } + payload["next_cursor"] = cursor + return json.Marshal(payload) +} + func sanitizeAutomationNamesValue(value any) { switch value := value.(type) { case map[string]any: @@ -743,14 +748,14 @@ func CreateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool properties := automationConfigurationProperties() properties["owner"] = automationStringProperty(DescriptionRepositoryOwner) properties["repo"] = automationStringProperty(DescriptionRepositoryName) - return agentAutomationConfirmationRequired(newAgentAutomationTool[CreateAutomationInput]( + return newAgentAutomationTool[CreateAutomationInput]( t, "create_automation", "Create a repository automation", - "Create a Copilot automation in a repository after explicit user confirmation.", + "Create a Copilot automation in a repository.", agentAutomationInputSchema(properties, "owner", "repo", "name", "prompt"), agentAutomationObjectOutputSchema(), - mcp.ToolAnnotations{}, + mcp.ToolAnnotations{DestructiveHint: new(false)}, func(args CreateAutomationInput) (agentAutomationRequest, error) { if err := requireAutomationRepository(args.Owner, args.Repo); err != nil { return agentAutomationRequest{}, err @@ -789,16 +794,14 @@ func CreateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool if args.ContinuousHistory != nil { body["continuous_history"] = *args.ContinuousHistory } - target := agentAutomationTarget(args.Owner, args.Repo, "") return agentAutomationRequest{ method: http.MethodPost, path: automationPath(args.Owner, args.Repo, ""), body: body, successStatus: http.StatusCreated, - confirmation: agentAutomationMutationConfirmation("create", target, body), }, nil }, - )) + ) } func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool { @@ -807,14 +810,14 @@ func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool properties["repo"] = automationStringProperty(DescriptionRepositoryName) properties["automation_id"] = automationStringProperty("The automation identifier.") delete(properties, "type") - return agentAutomationConfirmationRequired(newAgentAutomationTool[UpdateAutomationInput]( + return newAgentAutomationTool[UpdateAutomationInput]( t, "update_automation", "Update a repository automation", - "Update a Copilot automation in a repository after explicit user confirmation. Only the provided fields are changed.", + "Update a Copilot automation in a repository. Only the provided fields are changed.", agentAutomationInputSchema(properties, "owner", "repo", "automation_id"), agentAutomationObjectOutputSchema(), - mcp.ToolAnnotations{}, + mcp.ToolAnnotations{DestructiveHint: new(false)}, func(args UpdateAutomationInput) (agentAutomationRequest, error) { if err := requireAutomationRepository(args.Owner, args.Repo); err != nil { return agentAutomationRequest{}, err @@ -853,15 +856,13 @@ func UpdateAutomation(t translations.TranslationHelperFunc) inventory.ServerTool if len(body) == 0 { return agentAutomationRequest{}, errors.New("at least one automation field must be provided") } - target := agentAutomationTarget(args.Owner, args.Repo, args.AutomationID) return agentAutomationRequest{ - method: http.MethodPatch, - path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)), - body: body, - confirmation: agentAutomationMutationConfirmation("update", target, body), + method: http.MethodPatch, + path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)), + body: body, }, nil }, - )) + ) } func DeleteAutomation(t translations.TranslationHelperFunc) inventory.ServerTool { @@ -902,11 +903,11 @@ func DeleteAutomation(t translations.TranslationHelperFunc) inventory.ServerTool } func RunAutomation(t translations.TranslationHelperFunc) inventory.ServerTool { - return agentAutomationConfirmationRequired(newAgentAutomationTool[RunAutomationInput]( + return newAgentAutomationTool[RunAutomationInput]( t, "run_automation", "Run a repository automation", - "Start a manual run of a Copilot automation in a repository after explicit user confirmation.", + "Start a manual run of a Copilot automation in a repository.", agentAutomationInputSchema(map[string]*jsonschema.Schema{ "owner": automationStringProperty(DescriptionRepositoryOwner), "repo": automationStringProperty(DescriptionRepositoryName), @@ -941,24 +942,12 @@ func RunAutomation(t translations.TranslationHelperFunc) inventory.ServerTool { if args.Context != nil { body["context"] = *args.Context } - target := agentAutomationTarget(args.Owner, args.Repo, args.AutomationID) - message := fmt.Sprintf("Review the automation run request for %s.", target) - if args.Context != nil { - message += fmt.Sprintf("\nA run context is included (%d characters).", utf8.RuneCountInString(*args.Context)) - } - confirmationValue := "RUN " + target - message += fmt.Sprintf("\nType %q to start this run.", confirmationValue) return agentAutomationRequest{ method: http.MethodPost, path: automationPath(args.Owner, args.Repo, url.PathEscape(args.AutomationID)+"/tasks"), body: body, successStatus: http.StatusAccepted, - confirmation: &agentAutomationConfirmation{ - id: agentAutomationConfirmationIDPrefix + "run", - value: confirmationValue, - message: message, - }, }, nil }, - )) + ) } diff --git a/pkg/github/agent_automations_test.go b/pkg/github/agent_automations_test.go index 71359d5c72..6379e02850 100644 --- a/pkg/github/agent_automations_test.go +++ b/pkg/github/agent_automations_test.go @@ -232,6 +232,23 @@ func TestAgentAutomationReadTools(t *testing.T) { } } +func TestListAutomationRevisionsReturnsNextCursor(t *testing.T) { + t.Parallel() + + httpClient := MockHTTPClientWithHandler(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Link", `; rel="next", ; rel="first"`) + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"revisions":[]}`)) + }) + deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} + request := createMCPRequest(map[string]any{"owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id"}) + tool := ListAutomationRevisions(translations.NullTranslationHelper) + result, err := tool.Handler(nil)(ContextWithDeps(context.Background(), deps), &request) + require.NoError(t, err) + require.False(t, result.IsError) + assert.JSONEq(t, `{"revisions":[],"next_cursor":"rev_3"}`, getTextResult(t, result).Text) +} + func TestAgentAutomationReadToolsSanitizeNames(t *testing.T) { t.Parallel() @@ -242,20 +259,7 @@ func TestAgentAutomationReadToolsSanitizeNames(t *testing.T) { _, _ = w.Write([]byte(response)) }) deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - tool := ListAutomations(translations.NullTranslationHelper) - inv, err := inventory.NewBuilder().SetTools([]inventory.ServerTool{tool}). - WithToolsets([]string{"all"}). - WithFeatureChecker(func(context.Context, string) (bool, error) { return true, nil }).Build() - require.NoError(t, err) - server := mcp.NewServer(&mcp.Implementation{Name: "agent-automations-test", Version: "v1"}, nil) - server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) - inv.RegisterTools(context.Background(), server, deps) - session := connectCommentVisibilityClient(t, server, inventory.ProtocolVersionMultiRoundTrip) - result, err := session.CallTool(context.Background(), &mcp.CallToolParams{ - Name: tool.Tool.Name, - Arguments: map[string]any{"owner": "octo-org", "repo": "octo-repo"}, - }) - require.NoError(t, err) + result := callAgentAutomationToolOverMCP(t, ListAutomations(translations.NullTranslationHelper), deps, map[string]any{"owner": "octo-org", "repo": "octo-repo"}) require.False(t, result.IsError) assert.JSONEq(t, expected, getTextResult(t, result).Text) structuredContent, err := json.Marshal(result.StructuredContent) @@ -263,6 +267,21 @@ func TestAgentAutomationReadToolsSanitizeNames(t *testing.T) { assert.JSONEq(t, expected, string(structuredContent)) } +func TestAgentAutomationToolsRejectUnknownArguments(t *testing.T) { + t.Parallel() + + httpClient := MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { + assert.Fail(t, "unknown arguments must not make a request") + }) + deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} + result := callAgentAutomationToolOverMCP(t, CreateAutomation(translations.NullTranslationHelper), deps, map[string]any{ + "owner": "octo-org", "repo": "octo-repo", "name": "Issue triage", "prompt": "Triage new issues.", + "ownership": "repository", + }) + require.True(t, result.IsError) + assert.Contains(t, getErrorResult(t, result).Text, "ownership") +} + func TestAgentAutomationReadToolsRejectUnsafeIDsAndPathSegments(t *testing.T) { t.Parallel() @@ -413,19 +432,17 @@ func TestAgentAutomationWriteTools(t *testing.T) { t.Parallel() tests := []struct { - name string - tool func(translations.TranslationHelperFunc) inventory.ServerTool - args map[string]any - method string - endpoint string - status int - body string - response string - confirm bool - confirmID string - confirmValue string - messageContents []string - messageOmissions []string + name string + tool func(translations.TranslationHelperFunc) inventory.ServerTool + args map[string]any + method string + endpoint string + status int + body string + response string + confirm bool + confirmID string + confirmValue string }{ { name: "create automation", @@ -435,16 +452,11 @@ func TestAgentAutomationWriteTools(t *testing.T) { "type": "shared", "enabled": false, "tools": []string{"github/issue_read"}, "triggers": map[string]any{"issue_opened": map[string]any{"query": "is:open"}}, }, - method: http.MethodPost, - endpoint: "/agents/repos/octo-org/octo-repo/automations", - status: http.StatusCreated, - body: `{"name":"Issue triage","prompt":"Triage new issues.","type":"shared","enabled":false,"tools":["github/issue_read"],"triggers":{"issue_opened":{"query":"is:open"}}}`, - response: `{"id":"automation-id","name":"Issue triage"}`, - confirm: true, - confirmID: agentAutomationConfirmationIDPrefix + "create", - confirmValue: "CREATE octo-org/octo-repo", - messageContents: []string{"enabled, name, prompt, tools, triggers, type"}, - messageOmissions: []string{"Issue triage", "Triage new issues."}, + method: http.MethodPost, + endpoint: "/agents/repos/octo-org/octo-repo/automations", + status: http.StatusCreated, + body: `{"name":"Issue triage","prompt":"Triage new issues.","type":"shared","enabled":false,"tools":["github/issue_read"],"triggers":{"issue_opened":{"query":"is:open"}}}`, + response: `{"id":"automation-id","name":"Issue triage"}`, }, { name: "update automation", @@ -453,16 +465,11 @@ func TestAgentAutomationWriteTools(t *testing.T) { "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", "enabled": false, "prompt": "Review new issues.", "tools": []string{}, }, - method: http.MethodPatch, - endpoint: "/agents/repos/octo-org/octo-repo/automations/automation-id", - status: http.StatusOK, - body: `{"enabled":false,"prompt":"Review new issues.","tools":[]}`, - response: `{"id":"automation-id","prompt":"Review new issues."}`, - confirm: true, - confirmID: agentAutomationConfirmationIDPrefix + "update", - confirmValue: "UPDATE octo-org/octo-repo/automation-id", - messageContents: []string{"enabled, prompt, tools"}, - messageOmissions: []string{"Review new issues."}, + method: http.MethodPatch, + endpoint: "/agents/repos/octo-org/octo-repo/automations/automation-id", + status: http.StatusOK, + body: `{"enabled":false,"prompt":"Review new issues.","tools":[]}`, + response: `{"id":"automation-id","prompt":"Review new issues."}`, }, { name: "delete automation", @@ -504,7 +511,7 @@ func TestAgentAutomationWriteTools(t *testing.T) { sealer, err := requeststate.NewRandom() require.NoError(t, err) deps.StateSealer = sealer - result = invokeConfirmedAutomation(t, tool, deps, tt.args, tt.confirmID, tt.confirmValue, tt.messageContents, tt.messageOmissions) + result = invokeConfirmedAutomation(t, tool, deps, tt.args, tt.confirmID, tt.confirmValue) } else { request := createMCPRequest(tt.args) var err error @@ -517,43 +524,6 @@ func TestAgentAutomationWriteTools(t *testing.T) { } } -func TestAutomationCreateAndUpdateRequireRequestStateProtection(t *testing.T) { - t.Parallel() - - for _, tc := range []struct { - name string - tool inventory.ServerTool - args map[string]any - }{ - { - name: "create", - tool: CreateAutomation(translations.NullTranslationHelper), - args: map[string]any{ - "owner": "octo-org", "repo": "octo-repo", "name": "Issue triage", "prompt": "Triage new issues.", - }, - }, - { - name: "update", - tool: UpdateAutomation(translations.NullTranslationHelper), - args: map[string]any{ - "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", "enabled": false, - }, - }, - } { - t.Run(tc.name, func(t *testing.T) { - httpClient := MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { - t.Fatal("mutation should not be sent without request-state protection") - }) - deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - request := createMCPRequest(tc.args) - result, err := tc.tool.Handler(deps)(ContextWithDeps(context.Background(), deps), &request) - require.NoError(t, err) - require.True(t, result.IsError) - assert.Contains(t, getErrorResult(t, result).Text, "request-state protection is not configured") - }) - } -} - func TestAgentAutomationWriteToolsRejectUnsafePathSegments(t *testing.T) { t.Parallel() @@ -607,13 +577,12 @@ func TestRunAutomation(t *testing.T) { _, _ = w.Write([]byte(`{"task_id":"task-id","automation_id":"automation-id","event":"manual"}`)) }) deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - sealer, err := requeststate.NewRandom() - require.NoError(t, err) - deps.StateSealer = sealer - result := invokeConfirmedAutomation(t, tool, deps, map[string]any{ + request := createMCPRequest(map[string]any{ "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", "event": "manual", "context": "Please focus on recently opened issues.", - }, agentAutomationConfirmationIDPrefix+"run", "RUN octo-org/octo-repo/automation-id", []string{"39 characters"}, []string{"Please focus on recently opened issues."}) + }) + result, err := tool.Handler(nil)(ContextWithDeps(context.Background(), deps), &request) + require.NoError(t, err) if result.IsError { t.Fatal(getErrorResult(t, result).Text) } @@ -645,12 +614,11 @@ func TestRunAutomationRejectsEmptyAcceptedResponse(t *testing.T) { w.WriteHeader(http.StatusAccepted) }) deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - sealer, err := requeststate.NewRandom() - require.NoError(t, err) - deps.StateSealer = sealer - result := invokeConfirmedAutomation(t, tool, deps, map[string]any{ + request := createMCPRequest(map[string]any{ "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", "event": "manual", - }, agentAutomationConfirmationIDPrefix+"run", "RUN octo-org/octo-repo/automation-id", nil, nil) + }) + result, err := tool.Handler(nil)(ContextWithDeps(context.Background(), deps), &request) + require.NoError(t, err) require.True(t, result.IsError) assert.Contains(t, getErrorResult(t, result).Text, "empty response after HTTP 202") assert.Contains(t, getErrorResult(t, result).Text, "may have succeeded") @@ -714,65 +682,6 @@ func TestRunAutomationRejectsOversizedContext(t *testing.T) { assert.Contains(t, getErrorResult(t, result).Text, "no more than 10000 characters") } -func TestRunAutomationConfirmationBindsRequest(t *testing.T) { - t.Parallel() - - tool := RunAutomation(translations.NullTranslationHelper) - requestCount := 0 - httpClient := MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { - requestCount++ - }) - deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - sealer, err := requeststate.NewRandom() - require.NoError(t, err) - deps.StateSealer = sealer - - args := map[string]any{ - "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", - "event": "manual", "context": "Please focus on recently opened issues.", - } - handler := tool.Handler(deps) - request := createMCPRequest(args) - result, err := handler(ContextWithDeps(context.Background(), deps), &request) - require.NoError(t, err) - require.NotNil(t, result) - require.Contains(t, result.InputRequests, agentAutomationConfirmationIDPrefix+"run") - - args["context"] = "Run against a different set of issues." - retry := createMCPRequest(args) - retry.Params.RequestState = result.RequestState - retry.Params.InputResponses = mcp.InputResponseMap{ - agentAutomationConfirmationIDPrefix + "run": &mcp.ElicitResult{ - Action: "accept", - Content: map[string]any{ - agentAutomationConfirmationField: "RUN octo-org/octo-repo/automation-id", - }, - }, - } - result, err = handler(ContextWithDeps(context.Background(), deps), &retry) - require.NoError(t, err) - require.True(t, result.IsError) - assert.Contains(t, getErrorResult(t, result).Text, "changed after confirmation") - assert.Zero(t, requestCount) -} - -func TestRunAutomationRequiresRequestStateProtection(t *testing.T) { - t.Parallel() - - tool := RunAutomation(translations.NullTranslationHelper) - httpClient := MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { - t.Fatal("run should not be sent without request-state protection") - }) - deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - request := createMCPRequest(map[string]any{ - "owner": "octo-org", "repo": "octo-repo", "automation_id": "automation-id", "event": "manual", - }) - result, err := tool.Handler(deps)(ContextWithDeps(context.Background(), deps), &request) - require.NoError(t, err) - require.True(t, result.IsError) - assert.Contains(t, getErrorResult(t, result).Text, "request-state protection is not configured") -} - func TestDeleteAutomationRequiresRequestStateProtection(t *testing.T) { t.Parallel() @@ -862,27 +771,44 @@ func TestDeleteAutomationRequiresMatchingConfirmation(t *testing.T) { } } -func TestAutomationMutationsRequireFormElicitation(t *testing.T) { +func TestOnlyDeleteAutomationRequiresFormElicitation(t *testing.T) { t.Parallel() + deleteTool := DeleteAutomation(translations.NullTranslationHelper) + assert.Equal(t, inventory.ProtocolVersionMultiRoundTrip, deleteTool.MinimumProtocolVersion) + assert.Equal(t, inventory.ElicitationModeForm, deleteTool.RequiredElicitationMode) for _, tool := range []inventory.ServerTool{ CreateAutomation(translations.NullTranslationHelper), UpdateAutomation(translations.NullTranslationHelper), - DeleteAutomation(translations.NullTranslationHelper), RunAutomation(translations.NullTranslationHelper), } { - assert.Equal(t, inventory.ProtocolVersionMultiRoundTrip, tool.MinimumProtocolVersion) - assert.Equal(t, inventory.ElicitationModeForm, tool.RequiredElicitationMode) + assert.Empty(t, tool.MinimumProtocolVersion, tool.Tool.Name) + assert.Empty(t, tool.RequiredElicitationMode, tool.Tool.Name) } } +func callAgentAutomationToolOverMCP(t *testing.T, tool inventory.ServerTool, deps BaseDeps, args map[string]any) *mcp.CallToolResult { + t.Helper() + + inv, err := inventory.NewBuilder().SetTools([]inventory.ServerTool{tool}). + WithToolsets([]string{"all"}). + WithFeatureChecker(func(context.Context, string) (bool, error) { return true, nil }).Build() + require.NoError(t, err) + server := mcp.NewServer(&mcp.Implementation{Name: "agent-automations-test", Version: "v1"}, nil) + server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) + inv.RegisterTools(context.Background(), server, deps) + session := connectCommentVisibilityClient(t, server, inventory.ProtocolVersionMultiRoundTrip) + result, err := session.CallTool(context.Background(), &mcp.CallToolParams{Name: tool.Tool.Name, Arguments: args}) + require.NoError(t, err) + return result +} + func invokeConfirmedAutomation( t *testing.T, tool inventory.ServerTool, deps BaseDeps, args map[string]any, confirmationID, confirmationValue string, - messageContents, messageOmissions []string, ) *mcp.CallToolResult { t.Helper() @@ -895,12 +821,6 @@ func invokeConfirmedAutomation( require.True(t, ok) assert.Equal(t, "form", params.Mode) assert.Contains(t, params.Message, confirmationValue) - for _, content := range messageContents { - assert.Contains(t, params.Message, content) - } - for _, omission := range messageOmissions { - assert.NotContains(t, params.Message, omission) - } retry := createMCPRequest(args) retry.Params.RequestState = result.RequestState diff --git a/pkg/http/handler.go b/pkg/http/handler.go index f1607bdf95..14b7eb2a0f 100644 --- a/pkg/http/handler.go +++ b/pkg/http/handler.go @@ -399,20 +399,14 @@ func buildStaticInventory(cfg *ServerConfig, t translations.TranslationHelperFun opts := []github.ToolOption{github.WithHost(hostType)} tools := github.AllTools(t, opts...) filterUnavailable := func(tools []inventory.ServerTool) []inventory.ServerTool { - if !cfg.disableDeleteRepository && !cfg.disableAgentAutomationMutations { + if !cfg.disableDeleteRepository && !cfg.disableDeleteAutomation { return tools } return slices.DeleteFunc(tools, func(tool inventory.ServerTool) bool { if cfg.disableDeleteRepository && tool.Tool.Name == github.DeleteRepositoryToolName { return true } - if cfg.disableAgentAutomationMutations { - switch tool.Tool.Name { - case "create_automation", "update_automation", "delete_automation", "run_automation": - return true - } - } - return false + return cfg.disableDeleteAutomation && tool.Tool.Name == "delete_automation" }) } diff --git a/pkg/http/handler_test.go b/pkg/http/handler_test.go index 6c91df7f0b..2ccb12f7f4 100644 --- a/pkg/http/handler_test.go +++ b/pkg/http/handler_test.go @@ -813,8 +813,8 @@ func TestStaticInventoryPreservesPerRequestFeatureVariants(t *testing.T) { func TestStaticInventoryDisablesUnprotectedMutationTools(t *testing.T) { cfg := &ServerConfig{ - disableDeleteRepository: true, - disableAgentAutomationMutations: true, + disableDeleteRepository: true, + disableDeleteAutomation: true, } tools, _, _, err := buildStaticInventory(cfg, translations.NullTranslationHelper) require.NoError(t, err) @@ -824,17 +824,17 @@ func TestStaticInventoryDisablesUnprotectedMutationTools(t *testing.T) { names = append(names, tool.Tool.Name) } assert.NotContains(t, names, github.DeleteRepositoryToolName) - assert.NotContains(t, names, "create_automation") - assert.NotContains(t, names, "update_automation") assert.NotContains(t, names, "delete_automation") - assert.NotContains(t, names, "run_automation") + assert.Contains(t, names, "create_automation") + assert.Contains(t, names, "update_automation") + assert.Contains(t, names, "run_automation") assert.Contains(t, names, "actions_list", "non-default toolsets must remain available for per-request selection") } func TestStaticInventoryKeepsExplicitUnprotectedMutationToolsDisabled(t *testing.T) { cfg := &ServerConfig{ - EnabledTools: []string{"create_automation"}, - disableAgentAutomationMutations: true, + EnabledTools: []string{"delete_automation"}, + disableDeleteAutomation: true, } tools, _, _, err := buildStaticInventory(cfg, translations.NullTranslationHelper) require.NoError(t, err) diff --git a/pkg/http/server.go b/pkg/http/server.go index 320df45ef0..c7382f0569 100644 --- a/pkg/http/server.go +++ b/pkg/http/server.go @@ -119,8 +119,8 @@ type ServerConfig struct { // the MCP endpoints. When zero, middleware.DefaultMaxRequestBodyBytes is used. MaxRequestBodyBytes int64 - disableDeleteRepository bool - disableAgentAutomationMutations bool + disableDeleteRepository bool + disableDeleteAutomation bool } func RunHTTPServer(cfg ServerConfig) error { @@ -284,7 +284,7 @@ func resolveListenAddress(host string, port int) string { func configureRequestState(cfg *ServerConfig, logger *slog.Logger) (github.RequestStateSealer, error) { if cfg.MRTRStateKey == "" { cfg.disableDeleteRepository = true - cfg.disableAgentAutomationMutations = true + cfg.disableDeleteAutomation = true logger.Warn("multi-round-trip mutation tools disabled: request-state encryption key is not configured", "environmentVariable", MRTRStateKeyEnv) return nil, nil } diff --git a/pkg/http/server_test.go b/pkg/http/server_test.go index 1b1f170c82..2b69f77a00 100644 --- a/pkg/http/server_test.go +++ b/pkg/http/server_test.go @@ -523,7 +523,7 @@ func TestConfigureRequestState(t *testing.T) { require.NoError(t, err) assert.Nil(t, sealer) assert.True(t, cfg.disableDeleteRepository) - assert.True(t, cfg.disableAgentAutomationMutations) + assert.True(t, cfg.disableDeleteAutomation) }) t.Run("valid key configures sealer", func(t *testing.T) { @@ -534,7 +534,7 @@ func TestConfigureRequestState(t *testing.T) { require.NoError(t, err) require.NotNil(t, sealer) assert.False(t, cfg.disableDeleteRepository) - assert.False(t, cfg.disableAgentAutomationMutations) + assert.False(t, cfg.disableDeleteAutomation) token, err := sealer.Seal(context.Background(), []byte("state")) require.NoError(t, err) From f869039dd45a4beabffee8ed7721ebd2bca4eb34 Mon Sep 17 00:00:00 2001 From: Andrew Georgiou Date: Mon, 16 Nov 2026 13:45:54 +0000 Subject: [PATCH 2/2] Reject unknown automation arguments in direct handler calls The schema only rejected extra arguments when the SDK validated the call. A shared input normalizer now rejects them on every path, including direct ServerTool.Handler calls. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f921ed41-2ec0-4075-a572-7692f9529b7e --- pkg/github/agent_automations.go | 18 +++++++++++++ pkg/github/agent_automations_test.go | 40 ++++++++++++++-------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pkg/github/agent_automations.go b/pkg/github/agent_automations.go index 25415e84ab..17bd646d30 100644 --- a/pkg/github/agent_automations.go +++ b/pkg/github/agent_automations.go @@ -7,8 +7,10 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net/http" "net/url" + "slices" "strconv" "strings" "time" @@ -236,6 +238,7 @@ func newAgentAutomationTool[Input any]( } return utils.NewToolResultText(string(sanitizedResponse)), sanitizedResponse, nil }, + rejectUnknownAutomationArguments(inputSchema), ) tool.FeatureRule = featureEnabledRule(FeatureFlagAgentAutomations) return tool @@ -398,6 +401,21 @@ func agentAutomationInputSchema(properties map[string]*jsonschema.Schema, requir } } +func rejectUnknownAutomationArguments(schema *jsonschema.Schema) inventory.InputNormalizer { + return func(raw json.RawMessage) (json.RawMessage, error) { + var args map[string]json.RawMessage + if err := json.Unmarshal(raw, &args); err != nil { + return nil, &inventory.ToolInputError{Message: "invalid arguments: " + err.Error()} + } + for _, name := range slices.Sorted(maps.Keys(args)) { + if _, ok := schema.Properties[name]; !ok { + return nil, &inventory.ToolInputError{Message: fmt.Sprintf("invalid arguments: unexpected argument %q", name)} + } + } + return raw, nil + } +} + func automationStringProperty(description string) *jsonschema.Schema { return &jsonschema.Schema{Type: "string", Description: description} } diff --git a/pkg/github/agent_automations_test.go b/pkg/github/agent_automations_test.go index 6379e02850..b489a67abd 100644 --- a/pkg/github/agent_automations_test.go +++ b/pkg/github/agent_automations_test.go @@ -259,7 +259,20 @@ func TestAgentAutomationReadToolsSanitizeNames(t *testing.T) { _, _ = w.Write([]byte(response)) }) deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - result := callAgentAutomationToolOverMCP(t, ListAutomations(translations.NullTranslationHelper), deps, map[string]any{"owner": "octo-org", "repo": "octo-repo"}) + tool := ListAutomations(translations.NullTranslationHelper) + inv, err := inventory.NewBuilder().SetTools([]inventory.ServerTool{tool}). + WithToolsets([]string{"all"}). + WithFeatureChecker(func(context.Context, string) (bool, error) { return true, nil }).Build() + require.NoError(t, err) + server := mcp.NewServer(&mcp.Implementation{Name: "agent-automations-test", Version: "v1"}, nil) + server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) + inv.RegisterTools(context.Background(), server, deps) + session := connectCommentVisibilityClient(t, server, inventory.ProtocolVersionMultiRoundTrip) + result, err := session.CallTool(context.Background(), &mcp.CallToolParams{ + Name: tool.Tool.Name, + Arguments: map[string]any{"owner": "octo-org", "repo": "octo-repo"}, + }) + require.NoError(t, err) require.False(t, result.IsError) assert.JSONEq(t, expected, getTextResult(t, result).Text) structuredContent, err := json.Marshal(result.StructuredContent) @@ -271,15 +284,18 @@ func TestAgentAutomationToolsRejectUnknownArguments(t *testing.T) { t.Parallel() httpClient := MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { - assert.Fail(t, "unknown arguments must not make a request") + t.Fatal("unknown arguments must not make a request") }) deps := BaseDeps{Client: mustNewGHClient(t, httpClient)} - result := callAgentAutomationToolOverMCP(t, CreateAutomation(translations.NullTranslationHelper), deps, map[string]any{ + request := createMCPRequest(map[string]any{ "owner": "octo-org", "repo": "octo-repo", "name": "Issue triage", "prompt": "Triage new issues.", "ownership": "repository", }) + tool := CreateAutomation(translations.NullTranslationHelper) + result, err := tool.Handler(nil)(ContextWithDeps(context.Background(), deps), &request) + require.NoError(t, err) require.True(t, result.IsError) - assert.Contains(t, getErrorResult(t, result).Text, "ownership") + assert.Equal(t, `invalid arguments: unexpected argument "ownership"`, getErrorResult(t, result).Text) } func TestAgentAutomationReadToolsRejectUnsafeIDsAndPathSegments(t *testing.T) { @@ -787,22 +803,6 @@ func TestOnlyDeleteAutomationRequiresFormElicitation(t *testing.T) { } } -func callAgentAutomationToolOverMCP(t *testing.T, tool inventory.ServerTool, deps BaseDeps, args map[string]any) *mcp.CallToolResult { - t.Helper() - - inv, err := inventory.NewBuilder().SetTools([]inventory.ServerTool{tool}). - WithToolsets([]string{"all"}). - WithFeatureChecker(func(context.Context, string) (bool, error) { return true, nil }).Build() - require.NoError(t, err) - server := mcp.NewServer(&mcp.Implementation{Name: "agent-automations-test", Version: "v1"}, nil) - server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) - inv.RegisterTools(context.Background(), server, deps) - session := connectCommentVisibilityClient(t, server, inventory.ProtocolVersionMultiRoundTrip) - result, err := session.CallTool(context.Background(), &mcp.CallToolParams{Name: tool.Tool.Name, Arguments: args}) - require.NoError(t, err) - return result -} - func invokeConfirmedAutomation( t *testing.T, tool inventory.ServerTool,