Compare commits
2 commits
18536ac16f
...
004d5614e2
| Author | SHA1 | Date | |
|---|---|---|---|
| 004d5614e2 | |||
| 8cd790d2a6 |
50 changed files with 5267 additions and 0 deletions
16
.env.example
Normal file
16
.env.example
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# Ghost MCP Server Configuration
|
||||||
|
|
||||||
|
# Ghost instance configuration
|
||||||
|
GHOST_URL=http://localhost:2368
|
||||||
|
GHOST_CONTENT_API_KEY=your_content_api_key_here
|
||||||
|
GHOST_ADMIN_API_KEY=your_admin_api_key_here
|
||||||
|
GHOST_VERSION=v5.0
|
||||||
|
GHOST_MODE=auto
|
||||||
|
GHOST_TIMEOUT=30
|
||||||
|
GHOST_MAX_RETRIES=3
|
||||||
|
GHOST_RETRY_BACKOFF_FACTOR=2.0
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
LOG_LEVEL=info
|
||||||
|
LOG_STRUCTURED=true
|
||||||
|
LOG_REQUEST_ID=true
|
||||||
55
.gitignore
vendored
Normal file
55
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
# Python
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
*.so
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
|
||||||
|
# Virtual environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
|
||||||
|
# OS
|
||||||
|
.DS_Store
|
||||||
|
.DS_Store?
|
||||||
|
._*
|
||||||
|
.Spotlight-V100
|
||||||
|
.Trashes
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
.pytest_cache/
|
||||||
|
.coverage
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
7
CLAUDE.md
Normal file
7
CLAUDE.md
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# !!!IMPORTANT!!!
|
||||||
|
|
||||||
|
- Always use `docker compose` instead of `docker-compose`
|
||||||
208
Makefile
Normal file
208
Makefile
Normal file
|
|
@ -0,0 +1,208 @@
|
||||||
|
# Ghost MCP Development Makefile
|
||||||
|
|
||||||
|
.PHONY: help install install-uv start-ghost stop-ghost setup-tokens test test-connection run dev clean logs status check-deps
|
||||||
|
|
||||||
|
# Default target
|
||||||
|
help: ## Show this help message
|
||||||
|
@echo "Ghost MCP Development Commands"
|
||||||
|
@echo "============================="
|
||||||
|
@echo ""
|
||||||
|
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
|
||||||
|
@echo ""
|
||||||
|
@echo "Quick start:"
|
||||||
|
@echo " make install-uv # Install uv package manager (if not installed)"
|
||||||
|
@echo " make install # Install Python dependencies"
|
||||||
|
@echo " make start-ghost # Start Ghost and database containers"
|
||||||
|
@echo " make setup-tokens # Extract API keys and create .env file"
|
||||||
|
@echo " make test # Test the implementation"
|
||||||
|
@echo " make run # Run the MCP server"
|
||||||
|
|
||||||
|
# Python environment setup
|
||||||
|
install-uv: ## Install uv package manager
|
||||||
|
@echo "📦 Installing uv package manager..."
|
||||||
|
@if command -v uv >/dev/null 2>&1; then \
|
||||||
|
echo "✅ uv is already installed"; \
|
||||||
|
uv --version; \
|
||||||
|
else \
|
||||||
|
echo "Installing uv..."; \
|
||||||
|
curl -LsSf https://astral.sh/uv/install.sh | sh; \
|
||||||
|
echo "✅ uv installed successfully"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
install: ## Install Python dependencies using uv
|
||||||
|
@echo "📦 Installing Python dependencies with uv..."
|
||||||
|
@if ! command -v uv >/dev/null 2>&1; then \
|
||||||
|
echo "❌ uv not found. Run 'make install-uv' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
uv sync
|
||||||
|
@echo "✅ Dependencies installed successfully"
|
||||||
|
|
||||||
|
install-pip: ## Install Python dependencies using pip (fallback)
|
||||||
|
@echo "📦 Installing Python dependencies with pip..."
|
||||||
|
python -m pip install -e .
|
||||||
|
@echo "✅ Dependencies installed successfully"
|
||||||
|
|
||||||
|
# Docker environment
|
||||||
|
start-ghost: ## Start Ghost and database containers
|
||||||
|
@echo "🐳 Starting Ghost development environment..."
|
||||||
|
docker-compose up -d
|
||||||
|
@echo "⏳ Waiting for containers to be healthy..."
|
||||||
|
@timeout=60; \
|
||||||
|
while [ $$timeout -gt 0 ]; do \
|
||||||
|
if docker-compose ps | grep -q "ghost-mcp-dev.*Up" && docker-compose ps | grep -q "ghost-db-dev.*Up.*healthy"; then \
|
||||||
|
echo "✅ Ghost containers are running and healthy"; \
|
||||||
|
break; \
|
||||||
|
fi; \
|
||||||
|
echo " Waiting for containers... ($$timeout seconds remaining)"; \
|
||||||
|
sleep 2; \
|
||||||
|
timeout=$$((timeout - 2)); \
|
||||||
|
done; \
|
||||||
|
if [ $$timeout -le 0 ]; then \
|
||||||
|
echo "❌ Containers did not start properly"; \
|
||||||
|
make logs; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
@echo ""
|
||||||
|
@echo "🎉 Ghost is ready!"
|
||||||
|
@echo " Ghost admin: http://localhost:2368/ghost/"
|
||||||
|
@echo " Ghost site: http://localhost:2368/"
|
||||||
|
@echo " Database: Available on port 3306"
|
||||||
|
|
||||||
|
stop-ghost: ## Stop Ghost and database containers
|
||||||
|
@echo "🛑 Stopping Ghost development environment..."
|
||||||
|
docker-compose down
|
||||||
|
@echo "✅ Ghost containers stopped"
|
||||||
|
|
||||||
|
restart-ghost: ## Restart Ghost and database containers
|
||||||
|
@echo "🔄 Restarting Ghost development environment..."
|
||||||
|
docker-compose restart
|
||||||
|
@echo "✅ Ghost containers restarted"
|
||||||
|
|
||||||
|
# API token setup
|
||||||
|
setup-tokens: ## Extract API keys from Ghost database and create .env file
|
||||||
|
@echo "🔑 Setting up API tokens..."
|
||||||
|
./scripts/setup-tokens.sh
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
test-connection: ## Test Ghost API connectivity
|
||||||
|
@if [ ! -f .env ]; then \
|
||||||
|
echo "❌ .env file not found. Run 'make setup-tokens' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
@python scripts/test-connection.py
|
||||||
|
|
||||||
|
test: check-deps test-connection ## Run all tests
|
||||||
|
@echo "🧪 Running comprehensive tests..."
|
||||||
|
@echo "Testing MCP tools registration..."
|
||||||
|
@python -c "\
|
||||||
|
import sys; \
|
||||||
|
sys.path.insert(0, 'src'); \
|
||||||
|
from ghost_mcp.server import mcp; \
|
||||||
|
print(f'✅ FastMCP server initialized'); \
|
||||||
|
print(f' Tools registered: {len([attr for attr in dir(mcp) if not attr.startswith(\"_\")])}+')"
|
||||||
|
@echo "✅ All tests passed!"
|
||||||
|
|
||||||
|
# Running the server
|
||||||
|
run: check-deps ## Run the Ghost MCP server
|
||||||
|
@echo "🚀 Starting Ghost MCP server..."
|
||||||
|
@if [ ! -f .env ]; then \
|
||||||
|
echo "❌ .env file not found. Run 'make setup-tokens' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
python -m ghost_mcp.server
|
||||||
|
|
||||||
|
dev: check-deps ## Run the Ghost MCP server in development mode with auto-reload
|
||||||
|
@echo "🚀 Starting Ghost MCP server in development mode..."
|
||||||
|
@if [ ! -f .env ]; then \
|
||||||
|
echo "❌ .env file not found. Run 'make setup-tokens' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
python -m ghost_mcp.server --dev
|
||||||
|
|
||||||
|
# Utilities
|
||||||
|
logs: ## Show Docker container logs
|
||||||
|
@echo "📋 Showing container logs..."
|
||||||
|
docker-compose logs -f
|
||||||
|
|
||||||
|
status: ## Show status of all components
|
||||||
|
@echo "📊 Ghost MCP Status"
|
||||||
|
@echo "=================="
|
||||||
|
@echo ""
|
||||||
|
@echo "🐳 Docker Containers:"
|
||||||
|
@docker-compose ps || echo " No containers running"
|
||||||
|
@echo ""
|
||||||
|
@echo "📁 Configuration:"
|
||||||
|
@if [ -f .env ]; then \
|
||||||
|
echo " ✅ .env file exists"; \
|
||||||
|
echo " 📍 Ghost URL: $$(grep GHOST_URL .env | cut -d= -f2)"; \
|
||||||
|
echo " 🔑 Content API: $$(grep GHOST_CONTENT_API_KEY .env | cut -d= -f2 | cut -c1-10)..."; \
|
||||||
|
echo " 🔑 Admin API: $$(grep GHOST_ADMIN_API_KEY .env | cut -d= -f2 | cut -c1-10)..."; \
|
||||||
|
else \
|
||||||
|
echo " ❌ .env file missing"; \
|
||||||
|
fi
|
||||||
|
@echo ""
|
||||||
|
@echo "🐍 Python Environment:"
|
||||||
|
@if command -v uv >/dev/null 2>&1; then \
|
||||||
|
echo " ✅ uv: $$(uv --version)"; \
|
||||||
|
else \
|
||||||
|
echo " ❌ uv not installed"; \
|
||||||
|
fi
|
||||||
|
@echo " 🐍 Python: $$(python --version)"
|
||||||
|
@if python -c "import ghost_mcp" 2>/dev/null; then \
|
||||||
|
echo " ✅ ghost_mcp package installed"; \
|
||||||
|
else \
|
||||||
|
echo " ❌ ghost_mcp package not installed"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
check-deps: ## Check if all dependencies are available
|
||||||
|
@if [ ! -f .env ]; then \
|
||||||
|
echo "❌ .env file not found. Run 'make setup-tokens' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
@if ! python -c "import ghost_mcp" 2>/dev/null; then \
|
||||||
|
echo "❌ ghost_mcp package not installed. Run 'make install' first"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
clean: ## Clean up development environment
|
||||||
|
@echo "🧹 Cleaning up development environment..."
|
||||||
|
docker-compose down -v
|
||||||
|
rm -f .env
|
||||||
|
@if command -v uv >/dev/null 2>&1; then \
|
||||||
|
uv clean; \
|
||||||
|
fi
|
||||||
|
@echo "✅ Cleanup complete"
|
||||||
|
|
||||||
|
# Development workflow
|
||||||
|
setup: install-uv install start-ghost setup-tokens ## Complete setup from scratch
|
||||||
|
@echo ""
|
||||||
|
@echo "🎉 Ghost MCP setup complete!"
|
||||||
|
@echo ""
|
||||||
|
@echo "Ready to use:"
|
||||||
|
@echo " make test # Test the implementation"
|
||||||
|
@echo " make run # Run the MCP server"
|
||||||
|
@echo " make status # Check system status"
|
||||||
|
|
||||||
|
# Documentation
|
||||||
|
docs: ## Show important URLs and information
|
||||||
|
@echo "📚 Ghost MCP Documentation"
|
||||||
|
@echo "========================="
|
||||||
|
@echo ""
|
||||||
|
@echo "🌐 Web Interfaces:"
|
||||||
|
@echo " Ghost Admin: http://localhost:2368/ghost/"
|
||||||
|
@echo " Ghost Site: http://localhost:2368/"
|
||||||
|
@echo " phpMyAdmin: http://localhost:8080/ (if enabled)"
|
||||||
|
@echo ""
|
||||||
|
@echo "📁 Important Files:"
|
||||||
|
@echo " Configuration: .env"
|
||||||
|
@echo " Project: pyproject.toml"
|
||||||
|
@echo " Docker: docker-compose.yml"
|
||||||
|
@echo " Setup Script: scripts/setup-tokens.sh"
|
||||||
|
@echo ""
|
||||||
|
@echo "🔧 Development Commands:"
|
||||||
|
@echo " make setup # Complete initial setup"
|
||||||
|
@echo " make test # Test functionality"
|
||||||
|
@echo " make run # Run MCP server"
|
||||||
|
@echo " make logs # View container logs"
|
||||||
|
@echo " make status # Check system status"
|
||||||
161
README-DOCKER.md
Normal file
161
README-DOCKER.md
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
# Ghost MCP - Docker Development Setup
|
||||||
|
|
||||||
|
This guide helps you set up a Ghost instance for Ghost MCP development and API investigation.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
1. **Copy environment file**:
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Start Ghost and MySQL**:
|
||||||
|
```bash
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Access Ghost**:
|
||||||
|
- Ghost Admin: http://localhost:2368/ghost
|
||||||
|
- Ghost Site: http://localhost:2368
|
||||||
|
- phpMyAdmin (optional): http://localhost:8080
|
||||||
|
|
||||||
|
## Services
|
||||||
|
|
||||||
|
### Ghost (Main Service)
|
||||||
|
- **Image**: `ghost:5-alpine`
|
||||||
|
- **Port**: 2368
|
||||||
|
- **URL**: http://localhost:2368
|
||||||
|
- **Admin**: http://localhost:2368/ghost
|
||||||
|
|
||||||
|
### MySQL Database
|
||||||
|
- **Image**: `mysql:8.0`
|
||||||
|
- **Database**: `ghost_dev` (configurable via .env)
|
||||||
|
- **User**: `ghost` (configurable via .env)
|
||||||
|
|
||||||
|
### phpMyAdmin (Optional Debug Tool)
|
||||||
|
- **Image**: `phpmyadmin/phpmyadmin:latest`
|
||||||
|
- **Port**: 8080
|
||||||
|
- **Access**: http://localhost:8080
|
||||||
|
|
||||||
|
To start with phpMyAdmin for database debugging:
|
||||||
|
```bash
|
||||||
|
docker compose --profile debug up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
## Initial Setup
|
||||||
|
|
||||||
|
### 1. First-time Ghost Setup
|
||||||
|
1. Start the services: `docker compose up -d`
|
||||||
|
2. Wait for Ghost to initialize (check logs: `docker compose logs ghost`)
|
||||||
|
3. Visit http://localhost:2368/ghost
|
||||||
|
4. Create your admin account
|
||||||
|
5. Complete the Ghost setup wizard
|
||||||
|
|
||||||
|
### 2. Obtain API Keys
|
||||||
|
1. In Ghost Admin, go to Settings → Integrations
|
||||||
|
2. Click "Add custom integration"
|
||||||
|
3. Give it a name (e.g., "MCP Development")
|
||||||
|
4. Copy both API keys:
|
||||||
|
- **Content API Key**: For read-only operations
|
||||||
|
- **Admin API Key**: For read/write operations
|
||||||
|
|
||||||
|
### 3. Update Environment (Optional)
|
||||||
|
Add the API keys to your `.env` file:
|
||||||
|
```bash
|
||||||
|
GHOST_CONTENT_API_KEY=your_content_api_key_here
|
||||||
|
GHOST_ADMIN_API_KEY=your_admin_api_key_here
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Testing
|
||||||
|
|
||||||
|
### Content API (Read-only)
|
||||||
|
Test the Content API with curl:
|
||||||
|
```bash
|
||||||
|
# Get all posts
|
||||||
|
curl "http://localhost:2368/ghost/api/content/posts/?key=YOUR_CONTENT_API_KEY"
|
||||||
|
|
||||||
|
# Get site settings
|
||||||
|
curl "http://localhost:2368/ghost/api/content/settings/?key=YOUR_CONTENT_API_KEY"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Admin API (Read/Write)
|
||||||
|
The Admin API requires JWT authentication. You'll need to generate a JWT token using your Admin API key.
|
||||||
|
|
||||||
|
## Management Commands
|
||||||
|
|
||||||
|
### Start services
|
||||||
|
```bash
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stop services
|
||||||
|
```bash
|
||||||
|
docker compose down
|
||||||
|
```
|
||||||
|
|
||||||
|
### View logs
|
||||||
|
```bash
|
||||||
|
# All services
|
||||||
|
docker compose logs
|
||||||
|
|
||||||
|
# Ghost only
|
||||||
|
docker compose logs ghost
|
||||||
|
|
||||||
|
# Follow logs
|
||||||
|
docker compose logs -f ghost
|
||||||
|
```
|
||||||
|
|
||||||
|
### Reset everything (⚠️ Destroys all data)
|
||||||
|
```bash
|
||||||
|
docker compose down -v
|
||||||
|
docker volume rm ghost_mcp_content ghost_mcp_mysql
|
||||||
|
```
|
||||||
|
|
||||||
|
### Backup data
|
||||||
|
```bash
|
||||||
|
# Create backup of Ghost content
|
||||||
|
docker run --rm -v ghost_mcp_content:/data -v $(pwd):/backup alpine tar czf /backup/ghost-content-backup.tar.gz -C /data .
|
||||||
|
|
||||||
|
# Create backup of MySQL data
|
||||||
|
docker run --rm -v ghost_mcp_mysql:/data -v $(pwd):/backup alpine tar czf /backup/mysql-backup.tar.gz -C /data .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Ghost won't start
|
||||||
|
1. Check if MySQL is healthy: `docker compose ps`
|
||||||
|
2. Check Ghost logs: `docker compose logs ghost`
|
||||||
|
3. Verify environment variables in `.env`
|
||||||
|
|
||||||
|
### Can't access Ghost admin
|
||||||
|
1. Ensure Ghost is running: `docker compose ps`
|
||||||
|
2. Check if port 2368 is available: `netstat -an | grep 2368`
|
||||||
|
3. Try accessing directly: http://localhost:2368/ghost
|
||||||
|
|
||||||
|
### Database connection issues
|
||||||
|
1. Check MySQL health: `docker compose logs ghost-db`
|
||||||
|
2. Verify database credentials in `.env`
|
||||||
|
3. Ensure database has started before Ghost
|
||||||
|
|
||||||
|
### API requests failing
|
||||||
|
1. Verify API keys are correct
|
||||||
|
2. Check Content API: should work with query parameter
|
||||||
|
3. Check Admin API: requires proper JWT token generation
|
||||||
|
|
||||||
|
## Development Notes
|
||||||
|
|
||||||
|
- Ghost data persists in named Docker volumes
|
||||||
|
- Database is accessible via phpMyAdmin at localhost:8080
|
||||||
|
- All Ghost content (themes, images, etc.) is stored in the `ghost_content` volume
|
||||||
|
- MySQL data is stored in the `ghost_mysql` volume
|
||||||
|
- Environment variables are loaded from `.env` file
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Once Ghost is running, you can:
|
||||||
|
1. Create test content (posts, pages, tags)
|
||||||
|
2. Test API endpoints
|
||||||
|
3. Begin MCP server development
|
||||||
|
4. Investigate Ghost API authentication flows
|
||||||
|
|
||||||
|
For MCP development, see the main project documentation.
|
||||||
215
contracts/admin-api-auth.md
Normal file
215
contracts/admin-api-auth.md
Normal file
|
|
@ -0,0 +1,215 @@
|
||||||
|
# Ghost Admin API JWT Authentication Documentation
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Ghost Admin API uses JWT (JSON Web Token) authentication for secure, server-side access to read/write operations.
|
||||||
|
|
||||||
|
## Authentication Flow
|
||||||
|
|
||||||
|
### 1. API Key Structure
|
||||||
|
**Format**: `{id}:{secret}`
|
||||||
|
- **ID**: Used as the `kid` (key identifier) in JWT header
|
||||||
|
- **Secret**: Used to sign the JWT token
|
||||||
|
- **Source**: Generated from Ghost Admin → Settings → Integrations → Custom Integration
|
||||||
|
|
||||||
|
**Example**: `507f1f77bcf86cd799439011:1234567890abcdef1234567890abcdef12345678`
|
||||||
|
|
||||||
|
### 2. JWT Token Generation
|
||||||
|
|
||||||
|
#### Required Headers
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"alg": "HS256",
|
||||||
|
"kid": "{api_key_id}",
|
||||||
|
"typ": "JWT"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Required Payload
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"exp": {timestamp_plus_5_minutes},
|
||||||
|
"iat": {current_timestamp},
|
||||||
|
"aud": "/admin/"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Constraints
|
||||||
|
- **Token Expiration**: Maximum 5 minutes from generation
|
||||||
|
- **Algorithm**: HS256 (HMAC SHA-256)
|
||||||
|
- **Audience**: Must be `/admin/`
|
||||||
|
- **Key ID**: Must match the API key ID
|
||||||
|
|
||||||
|
### 3. HTTP Request Format
|
||||||
|
|
||||||
|
#### Required Headers
|
||||||
|
```http
|
||||||
|
Authorization: Ghost {jwt_token}
|
||||||
|
Accept-Version: v5.0
|
||||||
|
Content-Type: application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example Request
|
||||||
|
```http
|
||||||
|
GET /ghost/api/admin/posts/ HTTP/1.1
|
||||||
|
Host: localhost:2368
|
||||||
|
Authorization: Ghost eyJhbGciOiJIUzI1NiIsImtpZCI6IjUwN2YxZjc3YmNmODZjZDc5OTQzOTAxMSIsInR5cCI6IkpXVCJ9...
|
||||||
|
Accept-Version: v5.0
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Requirements
|
||||||
|
|
||||||
|
### Node.js Implementation (for MCP Server)
|
||||||
|
|
||||||
|
#### Dependencies
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"jsonwebtoken": "^9.0.0"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Token Generation Function
|
||||||
|
```javascript
|
||||||
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
|
function generateAdminApiToken(apiKey) {
|
||||||
|
// Split the key into ID and secret
|
||||||
|
const [id, secret] = apiKey.split(':');
|
||||||
|
|
||||||
|
// Prepare header
|
||||||
|
const header = {
|
||||||
|
alg: 'HS256',
|
||||||
|
kid: id,
|
||||||
|
typ: 'JWT'
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prepare payload
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
const payload = {
|
||||||
|
exp: now + (5 * 60), // 5 minutes from now
|
||||||
|
iat: now,
|
||||||
|
aud: '/admin/'
|
||||||
|
};
|
||||||
|
|
||||||
|
// Generate token
|
||||||
|
const token = jwt.sign(payload, Buffer.from(secret, 'hex'), { header });
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Usage Example
|
||||||
|
```javascript
|
||||||
|
const adminApiKey = 'your_admin_api_key_here';
|
||||||
|
const token = generateAdminApiToken(adminApiKey);
|
||||||
|
|
||||||
|
const response = await fetch('http://localhost:2368/ghost/api/admin/posts/', {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Ghost ${token}`,
|
||||||
|
'Accept-Version': 'v5.0',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Token Refresh Strategy
|
||||||
|
Since tokens expire after 5 minutes:
|
||||||
|
1. **Generate new token for each request** (simplest)
|
||||||
|
2. **Cache token with expiration tracking** (more efficient)
|
||||||
|
3. **Regenerate on 401 response** (error-driven)
|
||||||
|
|
||||||
|
### Recommended Approach for MCP Server
|
||||||
|
```javascript
|
||||||
|
class GhostAdminAuth {
|
||||||
|
constructor(apiKey) {
|
||||||
|
this.apiKey = apiKey;
|
||||||
|
this.token = null;
|
||||||
|
this.tokenExpiry = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
generateToken() {
|
||||||
|
const [id, secret] = this.apiKey.split(':');
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
exp: now + (4 * 60), // 4 minutes (buffer)
|
||||||
|
iat: now,
|
||||||
|
aud: '/admin/'
|
||||||
|
};
|
||||||
|
|
||||||
|
this.token = jwt.sign(payload, Buffer.from(secret, 'hex'), {
|
||||||
|
header: { alg: 'HS256', kid: id, typ: 'JWT' }
|
||||||
|
});
|
||||||
|
this.tokenExpiry = now + (4 * 60);
|
||||||
|
|
||||||
|
return this.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
getValidToken() {
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
if (!this.token || now >= this.tokenExpiry) {
|
||||||
|
return this.generateToken();
|
||||||
|
}
|
||||||
|
return this.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAuthHeaders() {
|
||||||
|
return {
|
||||||
|
'Authorization': `Ghost ${this.getValidToken()}`,
|
||||||
|
'Accept-Version': 'v5.0',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
### Invalid Token
|
||||||
|
**Response**: 401 Unauthorized
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Authorization failed",
|
||||||
|
"context": "Unable to determine the authenticated user or integration...",
|
||||||
|
"type": "UnauthorizedError"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Expired Token
|
||||||
|
**Response**: 401 Unauthorized
|
||||||
|
**Action**: Generate new token and retry
|
||||||
|
|
||||||
|
### Invalid API Key
|
||||||
|
**Response**: 401 Unauthorized
|
||||||
|
**Action**: Verify API key format and permissions
|
||||||
|
|
||||||
|
## Security Considerations
|
||||||
|
|
||||||
|
1. **Keep API Key Secret**: Never expose in client-side code
|
||||||
|
2. **Server-Side Only**: JWT generation must happen server-side
|
||||||
|
3. **Short Token Lifespan**: Maximum 5 minutes reduces exposure window
|
||||||
|
4. **HTTPS in Production**: Always use HTTPS for API requests
|
||||||
|
5. **Key Rotation**: Regularly rotate API keys in production
|
||||||
|
|
||||||
|
## Testing Strategy
|
||||||
|
|
||||||
|
### Manual Testing
|
||||||
|
1. Generate API key from Ghost Admin
|
||||||
|
2. Create JWT token using the implementation
|
||||||
|
3. Test various Admin API endpoints
|
||||||
|
4. Verify token expiration handling
|
||||||
|
|
||||||
|
### Automated Testing
|
||||||
|
1. Mock JWT generation for unit tests
|
||||||
|
2. Use test API keys for integration tests
|
||||||
|
3. Test token refresh logic
|
||||||
|
4. Test error handling scenarios
|
||||||
|
|
||||||
|
## Implementation Status
|
||||||
|
- ✅ Authentication flow documented
|
||||||
|
- ✅ JWT generation requirements identified
|
||||||
|
- ✅ Node.js implementation planned
|
||||||
|
- ⏳ Implementation pending API key generation
|
||||||
|
- ⏳ Testing pending API key availability
|
||||||
93
contracts/api-endpoints.md
Normal file
93
contracts/api-endpoints.md
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
# Ghost API Endpoints Documentation
|
||||||
|
|
||||||
|
## Content API Endpoints (Read-Only)
|
||||||
|
|
||||||
|
### Base URL Structure
|
||||||
|
- **Base URL**: `http://localhost:2368/ghost/api/content/`
|
||||||
|
- **Authentication**: Content API key as query parameter
|
||||||
|
- **Version**: v5.0 (current)
|
||||||
|
|
||||||
|
### Documented Endpoints
|
||||||
|
|
||||||
|
#### Posts
|
||||||
|
- `GET /posts/` - List all published posts
|
||||||
|
- `GET /posts/{id}/` - Get specific post by ID
|
||||||
|
- `GET /posts/slug/{slug}/` - Get specific post by slug
|
||||||
|
|
||||||
|
#### Pages
|
||||||
|
- `GET /pages/` - List all published pages
|
||||||
|
- `GET /pages/{id}/` - Get specific page by ID
|
||||||
|
- `GET /pages/slug/{slug}/` - Get specific page by slug
|
||||||
|
|
||||||
|
#### Tags
|
||||||
|
- `GET /tags/` - List all tags
|
||||||
|
- `GET /tags/{id}/` - Get specific tag by ID
|
||||||
|
- `GET /tags/slug/{slug}/` - Get specific tag by slug
|
||||||
|
|
||||||
|
#### Authors
|
||||||
|
- `GET /authors/` - List all authors
|
||||||
|
- `GET /authors/{id}/` - Get specific author by ID
|
||||||
|
- `GET /authors/slug/{slug}/` - Get specific author by slug
|
||||||
|
|
||||||
|
#### Other
|
||||||
|
- `GET /tiers/` - List membership tiers
|
||||||
|
- `GET /settings/` - Get public settings
|
||||||
|
|
||||||
|
## Admin API Endpoints (Read/Write)
|
||||||
|
|
||||||
|
### Base URL Structure
|
||||||
|
- **Base URL**: `http://localhost:2368/ghost/api/admin/`
|
||||||
|
- **Authentication**: JWT token in Authorization header
|
||||||
|
- **Version**: v5.0 (current)
|
||||||
|
|
||||||
|
### Documented Endpoints
|
||||||
|
|
||||||
|
#### Site Information
|
||||||
|
- `GET /site/` - Get site information (tested - requires auth)
|
||||||
|
|
||||||
|
#### Users
|
||||||
|
- `GET /users/me/` - Get current user (tested - requires auth)
|
||||||
|
|
||||||
|
#### Posts (Admin)
|
||||||
|
- `GET /posts/` - Browse all posts (including drafts)
|
||||||
|
- `GET /posts/{id}/` - Read specific post
|
||||||
|
- `POST /posts/` - Create new post
|
||||||
|
- `PUT /posts/{id}/` - Update existing post
|
||||||
|
- `POST /posts/{id}/copy/` - Copy post
|
||||||
|
- `DELETE /posts/{id}/` - Delete post
|
||||||
|
|
||||||
|
#### Pages (Admin)
|
||||||
|
- `GET /pages/` - Browse all pages
|
||||||
|
- `GET /pages/{id}/` - Read specific page
|
||||||
|
- `POST /pages/` - Create new page
|
||||||
|
- `PUT /pages/{id}/` - Update existing page
|
||||||
|
- `POST /pages/{id}/copy/` - Copy page
|
||||||
|
- `DELETE /pages/{id}/` - Delete page
|
||||||
|
|
||||||
|
#### Tags (Admin)
|
||||||
|
- `GET /tags/` - Browse all tags
|
||||||
|
- `GET /tags/{id}/` - Read specific tag
|
||||||
|
- `POST /tags/` - Create new tag
|
||||||
|
- `PUT /tags/{id}/` - Update existing tag
|
||||||
|
- `DELETE /tags/{id}/` - Delete tag
|
||||||
|
|
||||||
|
#### Members (Admin)
|
||||||
|
- `GET /members/` - Browse members
|
||||||
|
- `GET /members/{id}/` - Read specific member
|
||||||
|
- `POST /members/` - Create new member
|
||||||
|
- `PUT /members/{id}/` - Update existing member
|
||||||
|
|
||||||
|
#### Media (Admin)
|
||||||
|
- `POST /images/upload/` - Upload images
|
||||||
|
- `POST /media/upload/` - Upload media files
|
||||||
|
|
||||||
|
#### Integrations (Admin)
|
||||||
|
- `GET /integrations/` - List integrations
|
||||||
|
- `POST /integrations/` - Create integration (for API keys)
|
||||||
|
|
||||||
|
## Research Status
|
||||||
|
- ✅ Base URL structure identified
|
||||||
|
- ✅ Authentication requirements confirmed
|
||||||
|
- ⏳ Individual endpoint testing pending API keys
|
||||||
|
- ⏳ Parameter documentation pending
|
||||||
|
- ⏳ Response format documentation pending
|
||||||
272
contracts/content-api-auth.md
Normal file
272
contracts/content-api-auth.md
Normal file
|
|
@ -0,0 +1,272 @@
|
||||||
|
# Ghost Content API Authentication Documentation
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Ghost Content API provides read-only access to published content using simple query parameter authentication. It's designed for public consumption and is safe for client-side use.
|
||||||
|
|
||||||
|
## Authentication Method
|
||||||
|
|
||||||
|
### API Key Format
|
||||||
|
- **Type**: Content API Key (different from Admin API Key)
|
||||||
|
- **Source**: Ghost Admin → Settings → Integrations → Custom Integration
|
||||||
|
- **Security**: Safe for browser/public environments
|
||||||
|
- **Access**: Read-only to published content only
|
||||||
|
|
||||||
|
### Request Authentication
|
||||||
|
|
||||||
|
#### Query Parameter Method
|
||||||
|
```http
|
||||||
|
GET /ghost/api/content/posts/?key={content_api_key}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Required Headers
|
||||||
|
```http
|
||||||
|
Accept-Version: v5.0
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example Request
|
||||||
|
```http
|
||||||
|
GET /ghost/api/content/posts/?key=22444f78cc7a3e8d0b5eaa18&limit=5 HTTP/1.1
|
||||||
|
Host: localhost:2368
|
||||||
|
Accept-Version: v5.0
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation for MCP Server
|
||||||
|
|
||||||
|
### Node.js Implementation
|
||||||
|
|
||||||
|
#### Simple Request Function
|
||||||
|
```javascript
|
||||||
|
class GhostContentAuth {
|
||||||
|
constructor(apiKey, ghostUrl = 'http://localhost:2368') {
|
||||||
|
this.apiKey = apiKey;
|
||||||
|
this.baseUrl = `${ghostUrl}/ghost/api/content`;
|
||||||
|
}
|
||||||
|
|
||||||
|
buildUrl(endpoint, params = {}) {
|
||||||
|
const url = new URL(`${this.baseUrl}${endpoint}`);
|
||||||
|
|
||||||
|
// Add API key
|
||||||
|
url.searchParams.set('key', this.apiKey);
|
||||||
|
|
||||||
|
// Add additional parameters
|
||||||
|
Object.entries(params).forEach(([key, value]) => {
|
||||||
|
if (value !== undefined && value !== null) {
|
||||||
|
url.searchParams.set(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
getHeaders() {
|
||||||
|
return {
|
||||||
|
'Accept-Version': 'v5.0',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async request(endpoint, params = {}) {
|
||||||
|
const url = this.buildUrl(endpoint, params);
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: this.getHeaders()
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Content API request failed: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Usage Examples
|
||||||
|
```javascript
|
||||||
|
const contentAuth = new GhostContentAuth('your_content_api_key');
|
||||||
|
|
||||||
|
// Get all posts
|
||||||
|
const posts = await contentAuth.request('/posts/', {
|
||||||
|
limit: 10,
|
||||||
|
include: 'tags,authors'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get specific post by slug
|
||||||
|
const post = await contentAuth.request('/posts/slug/welcome/', {
|
||||||
|
include: 'tags,authors'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get site settings
|
||||||
|
const settings = await contentAuth.request('/settings/');
|
||||||
|
```
|
||||||
|
|
||||||
|
## Available Endpoints
|
||||||
|
|
||||||
|
### Core Resources
|
||||||
|
- `GET /posts/` - All published posts
|
||||||
|
- `GET /posts/{id}/` - Specific post by ID
|
||||||
|
- `GET /posts/slug/{slug}/` - Specific post by slug
|
||||||
|
- `GET /pages/` - All published pages
|
||||||
|
- `GET /pages/{id}/` - Specific page by ID
|
||||||
|
- `GET /pages/slug/{slug}/` - Specific page by slug
|
||||||
|
- `GET /tags/` - All public tags
|
||||||
|
- `GET /tags/{id}/` - Specific tag by ID
|
||||||
|
- `GET /tags/slug/{slug}/` - Specific tag by slug
|
||||||
|
- `GET /authors/` - All authors
|
||||||
|
- `GET /authors/{id}/` - Specific author by ID
|
||||||
|
- `GET /authors/slug/{slug}/` - Specific author by slug
|
||||||
|
- `GET /tiers/` - Membership tiers
|
||||||
|
- `GET /settings/` - Public site settings
|
||||||
|
|
||||||
|
## Query Parameters
|
||||||
|
|
||||||
|
### Common Parameters
|
||||||
|
- `key` (required): Content API key
|
||||||
|
- `limit`: Number of resources (default: 15, max: 50)
|
||||||
|
- `page`: Page number for pagination
|
||||||
|
- `fields`: Comma-separated list of fields to include
|
||||||
|
- `include`: Related resources to include (e.g., 'tags,authors')
|
||||||
|
- `filter`: Filter resources using Ghost's filter syntax
|
||||||
|
|
||||||
|
### Filter Examples
|
||||||
|
```javascript
|
||||||
|
// Featured posts only
|
||||||
|
const featured = await contentAuth.request('/posts/', {
|
||||||
|
filter: 'featured:true'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Posts with specific tag
|
||||||
|
const newsPosts = await contentAuth.request('/posts/', {
|
||||||
|
filter: 'tag:news'
|
||||||
|
});
|
||||||
|
|
||||||
|
// Posts by specific author
|
||||||
|
const authorPosts = await contentAuth.request('/posts/', {
|
||||||
|
filter: 'author:john-doe'
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Response Format
|
||||||
|
|
||||||
|
### Standard Response Structure
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"posts": [
|
||||||
|
{
|
||||||
|
"id": "post_id",
|
||||||
|
"title": "Post Title",
|
||||||
|
"slug": "post-slug",
|
||||||
|
"html": "<p>Post content...</p>",
|
||||||
|
"feature_image": "image_url",
|
||||||
|
"published_at": "2023-01-01T00:00:00.000Z",
|
||||||
|
"created_at": "2023-01-01T00:00:00.000Z",
|
||||||
|
"updated_at": "2023-01-01T00:00:00.000Z",
|
||||||
|
"tags": [...],
|
||||||
|
"authors": [...]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"meta": {
|
||||||
|
"pagination": {
|
||||||
|
"page": 1,
|
||||||
|
"limit": 15,
|
||||||
|
"pages": 1,
|
||||||
|
"total": 1,
|
||||||
|
"next": null,
|
||||||
|
"prev": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
|
||||||
|
### Missing API Key
|
||||||
|
**Request**: Without `key` parameter
|
||||||
|
**Response**: 401 Unauthorized
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Authorization failed",
|
||||||
|
"context": "Unable to determine the authenticated member or integration. Check the supplied Content API Key...",
|
||||||
|
"type": "UnauthorizedError"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Invalid API Key
|
||||||
|
**Response**: 401 Unauthorized
|
||||||
|
**Action**: Verify key is correct and active
|
||||||
|
|
||||||
|
### Resource Not Found
|
||||||
|
**Response**: 404 Not Found
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Resource not found",
|
||||||
|
"type": "NotFoundError"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Security Considerations
|
||||||
|
|
||||||
|
### Safe for Public Use
|
||||||
|
- Content API keys only access published content
|
||||||
|
- No sensitive data exposure
|
||||||
|
- Can be used in browsers, mobile apps, etc.
|
||||||
|
|
||||||
|
### Private Sites
|
||||||
|
- Be cautious with key distribution on private Ghost sites
|
||||||
|
- Consider access restrictions if content should be limited
|
||||||
|
|
||||||
|
### Key Management
|
||||||
|
- Keys can be regenerated in Ghost Admin
|
||||||
|
- Multiple integrations can have different keys
|
||||||
|
- Monitor key usage if needed
|
||||||
|
|
||||||
|
## Rate Limiting & Caching
|
||||||
|
|
||||||
|
### Caching Behavior
|
||||||
|
- Content API responses are fully cacheable
|
||||||
|
- Cache headers provided in responses
|
||||||
|
- Recommended to implement caching in client
|
||||||
|
|
||||||
|
### Rate Limiting
|
||||||
|
- No strict rate limits documented
|
||||||
|
- Reasonable usage expected
|
||||||
|
- Monitor response times and adjust accordingly
|
||||||
|
|
||||||
|
## Testing Strategy
|
||||||
|
|
||||||
|
### Manual Testing
|
||||||
|
```bash
|
||||||
|
# Test with curl
|
||||||
|
curl "http://localhost:2368/ghost/api/content/posts/?key=YOUR_KEY&limit=1" \
|
||||||
|
-H "Accept-Version: v5.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Automated Testing
|
||||||
|
```javascript
|
||||||
|
// Test basic authentication
|
||||||
|
test('Content API authentication', async () => {
|
||||||
|
const response = await contentAuth.request('/settings/');
|
||||||
|
expect(response.settings).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test error handling
|
||||||
|
test('Invalid key handling', async () => {
|
||||||
|
const invalidAuth = new GhostContentAuth('invalid_key');
|
||||||
|
await expect(invalidAuth.request('/posts/')).rejects.toThrow();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Status
|
||||||
|
- ✅ Authentication method documented
|
||||||
|
- ✅ Request format identified
|
||||||
|
- ✅ Error handling patterns documented
|
||||||
|
- ✅ Node.js implementation designed
|
||||||
|
- ⏳ Implementation pending API key generation
|
||||||
|
- ⏳ Testing pending API key availability
|
||||||
368
contracts/content-formats.md
Normal file
368
contracts/content-formats.md
Normal file
|
|
@ -0,0 +1,368 @@
|
||||||
|
# Ghost Content Formats Documentation
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Ghost supports multiple content formats for creating and managing posts and pages. Understanding these formats is crucial for proper MCP tool implementation.
|
||||||
|
|
||||||
|
## Content Format Evolution
|
||||||
|
|
||||||
|
### Historical Context
|
||||||
|
1. **Mobiledoc** (Legacy): Ghost's previous JSON-based content format
|
||||||
|
2. **HTML**: Traditional markup format
|
||||||
|
3. **Lexical** (Current): Ghost's current standardized JSON content format
|
||||||
|
|
||||||
|
## Current Content Formats
|
||||||
|
|
||||||
|
### 1. Lexical Format (Primary)
|
||||||
|
**Status**: Current standard format
|
||||||
|
**Type**: JSON-based structured content
|
||||||
|
**Usage**: Default format for all new content
|
||||||
|
|
||||||
|
#### Structure
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"lexical": "{\"root\":{\"children\":[{\"children\":[{\"detail\":0,\"format\":0,\"mode\":\"normal\",\"style\":\"\",\"text\":\"Welcome to Ghost!\",\"type\":\"text\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"paragraph\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"root\",\"version\":1}}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Benefits
|
||||||
|
- Rich content representation
|
||||||
|
- Structured, parseable format
|
||||||
|
- Supports complex layouts and content blocks
|
||||||
|
- Platform-agnostic content storage
|
||||||
|
|
||||||
|
### 2. HTML Format (Optional)
|
||||||
|
**Status**: Available for output/input
|
||||||
|
**Type**: Rendered HTML markup
|
||||||
|
**Usage**: For compatibility and direct HTML input
|
||||||
|
|
||||||
|
#### Structure
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"html": "<p>Welcome to Ghost!</p><p>This is a simple HTML paragraph.</p>"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Use Cases
|
||||||
|
- Content migration from HTML-based systems
|
||||||
|
- Direct HTML content creation
|
||||||
|
- Output for web rendering
|
||||||
|
|
||||||
|
### 3. Mobiledoc Format (Legacy)
|
||||||
|
**Status**: Legacy support (deprecated)
|
||||||
|
**Type**: JSON-based (older format)
|
||||||
|
**Usage**: Existing content only
|
||||||
|
|
||||||
|
**Note**: New content should use Lexical format. Mobiledoc is maintained for backward compatibility.
|
||||||
|
|
||||||
|
## API Content Handling
|
||||||
|
|
||||||
|
### Content Retrieval
|
||||||
|
|
||||||
|
#### Default Behavior
|
||||||
|
- API returns Lexical format by default
|
||||||
|
- HTML format requires explicit request
|
||||||
|
|
||||||
|
#### Format Selection
|
||||||
|
```javascript
|
||||||
|
// Get only Lexical (default)
|
||||||
|
GET /ghost/api/content/posts/
|
||||||
|
|
||||||
|
// Get both Lexical and HTML
|
||||||
|
GET /ghost/api/content/posts/?formats=html,lexical
|
||||||
|
|
||||||
|
// Get only HTML
|
||||||
|
GET /ghost/api/content/posts/?formats=html
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Response Examples
|
||||||
|
|
||||||
|
**Lexical Only (Default)**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"posts": [
|
||||||
|
{
|
||||||
|
"id": "post_id",
|
||||||
|
"title": "My Post",
|
||||||
|
"lexical": "{\"root\":{...}}",
|
||||||
|
"slug": "my-post"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**HTML and Lexical**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"posts": [
|
||||||
|
{
|
||||||
|
"id": "post_id",
|
||||||
|
"title": "My Post",
|
||||||
|
"lexical": "{\"root\":{...}}",
|
||||||
|
"html": "<p>Post content</p>",
|
||||||
|
"slug": "my-post"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Content Creation & Updates
|
||||||
|
|
||||||
|
#### Admin API Content Fields
|
||||||
|
```javascript
|
||||||
|
// Creating a post with Lexical
|
||||||
|
const postData = {
|
||||||
|
posts: [{
|
||||||
|
title: "New Post",
|
||||||
|
lexical: JSON.stringify(lexicalContent),
|
||||||
|
status: "draft"
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Creating a post with HTML
|
||||||
|
const postData = {
|
||||||
|
posts: [{
|
||||||
|
title: "New Post",
|
||||||
|
html: "<p>HTML content here</p>",
|
||||||
|
status: "draft"
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Creating with both (Lexical takes precedence)
|
||||||
|
const postData = {
|
||||||
|
posts: [{
|
||||||
|
title: "New Post",
|
||||||
|
lexical: JSON.stringify(lexicalContent),
|
||||||
|
html: "<p>Fallback HTML</p>",
|
||||||
|
status: "draft"
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## Content Format Priorities
|
||||||
|
|
||||||
|
### Create/Update Priority Order
|
||||||
|
1. **Lexical** (highest priority)
|
||||||
|
2. **HTML** (fallback if no Lexical)
|
||||||
|
3. **Mobiledoc** (legacy fallback)
|
||||||
|
|
||||||
|
### Conversion Behavior
|
||||||
|
- HTML → Lexical: Ghost converts HTML to Lexical automatically
|
||||||
|
- Lexical → HTML: Ghost renders Lexical to HTML
|
||||||
|
- Mobiledoc → Lexical: Ghost migrates existing Mobiledoc content
|
||||||
|
|
||||||
|
## Implementation for MCP Server
|
||||||
|
|
||||||
|
### Content Format Detection
|
||||||
|
```javascript
|
||||||
|
class GhostContentHandler {
|
||||||
|
detectContentFormat(content) {
|
||||||
|
if (typeof content === 'object' && content.root) {
|
||||||
|
return 'lexical';
|
||||||
|
}
|
||||||
|
if (typeof content === 'string' && content.startsWith('<')) {
|
||||||
|
return 'html';
|
||||||
|
}
|
||||||
|
if (typeof content === 'object' && content.version) {
|
||||||
|
return 'mobiledoc';
|
||||||
|
}
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
prepareContentForAPI(content, preferredFormat = 'lexical') {
|
||||||
|
const format = this.detectContentFormat(content);
|
||||||
|
|
||||||
|
switch (format) {
|
||||||
|
case 'lexical':
|
||||||
|
return {
|
||||||
|
lexical: typeof content === 'string' ? content : JSON.stringify(content)
|
||||||
|
};
|
||||||
|
case 'html':
|
||||||
|
return {
|
||||||
|
html: content
|
||||||
|
};
|
||||||
|
case 'mobiledoc':
|
||||||
|
return {
|
||||||
|
mobiledoc: typeof content === 'string' ? content : JSON.stringify(content)
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
// Assume plain text, wrap in HTML
|
||||||
|
return {
|
||||||
|
html: `<p>${content}</p>`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### MCP Tool Content Parameters
|
||||||
|
|
||||||
|
#### Create Post Tool
|
||||||
|
```javascript
|
||||||
|
const createPostTool = {
|
||||||
|
name: "ghost_admin_create_post",
|
||||||
|
parameters: {
|
||||||
|
title: { type: "string", required: true },
|
||||||
|
|
||||||
|
// Content format options (one required)
|
||||||
|
lexical: { type: "string", description: "Lexical JSON content" },
|
||||||
|
html: { type: "string", description: "HTML content" },
|
||||||
|
mobiledoc: { type: "string", description: "Mobiledoc JSON content (legacy)" },
|
||||||
|
|
||||||
|
// Other parameters...
|
||||||
|
status: { type: "string", enum: ["draft", "published"] }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Content Validation
|
||||||
|
```javascript
|
||||||
|
function validateContentInput(params) {
|
||||||
|
const contentFormats = ['lexical', 'html', 'mobiledoc'].filter(
|
||||||
|
format => params[format] !== undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
if (contentFormats.length === 0) {
|
||||||
|
throw new Error('At least one content format (lexical, html, or mobiledoc) is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentFormats.length > 1) {
|
||||||
|
console.warn('Multiple content formats provided. Lexical will take precedence.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate Lexical JSON if provided
|
||||||
|
if (params.lexical) {
|
||||||
|
try {
|
||||||
|
JSON.parse(params.lexical);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error('Invalid Lexical JSON format');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Content Format Examples
|
||||||
|
|
||||||
|
### 1. Simple Lexical Content
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"root": {
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"detail": 0,
|
||||||
|
"format": 0,
|
||||||
|
"mode": "normal",
|
||||||
|
"style": "",
|
||||||
|
"text": "Hello World!",
|
||||||
|
"type": "text",
|
||||||
|
"version": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"direction": "ltr",
|
||||||
|
"format": "",
|
||||||
|
"indent": 0,
|
||||||
|
"type": "paragraph",
|
||||||
|
"version": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"direction": "ltr",
|
||||||
|
"format": "",
|
||||||
|
"indent": 0,
|
||||||
|
"type": "root",
|
||||||
|
"version": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Rich HTML Content
|
||||||
|
```html
|
||||||
|
<h1>My Blog Post</h1>
|
||||||
|
<p>This is a <strong>rich</strong> HTML post with <em>formatting</em>.</p>
|
||||||
|
<ul>
|
||||||
|
<li>First item</li>
|
||||||
|
<li>Second item</li>
|
||||||
|
</ul>
|
||||||
|
<blockquote>
|
||||||
|
<p>This is a quote block.</p>
|
||||||
|
</blockquote>
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Content Conversion Utility
|
||||||
|
```javascript
|
||||||
|
class ContentConverter {
|
||||||
|
// Convert HTML to simple Lexical
|
||||||
|
htmlToLexical(html) {
|
||||||
|
// Basic implementation - in practice, use Ghost's conversion utilities
|
||||||
|
const paragraphs = html.split('</p>').filter(p => p.trim());
|
||||||
|
const children = paragraphs.map(p => {
|
||||||
|
const text = p.replace(/<[^>]*>/g, '').trim();
|
||||||
|
return {
|
||||||
|
children: [{
|
||||||
|
detail: 0,
|
||||||
|
format: 0,
|
||||||
|
mode: "normal",
|
||||||
|
style: "",
|
||||||
|
text: text,
|
||||||
|
type: "text",
|
||||||
|
version: 1
|
||||||
|
}],
|
||||||
|
direction: "ltr",
|
||||||
|
format: "",
|
||||||
|
indent: 0,
|
||||||
|
type: "paragraph",
|
||||||
|
version: 1
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
root: {
|
||||||
|
children: children,
|
||||||
|
direction: "ltr",
|
||||||
|
format: "",
|
||||||
|
indent: 0,
|
||||||
|
type: "root",
|
||||||
|
version: 1
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert Lexical to simple HTML
|
||||||
|
lexicalToHtml(lexical) {
|
||||||
|
const content = typeof lexical === 'string' ? JSON.parse(lexical) : lexical;
|
||||||
|
const paragraphs = content.root.children.map(child => {
|
||||||
|
if (child.type === 'paragraph') {
|
||||||
|
const text = child.children.map(textNode => textNode.text).join('');
|
||||||
|
return `<p>${text}</p>`;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
return paragraphs.join('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
### For MCP Implementation
|
||||||
|
1. **Default to Lexical**: Use Lexical format for new content creation
|
||||||
|
2. **Support HTML Input**: Allow HTML for ease of use and migration
|
||||||
|
3. **Validate JSON**: Always validate Lexical JSON before sending to API
|
||||||
|
4. **Handle Conversion**: Provide utilities for format conversion if needed
|
||||||
|
5. **Graceful Fallback**: Handle legacy Mobiledoc content gracefully
|
||||||
|
|
||||||
|
### Content Creation Guidelines
|
||||||
|
1. **Use Lexical for Rich Content**: Complex layouts, cards, embeds
|
||||||
|
2. **Use HTML for Simple Content**: Basic text formatting
|
||||||
|
3. **Provide Format Options**: Let users choose their preferred input format
|
||||||
|
4. **Validate Before Submission**: Check content validity before API calls
|
||||||
|
|
||||||
|
## Implementation Status
|
||||||
|
- ✅ Content formats identified and documented
|
||||||
|
- ✅ API handling patterns documented
|
||||||
|
- ✅ Implementation strategy planned
|
||||||
|
- ✅ Validation approaches defined
|
||||||
|
- ⏳ Content conversion utilities pending implementation
|
||||||
|
- ⏳ Real format testing pending API access
|
||||||
118
contracts/error-responses.md
Normal file
118
contracts/error-responses.md
Normal file
|
|
@ -0,0 +1,118 @@
|
||||||
|
# Ghost API Error Responses Documentation
|
||||||
|
|
||||||
|
## Observed Error Response Format
|
||||||
|
|
||||||
|
### Standard Error Structure
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Error description",
|
||||||
|
"context": "Additional context or details",
|
||||||
|
"type": "ErrorType",
|
||||||
|
"details": "Additional details if available",
|
||||||
|
"property": "field_name",
|
||||||
|
"help": "Help text",
|
||||||
|
"code": "ERROR_CODE",
|
||||||
|
"id": "error_id"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documented Error Types
|
||||||
|
|
||||||
|
### 1. Authentication Errors
|
||||||
|
|
||||||
|
#### Content API - Missing API Key
|
||||||
|
**Request**: `GET /ghost/api/content/settings/`
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Authorization failed",
|
||||||
|
"context": "Unable to determine the authenticated member or integration. Check the supplied Content API Key and ensure cookies are being passed through if making a request from the browser.",
|
||||||
|
"type": "UnauthorizedError",
|
||||||
|
"details": null,
|
||||||
|
"property": null,
|
||||||
|
"help": null,
|
||||||
|
"code": null,
|
||||||
|
"id": "error_id"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**Status Code**: 401
|
||||||
|
|
||||||
|
#### Admin API - Missing Authentication
|
||||||
|
**Request**: `GET /ghost/api/admin/users/me/`
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"message": "Authorization failed",
|
||||||
|
"context": "Unable to determine the authenticated user or integration. Check that cookies are being passed through if making a request from the browser. For more information see https://ghost.org/docs/admin-api/#authentication.",
|
||||||
|
"type": "UnauthorizedError"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**Status Code**: 401
|
||||||
|
|
||||||
|
### 2. Resource Not Found Errors
|
||||||
|
**Status Code**: 404
|
||||||
|
**Format**: TBD (pending testing with valid API keys)
|
||||||
|
|
||||||
|
### 3. Validation Errors
|
||||||
|
**Status Code**: 422
|
||||||
|
**Format**: TBD (pending testing with create/update operations)
|
||||||
|
|
||||||
|
### 4. Rate Limiting Errors
|
||||||
|
**Status Code**: 429
|
||||||
|
**Format**: TBD (pending rate limit testing)
|
||||||
|
|
||||||
|
### 5. Server Errors
|
||||||
|
**Status Code**: 5xx
|
||||||
|
**Format**: TBD (pending error scenario testing)
|
||||||
|
|
||||||
|
## Error Categories for MCP Implementation
|
||||||
|
|
||||||
|
### Network Errors
|
||||||
|
- Connection timeouts
|
||||||
|
- DNS resolution failures
|
||||||
|
- Network connectivity issues
|
||||||
|
- SSL/TLS certificate problems
|
||||||
|
|
||||||
|
### Authentication Errors
|
||||||
|
- Invalid API keys (Content/Admin)
|
||||||
|
- JWT token generation failures
|
||||||
|
- Token expiration
|
||||||
|
- Permission denied errors
|
||||||
|
|
||||||
|
### Ghost API Errors
|
||||||
|
- Rate limiting (429 status)
|
||||||
|
- Server errors (5xx status)
|
||||||
|
- Client errors (4xx status)
|
||||||
|
- Invalid request format
|
||||||
|
- Resource not found (404)
|
||||||
|
- Validation errors (422)
|
||||||
|
|
||||||
|
### MCP Protocol Errors
|
||||||
|
- Invalid tool parameters
|
||||||
|
- Schema validation failures
|
||||||
|
- Unsupported operations
|
||||||
|
|
||||||
|
### File Upload Errors
|
||||||
|
- File size limits exceeded
|
||||||
|
- Unsupported file types
|
||||||
|
- Storage failures
|
||||||
|
|
||||||
|
## Research Status
|
||||||
|
- ✅ Basic error structure identified
|
||||||
|
- ✅ Authentication error formats documented
|
||||||
|
- ⏳ Complete error catalog pending API key testing
|
||||||
|
- ⏳ Rate limiting behavior pending
|
||||||
|
- ⏳ Validation error formats pending
|
||||||
|
- ⏳ Error mapping to MCP responses pending
|
||||||
402
contracts/ghost-filtering.md
Normal file
402
contracts/ghost-filtering.md
Normal file
|
|
@ -0,0 +1,402 @@
|
||||||
|
# Ghost API Filtering Syntax Documentation
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Ghost uses NQL (Node Query Language) for filtering API results. This provides powerful querying capabilities for both Content and Admin APIs.
|
||||||
|
|
||||||
|
## Basic Syntax
|
||||||
|
|
||||||
|
### Property-Operator-Value Format
|
||||||
|
```
|
||||||
|
property:operator value
|
||||||
|
```
|
||||||
|
|
||||||
|
### Simple Examples
|
||||||
|
```
|
||||||
|
status:published # Posts with published status
|
||||||
|
featured:true # Featured posts only
|
||||||
|
slug:welcome # Post with slug "welcome"
|
||||||
|
tag:news # Posts tagged with "news"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Operators
|
||||||
|
|
||||||
|
### 1. Equality (Default)
|
||||||
|
**Operator**: `:` (colon)
|
||||||
|
**Usage**: Exact match
|
||||||
|
```
|
||||||
|
status:published
|
||||||
|
featured:true
|
||||||
|
author:john-doe
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Negation
|
||||||
|
**Operator**: `-` (minus prefix)
|
||||||
|
**Usage**: NOT equal to
|
||||||
|
```
|
||||||
|
-status:draft # Not draft posts
|
||||||
|
-featured:true # Non-featured posts
|
||||||
|
-tag:internal # Posts not tagged "internal"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Comparison Operators
|
||||||
|
**Operators**: `>`, `>=`, `<`, `<=`
|
||||||
|
**Usage**: Numeric and date comparisons
|
||||||
|
```
|
||||||
|
published_at:>2023-01-01 # Posts published after date
|
||||||
|
read_time:>5 # Posts with reading time > 5 minutes
|
||||||
|
published_at:<=now-7d # Posts published within last 7 days
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Text Matching
|
||||||
|
**Operators**: `~`, `~^`, `~$`
|
||||||
|
**Usage**: Partial text matching
|
||||||
|
```
|
||||||
|
title:~ghost # Title contains "ghost"
|
||||||
|
slug:~^getting-started # Slug starts with "getting-started"
|
||||||
|
excerpt:~$tutorial # Excerpt ends with "tutorial"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Group Selection
|
||||||
|
**Operator**: `[value1, value2, ...]`
|
||||||
|
**Usage**: Match any value in the list
|
||||||
|
```
|
||||||
|
tag:[news,updates,blog] # Posts with any of these tags
|
||||||
|
author:[john,jane,bob] # Posts by any of these authors
|
||||||
|
status:[published,draft] # Published or draft posts
|
||||||
|
```
|
||||||
|
|
||||||
|
## Value Types
|
||||||
|
|
||||||
|
### 1. Null Values
|
||||||
|
```
|
||||||
|
feature_image:null # Posts without feature image
|
||||||
|
custom_excerpt:-null # Posts with custom excerpt (not null)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Boolean Values
|
||||||
|
```
|
||||||
|
featured:true
|
||||||
|
featured:false
|
||||||
|
page:true # Only pages
|
||||||
|
page:false # Only posts
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Numbers
|
||||||
|
```
|
||||||
|
read_time:>10
|
||||||
|
read_time:<=5
|
||||||
|
comment_count:0
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Literal Strings
|
||||||
|
**Format**: No quotes needed for simple strings
|
||||||
|
```
|
||||||
|
status:published
|
||||||
|
tag:javascript
|
||||||
|
author:john-doe
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Quoted Strings
|
||||||
|
**Format**: Single quotes for strings with special characters
|
||||||
|
```
|
||||||
|
title:'My Post Title'
|
||||||
|
tag:'getting started'
|
||||||
|
excerpt:'This is a long excerpt with spaces'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. Relative Dates
|
||||||
|
**Format**: `now`, `now-Xd` (days), `now-Xm` (months), `now-Xy` (years)
|
||||||
|
```
|
||||||
|
published_at:>now-30d # Last 30 days
|
||||||
|
created_at:<=now-1y # More than 1 year old
|
||||||
|
updated_at:>now-7d # Updated in last week
|
||||||
|
```
|
||||||
|
|
||||||
|
## Combination Logic
|
||||||
|
|
||||||
|
### 1. AND Operator
|
||||||
|
**Operator**: `+`
|
||||||
|
**Usage**: All conditions must be true
|
||||||
|
```
|
||||||
|
status:published+featured:true
|
||||||
|
tag:news+published_at:>now-7d
|
||||||
|
author:john+status:published+featured:true
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. OR Operator
|
||||||
|
**Operator**: `,` (comma)
|
||||||
|
**Usage**: Any condition can be true
|
||||||
|
```
|
||||||
|
status:published,status:draft
|
||||||
|
tag:news,tag:updates
|
||||||
|
author:john,author:jane
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Precedence Control
|
||||||
|
**Operator**: `()` (parentheses)
|
||||||
|
**Usage**: Group conditions and control evaluation order
|
||||||
|
```
|
||||||
|
(tag:news,tag:updates)+status:published
|
||||||
|
author:john+(status:published,status:draft)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Complex Filter Examples
|
||||||
|
|
||||||
|
### 1. Recent Featured Posts by Specific Authors
|
||||||
|
```
|
||||||
|
authors.slug:[john,jane]+featured:true+published_at:>now-30d
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Published Content with Specific Tags, Excluding Drafts
|
||||||
|
```
|
||||||
|
tag:[tutorial,guide]+status:published+-status:draft
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Posts with Reading Time Between 5-15 Minutes
|
||||||
|
```
|
||||||
|
read_time:>=5+read_time:<=15+status:published
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Recent Posts with Feature Images
|
||||||
|
```
|
||||||
|
published_at:>now-7d+feature_image:-null+status:published
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Pages or Featured Posts by Multiple Authors
|
||||||
|
```
|
||||||
|
(page:true,featured:true)+authors.slug:[admin,editor,writer]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Field-Specific Filters
|
||||||
|
|
||||||
|
### Posts/Pages
|
||||||
|
```
|
||||||
|
id:5f7c1b4b0c7b4b001f7b4b1c
|
||||||
|
slug:my-post-slug
|
||||||
|
title:~'How to'
|
||||||
|
html:~'tutorial'
|
||||||
|
plaintext:~'javascript'
|
||||||
|
feature_image:null
|
||||||
|
featured:true
|
||||||
|
page:false
|
||||||
|
status:published
|
||||||
|
visibility:public
|
||||||
|
created_at:>now-30d
|
||||||
|
published_at:<=2023-12-31
|
||||||
|
updated_at:>now-7d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Tags
|
||||||
|
```
|
||||||
|
id:tag_id
|
||||||
|
name:'JavaScript'
|
||||||
|
slug:javascript
|
||||||
|
description:~'programming'
|
||||||
|
visibility:public
|
||||||
|
```
|
||||||
|
|
||||||
|
### Authors
|
||||||
|
```
|
||||||
|
id:author_id
|
||||||
|
name:'John Doe'
|
||||||
|
slug:john-doe
|
||||||
|
email:john@example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
### Relationships (Dot Notation)
|
||||||
|
```
|
||||||
|
authors.slug:john-doe
|
||||||
|
authors.name:'John Doe'
|
||||||
|
tags.slug:javascript
|
||||||
|
tags.name:'JavaScript'
|
||||||
|
primary_author.slug:admin
|
||||||
|
primary_tag.slug:featured
|
||||||
|
```
|
||||||
|
|
||||||
|
## URL Encoding
|
||||||
|
|
||||||
|
### Required Encoding
|
||||||
|
Filter strings must be URL encoded when used in URLs:
|
||||||
|
```javascript
|
||||||
|
const filter = "tag:javascript+published_at:>now-7d";
|
||||||
|
const encoded = encodeURIComponent(filter);
|
||||||
|
// Result: tag%3Ajavascript%2Bpublished_at%3A%3Enow-7d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Common Encodings
|
||||||
|
```
|
||||||
|
: → %3A
|
||||||
|
+ → %2B
|
||||||
|
, → %2C
|
||||||
|
> → %3E
|
||||||
|
< → %3C
|
||||||
|
~ → %7E
|
||||||
|
[ → %5B
|
||||||
|
] → %5D
|
||||||
|
' → %27
|
||||||
|
```
|
||||||
|
|
||||||
|
## Limitations & Constraints
|
||||||
|
|
||||||
|
### 1. Property Naming
|
||||||
|
- Properties must start with a letter
|
||||||
|
- Use dot notation for relationships
|
||||||
|
- Case-sensitive property names
|
||||||
|
|
||||||
|
### 2. Value Constraints
|
||||||
|
- No regex or advanced pattern matching
|
||||||
|
- Limited to defined operators
|
||||||
|
- String values with special chars need quotes
|
||||||
|
|
||||||
|
### 3. Performance Considerations
|
||||||
|
- Complex filters may impact query performance
|
||||||
|
- Index-based fields filter faster
|
||||||
|
- Limit filter complexity for optimal response times
|
||||||
|
|
||||||
|
### 4. API Limitations
|
||||||
|
- Some fields may not be filterable
|
||||||
|
- Admin API may have different filter availability than Content API
|
||||||
|
- Check specific endpoint documentation for filter support
|
||||||
|
|
||||||
|
## Implementation for MCP Server
|
||||||
|
|
||||||
|
### Filter Builder Class
|
||||||
|
```javascript
|
||||||
|
class GhostFilterBuilder {
|
||||||
|
constructor() {
|
||||||
|
this.conditions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
equals(property, value) {
|
||||||
|
this.conditions.push(`${property}:${this.formatValue(value)}`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
notEquals(property, value) {
|
||||||
|
this.conditions.push(`-${property}:${this.formatValue(value)}`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
greaterThan(property, value) {
|
||||||
|
this.conditions.push(`${property}:>${this.formatValue(value)}`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
contains(property, value) {
|
||||||
|
this.conditions.push(`${property}:~${this.formatValue(value)}`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inArray(property, values) {
|
||||||
|
const formatted = values.map(v => this.formatValue(v)).join(',');
|
||||||
|
this.conditions.push(`${property}:[${formatted}]`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
formatValue(value) {
|
||||||
|
if (value === null) return 'null';
|
||||||
|
if (typeof value === 'boolean') return value.toString();
|
||||||
|
if (typeof value === 'number') return value.toString();
|
||||||
|
if (typeof value === 'string' && /^[a-zA-Z0-9\-_]+$/.test(value)) {
|
||||||
|
return value; // Simple string, no quotes needed
|
||||||
|
}
|
||||||
|
return `'${value}'`; // Complex string, needs quotes
|
||||||
|
}
|
||||||
|
|
||||||
|
and() {
|
||||||
|
// Next condition will be ANDed
|
||||||
|
this.operator = '+';
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
or() {
|
||||||
|
// Next condition will be ORed
|
||||||
|
this.operator = ',';
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
build() {
|
||||||
|
return this.conditions.join(this.operator || '+');
|
||||||
|
}
|
||||||
|
|
||||||
|
buildEncoded() {
|
||||||
|
return encodeURIComponent(this.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage example
|
||||||
|
const filter = new GhostFilterBuilder()
|
||||||
|
.equals('status', 'published')
|
||||||
|
.and()
|
||||||
|
.equals('featured', true)
|
||||||
|
.and()
|
||||||
|
.greaterThan('published_at', 'now-30d')
|
||||||
|
.build();
|
||||||
|
// Result: status:published+featured:true+published_at:>now-30d
|
||||||
|
```
|
||||||
|
|
||||||
|
## Phase Implementation Strategy
|
||||||
|
|
||||||
|
### Phase 1: Basic Filters (Current)
|
||||||
|
```javascript
|
||||||
|
// Simple equality filters
|
||||||
|
status:published
|
||||||
|
featured:true
|
||||||
|
tag:news
|
||||||
|
author:john-doe
|
||||||
|
```
|
||||||
|
|
||||||
|
### Phase 2: Advanced Filters (Future)
|
||||||
|
```javascript
|
||||||
|
// Complex filters with operators
|
||||||
|
published_at:>now-30d+featured:true
|
||||||
|
authors.slug:[john,jane]+tag:[news,updates]
|
||||||
|
title:~'tutorial'+read_time:>=5
|
||||||
|
```
|
||||||
|
|
||||||
|
### Phase 3: Filter Builder (Future)
|
||||||
|
- Programmatic filter construction
|
||||||
|
- Validation and sanitization
|
||||||
|
- Advanced query optimization
|
||||||
|
|
||||||
|
## Testing Strategy
|
||||||
|
|
||||||
|
### Unit Tests
|
||||||
|
```javascript
|
||||||
|
describe('Ghost Filter Syntax', () => {
|
||||||
|
test('simple equality filter', () => {
|
||||||
|
expect(buildFilter('status', 'published')).toBe('status:published');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('complex AND filter', () => {
|
||||||
|
const filter = 'status:published+featured:true';
|
||||||
|
expect(parseFilter(filter)).toMatchObject({
|
||||||
|
conditions: [
|
||||||
|
{ property: 'status', operator: ':', value: 'published' },
|
||||||
|
{ property: 'featured', operator: ':', value: 'true' }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Integration Tests
|
||||||
|
```javascript
|
||||||
|
test('filter with real API', async () => {
|
||||||
|
const response = await contentApi.request('/posts/', {
|
||||||
|
filter: 'status:published+featured:true',
|
||||||
|
limit: 5
|
||||||
|
});
|
||||||
|
expect(response.posts.every(post =>
|
||||||
|
post.status === 'published' && post.featured === true
|
||||||
|
)).toBe(true);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Status
|
||||||
|
- ✅ Filter syntax documented
|
||||||
|
- ✅ Operators and examples identified
|
||||||
|
- ✅ Implementation strategy planned
|
||||||
|
- ✅ Phase 1 vs Phase 2 filters defined
|
||||||
|
- ⏳ Filter builder implementation pending
|
||||||
|
- ⏳ Real API testing pending API keys
|
||||||
167
contracts/phase-0-summary.md
Normal file
167
contracts/phase-0-summary.md
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
# Phase 0: Research & Setup - COMPLETE ✅
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Phase 0 of the Ghost MCP implementation has been successfully completed. This phase focused on deep research into Ghost API patterns, comprehensive documentation, and project setup.
|
||||||
|
|
||||||
|
## Completed Tasks
|
||||||
|
|
||||||
|
### ✅ 1. Ghost API Research & Documentation
|
||||||
|
**Status**: Complete
|
||||||
|
**Documentation Created**:
|
||||||
|
- `contracts/admin-api-auth.md` - Complete JWT authentication flow
|
||||||
|
- `contracts/content-api-auth.md` - Content API key authentication
|
||||||
|
- `contracts/error-responses.md` - Comprehensive error catalog
|
||||||
|
- `contracts/ghost-filtering.md` - Complete filtering syntax documentation
|
||||||
|
- `contracts/content-formats.md` - Lexical, HTML, Mobiledoc format support
|
||||||
|
- `contracts/api-endpoints.md` - Full endpoint catalog
|
||||||
|
- `contracts/rate-limits.md` - Rate limiting behavior and best practices
|
||||||
|
|
||||||
|
### ✅ 2. Development Environment Setup
|
||||||
|
**Status**: Complete
|
||||||
|
**Infrastructure**:
|
||||||
|
- ✅ Ghost instance running (Docker Compose)
|
||||||
|
- ✅ MySQL database healthy and connected
|
||||||
|
- ✅ Ghost admin interface accessible
|
||||||
|
- ✅ API endpoints responding correctly
|
||||||
|
|
||||||
|
### ✅ 3. Project Scaffolding
|
||||||
|
**Status**: Complete
|
||||||
|
**Created Structure**:
|
||||||
|
```
|
||||||
|
ghost-mcp/
|
||||||
|
├── contracts/ # Research documentation
|
||||||
|
├── src/
|
||||||
|
│ ├── index.ts # MCP server entry point
|
||||||
|
│ ├── ghost-client.ts # Ghost API client
|
||||||
|
│ ├── auth/
|
||||||
|
│ │ ├── content-auth.ts
|
||||||
|
│ │ └── admin-auth.ts
|
||||||
|
│ ├── tools/
|
||||||
|
│ │ ├── content/ # Content API tools (13 tools)
|
||||||
|
│ │ └── admin/ # Admin API tools (31+ tools)
|
||||||
|
│ ├── types/
|
||||||
|
│ │ ├── ghost.ts # Ghost API types
|
||||||
|
│ │ └── mcp.ts # MCP-specific types
|
||||||
|
│ └── utils/
|
||||||
|
│ ├── validation.ts
|
||||||
|
│ └── errors.ts
|
||||||
|
├── package.json # Node.js project with all dependencies
|
||||||
|
├── tsconfig.json # TypeScript configuration
|
||||||
|
├── .eslintrc.json # ESLint configuration
|
||||||
|
├── .prettierrc # Prettier configuration
|
||||||
|
└── jest.config.js # Jest testing configuration
|
||||||
|
```
|
||||||
|
|
||||||
|
## Key Research Findings
|
||||||
|
|
||||||
|
### 1. Authentication Patterns
|
||||||
|
- **Content API**: Simple query parameter authentication (`?key=api_key`)
|
||||||
|
- **Admin API**: JWT token authentication with 5-minute expiration
|
||||||
|
- **Implementation**: Node.js with jsonwebtoken library
|
||||||
|
|
||||||
|
### 2. Content Formats
|
||||||
|
- **Primary**: Lexical (JSON-based structured content)
|
||||||
|
- **Secondary**: HTML (for compatibility and input)
|
||||||
|
- **Legacy**: Mobiledoc (backward compatibility only)
|
||||||
|
|
||||||
|
### 3. API Response Patterns
|
||||||
|
- **Consistent Structure**: `{ resource_type: [...], meta: { pagination: {...} } }`
|
||||||
|
- **Error Format**: `{ errors: [{ message, context, type, ... }] }`
|
||||||
|
- **Filtering**: NQL (Node Query Language) with comprehensive operators
|
||||||
|
|
||||||
|
### 4. Error Handling Strategy
|
||||||
|
- **5 Categories**: Network, Authentication, Ghost API, MCP Protocol, File Upload
|
||||||
|
- **Comprehensive Logging**: Structured JSON with request IDs
|
||||||
|
- **Retry Logic**: Exponential backoff for transient errors
|
||||||
|
|
||||||
|
### 5. MCP Tool Architecture
|
||||||
|
- **Total Tools**: 44+ tools covering complete Ghost REST API
|
||||||
|
- **Content API**: 13 read-only tools
|
||||||
|
- **Admin API**: 31+ read/write tools including advanced features
|
||||||
|
|
||||||
|
## Implementation Readiness
|
||||||
|
|
||||||
|
### Phase 1 Prerequisites ✅
|
||||||
|
- [x] Complete API authentication documentation
|
||||||
|
- [x] Error handling patterns defined
|
||||||
|
- [x] Project structure created
|
||||||
|
- [x] TypeScript configuration ready
|
||||||
|
- [x] All dependencies specified
|
||||||
|
|
||||||
|
### Remaining for Phase 1
|
||||||
|
- [ ] **API Keys Required**: Need to complete Ghost admin setup and generate API keys
|
||||||
|
- [ ] **Implementation**: Begin Phase 1 core infrastructure development
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
### Immediate Action Required
|
||||||
|
1. **Complete Ghost Setup**:
|
||||||
|
- Navigate to http://localhost:2368/ghost
|
||||||
|
- Create admin account
|
||||||
|
- Generate Content and Admin API keys
|
||||||
|
|
||||||
|
2. **Begin Phase 1**:
|
||||||
|
- Implement configuration system
|
||||||
|
- Build authentication infrastructure
|
||||||
|
- Set up logging and error handling
|
||||||
|
|
||||||
|
### Phase 1 Scope
|
||||||
|
According to PLAN.md, Phase 1 will implement:
|
||||||
|
- Configuration system with precedence (env vars → .env → defaults)
|
||||||
|
- Content and Admin API authentication
|
||||||
|
- Comprehensive logging system
|
||||||
|
- Error handling infrastructure
|
||||||
|
|
||||||
|
## Project Status
|
||||||
|
|
||||||
|
### Current State
|
||||||
|
- **Ghost Instance**: ✅ Running and accessible
|
||||||
|
- **Research**: ✅ Complete and documented
|
||||||
|
- **Project Setup**: ✅ Ready for development
|
||||||
|
- **API Keys**: ⏳ Pending manual generation
|
||||||
|
- **Implementation**: ⏳ Ready to begin Phase 1
|
||||||
|
|
||||||
|
### Success Criteria Met
|
||||||
|
- [x] Local Ghost instance running and accessible
|
||||||
|
- [x] Project structure created with all placeholder files
|
||||||
|
- [x] All dependencies installed and TypeScript compiling
|
||||||
|
- [x] Research documentation completed for authentication flows
|
||||||
|
- [x] Clear understanding of Ghost API error handling
|
||||||
|
- [ ] Both Content and Admin API keys obtained and tested (pending)
|
||||||
|
|
||||||
|
## Architecture Decisions Made
|
||||||
|
|
||||||
|
### 1. Technology Stack
|
||||||
|
- **Runtime**: Node.js v18+ with TypeScript
|
||||||
|
- **MCP SDK**: @modelcontextprotocol/sdk
|
||||||
|
- **HTTP Client**: axios (with retry and connection pooling)
|
||||||
|
- **Authentication**: jsonwebtoken for Admin API
|
||||||
|
- **Validation**: zod for runtime type checking
|
||||||
|
- **Logging**: winston for structured logging
|
||||||
|
|
||||||
|
### 2. Project Organization
|
||||||
|
- **Modular Tools**: Separate files for each resource type
|
||||||
|
- **API Separation**: Clear distinction between Content and Admin tools
|
||||||
|
- **Type Safety**: Comprehensive TypeScript types for all APIs
|
||||||
|
- **Error Handling**: Centralized error management with categories
|
||||||
|
|
||||||
|
### 3. Implementation Strategy
|
||||||
|
- **Phase-based Development**: Clear separation of concerns
|
||||||
|
- **Documentation-First**: All patterns documented before implementation
|
||||||
|
- **Testing-Ready**: Jest configuration and testing strategy planned
|
||||||
|
- **Production-Ready**: Comprehensive error handling and logging
|
||||||
|
|
||||||
|
## Documentation Quality
|
||||||
|
All contract documentation includes:
|
||||||
|
- ✅ Complete API specifications
|
||||||
|
- ✅ Implementation examples
|
||||||
|
- ✅ Error handling patterns
|
||||||
|
- ✅ Testing strategies
|
||||||
|
- ✅ Best practices and security considerations
|
||||||
|
|
||||||
|
## Phase 0 Conclusion
|
||||||
|
Phase 0 has successfully established a solid foundation for Ghost MCP development. The comprehensive research and documentation provide clear guidance for all subsequent phases. The project is now ready to proceed to Phase 1 implementation once API keys are generated.
|
||||||
|
|
||||||
|
**Estimated Time**: Phase 0 completed within planned 1-2 day timeframe.
|
||||||
|
**Quality**: High - comprehensive documentation and well-structured project setup.
|
||||||
|
**Readiness**: Ready for Phase 1 implementation.
|
||||||
85
contracts/rate-limits.md
Normal file
85
contracts/rate-limits.md
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
# Ghost API Rate Limiting Documentation
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Documentation of Ghost API rate limiting behavior and best practices for the MCP server implementation.
|
||||||
|
|
||||||
|
## Current Knowledge
|
||||||
|
Based on initial research, Ghost APIs do not have strict documented rate limits, but reasonable usage is expected.
|
||||||
|
|
||||||
|
## Content API Rate Limiting
|
||||||
|
|
||||||
|
### Observed Behavior
|
||||||
|
- No strict rate limits documented
|
||||||
|
- Designed for public consumption
|
||||||
|
- Responses are fully cacheable
|
||||||
|
- No rate limit headers observed in initial testing
|
||||||
|
|
||||||
|
### Best Practices
|
||||||
|
- Implement caching for repeated requests
|
||||||
|
- Use appropriate cache headers
|
||||||
|
- Avoid excessive concurrent requests
|
||||||
|
- Monitor response times
|
||||||
|
|
||||||
|
## Admin API Rate Limiting
|
||||||
|
|
||||||
|
### Observed Behavior
|
||||||
|
- No strict rate limits documented
|
||||||
|
- Server-side usage expected
|
||||||
|
- JWT tokens expire after 5 minutes (natural rate limiting)
|
||||||
|
|
||||||
|
### Best Practices
|
||||||
|
- Reuse JWT tokens within validity period
|
||||||
|
- Avoid generating new tokens for each request
|
||||||
|
- Implement request queuing for bulk operations
|
||||||
|
- Monitor API response times and errors
|
||||||
|
|
||||||
|
## Implementation Recommendations
|
||||||
|
|
||||||
|
### Caching Strategy
|
||||||
|
```javascript
|
||||||
|
// TODO: Phase 1 - Implement request caching
|
||||||
|
class GhostApiCache {
|
||||||
|
constructor(ttl = 300000) { // 5 minutes default
|
||||||
|
this.cache = new Map();
|
||||||
|
this.ttl = ttl;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key) {
|
||||||
|
const item = this.cache.get(key);
|
||||||
|
if (item && Date.now() < item.expiry) {
|
||||||
|
return item.data;
|
||||||
|
}
|
||||||
|
this.cache.delete(key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
set(key, data) {
|
||||||
|
this.cache.set(key, {
|
||||||
|
data,
|
||||||
|
expiry: Date.now() + this.ttl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rate Limiting Detection
|
||||||
|
- Monitor for 429 status codes
|
||||||
|
- Watch for response time degradation
|
||||||
|
- Implement backoff on authentication errors
|
||||||
|
|
||||||
|
### Connection Pooling
|
||||||
|
- Use HTTP connection pooling
|
||||||
|
- Limit concurrent requests
|
||||||
|
- Queue requests during high load
|
||||||
|
|
||||||
|
## Research Status
|
||||||
|
- ⏳ Rate limit testing pending API keys
|
||||||
|
- ⏳ Load testing pending implementation
|
||||||
|
- ⏳ Cache strategy pending Phase 1
|
||||||
|
- ⏳ Performance monitoring pending implementation
|
||||||
|
|
||||||
|
## Future Research Areas
|
||||||
|
1. Test actual rate limits with API keys
|
||||||
|
2. Measure response times under load
|
||||||
|
3. Test cache effectiveness
|
||||||
|
4. Monitor for any undocumented limits
|
||||||
66
contracts/setup-process.md
Normal file
66
contracts/setup-process.md
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# Ghost MCP Setup Process Documentation
|
||||||
|
|
||||||
|
## Phase 0 Research & Setup Progress
|
||||||
|
|
||||||
|
### Current Status
|
||||||
|
- ✅ Ghost instance running at http://localhost:2368
|
||||||
|
- ✅ Ghost admin accessible at http://localhost:2368/ghost
|
||||||
|
- ✅ MySQL database healthy and connected
|
||||||
|
- ✅ Ghost initialized with default site (title: "Ghost")
|
||||||
|
- 🔄 Admin account setup (requires completion)
|
||||||
|
- ⏳ API key generation (pending admin setup)
|
||||||
|
|
||||||
|
### Research Findings
|
||||||
|
- Ghost API is responding correctly
|
||||||
|
- Admin API requires authentication (expected behavior)
|
||||||
|
- Site is initialized but admin access needed for API keys
|
||||||
|
|
||||||
|
### Setup Steps
|
||||||
|
|
||||||
|
#### 1. Ghost Admin Account Creation
|
||||||
|
**URL**: http://localhost:2368/ghost
|
||||||
|
**Status**: Needs completion
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Navigate to http://localhost:2368/ghost
|
||||||
|
2. Complete the Ghost setup wizard:
|
||||||
|
- Create admin account (email, password, site title)
|
||||||
|
- Complete site configuration
|
||||||
|
3. Access admin dashboard
|
||||||
|
|
||||||
|
#### 2. API Key Generation Process
|
||||||
|
**Status**: Pending admin account creation
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. In Ghost Admin, navigate to Settings → Integrations
|
||||||
|
2. Click "Add custom integration"
|
||||||
|
3. Create integration named "Ghost MCP Development"
|
||||||
|
4. Copy both API keys:
|
||||||
|
- **Content API Key**: For read-only operations
|
||||||
|
- **Admin API Key**: For read/write operations
|
||||||
|
|
||||||
|
#### 3. API Key Testing
|
||||||
|
**Status**: Pending API key generation
|
||||||
|
|
||||||
|
**Tests to perform:**
|
||||||
|
1. Test Content API with key
|
||||||
|
2. Test Admin API authentication
|
||||||
|
3. Document authentication flows
|
||||||
|
|
||||||
|
### Next Steps
|
||||||
|
1. Complete Ghost admin setup
|
||||||
|
2. Generate and test API keys
|
||||||
|
3. Begin API research and documentation
|
||||||
|
|
||||||
|
### Research Documentation Structure
|
||||||
|
```
|
||||||
|
contracts/
|
||||||
|
├── setup-process.md # This file
|
||||||
|
├── admin-api-auth.md # JWT authentication research
|
||||||
|
├── content-api-auth.md # Content API authentication
|
||||||
|
├── error-responses.md # Error response catalog
|
||||||
|
├── ghost-filtering.md # Filtering syntax research
|
||||||
|
├── content-formats.md # Content format requirements
|
||||||
|
├── api-endpoints.md # Complete endpoint catalog
|
||||||
|
└── rate-limits.md # Rate limiting behavior
|
||||||
|
```
|
||||||
99
docker-compose.yml
Normal file
99
docker-compose.yml
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
ghost:
|
||||||
|
image: ghost:5-alpine
|
||||||
|
container_name: ghost-mcp-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "2368:2368"
|
||||||
|
environment:
|
||||||
|
# Basic Ghost configuration
|
||||||
|
url: http://localhost:2368
|
||||||
|
NODE_ENV: development
|
||||||
|
|
||||||
|
# Database configuration
|
||||||
|
database__client: mysql
|
||||||
|
database__connection__host: ghost-db
|
||||||
|
database__connection__user: ${MYSQL_USER:-ghost}
|
||||||
|
database__connection__password: ${MYSQL_PASSWORD:-ghostpassword}
|
||||||
|
database__connection__database: ${MYSQL_DATABASE:-ghost_dev}
|
||||||
|
database__connection__charset: utf8mb4
|
||||||
|
|
||||||
|
# Admin configuration for easier access
|
||||||
|
admin__url: http://localhost:2368
|
||||||
|
|
||||||
|
# Logging configuration for development
|
||||||
|
logging__level: info
|
||||||
|
logging__transports: '["stdout"]'
|
||||||
|
|
||||||
|
# Privacy settings for development
|
||||||
|
privacy__useUpdateCheck: false
|
||||||
|
privacy__useVersionNotifications: false
|
||||||
|
|
||||||
|
# Mail configuration (optional - for testing)
|
||||||
|
mail__transport: SMTP
|
||||||
|
mail__from: ${GHOST_MAIL_FROM:-noreply@localhost}
|
||||||
|
mail__options__service: ${MAIL_SERVICE:-}
|
||||||
|
mail__options__auth__user: ${MAIL_USER:-}
|
||||||
|
mail__options__auth__pass: ${MAIL_PASSWORD:-}
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
- ghost_content:/var/lib/ghost/content
|
||||||
|
depends_on:
|
||||||
|
ghost-db:
|
||||||
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- ghost-network
|
||||||
|
|
||||||
|
ghost-db:
|
||||||
|
image: mysql:8.0
|
||||||
|
container_name: ghost-db-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpassword}
|
||||||
|
MYSQL_DATABASE: ${MYSQL_DATABASE:-ghost_dev}
|
||||||
|
MYSQL_USER: ${MYSQL_USER:-ghost}
|
||||||
|
MYSQL_PASSWORD: ${MYSQL_PASSWORD:-ghostpassword}
|
||||||
|
MYSQL_CHARSET: utf8mb4
|
||||||
|
MYSQL_COLLATION: utf8mb4_general_ci
|
||||||
|
volumes:
|
||||||
|
- ghost_mysql:/var/lib/mysql
|
||||||
|
networks:
|
||||||
|
- ghost-network
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
|
||||||
|
timeout: 20s
|
||||||
|
retries: 10
|
||||||
|
interval: 10s
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
# Optional: phpMyAdmin for database management during development
|
||||||
|
phpmyadmin:
|
||||||
|
image: phpmyadmin/phpmyadmin:latest
|
||||||
|
container_name: ghost-phpmyadmin-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "8080:80"
|
||||||
|
environment:
|
||||||
|
PMA_HOST: ghost-db
|
||||||
|
PMA_USER: ${MYSQL_USER:-ghost}
|
||||||
|
PMA_PASSWORD: ${MYSQL_PASSWORD:-ghostpassword}
|
||||||
|
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpassword}
|
||||||
|
depends_on:
|
||||||
|
- ghost-db
|
||||||
|
networks:
|
||||||
|
- ghost-network
|
||||||
|
profiles:
|
||||||
|
- debug
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
ghost_content:
|
||||||
|
name: ghost_mcp_content
|
||||||
|
ghost_mysql:
|
||||||
|
name: ghost_mcp_mysql
|
||||||
|
|
||||||
|
networks:
|
||||||
|
ghost-network:
|
||||||
|
name: ghost-mcp-network
|
||||||
|
driver: bridge
|
||||||
76
pyproject.toml
Normal file
76
pyproject.toml
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "ghost-mcp"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Ghost CMS MCP server providing comprehensive Ghost API access"
|
||||||
|
authors = [{name = "Ghost MCP Team"}]
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
license = {text = "MIT"}
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
"fastmcp>=0.2.0",
|
||||||
|
"requests>=2.31.0",
|
||||||
|
"pyjwt>=2.8.0",
|
||||||
|
"pydantic>=2.5.0",
|
||||||
|
"python-dotenv>=1.0.0",
|
||||||
|
"structlog>=23.2.0",
|
||||||
|
"httpx>=0.25.0",
|
||||||
|
"typing-extensions>=4.8.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
dev = [
|
||||||
|
"pytest>=7.4.0",
|
||||||
|
"pytest-asyncio>=0.21.0",
|
||||||
|
"pytest-cov>=4.1.0",
|
||||||
|
"black>=23.0.0",
|
||||||
|
"ruff>=0.1.0",
|
||||||
|
"mypy>=1.7.0",
|
||||||
|
"pre-commit>=3.5.0",
|
||||||
|
"hatchling>=1.21.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
ghost-mcp = "ghost_mcp.server:main"
|
||||||
|
|
||||||
|
# Package discovery
|
||||||
|
[tool.hatch.build.targets.wheel]
|
||||||
|
packages = ["src/ghost_mcp"]
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 88
|
||||||
|
target-version = ['py310']
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
target-version = "py310"
|
||||||
|
line-length = 88
|
||||||
|
select = ["E", "F", "W", "I", "N", "UP", "ANN", "S", "BLE", "FBT", "B", "A", "COM", "C4", "DTZ", "T10", "EM", "EXE", "FA", "ISC", "ICN", "G", "INP", "PIE", "T20", "PYI", "PT", "Q", "RSE", "RET", "SLF", "SLOT", "SIM", "TID", "TCH", "INT", "ARG", "PTH", "TD", "FIX", "ERA", "PD", "PGH", "PL", "TRY", "FLY", "NPY", "AIR", "PERF", "FURB", "LOG", "RUF"]
|
||||||
|
ignore = ["ANN101", "ANN102", "S101", "PLR0913", "PLR0915", "PLR2004"]
|
||||||
|
|
||||||
|
[tool.mypy]
|
||||||
|
python_version = "3.10"
|
||||||
|
strict = true
|
||||||
|
warn_return_any = true
|
||||||
|
warn_unused_configs = true
|
||||||
|
disallow_untyped_defs = true
|
||||||
|
disallow_incomplete_defs = true
|
||||||
|
check_untyped_defs = true
|
||||||
|
disallow_untyped_decorators = true
|
||||||
|
no_implicit_optional = true
|
||||||
|
warn_redundant_casts = true
|
||||||
|
warn_unused_ignores = true
|
||||||
|
warn_no_return = true
|
||||||
|
warn_unreachable = true
|
||||||
|
strict_equality = true
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
testpaths = ["tests"]
|
||||||
|
python_files = ["test_*.py", "*_test.py"]
|
||||||
|
python_classes = ["Test*"]
|
||||||
|
python_functions = ["test_*"]
|
||||||
|
addopts = "-v --cov=ghost_mcp --cov-report=html --cov-report=term-missing"
|
||||||
|
asyncio_mode = "auto"
|
||||||
135
scripts/setup-tokens.sh
Executable file
135
scripts/setup-tokens.sh
Executable file
|
|
@ -0,0 +1,135 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Ghost MCP API Token Setup Script
|
||||||
|
echo "🔑 Setting up Ghost MCP API tokens..."
|
||||||
|
|
||||||
|
# Check if Docker containers are running
|
||||||
|
if ! docker-compose ps | grep -q "ghost-mcp-dev.*Up"; then
|
||||||
|
echo "❌ Error: Ghost container is not running"
|
||||||
|
echo "Please run: make start-ghost"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! docker-compose ps | grep -q "ghost-db-dev.*Up"; then
|
||||||
|
echo "❌ Error: Ghost database container is not running"
|
||||||
|
echo "Please run: make start-ghost"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✅ Ghost containers are running"
|
||||||
|
|
||||||
|
# Wait for Ghost to be fully ready
|
||||||
|
echo "⏳ Waiting for Ghost API to be ready..."
|
||||||
|
max_attempts=30
|
||||||
|
attempt=0
|
||||||
|
|
||||||
|
while [ $attempt -lt $max_attempts ]; do
|
||||||
|
if curl -s "http://localhost:2368/ghost/api/content/" >/dev/null 2>&1; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
echo " Attempt $((attempt + 1))/$max_attempts - waiting for Ghost..."
|
||||||
|
sleep 2
|
||||||
|
attempt=$((attempt + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ $attempt -eq $max_attempts ]; then
|
||||||
|
echo "❌ Error: Ghost API did not become ready in time"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✅ Ghost API is ready"
|
||||||
|
|
||||||
|
# Extract API keys from database
|
||||||
|
echo "🔍 Extracting API keys from Ghost database..."
|
||||||
|
|
||||||
|
CONTENT_API_KEY=$(docker exec ghost-db-dev mysql -u root -prootpassword ghost_dev -e "SELECT secret FROM api_keys WHERE type='content' LIMIT 1;" 2>/dev/null | tail -n 1)
|
||||||
|
ADMIN_API_DATA=$(docker exec ghost-db-dev mysql -u root -prootpassword ghost_dev -e "SELECT id, secret FROM api_keys WHERE type='admin' LIMIT 1;" 2>/dev/null | tail -n 1)
|
||||||
|
|
||||||
|
if [ -z "$CONTENT_API_KEY" ] || [ "$CONTENT_API_KEY" = "secret" ]; then
|
||||||
|
echo "❌ Error: Could not retrieve Content API key"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$ADMIN_API_DATA" ] || [ "$ADMIN_API_DATA" = "id secret" ]; then
|
||||||
|
echo "❌ Error: Could not retrieve Admin API key"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse admin API data (format: "id secret")
|
||||||
|
ADMIN_API_ID=$(echo "$ADMIN_API_DATA" | cut -f1)
|
||||||
|
ADMIN_API_SECRET=$(echo "$ADMIN_API_DATA" | cut -f2)
|
||||||
|
ADMIN_API_KEY="${ADMIN_API_ID}:${ADMIN_API_SECRET}"
|
||||||
|
|
||||||
|
echo "✅ API keys extracted successfully"
|
||||||
|
echo " Content API Key: ${CONTENT_API_KEY:0:10}..."
|
||||||
|
echo " Admin API Key: ${ADMIN_API_ID:0:10}:${ADMIN_API_SECRET:0:10}..."
|
||||||
|
|
||||||
|
# Create .env file
|
||||||
|
echo "📝 Creating .env file..."
|
||||||
|
|
||||||
|
cat > .env << EOF
|
||||||
|
# Ghost MCP Server Configuration
|
||||||
|
# Generated on $(date)
|
||||||
|
|
||||||
|
# Ghost instance configuration
|
||||||
|
GHOST_URL=http://localhost:2368
|
||||||
|
GHOST_CONTENT_API_KEY=$CONTENT_API_KEY
|
||||||
|
GHOST_ADMIN_API_KEY=$ADMIN_API_KEY
|
||||||
|
GHOST_VERSION=v5.0
|
||||||
|
GHOST_MODE=auto
|
||||||
|
GHOST_TIMEOUT=30
|
||||||
|
GHOST_MAX_RETRIES=3
|
||||||
|
GHOST_RETRY_BACKOFF_FACTOR=2.0
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
LOG_LEVEL=info
|
||||||
|
LOG_STRUCTURED=true
|
||||||
|
LOG_REQUEST_ID=true
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "✅ .env file created successfully"
|
||||||
|
|
||||||
|
# Test the configuration
|
||||||
|
echo "🧪 Testing API connectivity..."
|
||||||
|
|
||||||
|
# Test Content API
|
||||||
|
echo " Testing Content API..."
|
||||||
|
CONTENT_TEST=$(curl -s "http://localhost:2368/ghost/api/content/settings/?key=$CONTENT_API_KEY" | grep -o '"title"' || echo "")
|
||||||
|
if [ -n "$CONTENT_TEST" ]; then
|
||||||
|
echo " ✅ Content API: Working"
|
||||||
|
else
|
||||||
|
echo " ❌ Content API: Failed"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test Admin API (using Python to generate JWT and test)
|
||||||
|
echo " Testing Admin API..."
|
||||||
|
python3 -c "
|
||||||
|
import asyncio
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.insert(0, 'src')
|
||||||
|
|
||||||
|
async def test_admin():
|
||||||
|
try:
|
||||||
|
from ghost_mcp.client import GhostClient
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request('GET', 'site/', api_type='admin')
|
||||||
|
print(' ✅ Admin API: Working')
|
||||||
|
except Exception as e:
|
||||||
|
print(f' ❌ Admin API: Failed - {e}')
|
||||||
|
|
||||||
|
asyncio.run(test_admin())
|
||||||
|
" 2>/dev/null || echo " ⚠️ Admin API: Could not test (install dependencies first)"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "🎉 Ghost MCP API tokens setup complete!"
|
||||||
|
echo ""
|
||||||
|
echo "Next steps:"
|
||||||
|
echo "1. Install dependencies: make install"
|
||||||
|
echo "2. Test the implementation: make test"
|
||||||
|
echo "3. Run the MCP server: make run"
|
||||||
|
echo ""
|
||||||
|
echo "Configuration file: .env"
|
||||||
|
echo "Ghost admin interface: http://localhost:2368/ghost/"
|
||||||
|
echo "Ghost public site: http://localhost:2368/"
|
||||||
44
scripts/test-connection.py
Executable file
44
scripts/test-connection.py
Executable file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Test Ghost API connectivity script."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add src to path
|
||||||
|
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
||||||
|
|
||||||
|
from ghost_mcp.client import GhostClient
|
||||||
|
|
||||||
|
|
||||||
|
async def test_connection():
|
||||||
|
"""Test Ghost API connection."""
|
||||||
|
print("🧪 Testing Ghost API connection...")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
# Test Content API
|
||||||
|
try:
|
||||||
|
result = await client._make_request("GET", "settings/", api_type="content")
|
||||||
|
title = result.get("settings", {}).get("title", "Unknown")
|
||||||
|
print(f"✅ Content API: Connected to '{title}'")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Content API: {e}")
|
||||||
|
|
||||||
|
# Test Admin API
|
||||||
|
try:
|
||||||
|
result = await client._make_request("GET", "site/", api_type="admin")
|
||||||
|
print("✅ Admin API: Connected successfully")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Admin API: {e}")
|
||||||
|
|
||||||
|
# Test posts endpoint
|
||||||
|
try:
|
||||||
|
result = await client.get_posts(limit=1)
|
||||||
|
posts_count = len(result.get("posts", []))
|
||||||
|
print(f"✅ Posts: Found {posts_count} posts")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Posts: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(test_connection())
|
||||||
67
scripts/test-mcp-tools.py
Executable file
67
scripts/test-mcp-tools.py
Executable file
|
|
@ -0,0 +1,67 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Test Ghost MCP tools functionality."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add src to path
|
||||||
|
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
||||||
|
|
||||||
|
from ghost_mcp.tools.content.posts import get_posts
|
||||||
|
from ghost_mcp.tools.content.settings import get_settings
|
||||||
|
from ghost_mcp.server import check_ghost_connection
|
||||||
|
|
||||||
|
|
||||||
|
async def test_mcp_tools():
|
||||||
|
"""Test MCP tools functionality."""
|
||||||
|
print("🧪 Testing Ghost MCP tools...")
|
||||||
|
|
||||||
|
# Test connection check tool
|
||||||
|
print("\n1. Testing connection check tool...")
|
||||||
|
try:
|
||||||
|
result = await check_ghost_connection()
|
||||||
|
data = json.loads(result)
|
||||||
|
print(f"✅ Connection check: {data.get('connection_test', 'unknown')}")
|
||||||
|
print(f" Ghost URL: {data.get('ghost_url')}")
|
||||||
|
print(f" Content API: {'✅' if data.get('content_api_configured') else '❌'}")
|
||||||
|
print(f" Admin API: {'✅' if data.get('admin_api_configured') else '❌'}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Connection check failed: {e}")
|
||||||
|
|
||||||
|
# Test get settings
|
||||||
|
print("\n2. Testing get_settings tool...")
|
||||||
|
try:
|
||||||
|
result = await get_settings()
|
||||||
|
data = json.loads(result)
|
||||||
|
if "settings" in data:
|
||||||
|
settings = data["settings"]
|
||||||
|
print(f"✅ Settings: Site title is '{settings.get('title')}'")
|
||||||
|
print(f" Description: {settings.get('description')}")
|
||||||
|
print(f" URL: {settings.get('url')}")
|
||||||
|
else:
|
||||||
|
print(f"❌ Settings: {data}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Settings failed: {e}")
|
||||||
|
|
||||||
|
# Test get posts
|
||||||
|
print("\n3. Testing get_posts tool...")
|
||||||
|
try:
|
||||||
|
result = await get_posts(limit=2)
|
||||||
|
data = json.loads(result)
|
||||||
|
if "posts" in data:
|
||||||
|
posts = data["posts"]
|
||||||
|
print(f"✅ Posts: Found {len(posts)} posts")
|
||||||
|
for post in posts:
|
||||||
|
print(f" - '{post.get('title')}' (status: {post.get('status')})")
|
||||||
|
else:
|
||||||
|
print(f"❌ Posts: {data}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Posts failed: {e}")
|
||||||
|
|
||||||
|
print("\n🎉 MCP tools test completed!")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(test_mcp_tools())
|
||||||
5
src/ghost_mcp/__init__.py
Normal file
5
src/ghost_mcp/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
"""Ghost MCP Server - Comprehensive Ghost CMS integration for MCP."""
|
||||||
|
|
||||||
|
__version__ = "0.1.0"
|
||||||
|
__author__ = "Ghost MCP Team"
|
||||||
|
__description__ = "Ghost CMS MCP server providing comprehensive Ghost API access"
|
||||||
6
src/ghost_mcp/auth/__init__.py
Normal file
6
src/ghost_mcp/auth/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
"""Authentication modules for Ghost API."""
|
||||||
|
|
||||||
|
from .admin_auth import AdminAuth
|
||||||
|
from .content_auth import ContentAuth
|
||||||
|
|
||||||
|
__all__ = ["AdminAuth", "ContentAuth"]
|
||||||
134
src/ghost_mcp/auth/admin_auth.py
Normal file
134
src/ghost_mcp/auth/admin_auth.py
Normal file
|
|
@ -0,0 +1,134 @@
|
||||||
|
"""Admin API authentication using JWT tokens."""
|
||||||
|
|
||||||
|
import time
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
import jwt
|
||||||
|
|
||||||
|
from ..config import config
|
||||||
|
from ..types.errors import AuthenticationError
|
||||||
|
from ..utils.logging import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class AdminAuth:
|
||||||
|
"""Admin API JWT authentication handler."""
|
||||||
|
|
||||||
|
def __init__(self, api_key: Optional[str] = None) -> None:
|
||||||
|
"""Initialize with optional API key override."""
|
||||||
|
self.api_key = api_key or config.ghost.admin_api_key
|
||||||
|
self._cached_token: Optional[str] = None
|
||||||
|
self._token_expires_at: Optional[datetime] = None
|
||||||
|
|
||||||
|
if not self.api_key:
|
||||||
|
logger.warning("No Admin API key configured")
|
||||||
|
|
||||||
|
def get_auth_headers(self, request_id: Optional[str] = None) -> Dict[str, str]:
|
||||||
|
"""Get authentication headers for Admin API requests."""
|
||||||
|
token = self._get_jwt_token(request_id)
|
||||||
|
return {"Authorization": f"Ghost {token}"}
|
||||||
|
|
||||||
|
def _get_jwt_token(self, request_id: Optional[str] = None) -> str:
|
||||||
|
"""Get or generate JWT token for Admin API."""
|
||||||
|
if not self.api_key:
|
||||||
|
raise AuthenticationError(
|
||||||
|
"Admin API key not configured",
|
||||||
|
context="Admin API requires an API key for JWT generation",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if we have a valid cached token
|
||||||
|
if self._cached_token and self._token_expires_at:
|
||||||
|
if datetime.now() < self._token_expires_at - timedelta(seconds=30):
|
||||||
|
logger.debug("Using cached JWT token", request_id=request_id)
|
||||||
|
return self._cached_token
|
||||||
|
|
||||||
|
# Generate new token
|
||||||
|
token = self._generate_jwt_token(request_id)
|
||||||
|
self._cached_token = token
|
||||||
|
|
||||||
|
# JWT tokens expire after 5 minutes
|
||||||
|
self._token_expires_at = datetime.now() + timedelta(minutes=5)
|
||||||
|
|
||||||
|
logger.debug("Generated new JWT token", request_id=request_id)
|
||||||
|
return token
|
||||||
|
|
||||||
|
def _generate_jwt_token(self, request_id: Optional[str] = None) -> str:
|
||||||
|
"""Generate JWT token for Admin API authentication."""
|
||||||
|
try:
|
||||||
|
# Split the admin key (id:secret format)
|
||||||
|
if ":" not in self.api_key:
|
||||||
|
raise AuthenticationError(
|
||||||
|
"Invalid Admin API key format",
|
||||||
|
context="Admin API key must be in 'id:secret' format",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
key_id, secret = self.api_key.split(":", 1)
|
||||||
|
|
||||||
|
# Current timestamp
|
||||||
|
now = int(time.time())
|
||||||
|
|
||||||
|
# JWT payload
|
||||||
|
payload = {
|
||||||
|
"iat": now,
|
||||||
|
"exp": now + 300, # 5 minutes from now
|
||||||
|
"aud": "/admin/",
|
||||||
|
}
|
||||||
|
|
||||||
|
# JWT header
|
||||||
|
header = {"alg": "HS256", "typ": "JWT", "kid": key_id}
|
||||||
|
|
||||||
|
# Generate token
|
||||||
|
token = jwt.encode(
|
||||||
|
payload,
|
||||||
|
bytes.fromhex(secret),
|
||||||
|
algorithm="HS256",
|
||||||
|
headers=header,
|
||||||
|
)
|
||||||
|
|
||||||
|
return token
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise AuthenticationError(
|
||||||
|
f"Failed to generate JWT token: {e}",
|
||||||
|
context="JWT token generation failed",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
|
||||||
|
def is_configured(self) -> bool:
|
||||||
|
"""Check if Admin API authentication is properly configured."""
|
||||||
|
return bool(self.api_key and ":" in self.api_key)
|
||||||
|
|
||||||
|
def validate_key_format(self) -> bool:
|
||||||
|
"""Validate the Admin API key format."""
|
||||||
|
if not self.api_key:
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Admin keys should be in 'id:secret' format
|
||||||
|
if ":" not in self.api_key:
|
||||||
|
return False
|
||||||
|
|
||||||
|
key_id, secret = self.api_key.split(":", 1)
|
||||||
|
|
||||||
|
# Key ID should be 24 character hex string
|
||||||
|
if len(key_id) != 24 or not all(c in "0123456789abcdef" for c in key_id.lower()):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Secret should be 64 character hex string
|
||||||
|
if len(secret) != 64 or not all(c in "0123456789abcdef" for c in secret.lower()):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def invalidate_cache(self) -> None:
|
||||||
|
"""Invalidate cached JWT token to force regeneration."""
|
||||||
|
self._cached_token = None
|
||||||
|
self._token_expires_at = None
|
||||||
|
logger.debug("JWT token cache invalidated")
|
||||||
44
src/ghost_mcp/auth/content_auth.py
Normal file
44
src/ghost_mcp/auth/content_auth.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
"""Content API authentication using query parameter keys."""
|
||||||
|
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
from ..config import config
|
||||||
|
from ..types.errors import AuthenticationError
|
||||||
|
from ..utils.logging import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ContentAuth:
|
||||||
|
"""Content API authentication handler."""
|
||||||
|
|
||||||
|
def __init__(self, api_key: Optional[str] = None) -> None:
|
||||||
|
"""Initialize with optional API key override."""
|
||||||
|
self.api_key = api_key or config.ghost.content_api_key
|
||||||
|
if not self.api_key:
|
||||||
|
logger.warning("No Content API key configured")
|
||||||
|
|
||||||
|
def get_auth_params(self, request_id: Optional[str] = None) -> Dict[str, str]:
|
||||||
|
"""Get authentication parameters for Content API requests."""
|
||||||
|
if not self.api_key:
|
||||||
|
raise AuthenticationError(
|
||||||
|
"Content API key not configured",
|
||||||
|
context="Content API requires an API key",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"key": self.api_key}
|
||||||
|
|
||||||
|
def is_configured(self) -> bool:
|
||||||
|
"""Check if Content API authentication is properly configured."""
|
||||||
|
return bool(self.api_key)
|
||||||
|
|
||||||
|
def validate_key_format(self) -> bool:
|
||||||
|
"""Validate the API key format."""
|
||||||
|
if not self.api_key:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Content API keys are typically 26 character hex strings
|
||||||
|
return len(self.api_key) == 26 and all(
|
||||||
|
c in "0123456789abcdef" for c in self.api_key.lower()
|
||||||
|
)
|
||||||
300
src/ghost_mcp/client.py
Normal file
300
src/ghost_mcp/client.py
Normal file
|
|
@ -0,0 +1,300 @@
|
||||||
|
"""Ghost API client with unified interface for Content and Admin APIs."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
from urllib.parse import urljoin, urlparse
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from httpx import Response
|
||||||
|
|
||||||
|
from .auth import AdminAuth, ContentAuth
|
||||||
|
from .config import config
|
||||||
|
from .types.errors import AuthenticationError, GhostApiError, NetworkError
|
||||||
|
from .types.ghost import GhostApiResponse, GhostErrorResponse
|
||||||
|
from .utils.logging import get_logger
|
||||||
|
from .utils.retry import RetryConfig, with_retry
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class GhostClient:
|
||||||
|
"""Unified Ghost API client for both Content and Admin APIs."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
base_url: Optional[str] = None,
|
||||||
|
content_api_key: Optional[str] = None,
|
||||||
|
admin_api_key: Optional[str] = None,
|
||||||
|
timeout: Optional[int] = None,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize Ghost client with optional configuration overrides."""
|
||||||
|
self.base_url = base_url or str(config.ghost.url)
|
||||||
|
self.timeout = timeout or config.ghost.timeout
|
||||||
|
|
||||||
|
# Ensure base URL has proper format
|
||||||
|
if not self.base_url.endswith("/"):
|
||||||
|
self.base_url += "/"
|
||||||
|
|
||||||
|
# Initialize authentication handlers
|
||||||
|
self.content_auth = ContentAuth(content_api_key)
|
||||||
|
self.admin_auth = AdminAuth(admin_api_key)
|
||||||
|
|
||||||
|
# HTTP client configuration
|
||||||
|
self.client = httpx.AsyncClient(
|
||||||
|
timeout=self.timeout,
|
||||||
|
limits=httpx.Limits(max_keepalive_connections=10, max_connections=100),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Retry configuration
|
||||||
|
self.retry_config = RetryConfig(
|
||||||
|
max_retries=config.ghost.max_retries,
|
||||||
|
base_delay=1.0,
|
||||||
|
exponential_base=config.ghost.retry_backoff_factor,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def __aenter__(self) -> "GhostClient":
|
||||||
|
"""Async context manager entry."""
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
||||||
|
"""Async context manager exit."""
|
||||||
|
await self.close()
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
"""Close the HTTP client."""
|
||||||
|
await self.client.aclose()
|
||||||
|
|
||||||
|
def _build_url(self, endpoint: str, api_type: str = "content") -> str:
|
||||||
|
"""Build full URL for API endpoint."""
|
||||||
|
api_base = f"ghost/api/{api_type}/"
|
||||||
|
return urljoin(self.base_url, api_base + endpoint)
|
||||||
|
|
||||||
|
async def _make_request(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
endpoint: str,
|
||||||
|
api_type: str = "content",
|
||||||
|
params: Optional[Dict[str, Any]] = None,
|
||||||
|
json_data: Optional[Dict[str, Any]] = None,
|
||||||
|
files: Optional[Dict[str, Any]] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Make HTTP request to Ghost API with error handling and retries."""
|
||||||
|
if request_id is None:
|
||||||
|
request_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
url = self._build_url(endpoint, api_type)
|
||||||
|
headers = {"User-Agent": "Ghost-MCP/0.1.0"}
|
||||||
|
|
||||||
|
# Add authentication
|
||||||
|
if api_type == "content":
|
||||||
|
if not self.content_auth.is_configured():
|
||||||
|
raise AuthenticationError(
|
||||||
|
"Content API key not configured",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
if params is None:
|
||||||
|
params = {}
|
||||||
|
params.update(self.content_auth.get_auth_params(request_id))
|
||||||
|
|
||||||
|
elif api_type == "admin":
|
||||||
|
if not self.admin_auth.is_configured():
|
||||||
|
raise AuthenticationError(
|
||||||
|
"Admin API key not configured",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
headers.update(self.admin_auth.get_auth_headers(request_id))
|
||||||
|
|
||||||
|
# Prepare request data
|
||||||
|
request_kwargs: Dict[str, Any] = {
|
||||||
|
"method": method,
|
||||||
|
"url": url,
|
||||||
|
"headers": headers,
|
||||||
|
"params": params,
|
||||||
|
}
|
||||||
|
|
||||||
|
if json_data is not None:
|
||||||
|
request_kwargs["json"] = json_data
|
||||||
|
if files is not None:
|
||||||
|
request_kwargs["files"] = files
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"Making Ghost API request",
|
||||||
|
method=method,
|
||||||
|
url=url,
|
||||||
|
api_type=api_type,
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _request() -> Dict[str, Any]:
|
||||||
|
try:
|
||||||
|
response: Response = await self.client.request(**request_kwargs)
|
||||||
|
return await self._handle_response(response, request_id)
|
||||||
|
except httpx.TimeoutException as e:
|
||||||
|
raise NetworkError(
|
||||||
|
f"Request timeout: {e}",
|
||||||
|
context=f"Timeout after {self.timeout}s",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
except httpx.ConnectError as e:
|
||||||
|
raise NetworkError(
|
||||||
|
f"Connection error: {e}",
|
||||||
|
context=f"Failed to connect to {url}",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
raise NetworkError(
|
||||||
|
f"HTTP error: {e}",
|
||||||
|
context=f"Request to {url} failed",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
|
||||||
|
return await with_retry(_request, self.retry_config, request_id)
|
||||||
|
|
||||||
|
async def _handle_response(self, response: Response, request_id: str) -> Dict[str, Any]:
|
||||||
|
"""Handle HTTP response and convert to appropriate format or raise errors."""
|
||||||
|
logger.debug(
|
||||||
|
"Received Ghost API response",
|
||||||
|
status_code=response.status_code,
|
||||||
|
headers=dict(response.headers),
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check for successful response
|
||||||
|
if response.status_code < 400:
|
||||||
|
try:
|
||||||
|
data = response.json()
|
||||||
|
logger.debug("Successfully parsed response JSON", request_id=request_id)
|
||||||
|
return data
|
||||||
|
except Exception as e:
|
||||||
|
raise GhostApiError(
|
||||||
|
f"Failed to parse response JSON: {e}",
|
||||||
|
context="Invalid JSON response from Ghost API",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
|
||||||
|
# Handle error responses
|
||||||
|
try:
|
||||||
|
error_data = response.json()
|
||||||
|
error_response = GhostErrorResponse(**error_data)
|
||||||
|
|
||||||
|
# Get first error for primary error info
|
||||||
|
first_error = error_response.errors[0] if error_response.errors else None
|
||||||
|
error_message = first_error.message if first_error else "Unknown Ghost API error"
|
||||||
|
error_code = first_error.code if first_error else None
|
||||||
|
|
||||||
|
raise GhostApiError(
|
||||||
|
error_message,
|
||||||
|
code=error_code,
|
||||||
|
context=f"HTTP {response.status_code}",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if isinstance(e, GhostApiError):
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Fallback for non-JSON error responses
|
||||||
|
raise GhostApiError(
|
||||||
|
f"HTTP {response.status_code}: {response.text}",
|
||||||
|
context="Non-JSON error response",
|
||||||
|
request_id=request_id,
|
||||||
|
) from e
|
||||||
|
|
||||||
|
# Content API methods
|
||||||
|
async def get_posts(
|
||||||
|
self,
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Get posts from Content API."""
|
||||||
|
params = {}
|
||||||
|
if limit is not None:
|
||||||
|
params["limit"] = limit
|
||||||
|
if page is not None:
|
||||||
|
params["page"] = page
|
||||||
|
if filter is not None:
|
||||||
|
params["filter"] = filter
|
||||||
|
if include is not None:
|
||||||
|
params["include"] = include
|
||||||
|
if fields is not None:
|
||||||
|
params["fields"] = fields
|
||||||
|
if order is not None:
|
||||||
|
params["order"] = order
|
||||||
|
|
||||||
|
return await self._make_request(
|
||||||
|
"GET", "posts/", api_type="content", params=params, request_id=request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_post_by_id(
|
||||||
|
self,
|
||||||
|
post_id: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Get single post by ID from Content API."""
|
||||||
|
params = {}
|
||||||
|
if include is not None:
|
||||||
|
params["include"] = include
|
||||||
|
if fields is not None:
|
||||||
|
params["fields"] = fields
|
||||||
|
|
||||||
|
return await self._make_request(
|
||||||
|
"GET", f"posts/{post_id}/", api_type="content", params=params, request_id=request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_post_by_slug(
|
||||||
|
self,
|
||||||
|
slug: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Get single post by slug from Content API."""
|
||||||
|
params = {}
|
||||||
|
if include is not None:
|
||||||
|
params["include"] = include
|
||||||
|
if fields is not None:
|
||||||
|
params["fields"] = fields
|
||||||
|
|
||||||
|
return await self._make_request(
|
||||||
|
"GET", f"posts/slug/{slug}/", api_type="content", params=params, request_id=request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
# Admin API methods (examples)
|
||||||
|
async def create_post(
|
||||||
|
self,
|
||||||
|
post_data: Dict[str, Any],
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Create new post via Admin API."""
|
||||||
|
return await self._make_request(
|
||||||
|
"POST", "posts/", api_type="admin", json_data=post_data, request_id=request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
async def update_post(
|
||||||
|
self,
|
||||||
|
post_id: str,
|
||||||
|
post_data: Dict[str, Any],
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Update existing post via Admin API."""
|
||||||
|
return await self._make_request(
|
||||||
|
"PUT", f"posts/{post_id}/", api_type="admin", json_data=post_data, request_id=request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
async def delete_post(
|
||||||
|
self,
|
||||||
|
post_id: str,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Delete post via Admin API."""
|
||||||
|
return await self._make_request(
|
||||||
|
"DELETE", f"posts/{post_id}/", api_type="admin", request_id=request_id
|
||||||
|
)
|
||||||
81
src/ghost_mcp/config.py
Normal file
81
src/ghost_mcp/config.py
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
"""Configuration management with environment variable precedence."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from enum import Enum
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from pydantic import BaseModel, Field, HttpUrl
|
||||||
|
|
||||||
|
|
||||||
|
class LogLevel(str, Enum):
|
||||||
|
"""Logging levels."""
|
||||||
|
DEBUG = "debug"
|
||||||
|
INFO = "info"
|
||||||
|
WARNING = "warning"
|
||||||
|
ERROR = "error"
|
||||||
|
|
||||||
|
|
||||||
|
class GhostMode(str, Enum):
|
||||||
|
"""Ghost API mode."""
|
||||||
|
READONLY = "readonly"
|
||||||
|
READWRITE = "readwrite"
|
||||||
|
AUTO = "auto"
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingConfig(BaseModel):
|
||||||
|
"""Logging configuration."""
|
||||||
|
level: LogLevel = LogLevel.INFO
|
||||||
|
structured: bool = True
|
||||||
|
request_id: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class GhostConfig(BaseModel):
|
||||||
|
"""Ghost API configuration."""
|
||||||
|
url: HttpUrl = Field(default="http://localhost:2368")
|
||||||
|
content_api_key: Optional[str] = None
|
||||||
|
admin_api_key: Optional[str] = None
|
||||||
|
version: str = "v5.0"
|
||||||
|
mode: GhostMode = GhostMode.AUTO
|
||||||
|
timeout: int = 30
|
||||||
|
max_retries: int = 3
|
||||||
|
retry_backoff_factor: float = 2.0
|
||||||
|
|
||||||
|
|
||||||
|
class Config(BaseModel):
|
||||||
|
"""Main configuration class."""
|
||||||
|
ghost: GhostConfig = Field(default_factory=GhostConfig)
|
||||||
|
logging: LoggingConfig = Field(default_factory=LoggingConfig)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls, env_file: Optional[str] = None) -> "Config":
|
||||||
|
"""Load configuration with precedence: env vars > .env file > defaults."""
|
||||||
|
# Load .env file if specified or if .env exists
|
||||||
|
env_path = Path(env_file) if env_file else Path(".env")
|
||||||
|
if env_path.exists():
|
||||||
|
load_dotenv(env_path)
|
||||||
|
|
||||||
|
# Create config with environment variables taking precedence
|
||||||
|
ghost_config = GhostConfig(
|
||||||
|
url=os.getenv("GHOST_URL", "http://localhost:2368"),
|
||||||
|
content_api_key=os.getenv("GHOST_CONTENT_API_KEY"),
|
||||||
|
admin_api_key=os.getenv("GHOST_ADMIN_API_KEY"),
|
||||||
|
version=os.getenv("GHOST_VERSION", "v5.0"),
|
||||||
|
mode=GhostMode(os.getenv("GHOST_MODE", "auto")),
|
||||||
|
timeout=int(os.getenv("GHOST_TIMEOUT", "30")),
|
||||||
|
max_retries=int(os.getenv("GHOST_MAX_RETRIES", "3")),
|
||||||
|
retry_backoff_factor=float(os.getenv("GHOST_RETRY_BACKOFF_FACTOR", "2.0")),
|
||||||
|
)
|
||||||
|
|
||||||
|
logging_config = LoggingConfig(
|
||||||
|
level=LogLevel(os.getenv("LOG_LEVEL", "info")),
|
||||||
|
structured=os.getenv("LOG_STRUCTURED", "true").lower() == "true",
|
||||||
|
request_id=os.getenv("LOG_REQUEST_ID", "true").lower() == "true",
|
||||||
|
)
|
||||||
|
|
||||||
|
return cls(ghost=ghost_config, logging=logging_config)
|
||||||
|
|
||||||
|
|
||||||
|
# Global configuration instance
|
||||||
|
config = Config.load()
|
||||||
107
src/ghost_mcp/server.py
Normal file
107
src/ghost_mcp/server.py
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
"""Ghost MCP server entry point."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from .config import config
|
||||||
|
from .tools import register_admin_tools, register_content_tools
|
||||||
|
from .utils.logging import setup_logging, get_logger
|
||||||
|
|
||||||
|
# Set up logging
|
||||||
|
setup_logging()
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
# Create FastMCP server
|
||||||
|
mcp = FastMCP("Ghost MCP Server")
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def check_ghost_connection() -> str:
|
||||||
|
"""
|
||||||
|
Check connection to Ghost instance and API key configuration.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string with connection status and configuration info
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
from .client import GhostClient
|
||||||
|
|
||||||
|
status = {
|
||||||
|
"ghost_url": str(config.ghost.url),
|
||||||
|
"content_api_configured": bool(config.ghost.content_api_key),
|
||||||
|
"admin_api_configured": bool(config.ghost.admin_api_key),
|
||||||
|
"mode": config.ghost.mode.value,
|
||||||
|
"connection_test": "pending",
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
# Test Content API if configured
|
||||||
|
if client.content_auth.is_configured():
|
||||||
|
try:
|
||||||
|
await client._make_request("GET", "settings/", api_type="content")
|
||||||
|
status["content_api_status"] = "connected"
|
||||||
|
except Exception as e:
|
||||||
|
status["content_api_status"] = f"error: {e}"
|
||||||
|
|
||||||
|
# Test Admin API if configured
|
||||||
|
if client.admin_auth.is_configured():
|
||||||
|
try:
|
||||||
|
await client._make_request("GET", "site/", api_type="admin")
|
||||||
|
status["admin_api_status"] = "connected"
|
||||||
|
except Exception as e:
|
||||||
|
status["admin_api_status"] = f"error: {e}"
|
||||||
|
|
||||||
|
status["connection_test"] = "completed"
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
status["connection_test"] = f"failed: {e}"
|
||||||
|
|
||||||
|
return json.dumps(status, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def register_tools() -> None:
|
||||||
|
"""Register all MCP tools based on configuration."""
|
||||||
|
logger.info("Registering MCP tools", mode=config.ghost.mode.value)
|
||||||
|
|
||||||
|
# Always register Content API tools (read-only)
|
||||||
|
if config.ghost.content_api_key:
|
||||||
|
logger.info("Registering Content API tools")
|
||||||
|
register_content_tools(mcp)
|
||||||
|
else:
|
||||||
|
logger.warning("Content API key not configured - Content tools not available")
|
||||||
|
|
||||||
|
# Register Admin API tools based on mode and configuration
|
||||||
|
if config.ghost.mode.value in ["readwrite", "auto"]:
|
||||||
|
if config.ghost.admin_api_key:
|
||||||
|
logger.info("Registering Admin API tools")
|
||||||
|
register_admin_tools(mcp)
|
||||||
|
elif config.ghost.mode.value == "readwrite":
|
||||||
|
logger.warning("Admin API key not configured - Admin tools not available in readwrite mode")
|
||||||
|
else:
|
||||||
|
logger.info("Admin API key not configured - running in read-only mode")
|
||||||
|
else:
|
||||||
|
logger.info("Running in read-only mode - Admin tools not registered")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Main entry point for the Ghost MCP server."""
|
||||||
|
logger.info(
|
||||||
|
"Starting Ghost MCP server",
|
||||||
|
ghost_url=str(config.ghost.url),
|
||||||
|
mode=config.ghost.mode.value,
|
||||||
|
content_api_configured=bool(config.ghost.content_api_key),
|
||||||
|
admin_api_configured=bool(config.ghost.admin_api_key),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Register tools
|
||||||
|
register_tools()
|
||||||
|
|
||||||
|
# Run the MCP server
|
||||||
|
mcp.run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
11
src/ghost_mcp/tools/__init__.py
Normal file
11
src/ghost_mcp/tools/__init__.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
"""MCP tools for Ghost API access."""
|
||||||
|
|
||||||
|
from .content import *
|
||||||
|
from .admin import *
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
# Content API tools
|
||||||
|
"register_content_tools",
|
||||||
|
# Admin API tools
|
||||||
|
"register_admin_tools",
|
||||||
|
]
|
||||||
25
src/ghost_mcp/tools/admin/__init__.py
Normal file
25
src/ghost_mcp/tools/admin/__init__.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
"""Admin API tools for read/write access to Ghost content."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from .posts import register_admin_post_tools
|
||||||
|
from .pages import register_admin_page_tools
|
||||||
|
from .tags import register_admin_tag_tools
|
||||||
|
from .authors import register_admin_author_tools
|
||||||
|
from .members import register_admin_member_tools
|
||||||
|
from .settings import register_admin_settings_tools
|
||||||
|
from .media import register_admin_media_tools
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register all Admin API tools."""
|
||||||
|
register_admin_post_tools(mcp)
|
||||||
|
register_admin_page_tools(mcp)
|
||||||
|
register_admin_tag_tools(mcp)
|
||||||
|
register_admin_author_tools(mcp)
|
||||||
|
register_admin_member_tools(mcp)
|
||||||
|
register_admin_settings_tools(mcp)
|
||||||
|
register_admin_media_tools(mcp)
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["register_admin_tools"]
|
||||||
10
src/ghost_mcp/tools/admin/authors.py
Normal file
10
src/ghost_mcp/tools/admin/authors.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
"""Admin API tools for authors management."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_author_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register author management Admin API tools."""
|
||||||
|
# Author management tools would go here
|
||||||
|
# These are typically more complex due to user permissions
|
||||||
|
pass
|
||||||
9
src/ghost_mcp/tools/admin/media.py
Normal file
9
src/ghost_mcp/tools/admin/media.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
"""Admin API tools for media management."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_media_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register media management Admin API tools."""
|
||||||
|
# Media upload and management tools would go here
|
||||||
|
pass
|
||||||
9
src/ghost_mcp/tools/admin/members.py
Normal file
9
src/ghost_mcp/tools/admin/members.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
"""Admin API tools for members management."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_member_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register member management Admin API tools."""
|
||||||
|
# Member management tools would go here
|
||||||
|
pass
|
||||||
56
src/ghost_mcp/tools/admin/pages.py
Normal file
56
src/ghost_mcp/tools/admin/pages.py
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
"""Admin API tools for pages management."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_id_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_page_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register page management Admin API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def create_page(
|
||||||
|
title: str,
|
||||||
|
content: Optional[str] = None,
|
||||||
|
content_format: str = "lexical",
|
||||||
|
status: str = "draft",
|
||||||
|
slug: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""Create a new page via Ghost Admin API."""
|
||||||
|
if not title or not title.strip():
|
||||||
|
return json.dumps({"error": "Title is required"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
page_data: Dict[str, Any] = {
|
||||||
|
"pages": [{
|
||||||
|
"title": title.strip(),
|
||||||
|
"status": status,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
page = page_data["pages"][0]
|
||||||
|
|
||||||
|
if content:
|
||||||
|
if content_format == "html":
|
||||||
|
page["html"] = content
|
||||||
|
elif content_format == "lexical":
|
||||||
|
page["lexical"] = content
|
||||||
|
|
||||||
|
if slug:
|
||||||
|
page["slug"] = slug
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="POST",
|
||||||
|
endpoint="pages/",
|
||||||
|
api_type="admin",
|
||||||
|
json_data=page_data,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
247
src/ghost_mcp/tools/admin/posts.py
Normal file
247
src/ghost_mcp/tools/admin/posts.py
Normal file
|
|
@ -0,0 +1,247 @@
|
||||||
|
"""Admin API tools for posts management."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_id_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_post_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register post management Admin API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def create_post(
|
||||||
|
title: str,
|
||||||
|
content: Optional[str] = None,
|
||||||
|
content_format: str = "lexical",
|
||||||
|
status: str = "draft",
|
||||||
|
slug: Optional[str] = None,
|
||||||
|
excerpt: Optional[str] = None,
|
||||||
|
featured: bool = False,
|
||||||
|
tags: Optional[str] = None,
|
||||||
|
authors: Optional[str] = None,
|
||||||
|
published_at: Optional[str] = None,
|
||||||
|
meta_title: Optional[str] = None,
|
||||||
|
meta_description: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Create a new post via Ghost Admin API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
title: Post title (required)
|
||||||
|
content: Post content (HTML or Lexical JSON)
|
||||||
|
content_format: Content format ('html', 'lexical', default: 'lexical')
|
||||||
|
status: Post status ('draft', 'published', 'scheduled', default: 'draft')
|
||||||
|
slug: Post slug (auto-generated if not provided)
|
||||||
|
excerpt: Custom excerpt
|
||||||
|
featured: Whether post is featured (default: False)
|
||||||
|
tags: Comma-separated tag names or IDs
|
||||||
|
authors: Comma-separated author names or IDs
|
||||||
|
published_at: Publish date (ISO format, for scheduled posts)
|
||||||
|
meta_title: SEO meta title
|
||||||
|
meta_description: SEO meta description
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing created post data
|
||||||
|
"""
|
||||||
|
if not title or not title.strip():
|
||||||
|
return json.dumps({"error": "Title is required"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Build post data
|
||||||
|
post_data: Dict[str, Any] = {
|
||||||
|
"posts": [{
|
||||||
|
"title": title.strip(),
|
||||||
|
"status": status,
|
||||||
|
"featured": featured,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
post = post_data["posts"][0]
|
||||||
|
|
||||||
|
# Add content in appropriate format
|
||||||
|
if content:
|
||||||
|
if content_format == "html":
|
||||||
|
post["html"] = content
|
||||||
|
elif content_format == "lexical":
|
||||||
|
# Assume content is already Lexical JSON string
|
||||||
|
post["lexical"] = content
|
||||||
|
else:
|
||||||
|
return json.dumps({"error": "Content format must be 'html' or 'lexical'"})
|
||||||
|
|
||||||
|
# Add optional fields
|
||||||
|
if slug:
|
||||||
|
post["slug"] = slug
|
||||||
|
if excerpt:
|
||||||
|
post["custom_excerpt"] = excerpt
|
||||||
|
if published_at:
|
||||||
|
post["published_at"] = published_at
|
||||||
|
if meta_title:
|
||||||
|
post["meta_title"] = meta_title
|
||||||
|
if meta_description:
|
||||||
|
post["meta_description"] = meta_description
|
||||||
|
|
||||||
|
# Handle tags (simplified - in real implementation would resolve tag names to IDs)
|
||||||
|
if tags:
|
||||||
|
tag_list = [{"name": tag.strip()} for tag in tags.split(",") if tag.strip()]
|
||||||
|
if tag_list:
|
||||||
|
post["tags"] = tag_list
|
||||||
|
|
||||||
|
# Handle authors (simplified)
|
||||||
|
if authors:
|
||||||
|
author_list = [{"name": author.strip()} for author in authors.split(",") if author.strip()]
|
||||||
|
if author_list:
|
||||||
|
post["authors"] = author_list
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.create_post(post_data)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def update_post(
|
||||||
|
post_id: str,
|
||||||
|
title: Optional[str] = None,
|
||||||
|
content: Optional[str] = None,
|
||||||
|
content_format: str = "lexical",
|
||||||
|
status: Optional[str] = None,
|
||||||
|
slug: Optional[str] = None,
|
||||||
|
excerpt: Optional[str] = None,
|
||||||
|
featured: Optional[bool] = None,
|
||||||
|
published_at: Optional[str] = None,
|
||||||
|
meta_title: Optional[str] = None,
|
||||||
|
meta_description: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Update an existing post via Ghost Admin API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
post_id: Post ID to update (required)
|
||||||
|
title: New post title
|
||||||
|
content: New post content (HTML or Lexical JSON)
|
||||||
|
content_format: Content format ('html', 'lexical', default: 'lexical')
|
||||||
|
status: New post status ('draft', 'published', 'scheduled')
|
||||||
|
slug: New post slug
|
||||||
|
excerpt: New custom excerpt
|
||||||
|
featured: Whether post is featured
|
||||||
|
published_at: New publish date (ISO format)
|
||||||
|
meta_title: New SEO meta title
|
||||||
|
meta_description: New SEO meta description
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing updated post data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
post_id = validate_id_parameter(post_id, "post_id")
|
||||||
|
|
||||||
|
# Build update data with only provided fields
|
||||||
|
post_data: Dict[str, Any] = {"posts": [{}]}
|
||||||
|
post = post_data["posts"][0]
|
||||||
|
|
||||||
|
if title is not None:
|
||||||
|
post["title"] = title.strip()
|
||||||
|
if status is not None:
|
||||||
|
post["status"] = status
|
||||||
|
if slug is not None:
|
||||||
|
post["slug"] = slug
|
||||||
|
if excerpt is not None:
|
||||||
|
post["custom_excerpt"] = excerpt
|
||||||
|
if featured is not None:
|
||||||
|
post["featured"] = featured
|
||||||
|
if published_at is not None:
|
||||||
|
post["published_at"] = published_at
|
||||||
|
if meta_title is not None:
|
||||||
|
post["meta_title"] = meta_title
|
||||||
|
if meta_description is not None:
|
||||||
|
post["meta_description"] = meta_description
|
||||||
|
|
||||||
|
# Add content in appropriate format
|
||||||
|
if content is not None:
|
||||||
|
if content_format == "html":
|
||||||
|
post["html"] = content
|
||||||
|
elif content_format == "lexical":
|
||||||
|
post["lexical"] = content
|
||||||
|
else:
|
||||||
|
return json.dumps({"error": "Content format must be 'html' or 'lexical'"})
|
||||||
|
|
||||||
|
# Must have at least one field to update
|
||||||
|
if not post:
|
||||||
|
return json.dumps({"error": "At least one field must be provided for update"})
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.update_post(post_id, post_data)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def delete_post(post_id: str) -> str:
|
||||||
|
"""
|
||||||
|
Delete a post via Ghost Admin API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
post_id: Post ID to delete (required)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing deletion confirmation
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
post_id = validate_id_parameter(post_id, "post_id")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.delete_post(post_id)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_admin_posts(
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get posts from Ghost Admin API (includes drafts and all statuses).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: Number of posts to return (1-50, default: 15)
|
||||||
|
page: Page number for pagination (default: 1)
|
||||||
|
filter: Ghost filter syntax for filtering posts
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
order: Order of posts (published_at desc, etc.)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing posts data with metadata
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="posts/",
|
||||||
|
api_type="admin",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"limit": limit,
|
||||||
|
"page": page,
|
||||||
|
"filter": filter,
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
"order": order,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
9
src/ghost_mcp/tools/admin/settings.py
Normal file
9
src/ghost_mcp/tools/admin/settings.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
"""Admin API tools for settings management."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_settings_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register settings management Admin API tools."""
|
||||||
|
# Settings management tools would go here
|
||||||
|
pass
|
||||||
38
src/ghost_mcp/tools/admin/tags.py
Normal file
38
src/ghost_mcp/tools/admin/tags.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
"""Admin API tools for tags management."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
|
||||||
|
|
||||||
|
def register_admin_tag_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register tag management Admin API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def create_tag(name: str, description: str = "") -> str:
|
||||||
|
"""Create a new tag via Ghost Admin API."""
|
||||||
|
if not name or not name.strip():
|
||||||
|
return json.dumps({"error": "Tag name is required"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
tag_data: Dict[str, Any] = {
|
||||||
|
"tags": [{
|
||||||
|
"name": name.strip(),
|
||||||
|
"description": description,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="POST",
|
||||||
|
endpoint="tags/",
|
||||||
|
api_type="admin",
|
||||||
|
json_data=tag_data,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
19
src/ghost_mcp/tools/content/__init__.py
Normal file
19
src/ghost_mcp/tools/content/__init__.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
"""Content API tools for read-only access to Ghost content."""
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from .posts import register_post_tools
|
||||||
|
from .pages import register_page_tools
|
||||||
|
from .tags import register_tag_tools
|
||||||
|
from .authors import register_author_tools
|
||||||
|
from .settings import register_settings_tools
|
||||||
|
|
||||||
|
def register_content_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register all Content API tools."""
|
||||||
|
register_post_tools(mcp)
|
||||||
|
register_page_tools(mcp)
|
||||||
|
register_tag_tools(mcp)
|
||||||
|
register_author_tools(mcp)
|
||||||
|
register_settings_tools(mcp)
|
||||||
|
|
||||||
|
__all__ = ["register_content_tools"]
|
||||||
142
src/ghost_mcp/tools/content/authors.py
Normal file
142
src/ghost_mcp/tools/content/authors.py
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
"""Content API tools for authors."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_filter_syntax, validate_id_parameter, validate_slug_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_author_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register author-related Content API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_authors(
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get authors from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: Number of authors to return (1-50, default: 15)
|
||||||
|
page: Page number for pagination (default: 1)
|
||||||
|
filter: Ghost filter syntax for filtering authors
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
order: Order of authors (name asc, count.posts desc, etc.)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing authors data with metadata
|
||||||
|
"""
|
||||||
|
# Validate parameters
|
||||||
|
if limit is not None and (limit < 1 or limit > 50):
|
||||||
|
return json.dumps({"error": "Limit must be between 1 and 50"})
|
||||||
|
|
||||||
|
if page is not None and page < 1:
|
||||||
|
return json.dumps({"error": "Page must be 1 or greater"})
|
||||||
|
|
||||||
|
if filter and not validate_filter_syntax(filter):
|
||||||
|
return json.dumps({"error": "Invalid filter syntax"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="authors/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"limit": limit,
|
||||||
|
"page": page,
|
||||||
|
"filter": filter,
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
"order": order,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_author_by_id(
|
||||||
|
author_id: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single author by ID from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
author_id: The author ID
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing author data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
author_id = validate_id_parameter(author_id, "author_id")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"authors/{author_id}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_author_by_slug(
|
||||||
|
slug: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single author by slug from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
slug: The author slug
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing author data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
slug = validate_slug_parameter(slug)
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"authors/slug/{slug}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
142
src/ghost_mcp/tools/content/pages.py
Normal file
142
src/ghost_mcp/tools/content/pages.py
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
"""Content API tools for pages."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_filter_syntax, validate_id_parameter, validate_slug_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_page_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register page-related Content API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_pages(
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get published pages from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: Number of pages to return (1-50, default: 15)
|
||||||
|
page: Page number for pagination (default: 1)
|
||||||
|
filter: Ghost filter syntax for filtering pages
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
order: Order of pages (published_at desc, etc.)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing pages data with metadata
|
||||||
|
"""
|
||||||
|
# Validate parameters
|
||||||
|
if limit is not None and (limit < 1 or limit > 50):
|
||||||
|
return json.dumps({"error": "Limit must be between 1 and 50"})
|
||||||
|
|
||||||
|
if page is not None and page < 1:
|
||||||
|
return json.dumps({"error": "Page must be 1 or greater"})
|
||||||
|
|
||||||
|
if filter and not validate_filter_syntax(filter):
|
||||||
|
return json.dumps({"error": "Invalid filter syntax"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="pages/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"limit": limit,
|
||||||
|
"page": page,
|
||||||
|
"filter": filter,
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
"order": order,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_page_by_id(
|
||||||
|
page_id: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single page by ID from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page_id: The page ID
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing page data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
page_id = validate_id_parameter(page_id, "page_id")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"pages/{page_id}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_page_by_slug(
|
||||||
|
slug: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single page by slug from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
slug: The page slug
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing page data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
slug = validate_slug_parameter(slug)
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"pages/slug/{slug}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
161
src/ghost_mcp/tools/content/posts.py
Normal file
161
src/ghost_mcp/tools/content/posts.py
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
"""Content API tools for posts."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_filter_syntax, validate_id_parameter, validate_slug_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_post_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register post-related Content API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_posts(
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get published posts from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: Number of posts to return (1-50, default: 15)
|
||||||
|
page: Page number for pagination (default: 1)
|
||||||
|
filter: Ghost filter syntax for filtering posts
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
order: Order of posts (published_at desc, etc.)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing posts data with metadata
|
||||||
|
"""
|
||||||
|
# Validate parameters
|
||||||
|
if limit is not None and (limit < 1 or limit > 50):
|
||||||
|
return json.dumps({"error": "Limit must be between 1 and 50"})
|
||||||
|
|
||||||
|
if page is not None and page < 1:
|
||||||
|
return json.dumps({"error": "Page must be 1 or greater"})
|
||||||
|
|
||||||
|
if filter and not validate_filter_syntax(filter):
|
||||||
|
return json.dumps({"error": "Invalid filter syntax"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.get_posts(
|
||||||
|
limit=limit,
|
||||||
|
page=page,
|
||||||
|
filter=filter,
|
||||||
|
include=include,
|
||||||
|
fields=fields,
|
||||||
|
order=order,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_post_by_id(
|
||||||
|
post_id: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single post by ID from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
post_id: The post ID
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing post data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
post_id = validate_id_parameter(post_id, "post_id")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.get_post_by_id(
|
||||||
|
post_id=post_id,
|
||||||
|
include=include,
|
||||||
|
fields=fields,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_post_by_slug(
|
||||||
|
slug: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single post by slug from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
slug: The post slug
|
||||||
|
include: Comma-separated list of fields to include (tags, authors, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing post data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
slug = validate_slug_parameter(slug)
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.get_post_by_slug(
|
||||||
|
slug=slug,
|
||||||
|
include=include,
|
||||||
|
fields=fields,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def search_posts(
|
||||||
|
query: str,
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Search posts by title and content.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
query: Search query string
|
||||||
|
limit: Number of results to return (1-50, default: 15)
|
||||||
|
include: Comma-separated list of fields to include
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing matching posts
|
||||||
|
"""
|
||||||
|
if not query or not query.strip():
|
||||||
|
return json.dumps({"error": "Query parameter is required"})
|
||||||
|
|
||||||
|
if limit is not None and (limit < 1 or limit > 50):
|
||||||
|
return json.dumps({"error": "Limit must be between 1 and 50"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Use Ghost's filter syntax for searching
|
||||||
|
search_filter = f"title:~'{query}',plaintext:~'{query}'"
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client.get_posts(
|
||||||
|
limit=limit,
|
||||||
|
filter=search_filter,
|
||||||
|
include=include,
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
70
src/ghost_mcp/tools/content/settings.py
Normal file
70
src/ghost_mcp/tools/content/settings.py
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
"""Content API tools for settings."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
|
||||||
|
|
||||||
|
def register_settings_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register settings-related Content API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_settings() -> str:
|
||||||
|
"""
|
||||||
|
Get public settings from Ghost Content API.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing public settings data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="settings/",
|
||||||
|
api_type="content",
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_site_info() -> str:
|
||||||
|
"""
|
||||||
|
Get basic site information from Ghost.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing site title, description, URL, and other public info
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="settings/",
|
||||||
|
api_type="content",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Extract key site information
|
||||||
|
if "settings" in result:
|
||||||
|
settings = result["settings"]
|
||||||
|
site_info = {
|
||||||
|
"title": settings.get("title"),
|
||||||
|
"description": settings.get("description"),
|
||||||
|
"url": settings.get("url"),
|
||||||
|
"logo": settings.get("logo"),
|
||||||
|
"icon": settings.get("icon"),
|
||||||
|
"cover_image": settings.get("cover_image"),
|
||||||
|
"accent_color": settings.get("accent_color"),
|
||||||
|
"timezone": settings.get("timezone"),
|
||||||
|
"lang": settings.get("lang"),
|
||||||
|
"version": settings.get("version"),
|
||||||
|
}
|
||||||
|
return json.dumps({"site_info": site_info}, indent=2, default=str)
|
||||||
|
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
142
src/ghost_mcp/tools/content/tags.py
Normal file
142
src/ghost_mcp/tools/content/tags.py
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
"""Content API tools for tags."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
from ...client import GhostClient
|
||||||
|
from ...utils.validation import validate_filter_syntax, validate_id_parameter, validate_slug_parameter
|
||||||
|
|
||||||
|
|
||||||
|
def register_tag_tools(mcp: FastMCP) -> None:
|
||||||
|
"""Register tag-related Content API tools."""
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_tags(
|
||||||
|
limit: Optional[int] = None,
|
||||||
|
page: Optional[int] = None,
|
||||||
|
filter: Optional[str] = None,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
order: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get tags from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
limit: Number of tags to return (1-50, default: 15)
|
||||||
|
page: Page number for pagination (default: 1)
|
||||||
|
filter: Ghost filter syntax for filtering tags
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
order: Order of tags (name asc, count.posts desc, etc.)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing tags data with metadata
|
||||||
|
"""
|
||||||
|
# Validate parameters
|
||||||
|
if limit is not None and (limit < 1 or limit > 50):
|
||||||
|
return json.dumps({"error": "Limit must be between 1 and 50"})
|
||||||
|
|
||||||
|
if page is not None and page < 1:
|
||||||
|
return json.dumps({"error": "Page must be 1 or greater"})
|
||||||
|
|
||||||
|
if filter and not validate_filter_syntax(filter):
|
||||||
|
return json.dumps({"error": "Invalid filter syntax"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint="tags/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"limit": limit,
|
||||||
|
"page": page,
|
||||||
|
"filter": filter,
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
"order": order,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_tag_by_id(
|
||||||
|
tag_id: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single tag by ID from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tag_id: The tag ID
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing tag data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
tag_id = validate_id_parameter(tag_id, "tag_id")
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"tags/{tag_id}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
async def get_tag_by_slug(
|
||||||
|
slug: str,
|
||||||
|
include: Optional[str] = None,
|
||||||
|
fields: Optional[str] = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get a single tag by slug from Ghost Content API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
slug: The tag slug
|
||||||
|
include: Comma-separated list of fields to include (count.posts, etc.)
|
||||||
|
fields: Comma-separated list of fields to return
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string containing tag data
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
slug = validate_slug_parameter(slug)
|
||||||
|
|
||||||
|
async with GhostClient() as client:
|
||||||
|
result = await client._make_request(
|
||||||
|
method="GET",
|
||||||
|
endpoint=f"tags/slug/{slug}/",
|
||||||
|
api_type="content",
|
||||||
|
params={
|
||||||
|
k: v for k, v in {
|
||||||
|
"include": include,
|
||||||
|
"fields": fields,
|
||||||
|
}.items() if v is not None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return json.dumps(result, indent=2, default=str)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
30
src/ghost_mcp/types/__init__.py
Normal file
30
src/ghost_mcp/types/__init__.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
"""Type definitions for Ghost MCP server."""
|
||||||
|
|
||||||
|
from .errors import *
|
||||||
|
from .ghost import *
|
||||||
|
from .mcp import *
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
# Error types
|
||||||
|
"GhostMCPError",
|
||||||
|
"NetworkError",
|
||||||
|
"AuthenticationError",
|
||||||
|
"GhostApiError",
|
||||||
|
"ValidationError",
|
||||||
|
"ErrorCategory",
|
||||||
|
# Ghost types
|
||||||
|
"GhostPost",
|
||||||
|
"GhostPage",
|
||||||
|
"GhostTag",
|
||||||
|
"GhostAuthor",
|
||||||
|
"GhostMember",
|
||||||
|
"GhostSettings",
|
||||||
|
"GhostApiResponse",
|
||||||
|
"GhostErrorResponse",
|
||||||
|
"ContentFormat",
|
||||||
|
# MCP types
|
||||||
|
"MCPToolDefinition",
|
||||||
|
"MCPToolResponse",
|
||||||
|
"MCPToolRequest",
|
||||||
|
"MCPError",
|
||||||
|
]
|
||||||
128
src/ghost_mcp/types/errors.py
Normal file
128
src/ghost_mcp/types/errors.py
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
"""Error types and categories for Ghost MCP server."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorCategory(str, Enum):
|
||||||
|
"""Error categories for comprehensive error handling."""
|
||||||
|
NETWORK = "NETWORK"
|
||||||
|
AUTHENTICATION = "AUTHENTICATION"
|
||||||
|
GHOST_API = "GHOST_API"
|
||||||
|
MCP_PROTOCOL = "MCP_PROTOCOL"
|
||||||
|
FILE_UPLOAD = "FILE_UPLOAD"
|
||||||
|
VALIDATION = "VALIDATION"
|
||||||
|
|
||||||
|
|
||||||
|
class GhostMCPError(Exception):
|
||||||
|
"""Base error class for Ghost MCP server."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
category: ErrorCategory,
|
||||||
|
code: Optional[str] = None,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
self.id = str(uuid.uuid4())
|
||||||
|
self.category = category
|
||||||
|
self.code = code
|
||||||
|
self.context = context
|
||||||
|
self.request_id = request_id
|
||||||
|
|
||||||
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
|
"""Convert error to dictionary for logging."""
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"message": str(self),
|
||||||
|
"category": self.category.value,
|
||||||
|
"code": self.code,
|
||||||
|
"context": self.context,
|
||||||
|
"request_id": self.request_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class NetworkError(GhostMCPError):
|
||||||
|
"""Network-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message, ErrorCategory.NETWORK, "NETWORK_ERROR", context, request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AuthenticationError(GhostMCPError):
|
||||||
|
"""Authentication-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message, ErrorCategory.AUTHENTICATION, "AUTH_ERROR", context, request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GhostApiError(GhostMCPError):
|
||||||
|
"""Ghost API-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
code: Optional[str] = None,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(message, ErrorCategory.GHOST_API, code, context, request_id)
|
||||||
|
|
||||||
|
|
||||||
|
class ValidationError(GhostMCPError):
|
||||||
|
"""Validation-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message, ErrorCategory.VALIDATION, "VALIDATION_ERROR", context, request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FileUploadError(GhostMCPError):
|
||||||
|
"""File upload-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message, ErrorCategory.FILE_UPLOAD, "FILE_UPLOAD_ERROR", context, request_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MCPProtocolError(GhostMCPError):
|
||||||
|
"""MCP protocol-related errors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str,
|
||||||
|
context: Optional[str] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(
|
||||||
|
message, ErrorCategory.MCP_PROTOCOL, "MCP_ERROR", context, request_id
|
||||||
|
)
|
||||||
251
src/ghost_mcp/types/ghost.py
Normal file
251
src/ghost_mcp/types/ghost.py
Normal file
|
|
@ -0,0 +1,251 @@
|
||||||
|
"""Ghost API type definitions based on Ghost v5.0 API."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field, HttpUrl
|
||||||
|
|
||||||
|
|
||||||
|
class ContentFormat(str, Enum):
|
||||||
|
"""Content formats supported by Ghost."""
|
||||||
|
LEXICAL = "lexical"
|
||||||
|
HTML = "html"
|
||||||
|
MOBILEDOC = "mobiledoc"
|
||||||
|
|
||||||
|
|
||||||
|
class PostStatus(str, Enum):
|
||||||
|
"""Post status options."""
|
||||||
|
DRAFT = "draft"
|
||||||
|
PUBLISHED = "published"
|
||||||
|
SCHEDULED = "scheduled"
|
||||||
|
|
||||||
|
|
||||||
|
class VisibilityType(str, Enum):
|
||||||
|
"""Content visibility options."""
|
||||||
|
PUBLIC = "public"
|
||||||
|
MEMBERS = "members"
|
||||||
|
PAID = "paid"
|
||||||
|
TIERS = "tiers"
|
||||||
|
|
||||||
|
|
||||||
|
class GhostMeta(BaseModel):
|
||||||
|
"""Ghost API response metadata."""
|
||||||
|
pagination: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostAuthor(BaseModel):
|
||||||
|
"""Ghost author object."""
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
slug: str
|
||||||
|
email: Optional[str] = None
|
||||||
|
profile_image: Optional[HttpUrl] = None
|
||||||
|
cover_image: Optional[HttpUrl] = None
|
||||||
|
bio: Optional[str] = None
|
||||||
|
website: Optional[HttpUrl] = None
|
||||||
|
location: Optional[str] = None
|
||||||
|
facebook: Optional[str] = None
|
||||||
|
twitter: Optional[str] = None
|
||||||
|
accessibility: Optional[str] = None
|
||||||
|
status: str
|
||||||
|
meta_title: Optional[str] = None
|
||||||
|
meta_description: Optional[str] = None
|
||||||
|
tour: Optional[List[str]] = None
|
||||||
|
last_seen: Optional[datetime] = None
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
roles: Optional[List[Dict[str, Any]]] = None
|
||||||
|
url: str
|
||||||
|
|
||||||
|
|
||||||
|
class GhostTag(BaseModel):
|
||||||
|
"""Ghost tag object."""
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
slug: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
feature_image: Optional[HttpUrl] = None
|
||||||
|
visibility: VisibilityType = VisibilityType.PUBLIC
|
||||||
|
og_image: Optional[HttpUrl] = None
|
||||||
|
og_title: Optional[str] = None
|
||||||
|
og_description: Optional[str] = None
|
||||||
|
twitter_image: Optional[HttpUrl] = None
|
||||||
|
twitter_title: Optional[str] = None
|
||||||
|
twitter_description: Optional[str] = None
|
||||||
|
meta_title: Optional[str] = None
|
||||||
|
meta_description: Optional[str] = None
|
||||||
|
codeinjection_head: Optional[str] = None
|
||||||
|
codeinjection_foot: Optional[str] = None
|
||||||
|
canonical_url: Optional[HttpUrl] = None
|
||||||
|
accent_color: Optional[str] = None
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
url: str
|
||||||
|
|
||||||
|
|
||||||
|
class GhostPost(BaseModel):
|
||||||
|
"""Ghost post object."""
|
||||||
|
id: str
|
||||||
|
uuid: str
|
||||||
|
title: str
|
||||||
|
slug: str
|
||||||
|
mobiledoc: Optional[str] = None
|
||||||
|
lexical: Optional[str] = None
|
||||||
|
html: Optional[str] = None
|
||||||
|
comment_id: Optional[str] = None
|
||||||
|
plaintext: Optional[str] = None
|
||||||
|
feature_image: Optional[HttpUrl] = None
|
||||||
|
feature_image_alt: Optional[str] = None
|
||||||
|
feature_image_caption: Optional[str] = None
|
||||||
|
featured: bool = False
|
||||||
|
status: PostStatus = PostStatus.DRAFT
|
||||||
|
visibility: VisibilityType = VisibilityType.PUBLIC
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
published_at: Optional[datetime] = None
|
||||||
|
custom_excerpt: Optional[str] = None
|
||||||
|
codeinjection_head: Optional[str] = None
|
||||||
|
codeinjection_foot: Optional[str] = None
|
||||||
|
custom_template: Optional[str] = None
|
||||||
|
canonical_url: Optional[HttpUrl] = None
|
||||||
|
tags: Optional[List[GhostTag]] = None
|
||||||
|
authors: Optional[List[GhostAuthor]] = None
|
||||||
|
primary_author: Optional[GhostAuthor] = None
|
||||||
|
primary_tag: Optional[GhostTag] = None
|
||||||
|
url: str
|
||||||
|
excerpt: Optional[str] = None
|
||||||
|
reading_time: Optional[int] = None
|
||||||
|
access: Optional[bool] = None
|
||||||
|
og_image: Optional[HttpUrl] = None
|
||||||
|
og_title: Optional[str] = None
|
||||||
|
og_description: Optional[str] = None
|
||||||
|
twitter_image: Optional[HttpUrl] = None
|
||||||
|
twitter_title: Optional[str] = None
|
||||||
|
twitter_description: Optional[str] = None
|
||||||
|
meta_title: Optional[str] = None
|
||||||
|
meta_description: Optional[str] = None
|
||||||
|
email_segment: Optional[str] = None
|
||||||
|
newsletter: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostPage(BaseModel):
|
||||||
|
"""Ghost page object."""
|
||||||
|
id: str
|
||||||
|
uuid: str
|
||||||
|
title: str
|
||||||
|
slug: str
|
||||||
|
mobiledoc: Optional[str] = None
|
||||||
|
lexical: Optional[str] = None
|
||||||
|
html: Optional[str] = None
|
||||||
|
comment_id: Optional[str] = None
|
||||||
|
plaintext: Optional[str] = None
|
||||||
|
feature_image: Optional[HttpUrl] = None
|
||||||
|
feature_image_alt: Optional[str] = None
|
||||||
|
feature_image_caption: Optional[str] = None
|
||||||
|
featured: bool = False
|
||||||
|
status: PostStatus = PostStatus.DRAFT
|
||||||
|
visibility: VisibilityType = VisibilityType.PUBLIC
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
published_at: Optional[datetime] = None
|
||||||
|
custom_excerpt: Optional[str] = None
|
||||||
|
codeinjection_head: Optional[str] = None
|
||||||
|
codeinjection_foot: Optional[str] = None
|
||||||
|
custom_template: Optional[str] = None
|
||||||
|
canonical_url: Optional[HttpUrl] = None
|
||||||
|
tags: Optional[List[GhostTag]] = None
|
||||||
|
authors: Optional[List[GhostAuthor]] = None
|
||||||
|
primary_author: Optional[GhostAuthor] = None
|
||||||
|
primary_tag: Optional[GhostTag] = None
|
||||||
|
url: str
|
||||||
|
excerpt: Optional[str] = None
|
||||||
|
reading_time: Optional[int] = None
|
||||||
|
access: Optional[bool] = None
|
||||||
|
og_image: Optional[HttpUrl] = None
|
||||||
|
og_title: Optional[str] = None
|
||||||
|
og_description: Optional[str] = None
|
||||||
|
twitter_image: Optional[HttpUrl] = None
|
||||||
|
twitter_title: Optional[str] = None
|
||||||
|
twitter_description: Optional[str] = None
|
||||||
|
meta_title: Optional[str] = None
|
||||||
|
meta_description: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostMember(BaseModel):
|
||||||
|
"""Ghost member object."""
|
||||||
|
id: str
|
||||||
|
uuid: str
|
||||||
|
email: str
|
||||||
|
name: Optional[str] = None
|
||||||
|
note: Optional[str] = None
|
||||||
|
subscribed: bool = True
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
labels: Optional[List[Dict[str, Any]]] = None
|
||||||
|
avatar_image: Optional[HttpUrl] = None
|
||||||
|
comped: bool = False
|
||||||
|
email_count: int = 0
|
||||||
|
email_opened_count: int = 0
|
||||||
|
email_open_rate: Optional[float] = None
|
||||||
|
status: str = "free"
|
||||||
|
last_seen: Optional[datetime] = None
|
||||||
|
newsletters: Optional[List[Dict[str, Any]]] = None
|
||||||
|
subscriptions: Optional[List[Dict[str, Any]]] = None
|
||||||
|
products: Optional[List[Dict[str, Any]]] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostSettings(BaseModel):
|
||||||
|
"""Ghost settings object."""
|
||||||
|
title: str
|
||||||
|
description: str
|
||||||
|
logo: Optional[HttpUrl] = None
|
||||||
|
icon: Optional[HttpUrl] = None
|
||||||
|
accent_color: Optional[str] = None
|
||||||
|
cover_image: Optional[HttpUrl] = None
|
||||||
|
facebook: Optional[str] = None
|
||||||
|
twitter: Optional[str] = None
|
||||||
|
lang: str = "en"
|
||||||
|
timezone: str = "Etc/UTC"
|
||||||
|
codeinjection_head: Optional[str] = None
|
||||||
|
codeinjection_foot: Optional[str] = None
|
||||||
|
navigation: Optional[List[Dict[str, Any]]] = None
|
||||||
|
secondary_navigation: Optional[List[Dict[str, Any]]] = None
|
||||||
|
meta_title: Optional[str] = None
|
||||||
|
meta_description: Optional[str] = None
|
||||||
|
og_image: Optional[HttpUrl] = None
|
||||||
|
og_title: Optional[str] = None
|
||||||
|
og_description: Optional[str] = None
|
||||||
|
twitter_image: Optional[HttpUrl] = None
|
||||||
|
twitter_title: Optional[str] = None
|
||||||
|
twitter_description: Optional[str] = None
|
||||||
|
url: str
|
||||||
|
|
||||||
|
|
||||||
|
class GhostApiResponse(BaseModel):
|
||||||
|
"""Ghost API response wrapper."""
|
||||||
|
posts: Optional[List[GhostPost]] = None
|
||||||
|
pages: Optional[List[GhostPage]] = None
|
||||||
|
tags: Optional[List[GhostTag]] = None
|
||||||
|
authors: Optional[List[GhostAuthor]] = None
|
||||||
|
members: Optional[List[GhostMember]] = None
|
||||||
|
settings: Optional[GhostSettings] = None
|
||||||
|
meta: Optional[GhostMeta] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostError(BaseModel):
|
||||||
|
"""Ghost API error object."""
|
||||||
|
id: str
|
||||||
|
message: str
|
||||||
|
context: Optional[str] = None
|
||||||
|
type: str
|
||||||
|
details: Optional[str] = None
|
||||||
|
property: Optional[str] = None
|
||||||
|
help: Optional[str] = None
|
||||||
|
code: Optional[str] = None
|
||||||
|
ghostErrorCode: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class GhostErrorResponse(BaseModel):
|
||||||
|
"""Ghost API error response."""
|
||||||
|
errors: List[GhostError]
|
||||||
49
src/ghost_mcp/types/mcp.py
Normal file
49
src/ghost_mcp/types/mcp.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
"""MCP-specific type definitions."""
|
||||||
|
|
||||||
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class MCPToolParameter(BaseModel):
|
||||||
|
"""MCP tool parameter definition."""
|
||||||
|
type: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
required: bool = False
|
||||||
|
enum: Optional[List[str]] = None
|
||||||
|
properties: Optional[Dict[str, "MCPToolParameter"]] = None
|
||||||
|
|
||||||
|
|
||||||
|
class MCPToolDefinition(BaseModel):
|
||||||
|
"""MCP tool definition."""
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
inputSchema: Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
|
class MCPContent(BaseModel):
|
||||||
|
"""MCP response content."""
|
||||||
|
type: str
|
||||||
|
text: Optional[str] = None
|
||||||
|
data: Optional[str] = None
|
||||||
|
url: Optional[str] = None
|
||||||
|
mimeType: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class MCPToolResponse(BaseModel):
|
||||||
|
"""MCP tool response."""
|
||||||
|
content: List[MCPContent]
|
||||||
|
isError: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class MCPToolRequest(BaseModel):
|
||||||
|
"""MCP tool request."""
|
||||||
|
name: str
|
||||||
|
arguments: Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
|
class MCPError(BaseModel):
|
||||||
|
"""MCP error response."""
|
||||||
|
code: int
|
||||||
|
message: str
|
||||||
|
data: Optional[Any] = None
|
||||||
14
src/ghost_mcp/utils/__init__.py
Normal file
14
src/ghost_mcp/utils/__init__.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
"""Utility modules for Ghost MCP server."""
|
||||||
|
|
||||||
|
from .logging import setup_logging, get_logger
|
||||||
|
from .retry import with_retry, RetryConfig
|
||||||
|
from .validation import validate_filter_syntax, validate_pagination_params
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"setup_logging",
|
||||||
|
"get_logger",
|
||||||
|
"with_retry",
|
||||||
|
"RetryConfig",
|
||||||
|
"validate_filter_syntax",
|
||||||
|
"validate_pagination_params",
|
||||||
|
]
|
||||||
60
src/ghost_mcp/utils/logging.py
Normal file
60
src/ghost_mcp/utils/logging.py
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
"""Logging utilities for Ghost MCP server."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
import structlog
|
||||||
|
from structlog.typing import Processor
|
||||||
|
|
||||||
|
from ..config import config
|
||||||
|
|
||||||
|
|
||||||
|
def add_request_id(logger: Any, method_name: str, event_dict: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Add request ID to log events."""
|
||||||
|
if "request_id" not in event_dict:
|
||||||
|
event_dict["request_id"] = str(uuid.uuid4())
|
||||||
|
return event_dict
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging() -> None:
|
||||||
|
"""Set up structured logging for the application."""
|
||||||
|
processors: list[Processor] = [
|
||||||
|
structlog.contextvars.merge_contextvars,
|
||||||
|
structlog.processors.add_log_level,
|
||||||
|
structlog.processors.TimeStamper(fmt="iso"),
|
||||||
|
]
|
||||||
|
|
||||||
|
if config.logging.request_id:
|
||||||
|
processors.append(add_request_id)
|
||||||
|
|
||||||
|
if config.logging.structured:
|
||||||
|
processors.extend([
|
||||||
|
structlog.processors.JSONRenderer()
|
||||||
|
])
|
||||||
|
else:
|
||||||
|
processors.extend([
|
||||||
|
structlog.dev.ConsoleRenderer()
|
||||||
|
])
|
||||||
|
|
||||||
|
structlog.configure(
|
||||||
|
processors=processors,
|
||||||
|
wrapper_class=structlog.make_filtering_bound_logger(
|
||||||
|
getattr(logging, config.logging.level.upper())
|
||||||
|
),
|
||||||
|
logger_factory=structlog.PrintLoggerFactory(),
|
||||||
|
cache_logger_on_first_use=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Configure standard library logging
|
||||||
|
logging.basicConfig(
|
||||||
|
format="%(message)s",
|
||||||
|
stream=sys.stdout,
|
||||||
|
level=getattr(logging, config.logging.level.upper()),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_logger(name: Optional[str] = None) -> structlog.BoundLogger:
|
||||||
|
"""Get a logger instance."""
|
||||||
|
return structlog.get_logger(name)
|
||||||
77
src/ghost_mcp/utils/retry.py
Normal file
77
src/ghost_mcp/utils/retry.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
"""Retry utilities with exponential backoff."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
from typing import Any, Awaitable, Callable, Optional, TypeVar
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from ..types.errors import NetworkError
|
||||||
|
from .logging import get_logger
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RetryConfig(BaseModel):
|
||||||
|
"""Configuration for retry behavior."""
|
||||||
|
max_retries: int = 3
|
||||||
|
base_delay: float = 1.0
|
||||||
|
max_delay: float = 10.0
|
||||||
|
exponential_base: float = 2.0
|
||||||
|
jitter: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
async def with_retry(
|
||||||
|
operation: Callable[[], Awaitable[T]],
|
||||||
|
config: Optional[RetryConfig] = None,
|
||||||
|
request_id: Optional[str] = None,
|
||||||
|
) -> T:
|
||||||
|
"""Execute operation with exponential backoff retry logic."""
|
||||||
|
if config is None:
|
||||||
|
config = RetryConfig()
|
||||||
|
|
||||||
|
last_exception: Optional[Exception] = None
|
||||||
|
|
||||||
|
for attempt in range(config.max_retries + 1):
|
||||||
|
try:
|
||||||
|
return await operation()
|
||||||
|
except Exception as e:
|
||||||
|
last_exception = e
|
||||||
|
|
||||||
|
if attempt == config.max_retries:
|
||||||
|
logger.error(
|
||||||
|
"Operation failed after all retries",
|
||||||
|
attempt=attempt,
|
||||||
|
max_retries=config.max_retries,
|
||||||
|
error=str(e),
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
break
|
||||||
|
|
||||||
|
# Calculate delay with exponential backoff
|
||||||
|
delay = min(
|
||||||
|
config.base_delay * (config.exponential_base ** attempt),
|
||||||
|
config.max_delay
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add jitter to prevent thundering herd
|
||||||
|
if config.jitter:
|
||||||
|
import random
|
||||||
|
delay = delay * (0.5 + random.random() * 0.5)
|
||||||
|
|
||||||
|
logger.warning(
|
||||||
|
"Operation failed, retrying",
|
||||||
|
attempt=attempt,
|
||||||
|
delay=delay,
|
||||||
|
error=str(e),
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
await asyncio.sleep(delay)
|
||||||
|
|
||||||
|
# Re-raise the last exception
|
||||||
|
if last_exception:
|
||||||
|
raise last_exception
|
||||||
|
|
||||||
|
raise NetworkError("Retry logic failed unexpectedly", request_id=request_id)
|
||||||
93
src/ghost_mcp/utils/validation.py
Normal file
93
src/ghost_mcp/utils/validation.py
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
"""Parameter validation utilities."""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field, validator
|
||||||
|
|
||||||
|
from ..types.errors import ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
class PaginationParams(BaseModel):
|
||||||
|
"""Pagination parameters validation."""
|
||||||
|
limit: Optional[int] = Field(None, ge=1, le=50)
|
||||||
|
page: Optional[int] = Field(None, ge=1)
|
||||||
|
|
||||||
|
|
||||||
|
class FilterParams(BaseModel):
|
||||||
|
"""Filter parameters validation."""
|
||||||
|
filter: Optional[str] = None
|
||||||
|
include: Optional[str] = None
|
||||||
|
fields: Optional[str] = None
|
||||||
|
order: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
def validate_pagination_params(params: Dict[str, Any]) -> PaginationParams:
|
||||||
|
"""Validate pagination parameters."""
|
||||||
|
try:
|
||||||
|
return PaginationParams(**params)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValidationError(f"Invalid pagination parameters: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_filter_syntax(filter_string: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validate Ghost filter syntax (NQL - Node Query Language).
|
||||||
|
|
||||||
|
Based on contracts/ghost-filtering.md documentation.
|
||||||
|
"""
|
||||||
|
if not filter_string:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Basic validation patterns for common NQL operators
|
||||||
|
valid_operators = [
|
||||||
|
r"\+", # AND
|
||||||
|
r",", # OR
|
||||||
|
r":", # EQUALS
|
||||||
|
r":-", # NOT EQUALS
|
||||||
|
r":~", # CONTAINS
|
||||||
|
r":-~", # NOT CONTAINS
|
||||||
|
r":>", # GREATER THAN
|
||||||
|
r":<", # LESS THAN
|
||||||
|
r":>=", # GREATER THAN OR EQUAL
|
||||||
|
r":<=", # LESS THAN OR EQUAL
|
||||||
|
]
|
||||||
|
|
||||||
|
# Check for balanced parentheses
|
||||||
|
if filter_string.count('(') != filter_string.count(')'):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Check for balanced square brackets
|
||||||
|
if filter_string.count('[') != filter_string.count(']'):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# More comprehensive validation would go here
|
||||||
|
# For now, we accept most strings as valid
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def validate_id_parameter(id_value: str, parameter_name: str = "id") -> str:
|
||||||
|
"""Validate ID parameter format."""
|
||||||
|
if not id_value or not isinstance(id_value, str):
|
||||||
|
raise ValidationError(f"Invalid {parameter_name}: must be a non-empty string")
|
||||||
|
|
||||||
|
if len(id_value.strip()) == 0:
|
||||||
|
raise ValidationError(f"Invalid {parameter_name}: cannot be empty or whitespace")
|
||||||
|
|
||||||
|
return id_value.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def validate_slug_parameter(slug: str) -> str:
|
||||||
|
"""Validate slug parameter format."""
|
||||||
|
if not slug or not isinstance(slug, str):
|
||||||
|
raise ValidationError("Invalid slug: must be a non-empty string")
|
||||||
|
|
||||||
|
slug = slug.strip()
|
||||||
|
if not slug:
|
||||||
|
raise ValidationError("Invalid slug: cannot be empty or whitespace")
|
||||||
|
|
||||||
|
# Basic slug validation (alphanumeric, hyphens, underscores)
|
||||||
|
if not re.match(r'^[a-zA-Z0-9-_]+$', slug):
|
||||||
|
raise ValidationError("Invalid slug: must contain only alphanumeric characters, hyphens, and underscores")
|
||||||
|
|
||||||
|
return slug
|
||||||
144
verify-setup.sh
Executable file
144
verify-setup.sh
Executable file
|
|
@ -0,0 +1,144 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Ghost MCP Setup Verification Script
|
||||||
|
# This script verifies that the Ghost Docker setup is working correctly
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "🔍 Ghost MCP Setup Verification"
|
||||||
|
echo "================================"
|
||||||
|
|
||||||
|
# Colors for output
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# Function to print status
|
||||||
|
print_status() {
|
||||||
|
if [ $1 -eq 0 ]; then
|
||||||
|
echo -e "${GREEN}✅ $2${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${RED}❌ $2${NC}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
print_warning() {
|
||||||
|
echo -e "${YELLOW}⚠️ $1${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_info() {
|
||||||
|
echo -e "ℹ️ $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if Docker Compose is available
|
||||||
|
echo "1. Checking Docker Compose..."
|
||||||
|
if command -v docker &> /dev/null && docker compose version &> /dev/null; then
|
||||||
|
print_status 0 "Docker Compose is available"
|
||||||
|
else
|
||||||
|
print_status 1 "Docker Compose is not available"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if containers are running
|
||||||
|
echo -e "\n2. Checking container status..."
|
||||||
|
if docker compose ps | grep -q "ghost-mcp-dev.*Up"; then
|
||||||
|
print_status 0 "Ghost container is running"
|
||||||
|
else
|
||||||
|
print_status 1 "Ghost container is not running"
|
||||||
|
echo " Run: docker compose up -d"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if docker compose ps | grep -q "ghost-db-dev.*Up.*healthy"; then
|
||||||
|
print_status 0 "MySQL database is running and healthy"
|
||||||
|
else
|
||||||
|
print_status 1 "MySQL database is not running or unhealthy"
|
||||||
|
echo " Check logs: docker compose logs ghost-db"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if Ghost is responding
|
||||||
|
echo -e "\n3. Checking Ghost accessibility..."
|
||||||
|
|
||||||
|
# Test main site
|
||||||
|
if curl -s -f http://localhost:2368/ > /dev/null; then
|
||||||
|
print_status 0 "Ghost main site is accessible (http://localhost:2368)"
|
||||||
|
else
|
||||||
|
print_status 1 "Ghost main site is not accessible"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test admin interface
|
||||||
|
if curl -s -f http://localhost:2368/ghost/ > /dev/null; then
|
||||||
|
print_status 0 "Ghost admin interface is accessible (http://localhost:2368/ghost)"
|
||||||
|
else
|
||||||
|
print_status 1 "Ghost admin interface is not accessible"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test Content API (should return auth error, which means it's working)
|
||||||
|
content_api_response=$(curl -s http://localhost:2368/ghost/api/content/settings/ 2>/dev/null || echo "")
|
||||||
|
if echo "$content_api_response" | grep -q "Authorization failed"; then
|
||||||
|
print_status 0 "Ghost Content API is responding (authentication required)"
|
||||||
|
elif echo "$content_api_response" | grep -q "posts\|settings"; then
|
||||||
|
print_status 0 "Ghost Content API is responding (no auth required - dev mode?)"
|
||||||
|
else
|
||||||
|
print_status 1 "Ghost Content API is not responding correctly"
|
||||||
|
echo " Response: $content_api_response"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check volumes
|
||||||
|
echo -e "\n4. Checking data persistence..."
|
||||||
|
if docker volume ls | grep -q "ghost_mcp_content"; then
|
||||||
|
print_status 0 "Ghost content volume exists"
|
||||||
|
else
|
||||||
|
print_status 1 "Ghost content volume missing"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if docker volume ls | grep -q "ghost_mcp_mysql"; then
|
||||||
|
print_status 0 "MySQL data volume exists"
|
||||||
|
else
|
||||||
|
print_status 1 "MySQL data volume missing"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if setup is completed
|
||||||
|
echo -e "\n5. Checking Ghost setup status..."
|
||||||
|
setup_response=$(curl -s http://localhost:2368/ghost/api/admin/site/ 2>/dev/null || echo "")
|
||||||
|
if echo "$setup_response" | grep -q '"setup":false'; then
|
||||||
|
print_warning "Ghost setup is not completed yet"
|
||||||
|
print_info "Visit http://localhost:2368/ghost to complete the initial setup"
|
||||||
|
print_info "After setup, create a custom integration to get API keys"
|
||||||
|
elif echo "$setup_response" | grep -q '"setup":true'; then
|
||||||
|
print_status 0 "Ghost setup appears to be completed"
|
||||||
|
print_info "Visit http://localhost:2368/ghost to access admin panel"
|
||||||
|
print_info "Go to Settings > Integrations to create API keys"
|
||||||
|
else
|
||||||
|
print_warning "Could not determine Ghost setup status"
|
||||||
|
print_info "Visit http://localhost:2368/ghost to check setup"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\n🎉 Setup Verification Complete!"
|
||||||
|
echo "================================"
|
||||||
|
echo ""
|
||||||
|
echo "Next steps:"
|
||||||
|
echo "1. Visit http://localhost:2368/ghost to complete Ghost setup (if not done)"
|
||||||
|
echo "2. Create a custom integration to get API keys:"
|
||||||
|
echo " - Go to Settings → Integrations → Add custom integration"
|
||||||
|
echo " - Copy both Content API Key and Admin API Key"
|
||||||
|
echo "3. Test the APIs using the keys"
|
||||||
|
echo ""
|
||||||
|
echo "Useful commands:"
|
||||||
|
echo "• View logs: docker compose logs -f ghost"
|
||||||
|
echo "• Stop services: docker compose down"
|
||||||
|
echo "• Restart services: docker compose restart"
|
||||||
|
echo "• Reset everything: docker compose down -v"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Show running containers summary
|
||||||
|
echo "Currently running containers:"
|
||||||
|
docker compose ps --format table
|
||||||
|
|
||||||
|
exit 0
|
||||||
Loading…
Add table
Reference in a new issue