#!/usr/bin/env python3 """ Portainer Core MCP Server - Entry Point Script. This script serves as the main entry point for the Portainer Core MCP Server, providing authentication and user management functionality for Portainer Business Edition. It handles environment setup, configuration validation, and server startup. Usage: python run_server.py Environment Variables: PORTAINER_URL: The base URL of your Portainer instance (required) PORTAINER_API_KEY: API key for authentication (option 1) PORTAINER_USERNAME: Username for authentication (option 2) PORTAINER_PASSWORD: Password for authentication (option 2) Example: export PORTAINER_URL=https://portainer.example.com export PORTAINER_API_KEY=your-api-key-here python run_server.py Architecture: - Environment setup and validation - Configuration loading from environment variables - Async server initialization and startup - Graceful error handling and shutdown """ import os import sys import asyncio from portainer_core.server import main def setup_environment(): """ Set up environment variables with fallback values for demo/testing. This function ensures the required environment variables are set for the server to start properly. It provides fallback values for demo purposes when specific configuration is not provided. Environment Variables Handled: PORTAINER_URL: Sets default demo URL if not provided PORTAINER_API_KEY: Sets placeholder if no authentication is configured PORTAINER_USERNAME/PORTAINER_PASSWORD: Alternative authentication method Raises: None: This function doesn't raise exceptions but prints warnings for missing configuration. Side Effects: - Modifies os.environ with default values - Prints configuration messages to stdout - Ensures minimum viable configuration for server startup Note: In production environments, always provide proper authentication credentials rather than relying on demo/placeholder values. """ # Configure Portainer URL with fallback to demo instance if not os.environ.get('PORTAINER_URL'): os.environ['PORTAINER_URL'] = 'https://demo.portainer.io' print("šŸ”§ Using demo Portainer URL: https://demo.portainer.io") # Configure authentication - requires either API key or username/password has_api_key = os.environ.get('PORTAINER_API_KEY') has_credentials = (os.environ.get('PORTAINER_USERNAME') and os.environ.get('PORTAINER_PASSWORD')) if not has_api_key and not has_credentials: print("āš ļø No authentication configured. Set PORTAINER_API_KEY or PORTAINER_USERNAME/PORTAINER_PASSWORD") print(" For demo purposes, using placeholder API key") os.environ['PORTAINER_API_KEY'] = 'demo-api-key' if __name__ == "__main__": """ Main execution block for the Portainer Core MCP Server. This block handles the complete server lifecycle: 1. Environment setup and configuration validation 2. Async server initialization 3. Graceful error handling and shutdown 4. User-friendly status messages Exit Codes: 0: Successful shutdown (user interrupt) 1: Server failure or configuration error Flow: 1. Print startup messages and configuration requirements 2. Setup environment variables with fallbacks 3. Start async server using asyncio.run() 4. Handle interrupts and exceptions gracefully """ print("šŸš€ Starting Portainer Core MCP Server...") print(" Configuration will be loaded from environment variables") print(" Set PORTAINER_URL and PORTAINER_API_KEY (or username/password) before running") print("") # Initialize environment with fallback values for demo/testing setup_environment() try: # Start the async MCP server - this blocks until shutdown asyncio.run(main()) except KeyboardInterrupt: # Handle graceful shutdown on Ctrl+C print("\nšŸ‘‹ Server stopped by user") sys.exit(0) except Exception as e: # Handle any startup or runtime errors print(f"\nāŒ Server failed: {e}") sys.exit(1)