|
| 1 | +"""Repository overview tool for CodeAlive MCP server.""" |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | +from xml.etree import ElementTree as ET |
| 5 | + |
| 6 | +from mcp.server.fastmcp import Context |
| 7 | + |
| 8 | +from core.config import get_api_key_from_context |
| 9 | +from core.logging import log_api_request, log_api_response, logger |
| 10 | +from utils.errors import ( |
| 11 | + handle_api_error, |
| 12 | + normalize_data_source_names, |
| 13 | + format_data_source_names, |
| 14 | +) |
| 15 | +import httpx |
| 16 | + |
| 17 | + |
| 18 | +async def get_repo_overview( |
| 19 | + ctx: Context, |
| 20 | + data_sources: Optional[list[str]] = None |
| 21 | +) -> str: |
| 22 | + """Get high-level overview of repositories including purpose, responsibilities, ubiquitous language, and domain descriptions. |
| 23 | +
|
| 24 | + This tool retrieves domain-focused information about repositories to help users understand |
| 25 | + the business context and vocabulary of codebases. It returns structured information including: |
| 26 | + - Purpose: What the repository is for |
| 27 | + - Responsibilities: What it does |
| 28 | + - Ubiquitous Language: Domain-specific terminology and concepts |
| 29 | + - Domain(s): Business domains covered with their vocabulary |
| 30 | +
|
| 31 | + Args: |
| 32 | + ctx: FastMCP context containing API client and configuration |
| 33 | + data_sources: Optional list of repository/workspace names. If not provided, returns |
| 34 | + overviews for all available data sources |
| 35 | +
|
| 36 | + Returns: |
| 37 | + XML formatted string containing repository overviews in markdown format |
| 38 | +
|
| 39 | + Example: |
| 40 | + # Get overview for specific repositories |
| 41 | + result = await get_repo_overview(ctx, ["my-backend-api", "frontend-app"]) |
| 42 | +
|
| 43 | + # Get overviews for all repositories |
| 44 | + result = await get_repo_overview(ctx) |
| 45 | +
|
| 46 | + # Example output structure: |
| 47 | + # <repository_overviews> |
| 48 | + # <repository name="my-backend-api"> |
| 49 | + # <overview> |
| 50 | + # # Purpose |
| 51 | + # Backend API for e-commerce platform |
| 52 | + # |
| 53 | + # ## Responsibilities |
| 54 | + # - Handle user authentication |
| 55 | + # - Process orders and payments |
| 56 | + # |
| 57 | + # ## Ubiquitous Language |
| 58 | + # - Order: A customer purchase request |
| 59 | + # - Cart: Collection of items before checkout |
| 60 | + # |
| 61 | + # ## Domains |
| 62 | + # ### E-commerce |
| 63 | + # - Product catalog management |
| 64 | + # - Order processing |
| 65 | + # </overview> |
| 66 | + # </repository> |
| 67 | + # </repository_overviews> |
| 68 | + """ |
| 69 | + try: |
| 70 | + # Get context and API key |
| 71 | + context = ctx.request_context.lifespan_context |
| 72 | + api_key = get_api_key_from_context(ctx) |
| 73 | + |
| 74 | + # Normalize and format data_sources if provided |
| 75 | + data_sources = normalize_data_source_names(data_sources) |
| 76 | + |
| 77 | + # Build request URL and params |
| 78 | + url = f"{context.base_url}/api/overview" |
| 79 | + params = {} |
| 80 | + |
| 81 | + if data_sources: |
| 82 | + formatted_names = format_data_source_names(data_sources) |
| 83 | + params = formatted_names |
| 84 | + |
| 85 | + # Log and execute GET request |
| 86 | + log_api_request(logger, "GET", url, params) |
| 87 | + |
| 88 | + headers = {"Authorization": f"Bearer {api_key}"} |
| 89 | + response = await context.client.get(url, headers=headers, params=params) |
| 90 | + response.raise_for_status() |
| 91 | + |
| 92 | + # Parse JSON response |
| 93 | + overview_data = response.json() |
| 94 | + log_api_response(logger, response.status_code, overview_data) |
| 95 | + |
| 96 | + # Transform to XML format |
| 97 | + root = ET.Element("repository_overviews") |
| 98 | + |
| 99 | + for repo in overview_data: |
| 100 | + repo_element = ET.SubElement(root, "repository") |
| 101 | + repo_element.set("name", repo.get("name", "unknown")) |
| 102 | + |
| 103 | + overview_element = ET.SubElement(repo_element, "overview") |
| 104 | + overview_element.text = repo.get("overview", "") |
| 105 | + |
| 106 | + # Convert to string with proper formatting |
| 107 | + xml_string = ET.tostring(root, encoding="unicode", method="xml") |
| 108 | + |
| 109 | + return xml_string |
| 110 | + |
| 111 | + except httpx.HTTPError as e: |
| 112 | + return handle_api_error(ctx, e, "get repository overview") |
| 113 | + except Exception as e: |
| 114 | + return handle_api_error(ctx, e, "get repository overview") |
0 commit comments