Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /scripts /security /validate_mcp_config.py
| #!/usr/bin/env python3 | |
| """ | |
| MCP Configuration Path Validator. | |
| Validates that all paths in mcp_config.json are valid and secure. | |
| """ | |
| import json | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| from typing import Any | |
| # Add project root to path | |
| project_root = Path(__file__).parent.parent.parent | |
| sys.path.insert(0, str(project_root / "src")) | |
| try: | |
| from grid.security.path_manager import SecurePathManager | |
| PATH_MANAGER_AVAILABLE = True | |
| except ImportError: | |
| PATH_MANAGER_AVAILABLE = False | |
| print("Warning: SecurePathManager not available, using basic validation") | |
| def _extract_servers(config: dict[str, Any]) -> list[dict[str, Any]]: | |
| """Normalize MCP config into a list of server dictionaries.""" | |
| if "servers" in config: | |
| servers = config.get("servers", []) | |
| if isinstance(servers, list): | |
| return servers | |
| raise ValueError("MCP config 'servers' key must be a list") | |
| if "mcpServers" in config: | |
| mcp_servers = config.get("mcpServers", {}) | |
| if not isinstance(mcp_servers, dict): | |
| raise ValueError("MCP config 'mcpServers' key must be an object") | |
| normalized_servers: list[dict[str, Any]] = [] | |
| for server_name, server_config in mcp_servers.items(): | |
| if not isinstance(server_config, dict): | |
| raise ValueError(f"MCP server '{server_name}' config must be an object") | |
| normalized_servers.append({"name": server_name, **server_config}) | |
| return normalized_servers | |
| raise ValueError("MCP config missing 'servers' or 'mcpServers' key") | |
| def validate_mcp_config(config_path: Path | None = None) -> dict[str, Any]: | |
| """ | |
| Validate MCP configuration file paths. | |
| Args: | |
| config_path: Path to mcp_config.json (defaults to mcp-setup/mcp_config.json) | |
| Returns: | |
| Dictionary with validation results | |
| """ | |
| if config_path is None: | |
| config_path = project_root / "mcp-setup" / "mcp_config.json" | |
| if not config_path.exists(): | |
| return { | |
| "success": False, | |
| "error": f"MCP config file not found: {config_path}", | |
| } | |
| results: dict[str, Any] = { | |
| "success": True, | |
| "config_path": str(config_path), | |
| "servers_validated": 0, | |
| "servers_with_issues": 0, | |
| "issues": [], | |
| "warnings": [], | |
| } | |
| try: | |
| with open(config_path) as f: | |
| config = json.load(f) | |
| servers = _extract_servers(config) | |
| manager = SecurePathManager(base_dir=project_root) if PATH_MANAGER_AVAILABLE else None | |
| for server in servers: | |
| server_name = server.get("name", "unknown") | |
| results["servers_validated"] += 1 | |
| server_issues: list[str] = [] | |
| # Validate cwd | |
| cwd = server.get("cwd") | |
| if cwd: | |
| try: | |
| cwd_path = Path(cwd) | |
| if not cwd_path.exists(): | |
| server_issues.append(f"cwd does not exist: {cwd}") | |
| elif not cwd_path.is_dir(): | |
| server_issues.append(f"cwd is not a directory: {cwd}") | |
| except Exception as e: | |
| server_issues.append(f"Invalid cwd path '{cwd}': {e}") | |
| # Validate PYTHONPATH entries | |
| env = server.get("env", {}) | |
| pythonpath = env.get("PYTHONPATH", "") | |
| if pythonpath: | |
| # Split by platform-appropriate separator | |
| import os | |
| if os.name == "nt": # Windows | |
| paths = pythonpath.split(";") | |
| else: # Unix-like | |
| paths = pythonpath.split(":") | |
| for path_str in paths: | |
| if not path_str.strip(): | |
| continue | |
| try: | |
| path = Path(path_str.strip()) | |
| if manager: | |
| # Use SecurePathManager for validation | |
| result = manager.validate_path(path) | |
| if not result.is_valid: | |
| server_issues.append(f"Invalid PYTHONPATH entry '{path_str}': {result.reason}") | |
| else: | |
| # Basic validation | |
| if not path.exists(): | |
| server_issues.append(f"PYTHONPATH entry does not exist: {path_str}") | |
| elif not path.is_dir(): | |
| server_issues.append(f"PYTHONPATH entry is not a directory: {path_str}") | |
| except Exception as e: | |
| server_issues.append(f"Error validating PYTHONPATH entry '{path_str}': {e}") | |
| # Validate command path | |
| command = server.get("command") | |
| if command: | |
| try: | |
| cmd_path = Path(command) | |
| if cmd_path.is_absolute() or "/" in command: | |
| if not cmd_path.exists(): | |
| server_issues.append(f"Command executable does not exist: {command}") | |
| elif not cmd_path.is_file(): | |
| server_issues.append(f"Command is not a file: {command}") | |
| elif shutil.which(command) is None: | |
| server_issues.append(f"Command executable not found in PATH: {command}") | |
| except Exception as e: | |
| server_issues.append(f"Invalid command path '{command}': {e}") | |
| if server_issues: | |
| results["servers_with_issues"] += 1 | |
| results["issues"].append( | |
| { | |
| "server": server_name, | |
| "issues": server_issues, | |
| } | |
| ) | |
| except json.JSONDecodeError as e: | |
| return { | |
| "success": False, | |
| "error": f"Invalid JSON in config file: {e}", | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "error": f"Error validating config: {e}", | |
| } | |
| return results | |
| def main() -> int: | |
| """Main entry point.""" | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Validate MCP configuration paths") | |
| parser.add_argument( | |
| "--config", | |
| type=Path, | |
| help="Path to mcp_config.json (defaults to mcp-setup/mcp_config.json)", | |
| ) | |
| parser.add_argument("--json", action="store_true", help="Output results as JSON") | |
| args = parser.parse_args() | |
| results = validate_mcp_config(args.config) | |
| if args.json: | |
| import json | |
| print(json.dumps(results, indent=2)) | |
| else: | |
| print("=" * 70) | |
| print("MCP Configuration Path Validation") | |
| print("=" * 70) | |
| print() | |
| if not results["success"]: | |
| print(f"❌ Validation failed: {results.get('error', 'Unknown error')}") | |
| return 1 | |
| print(f"Config file: {results['config_path']}") | |
| print(f"Servers validated: {results['servers_validated']}") | |
| print(f"Servers with issues: {results['servers_with_issues']}") | |
| print() | |
| if results["issues"]: | |
| print("Issues found:") | |
| print("-" * 70) | |
| for issue_group in results["issues"]: | |
| print(f"Server: {issue_group['server']}") | |
| for issue in issue_group["issues"]: | |
| print(f" ⚠️ {issue}") | |
| print() | |
| else: | |
| print("✅ All paths validated successfully!") | |
| print() | |
| if results.get("warnings"): | |
| print("Warnings:") | |
| for warning in results["warnings"]: | |
| print(f" ⚠️ {warning}") | |
| print("=" * 70) | |
| return 0 if results["success"] and results["servers_with_issues"] == 0 else 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |
Xet Storage Details
- Size:
- 7.9 kB
- Xet hash:
- ce08903e1e9b7792e5e182b833f13037a83b499aaa08c4e1419c8b738bc4720c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.