#!/usr/bin/env python3 """ Fetch BIND9 grammar files using Gitea MCP tools. This script must be run from an environment where MCP tools are available. """ import base64 import json import sys from pathlib import Path # Files to fetch GRAMMAR_FILES = [ "options", "forward.zoneopt", "hint.zoneopt", "in-view.zoneopt", "mirror.zoneopt", "primary.zoneopt", "redirect.zoneopt", "secondary.zoneopt", "static-stub.zoneopt", "stub.zoneopt", "delegation-only.zoneopt", "rndc.grammar", "parsegrammar.py", "checkgrammar.py", ] def save_file_from_mcp_result(result_json: dict, output_path: Path) -> bool: """Save a file from MCP tool result JSON.""" try: if 'Result' in result_json and 'content' in result_json['Result']: content_b64 = result_json['Result']['content'] content = base64.b64decode(content_b64).decode('utf-8') output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, 'w', encoding='utf-8') as f: f.write(content) return True return False except Exception as e: print(f"Error saving {output_path}: {e}", file=sys.stderr) return False def main(): """Process stdin JSON from MCP tool calls.""" print("Reading MCP tool results from stdin...") # Read all input input_data = sys.stdin.read() try: result = json.loads(input_data) # Determine output path from result metadata if 'Result' in result and 'name' in result['Result']: filename = result['Result']['name'] # Output path will be provided as command line argument if len(sys.argv) > 1: output_path = Path(sys.argv[1]) if save_file_from_mcp_result(result, output_path): print(f"✓ Saved: {output_path}") sys.exit(0) else: print(f"✗ Failed to save: {output_path}", file=sys.stderr) sys.exit(1) print("✗ Could not determine output path", file=sys.stderr) sys.exit(1) except json.JSONDecodeError as e: print(f"✗ Invalid JSON input: {e}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()