create_test_page.pyā¢5 kB
#!/home/borjigin/dev/bookstack-mcp/venv/bin/python
"""
Create a test page in BookStack
"""
import os
import asyncio
import httpx
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
BS_URL = os.getenv("BS_URL", "http://192.168.1.193:6875")
BS_TOKEN_ID = os.getenv("BS_TOKEN_ID")
BS_TOKEN_SECRET = os.getenv("BS_TOKEN_SECRET")
async def create_test_page():
"""Create a test page in BookStack"""
if not BS_TOKEN_ID or not BS_TOKEN_SECRET:
print("ā ERROR: BS_TOKEN_ID and BS_TOKEN_SECRET must be set in .env file")
return None
headers = {
"Authorization": f"Token {BS_TOKEN_ID}:{BS_TOKEN_SECRET}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient(timeout=30.0) as client:
# Step 1: Get or create a book
print("š Checking for existing books...")
try:
response = await client.get(f"{BS_URL}/api/books", headers=headers)
response.raise_for_status()
books = response.json()
if books.get('data'):
# Use the first book
book = books['data'][0]
book_id = book['id']
book_name = book['name']
print(f" ā
Using existing book: '{book_name}' (ID: {book_id})")
else:
# Create a new book
print(" š No books found, creating a new one...")
response = await client.post(
f"{BS_URL}/api/books",
headers=headers,
json={
"name": "Test Book - Created by MCP",
"description": "This book was created by the BookStack MCP Server"
}
)
response.raise_for_status()
book = response.json()
book_id = book['id']
book_name = book['name']
print(f" ā
Created new book: '{book_name}' (ID: {book_id})")
except httpx.HTTPStatusError as e:
print(f"ā API Error: {e.response.status_code}")
print(f" Response: {e.response.text}")
return None
except Exception as e:
print(f"ā Error: {e}")
return None
# Step 2: Create the test page
print("\nš Creating test page...")
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
page_data = {
"book_id": book_id,
"name": f"Test Page - {timestamp}",
"markdown": f"""# Test Page
This is a test page created by the BookStack MCP Server!
## Information
- **Created:** {timestamp}
- **Book:** {book_name} (ID: {book_id})
- **Created by:** MCP Server for Cursor
## Features
The MCP server can:
- ā
List books, chapters, and pages
- ā
Search across all content
- ā
Create new pages (like this one!)
- ā
Update existing pages
- ā
Delete pages
- ā
Manage books and chapters
## Next Steps
You can now use Cursor to:
1. List all your books: "List all books in BookStack"
2. Search content: "Search for 'test' in BookStack"
3. Create more pages: "Create a page about Docker in book {book_id}"
4. Update this page: "Update page [ID] with new content"
Happy documenting! š
"""
}
try:
response = await client.post(
f"{BS_URL}/api/pages",
headers=headers,
json=page_data
)
response.raise_for_status()
page = response.json()
page_id = page['id']
page_name = page['name']
page_slug = page['slug']
# Construct the page URL
page_url = f"{BS_URL}/books/{book['slug']}/page/{page_slug}"
print(f" ā
Page created successfully!")
print(f"\n" + "="*70)
print(f"š Test Page Created!")
print(f"="*70)
print(f"Name: {page_name}")
print(f"ID: {page_id}")
print(f"Book: {book_name}")
print(f"URL: {page_url}")
print(f"="*70)
print(f"\nš Open in browser: {page_url}")
print(f"\nš” Try in Cursor: \"Show me page {page_id}\"")
return {
'page_id': page_id,
'page_name': page_name,
'page_url': page_url,
'book_id': book_id,
'book_name': book_name
}
except httpx.HTTPStatusError as e:
print(f"ā Failed to create page: {e.response.status_code}")
print(f" Response: {e.response.text}")
return None
except Exception as e:
print(f"ā Error creating page: {e}")
return None
if __name__ == "__main__":
result = asyncio.run(create_test_page())
exit(0 if result else 1)