Major configuration and tooling updates: Authentication Changes: - Remove username/password authentication support - Require PORTAINER_URL and PORTAINER_API_KEY (both mandatory) - Simplify PortainerConfig class and validation logic - Update all documentation to reflect API key requirement Multiple Runtime Support: - Add uvx support for running without installation - Add uv support with dedicated wrapper script - Add npx support with Node.js wrapper script - Maintain backward compatibility with direct Python execution Documentation Updates: - Comprehensive README.md with all execution methods - Detailed USAGE.md with step-by-step instructions - Updated .env.example with clear required vs optional sections - Enhanced docstrings in server.py and config.py Tooling Support: - package.json for npm/npx support with cross-platform wrapper - scripts/run-with-uv.py for uv integration - bin/portainer-core-mcp Node.js wrapper for npx - test_uvx.py for uvx functionality testing Configuration Improvements: - Clear separation of required vs optional environment variables - Better validation error messages - Simplified authentication flow - Enhanced project metadata in pyproject.toml 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
5.1 KiB
5.1 KiB
Portainer Core MCP Server Usage Guide
Quick Start
1. Set Required Environment Variables
# Required configuration
export PORTAINER_URL=https://your-portainer-instance.com
export PORTAINER_API_KEY=your-api-key-here
2. Generate Portainer API Key
- Log in to your Portainer instance
- Navigate to User Settings > API Tokens
- Click Add API Token
- Give it a name (e.g., "MCP Server")
- Copy the generated token
3. Start the Server
Choose your preferred method:
Option A: Using Python (Direct)
python run_server.py
Option B: Using uv (Recommended)
# Install uv if not installed
pip install uv
# Run with uv
uv run python run_server.py
Option C: Using uvx (Run without installing)
# Install uv if not installed
pip install uv
# Run directly without installation
uvx --from . portainer-core-mcp
Option D: Using npm/npx
# Install globally
npm install -g portainer-core-mcp
# Run with npx
npx portainer-core-mcp
Option E: Using the uv wrapper script
# Make executable (if not already)
chmod +x scripts/run-with-uv.py
# Run with uv wrapper
python scripts/run-with-uv.py
Environment Configuration
Using .env File
-
Copy the example file:
cp .env.example .env
-
Edit the file:
nano .env
-
Set your values:
# Required PORTAINER_URL=https://your-portainer-instance.com PORTAINER_API_KEY=your-api-key-here # Optional (with defaults) HTTP_TIMEOUT=30 MAX_RETRIES=3 LOG_LEVEL=INFO DEBUG=false
Using Environment Variables
# Export variables
export PORTAINER_URL=https://your-portainer-instance.com
export PORTAINER_API_KEY=your-api-key-here
# Start server
python run_server.py
Installation Methods
Development Installation
# Clone and install in development mode
git clone <repository-url>
cd portainer-mcp
pip install -e .
# Or with uv
uv pip install -e .
Production Installation
# Install from PyPI (when published)
pip install portainer-core-mcp
# Or with uv
uv pip install portainer-core-mcp
Run Without Installing (uvx)
# Run directly from source without installation
uvx --from . portainer-core-mcp
# Or from PyPI (when published)
uvx portainer-core-mcp
NPM Installation
# Install globally
npm install -g portainer-core-mcp
# Or run without installing
npx portainer-core-mcp
Available Commands
MCP Tools
Once the server is running, the following tools are available:
authenticate
- Login with username/passwordgenerate_token
- Generate API tokensget_current_user
- Get current user infolist_users
- List all userscreate_user
- Create new userupdate_user
- Update user detailsdelete_user
- Delete userget_settings
- Get Portainer settingsupdate_settings
- Update configurationhealth_check
- Server health status
MCP Resources
portainer://users
- User management dataportainer://settings
- Configuration settingsportainer://health
- Server health status
Testing
Run Tests
# Run all tests
python -m pytest
# Run with coverage
python -m pytest --cov=src
# Run specific test file
python -m pytest tests/test_basic.py
Test Configuration
# Test configuration loading
python -c "
import os
os.environ['PORTAINER_URL'] = 'https://test.com'
os.environ['PORTAINER_API_KEY'] = 'test-key'
from src.portainer_core.config import get_config
config = get_config()
print('✅ Configuration OK')
print(f'URL: {config.portainer_url}')
print(f'API Base: {config.api_base_url}')
"
Troubleshooting
Common Issues
-
Missing API Key Error
ValueError: PORTAINER_API_KEY must be provided and cannot be empty
Solution: Set the
PORTAINER_API_KEY
environment variable -
Invalid URL Error
ValueError: Invalid Portainer URL
Solution: Ensure
PORTAINER_URL
includes the protocol (http/https) -
Python Not Found (npx)
Error: Python is not installed or not in PATH
Solution: Install Python 3.8+ and ensure it's in your PATH
Debug Mode
Enable debug logging for troubleshooting:
DEBUG=true LOG_LEVEL=DEBUG python run_server.py
Health Check
Test if the server is working:
# Check server health
curl -X POST http://localhost:8000/health
# Or use the health_check tool through MCP
Advanced Usage
Custom Configuration
from portainer_core.config import PortainerConfig
# Create custom config
config = PortainerConfig(
portainer_url="https://custom.portainer.com",
portainer_api_key="custom-key",
http_timeout=60,
log_level="DEBUG"
)
Programmatic Usage
from portainer_core.server import create_server
import asyncio
async def main():
server = create_server()
await server.run()
asyncio.run(main())
Support
- Documentation: See README.md and code comments
- Issues: Report via GitHub Issues
- Configuration: Check .env.example for all options