analyze_file
Analyze Python file structure and diagnostics to understand code organization and identify issues for improved editing accuracy.
Instructions
Analyze a Python file: get structure and diagnostics summary.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes |
Implementation Reference
- src/mcp_ty/server.py:516-561 (handler)The analyze_file function is a registered MCP tool that takes a file path, checks for its existence, opens it with the client, retrieves diagnostics (errors, warnings, hints), and returns a summary report.
@mcp.tool() async def analyze_file(file_path: str) -> str: """Analyze a Python file: get structure and diagnostics summary.""" client = _get_client() path = Path(file_path).resolve() if not path.exists(): return _error(f"File not found: {file_path}") try: content = path.read_text(encoding="utf-8") line_count = len(content.splitlines()) await client.open_document(path) import asyncio await asyncio.sleep(0.5) diagnostics = client.get_diagnostics(path) errors = sum(1 for d in diagnostics if d.severity == 1) warnings = sum(1 for d in diagnostics if d.severity == 2) hints = len(diagnostics) - errors - warnings issues = [] for diag in diagnostics[:15]: severity = SEVERITY_MAP.get(diag.severity or 1, "error") issues.append({ "line": diag.range.start.line + 1, "column": diag.range.start.character + 1, "severity": severity, "message": diag.message }) return _ok({ "file": path.name, "path": str(path), "lines": line_count, "errors": errors, "warnings": warnings, "hints": hints, "issues": issues, "total_issues": len(diagnostics) }) except Exception as e: return _error(str(e))