Design and Architecture

MCP Uploaded-Diff Code Reviews

How local coding agents upload diffs and run the shared code-review agents through MCP.

Summary

The MCP uploaded-diff flow lets local coding agents ask Zeeq for an expert review before a pull request exists. The local agent uploads a raw git unified diff through a token-protected anonymous endpoint, then calls the MCP tool to run the same reviewer execution core used by GitHub pull request reviews.

This path runs synchronously, but it records the review result. It creates a CodeReviewRecord and stores review artifacts. When the caller omits reviewGroupId, the runner creates a new group id; callers can pass a group id to link related MCP runs. It does not acquire pull-request review locks, publish queue messages, or write GitHub comments. The review XML is returned directly to the MCP caller.

Flow

MCP tool create_upload_url
  -> authenticate MCP caller
  -> create jobId and encrypted uploadToken
  -> return PUT URL and curl --data-binary example

Local agent uploads git diff
  -> anonymous PUT /api/v1/code-review/mcp-diffs/{jobId}?token=...
  -> endpoint decrypts token and checks jobId, expiry, byte limit, UTF-8, and diff header
  -> storage writes {jobId}/diff.txt in CodeReviewDiffs with token organization id

MCP tool run_review
  -> decrypt uploadToken and recover jobId, organization id, and expiry
  -> read and parse uploaded diff
  -> map files to CodeReviewFileSnapshot
  -> resolve repository mapping by caller organization + github owner/repo
  -> mapped: apply repository file filter and configured reviewer agents
  -> unmapped: use built-in principal software engineer reviewer
  -> build CodeReviewPromptInput
  -> execute ICodeReviewAgentExecutor
  -> validate XML
  -> delete uploaded diff in finally
  -> return instruction-wrapped XML to the caller

Shared core

The GitHub and MCP paths share these code-review components:

ComponentRole
GitDiffParserParses uploaded unified diffs into file snapshots.
CodeReviewFileFilterEvaluatorApplies repository include/exclude filters.
CodeReviewerAgentResolverResolves configured reviewer agents or no-agents state.
CodeReviewPromptBuilderBuilds the reviewer prompt from CodeReviewPromptInput.
ICodeReviewAgentExecutorRuns the Agent Framework fan-out/fan-in reviewer workflow.
CodeReviewXmlOutputValidatorValidates canonical <reviews> XML.

The GitHub path wraps those pieces with durable pull request records, queue messages, artifact storage, and GitHub comment rendering. The MCP path wraps them with upload-token validation, temporary diff storage, and direct XML return.

Security model

The MCP server itself is authenticated. Only authenticated MCP callers can create an upload URL.

The upload endpoint is anonymous by design because curl uploads are separate from the MCP transport. Trust comes from the encrypted upload token. The token carries the job id, creator id, organization id, expiry, and optional W3C trace context. It uses the same configured key material as review request links but a distinct purpose string, so request-link tokens and upload tokens cannot be redeemed across purposes.

The upload endpoint rejects missing or invalid tokens, route job ids that do not match the token, expired tokens, empty bodies, invalid UTF-8, oversized bodies, and bodies without a diff --git header. The organization id used for storage writes comes from the token, not from storage metadata or request headers.

Storage and cleanup

Uploaded diffs are stored through IStorageProvider<PostgresStorageWriteOptions> in StorageContainer.CodeReviewDiffs at {jobId}/diff.txt.

Re-uploading to the same job path overwrites the previous diff. This makes retry simple: if a review run consumes the diff or fails, the caller can upload the same path again while the token is still valid.

ExpertCodeReviewRunner deletes the uploaded diff in finally with CancellationToken.None after every run attempt. This includes success, validation errors after reading the diff, agent execution failures, and caller cancellation. Uploaded-but-never-run cleanup is intentionally deferred to a future background cleanup worker.

Repository resolution

run_review receives ownerQualifiedRepoName from the MCP caller.

The runner resolves the repository with FindActiveForOrganizationByProviderIdentityAsync using the organization id from the upload token and the GitHub owner/repo identity. This prevents a caller from resolving another organization's repository, reviewer agents, or guidance. If a mapping exists, the review uses its file filter and configured reviewer agents; the review record remains under the caller organization.

If no repository mapping exists in the caller organization, the review still runs with CodeReviewerAgentResolver.CreateDefaultRuntimeAgent() and the organization id from the upload token. This fallback keeps local pre-PR reviews useful even when Zeeq has not yet been configured for the repository.

MCP and pull-request review inputs

The MCP and pull-request paths share the reviewer executor, but their inputs can differ. MCP reviews use the uploaded diff and caller-provided description. Pull-request reviews use the full merge-base diff, pull-request body, and developer comments. The MCP caller must therefore provide an appropriate diff and context; a local review does not guarantee the same findings as the pull-request review.

Production comparison found a Critical/Major gap rate of 25.1% for chained MCP reviews and 4.7% for unchained reviews. At similar input size, the rates were 21.7% (n=157) and 5.2% (n=287). This supports doing an independent pass over the diff before consulting earlier findings. First inspect the whole supplied diff as if no previous reviews existed. Then re-check unresolved Critical/Major findings and avoid repeating resolved lower-level findings. Previous reviews must not narrow the inspected diff. MCP history chains by review group, not agent session, because sessions can include unrelated repositories or slices. The history passed to the agent includes every reviewer facet from each prior review record.

Use scope=full for a full branch diff and scope=slice for an intentional path-limited diff. Slices are normal during parallel work and iteration. Before opening or updating a pull request, run one fresh review of the full branch diff and omit reviewGroupId. Use the tool's default command: git add -N . && git diff --binary $(git merge-base origin/HEAD HEAD). This marks new files as intent-to-add in the index. A slice can add a pathspec, such as -- src/web.

The MCP repository lookup must always include the caller's organization. Do not use a cross-organization repository lookup on the MCP path. The webhook path can use the repository identity received from GitHub.

For follow-up measurement, see MCP vs PR review gap measurement.