Add MCP tool for on-demand Git documentation sync

This commit is contained in:
Joren de Graaf
2026-11-06 15:01:54 +01:00
parent 3a12d56f8b
commit 2a6450b416
3 changed files with 104 additions and 3 deletions
+43 -1
View File
@@ -121,6 +121,44 @@ def _documentation_status(project, state):
'audit_state': state.audit_state if state else {}}
def _docs_sync_result(project, expected_remote_commit=None):
user = _actor('projects:write')
p = _project(user, project, write=True)
if expected_remote_commit is not None and not documentation.HASH_RE.fullmatch(expected_remote_commit):
raise ValidationError('Expected remote commit must be a full Git commit hash.')
if not p.git_repository_url:
return {'status': 'failed', 'project_id': str(p.pk), 'expected_remote_commit': expected_remote_commit,
'remote_head': None, 'indexed_commit': None, 'document_count': 0,
'last_success_at': None, 'last_attempt_at': None, 'retry_after_seconds': None,
'error': 'Configure a Git repository URL before refreshing documentation.',
'freshness': 'No repository configured'}
previous = DocumentationSync.objects.filter(project=p).first()
previous_attempt = previous.last_attempt_at if previous else None
previous_success = previous.last_success_at if previous else None
state = documentation.sync_project(user, p, force=True)
cooled_down = state.last_attempt_at == previous_attempt
diverged = bool(expected_remote_commit and state.remote_head and state.remote_head != expected_remote_commit)
if state.sync_token:
status = 'in_progress'
elif cooled_down:
status = 'already_current' if state.indexed_commit and state.remote_head == state.indexed_commit and not state.last_error else 'failed'
elif state.last_error or state.last_success_at == previous_success:
status = 'failed'
elif previous and previous.indexed_commit == state.indexed_commit and previous.indexed_repository_url == state.indexed_repository_url:
status = 'already_current'
else:
status = 'completed'
if diverged:
status = 'failed'
return {'status': status, 'project_id': str(p.pk), 'expected_remote_commit': expected_remote_commit,
'remote_head': state.remote_head or None, 'indexed_commit': state.indexed_commit or None,
'document_count': ProjectDocument.objects.filter(project=p).count(),
'last_success_at': state.last_success_at.isoformat() if state.last_success_at else None,
'last_attempt_at': state.last_attempt_at.isoformat() if state.last_attempt_at else None,
'retry_after_seconds': 30 if cooled_down else None,
'error': ('Observed remote commit differs from expected commit.' if diverged else state.last_error or ('Refresh is cooling down; retry shortly.' if cooled_down and status == 'failed' else None)),
'freshness': documentation.sync_status(p, state)}
def _docs_offset(cursor, token):
if not cursor:
return 0
@@ -181,6 +219,10 @@ def create_server():
return {**_project_data(p), 'default_column_id': str(p.board.columns.get(is_default=True, archived=False).pk)}
return await sync_to_async(lambda: _safe(run))()
@server.tool(structured_output=True, description='Refresh a project’s read-only Git documentation index on demand. Requires project write access. The synchronous, bounded refresh shares the UI cooldown and retains the last good index on failure; it does not audit document accuracy.')
async def docs_sync(project: str, expected_remote_commit: str | None = None) -> dict[str, Any]:
return await sync_to_async(lambda: _safe(lambda: _docs_sync_result(project, expected_remote_commit)))()
@server.tool(structured_output=True, description='List indexed Git Markdown documents in an accessible project. Paths and source commits are returned without full content.')
async def docs_list(project: str, path: str = '', limit: int = 50, cursor: str = '') -> dict[str, Any]:
def run():
@@ -736,7 +778,7 @@ def create_server():
async def system_capabilities() -> dict[str, Any]:
_actor()
return {'scopes': ['projects:read', 'projects:write'], 'max_page_size': 100, 'version': '0.3',
'transport': 'streamable_http', 'documentation': ['docs_list', 'docs_search', 'docs_read', 'docs_history', 'docs_diff'],
'transport': 'streamable_http', 'documentation': ['docs_sync', 'docs_list', 'docs_search', 'docs_read', 'docs_history', 'docs_diff'],
'card_moves': {'same_project': 'cards_move', 'between_projects': 'cards_move_project'}}
return server