Skip to content

feat(mcp): expose git status/sync/log/pull as direct MCP tools (bypass LLM) #905

Closed

Description

@vybe

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.ts exposing read+sync operations as MCP tools:
    • get_git_status(agent_name) → wraps GET /api/agents/{name}/git/status
    • git_sync(agent_name, message?) → wraps POST /api/agents/{name}/git/sync
    • get_git_log(agent_name, limit?) → wraps GET /api/agents/{name}/git/log
    • git_pull(agent_name) → wraps POST /api/agents/{name}/git/pull
    • get_git_sync_state(agent_name) → wraps GET /api/agents/{name}/git/sync-state
    • reset_to_main_preserve_state(agent_name) → wraps POST /api/agents/{name}/git/reset-to-main-preserve-state
  • Tools registered in src/mcp-server/src/index.ts and visible via list_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_agent for 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 an audit_log row with source='mcp', event_type='mcp_tool_call', correct actor_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 fire platform_audit_service.log(event_type=AuditEventType.GIT_OPERATION, ...) with event_action distinguishing sync vs pull vs reset, actor_type derived from the caller (user vs agent vs mcp_client), target_type='agent', target_id=<agent_name>, and a details payload 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 backend GIT_OPERATION audit row is required — the MCP-layer mcp_tool_call audit 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 in audit_log is the only trail; do not add per-read GIT_OPERATION rows.
  • 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-Id header set by MCP must reach the backend's audit middleware so the MCP mcp_tool_call row and the backend git_operation row share a request_id and can be joined in GET /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.md MCP tools table updated with the new git.ts row (count + tool list)
  • Architecture audit section gains a one-line note that git_sync / git_pull / reset_to_main_preserve_state fire AuditEventType.GIT_OPERATION in addition to the standard mcp_tool_call row
  • docs/TRINITY_COMPATIBLE_AGENT_GUIDE.md (or equivalent) updated to describe the new direct git surface as the preferred path for sync tooling, with chat_with_agent reserved 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/sync etc.; confirm coverage on /git/pull and /git/reset-to-main-preserve-state and add if missing).
  • Follow the existing pattern in src/mcp-server/src/tools/agents.ts (e.g. initialize_github_sync for 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 (409 with X-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 sync CLI 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_agent per the conflict-fallback rule above.

Activity

  1. added
    priority-p2
    type-feature
    on May 21, 2026
  2. added
    complexity-low
    status-incubating
    on Jun 12, 2026
  3. AndriiPasternak31 commented on Jun 26, 2026

    @AndriiPasternak31
    Contributor

    /claim

  4. added
    status-in-progress
    on Jun 26, 2026
  5. github-actions commented on Jun 26, 2026

    @github-actions

    Claimed by @AndriiPasternak31. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/905-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #905
  6. added
    complexity-medium
    and removed
    complexity-low
    status-incubating
    on Jun 26, 2026
  7. added
    status-in-dev
    and removed
    status-in-progress
    on Jun 27, 2026
  8. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

complexity-mediumpriority-p2theme-devextype-feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions