diff --git a/docs/agentic-batch-changes/mcp.mdx b/docs/agentic-batch-changes/mcp.mdx new file mode 100644 index 000000000..5f923c597 --- /dev/null +++ b/docs/agentic-batch-changes/mcp.mdx @@ -0,0 +1,430 @@ +# Agentic Batch Changes over MCP + +

+ Start, follow, and steer agentic batch changes from an MCP client. +

+ + + Supported on [Enterprise](/pricing/plans/enterprise) plans. + + +An MCP client, such as Claude Code, Codex, or Cursor, can do what you do on the page of an [agentic batch change](/agentic-batch-changes): start an agent, follow its progress, answer its questions and publication approvals, send follow-up messages, review workspaces, patches, and logs, and stop it. The agent runs on your Sourcegraph instance as you, with your repository permissions and your Batch Changes credentials. The client sends requests and reads results; all edits happen on Sourcegraph. + +You can see and act only on agents you own. Open agents others share with you or invite you to in Sourcegraph. + +## Before you start + +- Agentic Batch Changes is available to you. The read tools need `Batch Changes: Read` and `Batch Changes: Agent Read`; tools that start or change an agent also need `Batch Changes: Write` and `Batch Changes: Agent Write`. See [Access](/agentic-batch-changes/configuration#access). +- MCP is enabled and you have the `MCP#ACCESS` permission. See [Availability and access control](/api/mcp#availability-and-access-control). +- The `abc-mcp` feature flag is on for you. While it is off, the endpoint lists no tools and every call returns `FEATURE_DISABLED`. + - On self-hosted instances, a site admin creates it under **Administration > Configuration > Feature flags** and enables it for users or organizations. + - On Sourcegraph Cloud, Sourcegraph manages feature flags. Ask your account team to enable it. + + + On self-hosted instances, users can turn the feature flag on for themselves. + To keep a user from using these tools, remove their Agentic Batch Changes + permissions. + + +## Authentication and tool access + +The endpoint uses the same `mcp` scope as the other MCP endpoints. Sign in through OAuth, or use an access token with the `mcp` or `user:all` scope. See [MCP authentication](/api/mcp/authentication). + +The endpoint lists tools based on your permissions and the site configuration: + +- The read tools need the read permissions above. They are `list_batch_changes`, `read_batch_change`, `list_batch_change_workspaces`, `read_batch_change_workspace_diff`, `read_batch_change_workspace_logs`, `read_batch_change_spec`, `read_batch_change_transcript`, and `list_batch_change_standing_approvals`. +- The tools that start or change an agent also need the write permissions. Without them, the endpoint lists only the read tools, and calls to write tools return `FEATURE_DISABLED`. +- `start_batch_change_unattended` is listed only after a site admin enables unattended starts in site configuration. They are off by default: + +```json +{ + "batchChanges.agenticBatchChanges.unattended.enabled": true +} +``` + +The endpoint hides tools in [`mcp.tools.disabled`](/api/mcp#disabling-individual-tools), even with unattended starts enabled, and calls to them return `FEATURE_DISABLED`. + + + **Unattended agents publish without another approval.** An agent can push + branches or open changesets within the repositories and publication mode + selected at start. Turning unattended starts off blocks new starts; agents + already running keep publishing within their scope until their owner or a + site admin stops them. + + +## Connect a client + +The tools are only on this endpoint: + +``` +https://sourcegraph.example.com/.api/mcp/abc +``` + +If you already connect to `/.api/mcp` or `/.api/mcp/all`, add a second server entry for this URL. + +In all examples, replace `sourcegraph.example.com` with your Sourcegraph instance URL. For other clients, or to use an access token, follow [Client integrations](/api/mcp/client-integrations) with this URL. + +### Claude Code + +```bash +claude mcp add --transport http sourcegraph-abc https://sourcegraph.example.com/.api/mcp/abc +``` + +Then run `/mcp` in Claude Code and follow the browser authentication flow. + +### Codex + +Add the following to `~/.codex/config.toml`: + +```toml +[mcp_servers.sourcegraph-abc] +url = "https://sourcegraph.example.com/.api/mcp/abc" +scopes = ["mcp"] +``` + +Then authenticate with `codex mcp login sourcegraph-abc`. + +These tools are marked destructive: `decide_batch_change_approval`, `message_batch_change`, `control_batch_change_turn`, `stop_batch_change`, and `revoke_batch_change_standing_approval`. Codex can ask for approval before calling them. With the approval policy `never`, as in `codex exec`, such a call fails with `MCP tool call requires approval`. To skip the prompt, set `approval_mode` per tool, and repeat the table for each tool you want: + +```toml +[mcp_servers.sourcegraph-abc.tools.decide_batch_change_approval] +approval_mode = "approve" +``` + +To approve every tool on this server instead, set `default_tools_approval_mode = "approve"` under `[mcp_servers.sourcegraph-abc]`. See the [Codex configuration reference](https://developers.openai.com/codex/config-reference). + +### Cursor + +Add the following to `~/.cursor/mcp.json`, or to `.cursor/mcp.json` for a project: + +```json +{ + "mcpServers": { + "sourcegraph-abc": { + "url": "https://sourcegraph.example.com/.api/mcp/abc" + } + } +} +``` + +Enable the server from **Cursor Settings > Tools & MCP** and follow the OAuth prompt. + +## Available tools + + + Every tool takes and returns IDs as strings. Each result stays under 36 KiB, + counting both the structured result and its text copy, so clients keep it + inline and whole. Tools that page return `next_cursor`; pass it as `after` + until it is absent. Long text is shortened in the middle and marked with + `[truncated; see url]`. + + +### Start an agent + +#### `start_batch_change` + +
+Start an attended agent. It plans and makes the change, and publishes only with an approval from you. + +**Parameters:** + +- `message` - The change to make, with context, constraints, and what must not change (required, up to 32,768 characters) +- `repositories` - Full repository names such as `github.com/org/repo` (optional, up to 1,000). Omit to let the agent find repositories. +- `request_key` - A key you generate, such as a UUID (required, up to 200 characters). Reuse it with identical input on every retry; Sourcegraph then returns the first agent instead of starting a second one. + +Names that do not exist or that you cannot see come back in `unresolved`. If none resolve, the call fails with `EMPTY_SCOPE`. + +The agent skips the approval when an approval you already gave covers the repositories and publication mode: an earlier one on the same agent, or a [standing approval](#list_batch_change_standing_approvals) for all your attended agents. + +
+ +#### `start_batch_change_unattended` + +
+Start an agent for a pipeline with no person present. You set the repositories and how far it may publish up front. + +**Parameters:** + +- `message` - The change to make, with everything the agent needs without a person present (required, up to 32,768 characters) +- `repositories` - Full repository names (optional, up to 1,000) +- `query` - A Sourcegraph search whose matching repositories join the scope (optional, up to 32,768 characters) +- `mode` - How far the agent may publish without asking (required): `none` keeps edits in Sourcegraph, `branch` pushes branches, `draft` opens draft changesets, `ready` opens changesets ready for review +- `max_repositories` - Refuse to start if the resolved scope is larger (optional; no limit when omitted) +- `canary` - Run one repository before the rest (optional, default `true`) +- `ci_repair` - Fix failing checks on changesets this agent opened (optional, default `true`) +- `request_key` - As for `start_batch_change` (required) + +Pass `repositories`, `query`, or both. Their union must resolve to at least one repository you can see, or the call fails with `EMPTY_SCOPE`; unlike `start_batch_change`, this agent cannot find repositories on its own. + +The endpoint lists this tool only after a site admin enables unattended starts; see [Authentication and tool access](#authentication-and-tool-access). The scope is fixed at start. Sourcegraph refuses publishing outside the scope or above `mode` and adds a `refusal` item to `attention`. The agent uses only secrets already allowed in Sourcegraph. If it needs a decision it cannot make, it asks and waits for an answer. + +
+ +### Follow an agent + +#### `list_batch_changes` + +
+List agents you own, newest first, with their state and original request. + +**Parameters:** + +- `query` - Return agents whose title contains this text, ignoring case (optional) +- `first` - Agents to scan, 1 to 25 (optional, default 25) +- `after` - `next_cursor` from the previous page (optional) + +
+ +#### `read_batch_change` + +
+Read one agent: state, progress, items that need you, scope, and the latest answer. Optionally wait for a change. + +**Parameters:** + +- `agent_id` - The agent to read (required) +- `version` - The `version` object from your last read, to wait for a change (optional). See [Waiting for updates](#waiting-for-updates). +- `wait_seconds` - Longest wait when `version` is set, 0 to 50 (optional, default 25) +- `decision_id` - Return only this pending decision, in full (optional) +- `after` - With `decision_id`, the `next_cursor` from the previous page of that approval's repositories (optional) + +`state` is `working`, `awaiting_merge`, `stopping`, or `done`. `attention` lists what needs you: a `question` or `approval` with a `decision_id`, or a `refusal` or `failure` with a `reason` that says what to do. `publication_approval` shows what is already approved for publishing; pending requests are in `attention`. Counts cover the current plan and the repositories you can see. + +A regular read shows up to five repository names per approval and may leave out a question form to fit. Read a decision with `decision_id` before you answer it. If its `questions` are still absent, the form is too large for one response; ask the agent to ask fewer questions at a time, or answer on its page. + +
+ +### Answer questions and approvals + +#### `answer_batch_change_question` + +
+Answer every question in one decision. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `decision_id` - From `read_batch_change` (required) +- `answers` - One entry per question, 1 to 100 (required). Each has `question_id` and one of these forms: + - `option_id` picks a choice. + - `option_id` with `text` picks a choice marked `freeform` and fills it in, for example `{"question_id":"q0","option_id":"other","text":"Only frontend repositories"}`. A freeform choice without `text` returns `INVALID_ANSWER`. + - `text` alone gives your own answer, also to a question with choices. + - Neither skips the question. + +Include every question ID, including skipped ones. After a timeout, repeat the call with identical answers in the same order; Sourcegraph records them once. + +
+ +#### `decide_batch_change_approval` + +
+Allow a publication approval, or request changes. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `decision_id` - From `read_batch_change` (required) +- `response` - One of the forms below (required) + +Read every repository page of the decision first. Then pass one of: + +- `{"decision":"allow"}` keeps the requested mode and the `this_agent` scope. +- `{"decision":"allow","publication":{"mode":"draft","scope":"this_agent"}}` sets both. `mode` must match the request, except that `ready` can be lowered to `draft`. +- With `"scope":"all_attended_agents"`, allowing also saves standing draft approval for the reviewed repositories for all your attended agents. +- `{"decision":"request_changes","note":"What to change"}` declines and gives the agent feedback. + +After a timeout, repeat the call with identical fields. Adding a field you left out, even with its default value, can return `ALREADY_DECIDED`. + +Allowing a secret approval returns `REFUSED`; decide those in Sourcegraph. Requesting changes on one works. + +
+ +#### `list_batch_change_standing_approvals` + +
+List your standing approvals. + +Takes no parameters. Returns the approval's `id`, `mode`, and a sample of its repositories. + +
+ +#### `revoke_batch_change_standing_approval` + +
+Revoke your standing approval. + +**Parameters:** + +- `id` - `publication.id` from `list_batch_change_standing_approvals` (required) + +Approvals for a single agent and changesets already published stay in place. + +
+ +### Review the work + +#### `list_batch_change_workspaces` + +
+List workspaces in the agent's current plan with execution, publication, and check state, and their changesets. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `status` - One publication status: `no_changeset`, `no_change`, `unpublished`, `branch`, `draft`, `open`, `merged`, or `closed` (optional) +- `checks` - Draft or open changesets with this check result: `none`, `pending`, `passed`, `failed`, or `unavailable` (optional) +- `repository` - Exact repository name (optional) +- `first` - Workspaces per page, 1 to 100 (optional, default 50) +- `after` - `next_cursor` from the previous page, with the same filters (optional) + +Workspaces in repositories you cannot see, or excluded by [context filters](/context-filters), are left out. When a changeset has `publication_failed`, publishing, updating, or closing it failed. Open the agent's page to see why, fix the cause, and send a message to retry. A stopped agent does not retry; see [`stop_batch_change`](#stop_batch_change). + +
+ +#### `read_batch_change_workspace_diff` + +
+Read a workspace's patches, page by page. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `workspace_id` - From `list_batch_change_workspaces` (required) +- `after` - `next_cursor` from the previous page (optional) + +Pages cover every branch patch the workspace produced, and `changeset_spec_id` names the patch a page belongs to. A workspace with no current result can return its previous run's patches; those pages have `source: "previous_run"`. + +A patch over 1 MiB returns `REFUSED`; read it on the agent's page. + +
+ +#### `read_batch_change_workspace_logs` + +
+Read a workspace's latest execution logs, page by page. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `workspace_id` - From `list_batch_change_workspaces` (required) +- `entry_key` - Read only this log entry, using a `key` from the returned `catalog` (optional) +- `after` - `next_cursor` from the previous page, with the same `entry_key` (optional) + +Without `entry_key`, setup and teardown entries that succeeded are left out. Sourcegraph condenses logs as the agent reads them: it summarizes structured events, drops embedded step diffs, and shortens long lines. Logs whose stored JSON is over 1 MiB return `REFUSED`; read them on the agent's page. + +
+ +#### `read_batch_change_spec` + +
+Read the agent's current batch spec, page by page. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `after` - `next_cursor` from the previous page (optional) + +The plan view shows the same spec. A plan the agent has proposed but not created yet is in the transcript instead. + +
+ +#### `read_batch_change_transcript` + +
+Read the agent's conversation, newest entry first, page by page. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `after` - `next_cursor` from the previous page (optional) + +Each page holds one entry, or part of a long one: messages, thinking, tool calls and results, and the reason for each automatic resume. The tool reads only the agent's first thread, without attachments. + +
+ +### Steer and stop + +#### `message_batch_change` + +
+Send follow-up instructions. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `text` - Instructions (required, up to 32,768 characters) +- `interrupt` - Cancel the message the agent is working on before sending (optional, default `false`) + +If the agent is answering a message and `interrupt` is not set, the call returns `AGENT_BUSY`. Do not retry a message blindly: after a timeout, read the agent and compare `latest_message_id` before you send again. + +
+ +#### `control_batch_change_turn` + +
+Cancel one message. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `message_id` - From `read_batch_change`, `message_batch_change`, or `read_batch_change_transcript` (required) + +Other active messages keep running. + +
+ +#### `stop_batch_change` + +
+Stop the agent, and optionally close its changesets. + +**Parameters:** + +- `agent_id` - The agent's ID, from `list_batch_changes` or a start tool (required) +- `close_changesets` - Also queue closing its open changesets (optional, default `false`) + +Stopping can take several minutes. A successful call queues cleanup; some changesets may still be open. If cleanup does not finish, the `read_batch_change` summary says so; fix the cause and call `stop_batch_change` again. On an agent that is already stopped, `close_changesets` set to `true` still queues closing its open changesets. Pushed branches stay on the code host. A stopped agent cannot continue; start a new one. + +
+ +## Waiting for updates + +To wait for a change instead of polling, pass the `version` object from your last `read_batch_change` response back unchanged. The call returns once something changed, or after `wait_seconds`. If `version` comes back unchanged, nothing changed yet; call again with it. After you start an agent or allow a publication, keep reading until the work succeeds, fails, or needs you. To confirm a publication and get pull request links, use `list_batch_change_workspaces`. + +`wait_seconds` tops out at 50 so the reply arrives before the 60-second timeout most clients use; pass a smaller value if your client's tool timeout is shorter. Each Sourcegraph server holds up to four waiting reads per user; an extra one returns the current state within about 5 seconds. + +## Errors + +A failed call returns an MCP tool error whose text starts with a code, followed by what happened and what to do: + +``` +AGENT_BUSY: batch change agent has a running turn; read or wait, then decide whether to send or retry; do not interrupt automatically +``` + +These codes usually need a change outside the client: + +| Code | What to do | +| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | +| `FEATURE_DISABLED` | Agentic Batch Changes, the `abc-mcp` flag, or unattended starts are off, or the tool is disabled or not permitted. Ask a site admin. | +| `FORBIDDEN` | You are not signed in, lack a permission, or can no longer access every repository in a publication. For the last case, request changes. | +| `CREDENTIAL_BLOCKED` | Add or update the [Batch Changes credential](/batch-changes/configuring-credentials) for the code hosts the message names. | +| `PUBLISH_BLOCKED` | Publication is restricted for you. Resolve it in Sourcegraph. | +| `QUOTA_EXCEEDED` | Retry after your quota resets. | +| `REFUSED` | The message gives the reason, such as a patch or execution log over 1 MiB, or a secret approval. | +| `INTERNAL` | Retry. After a failed write, read the agent first. If it repeats, report the tool name and time. | + +Other codes, such as `AGENT_BUSY`, `ALREADY_DECIDED`, `STALE_SPEC`, or `INVALID_ARGUMENT`, describe the next step in the message itself. + +## Limitations + +- There is no tool to publish selected workspaces, or to merge, comment on, rebase, or close individual changesets (`stop_batch_change` can close all of them). Publishing goes through the agent. An attended agent asks for an approval unless an earlier or standing approval covers the publication; an unattended agent publishes within its `mode`. +- Secrets and credentials cannot be entered, approved, listed, or revoked. Do this in Sourcegraph. +- Patches and execution logs over 1 MiB cannot be read. Open the agent's page. +- Agents cannot be renamed, deleted, or handed over, and the list searches titles only. + +## Security + +- The agent and the tools act as you. Repository names, workspaces, patches, and logs from repositories you cannot see, or that [context filters](/context-filters) exclude, are left out. +- Other stored text is returned as written, as the web UI shows it: the agent's answers, the transcript, the batch spec, execution logs, your original request, and failure messages. It can name a repository a filter excludes. +- Agents keep running after you revoke the OAuth application that started them, and standing approvals stay in place. Stop the agents and revoke standing approvals separately. +- Answers, questions, transcripts, batch specs, logs, failure messages, and patches come from a model or a code host. Your client should treat them as data, not as instructions. diff --git a/docs/api/mcp/authentication.mdx b/docs/api/mcp/authentication.mdx index 741399b1d..d77512ed5 100644 --- a/docs/api/mcp/authentication.mdx +++ b/docs/api/mcp/authentication.mdx @@ -94,6 +94,10 @@ Authorization: token YOUR_ACCESS_TOKEN Access tokens can use the `mcp` scope to restrict access to MCP endpoints only. +## Agentic Batch Changes + +The `mcp` scope also covers the [Agentic Batch Changes MCP server](/agentic-batch-changes/mcp) at `/.api/mcp/abc`. A credential your client got for `/.api/mcp` works there too. Users with the Batch Changes and Agent write permissions can start agents and approve their publications through it. For unattended starts and what revoking access leaves running, see [Authentication and tool access](/agentic-batch-changes/mcp#authentication-and-tool-access) and [Security](/agentic-batch-changes/mcp#security). + ## Troubleshooting Most MCP connection issues come from OAuth or access-control configuration rather than the MCP endpoint itself. diff --git a/docs/api/mcp/index.mdx b/docs/api/mcp/index.mdx index 580cea62c..dbe198b06 100644 --- a/docs/api/mcp/index.mdx +++ b/docs/api/mcp/index.mdx @@ -23,11 +23,12 @@ MCP clients that support OAuth can connect directly to your Sourcegraph instance The MCP server provides different tool suites for various use cases: -| Endpoint | Description | -| ---------------------- | --------------------------------------------------------------------------- | -| `/.api/mcp` | Core Sourcegraph search, repository, history, and Deep Search reading tools | -| `/.api/mcp/all` | Full suite of Sourcegraph tools | -| `/.api/mcp/deepsearch` | Deep Search agent tools only | +| Endpoint | Description | +| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| `/.api/mcp` | Core Sourcegraph search, repository, history, and Deep Search reading tools | +| `/.api/mcp/all` | Full suite of Sourcegraph tools | +| `/.api/mcp/deepsearch` | Deep Search agent tools only | +| `/.api/mcp/abc` | Agentic Batch Changes tools: start, follow, and steer agents. See [Agentic Batch Changes over MCP](/agentic-batch-changes/mcp) | Example URL: @@ -455,6 +456,8 @@ Use this matrix to choose the smallest endpoint that has the tools your MCP clie On `/.api/mcp`, `nls_search` is available as a fallback when `code_finder` is unavailable or disabled. +Agentic Batch Changes tools are only on `/.api/mcp/abc`. See [Agentic Batch Changes over MCP](/agentic-batch-changes/mcp#available-tools). + The default `/.api/mcp` endpoint can read existing Deep Search conversations with `deepsearch_read`, but it cannot create new Deep Search conversations. diff --git a/src/data/navigation.ts b/src/data/navigation.ts index a4c5efff0..e6780462a 100644 --- a/src/data/navigation.ts +++ b/src/data/navigation.ts @@ -42,6 +42,10 @@ export const navigation: NavigationItem[] = [ { title: 'Connectors', href: '/agentic-batch-changes/connectors' + }, + { + title: 'MCP', + href: '/agentic-batch-changes/mcp' } ] },