test_connection.pyā¢3.42 kB
#!/home/borjigin/dev/bookstack-mcp/venv/bin/python
"""
Test script to verify BookStack connection and API access.
"""
import os
import asyncio
import httpx
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 test_connection():
"""Test basic connectivity and API access"""
print("š Testing BookStack MCP Server Connection\n")
print(f"BookStack URL: {BS_URL}")
# Check environment variables
if not BS_TOKEN_ID or not BS_TOKEN_SECRET:
print("\nā ERROR: BS_TOKEN_ID and BS_TOKEN_SECRET must be set in .env file")
print("Please copy env.template to .env and fill in your API credentials")
return False
print(f"Token ID: {BS_TOKEN_ID[:10]}..." if len(BS_TOKEN_ID) > 10 else BS_TOKEN_ID)
print("Token Secret: [HIDDEN]\n")
# Test basic connectivity
print("1ļøā£ Testing basic connectivity...")
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(BS_URL)
print(f" ā
BookStack is reachable (status: {response.status_code})\n")
except Exception as e:
print(f" ā Cannot reach BookStack: {e}\n")
return False
# Test API access
print("2ļøā£ Testing API authentication...")
try:
headers = {
"Authorization": f"Token {BS_TOKEN_ID}:{BS_TOKEN_SECRET}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(f"{BS_URL}/api/books", headers=headers)
response.raise_for_status()
data = response.json()
print(f" ā
API authentication successful!")
print(f" š Found {data.get('total', 0)} books in your BookStack\n")
# Show first few books
if data.get('data'):
print(" First few books:")
for book in data['data'][:3]:
print(f" - {book.get('name')} (ID: {book.get('id')})")
print()
except httpx.HTTPStatusError as e:
print(f" ā API authentication failed: {e.response.status_code}")
if e.response.status_code == 401:
print(" Check your token ID and secret")
elif e.response.status_code == 403:
print(" Your token may lack necessary permissions")
print()
return False
except Exception as e:
print(f" ā API error: {e}\n")
return False
# Test MCP server import
print("3ļøā£ Testing MCP server...")
try:
from fastmcp import FastMCP
print(" ā
FastMCP is installed\n")
except ImportError as e:
print(f" ā FastMCP not installed: {e}")
print(" Run: pip install -r requirements.txt\n")
return False
print("ā
All tests passed! Your BookStack MCP server is ready to use.\n")
print("Next steps:")
print("1. Configure Cursor to use this MCP server (see QUICKSTART.md)")
print("2. Run the server: python server.py")
print("3. Or use in Cursor: add to MCP settings\n")
return True
if __name__ == "__main__":
success = asyncio.run(test_connection())
exit(0 if success else 1)