import json
from jsonschema import validate, ValidationError

class MCPConfigValidator:
    """
    Validates MCP configuration files against a predefined schema.
    """

    def __init__(self, schema_path):
        """
        Initializes the validator with the schema path.

        Args:
            schema_path (str): The path to the JSON schema file.
        """
        self.schema = self._load_schema(schema_path)

    def _load_schema(self, schema_path):
        """
        Loads the JSON schema from the given path.

        Args:
            schema_path (str): The path to the JSON schema file.

        Returns:
            dict: The loaded JSON schema.

        Raises:
            FileNotFoundError: If the schema file is not found.
            json.JSONDecodeError: If the schema file is not valid JSON.
        """
        try:
            with open(schema_path, 'r') as f:
                return json.load(f)
        except FileNotFoundError:
            raise FileNotFoundError(f"Schema file not found: {schema_path}")
        except json.JSONDecodeError as e:
            raise json.JSONDecodeError(f"Invalid JSON in schema file: {schema_path}", e.doc, e.pos)

    def validate_config(self, config_path):
        """
        Validates the MCP configuration file against the loaded schema.

        Args:
            config_path (str): The path to the MCP configuration file.

        Returns:
            list: A list of validation error messages.  Empty if valid.

        Raises:
            FileNotFoundError: If the configuration file is not found.
            json.JSONDecodeError: If the configuration file is not valid JSON.
        """
        try:
            with open(config_path, 'r') as f:
                config_data = json.load(f)
        except FileNotFoundError:
            raise FileNotFoundError(f"Config file not found: {config_path}")
        except json.JSONDecodeError as e:
            raise json.JSONDecodeError(f"Invalid JSON in config file: {config_path}", e.doc, e.pos)

        errors = []
        try:
            validate(instance=config_data, schema=self.schema)
        except ValidationError as e:
            errors.append(f"Validation error: {e.message} at {'.'.join(map(str, e.path))}")  # Join the path elements using '.'
        return errors

    def is_valid(self, config_path):
        """
        Validates and returns a boolean result.

        Args:
            config_path (str): The path to the MCP configuration file.

        Returns:
            bool: True if the config is valid, False otherwise.
        """
        return len(self.validate_config(config_path)) == 0

if __name__ == '__main__':
    # Example usage (replace with actual paths)
    schema_path = 'mcp_schema.json'  # Path to your schema file
    config_path = 'mcp-config.json'  # Path to your config file

    # Create a dummy schema file
    with open(schema_path, 'w') as f:
        json.dump({
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "version": {"type": "number", "minimum": 1.0}
            },
            "required": ["name", "version"]
        }, f, indent=4)

    # Create a dummy config file
    with open(config_path, 'w') as f:
        json.dump({
            "name": "MyMCP",
            "version": 1.2
        }, f, indent=4)

    validator = MCPConfigValidator(schema_path)

    errors = validator.validate_config(config_path)

    if errors:
        print("Config validation failed:")
        for error in errors:
            print(f"- {error}")
    else:
        print("Config validation successful!")