Repository navigation
Description
Summary
Expose existing backend git endpoints as direct MCP tools so external clients (CLI, local trinity sync, scripts) can perform read/sync operations on an agent's repo without invoking the agent's LLM. Today these operations require a chat_with_agent round-trip, which is slow, costs tokens, and is unreliable for deterministic sync tooling.
Context
The backend already implements direct API endpoints on src/backend/routers/git.py (status, sync, log, pull, reset-to-main-preserve-state, sync-state, auto-sync, freeze-schedules) and the agent server mirrors them at docker/base-image/agent_server/routers/git.py. The MCP layer however only exposes three git/github tools (initialize_github_sync, get_agent_github_pat_status, set_agent_github_pat) — none of the day-to-day git operations are reachable via MCP.
Concretely, the trinity sync flow from a local container currently has two choices: (a) authenticate directly to the backend REST API and bypass MCP entirely, or (b) chat with the agent and ask its LLM to run git commands. Both are bad: (a) duplicates auth surface; (b) is non-deterministic and burns tokens for a git status.
Conflict resolution is the one exception that should stay LLM-mediated — when a sync produces merge conflicts, the user/agent reasoning is genuinely needed, and chat_with_agent is the right escape hatch.
Related: #232 (broader "direct container operations via CLI + API") and Epic #381 (Git Sync Reliability). This is a narrower, MCP-surface-only slice of #232.
Acceptance Criteria
MCP surface
- New MCP tool module
src/mcp-server/src/tools/git.tsexposing read+sync operations as MCP tools:-
get_git_status(agent_name)→ wrapsGET /api/agents/{name}/git/status -
git_sync(agent_name, message?)→ wrapsPOST /api/agents/{name}/git/sync -
get_git_log(agent_name, limit?)→ wrapsGET /api/agents/{name}/git/log -
git_pull(agent_name)→ wrapsPOST /api/agents/{name}/git/pull -
get_git_sync_state(agent_name)→ wrapsGET /api/agents/{name}/git/sync-state -
reset_to_main_preserve_state(agent_name)→ wrapsPOST /api/agents/{name}/git/reset-to-main-preserve-state
-
- Tools registered in
src/mcp-server/src/index.tsand visible vialist_tools - Auth: tools work with both user-scoped and agent-scoped MCP keys, using existing ownership/sharing checks (no new auth code in MCP — just passthrough)
- Conflict handling: when the underlying endpoint returns a conflict response (e.g. push collision, parallel history), the MCP tool surfaces the conflict cleanly and the tool description explicitly directs the caller to
chat_with_agentfor resolution — no LLM-mediated retry inside the tool itself
Audit trail (mandatory — do not skip)
- MCP-layer audit: every new tool is automatically captured by the transparent wrapper at
src/mcp-server/src/audit.ts(POST →/api/internal/audit). Verify in a fresh DB that each tool produces anaudit_logrow withsource='mcp',event_type='mcp_tool_call', correctactor_id/mcp_key_id/mcp_scope,target_type='agent',target_id=<agent_name>, and a request-id correlatable with the backend row. - Backend-layer audit: write/sync endpoints (
/git/sync,/git/pull,/git/reset-to-main-preserve-state) MUST continue to fireplatform_audit_service.log(event_type=AuditEventType.GIT_OPERATION, ...)withevent_actiondistinguishing sync vs pull vs reset,actor_typederived from the caller (user vs agent vs mcp_client),target_type='agent',target_id=<agent_name>, and adetailspayload carrying branch + ahead/behind + remote SHA + conflict-type (when applicable). Audit row must be written on both success and failure paths. - Read endpoints (
/git/status,/git/log,/git/sync-state): no new backendGIT_OPERATIONaudit row is required — the MCP-layermcp_tool_callaudit row is sufficient and avoids audit-log bloat from polling. If a caller hits the REST endpoint directly (not via MCP), the existing request-ID middleware row inaudit_logis the only trail; do not add per-readGIT_OPERATIONrows. - Hash chain compatibility: new audit calls must use
platform_audit_service.log()(not raw inserts) so they participate in the SEC-001 Phase 4 hash chain when enabled. No bypass paths. - Request-ID propagation: the
X-Request-Idheader set by MCP must reach the backend's audit middleware so the MCPmcp_tool_callrow and the backendgit_operationrow share arequest_idand can be joined inGET /api/audit-log?request_id=.... - Test: integration test under
tests/audit/(or extend an existing audit test) exercises each new MCP tool once and asserts the expected audit rows exist with the expected fields populated. Use the existing pattern from the MCP tool-call audit tests added in SEC-001 Phase 3.
Docs
-
docs/memory/architecture.mdMCP tools table updated with the newgit.tsrow (count + tool list) - Architecture audit section gains a one-line note that
git_sync/git_pull/reset_to_main_preserve_statefireAuditEventType.GIT_OPERATIONin addition to the standardmcp_tool_callrow -
docs/TRINITY_COMPATIBLE_AGENT_GUIDE.md(or equivalent) updated to describe the new direct git surface as the preferred path for sync tooling, withchat_with_agentreserved for conflict resolution
Technical Notes
- All target endpoints already exist on
src/backend/routers/git.py— no new backend routes needed. This is purely an MCP wrapper PR plus a verification pass on the existing backend audit calls (platform_audit_service.log(event_type=AuditEventType.GIT_OPERATION, ...)is already in place on/git/syncetc.; confirm coverage on/git/pulland/git/reset-to-main-preserve-stateand add if missing). - Follow the existing pattern in
src/mcp-server/src/tools/agents.ts(e.g.initialize_github_syncfor shape; use the same auth context plumbing). The transparent audit wrapper is hooked at the server level — no per-tool audit code required, but the test must prove it fires. - The backend's per-endpoint conflict responses (
409withX-Conflict-Type) are already structured — surface them verbatim in the MCP tool output so callers can branch on the conflict type without parsing free text. Conflict responses must still produce an audit row (failure path). - Out of scope: a new
trinity syncCLI command itself — this issue just unblocks the MCP surface. The CLI work can follow once MCP tools exist. - Out of scope: write/edit of arbitrary git config, branch creation, rebase, merge conflict resolution — keep the tool surface narrow and read+sync only. Anything destructive or interactive stays in
chat_with_agentper the conflict-fallback rule above.
Activity
AndriiPasternak31 commentedon Jun 26, 2026
/claim
github-actions commentedon Jun 26, 2026
Claimed by @AndriiPasternak31. Label
status-in-progressadded.Next steps:
feature/905-<short-slug>Fixes #905feat(mcp): expose git status/sync/log/pull as direct MCP tools (#905) (…
feat(git): bind an agent to a GitHub repo you own — post-creation own…
fix(git-sync): the canonical .gitignore stops reversing the agent's o…
fix(mcp): a denied tool call is audited as a refusal, not a success (#…