pdfparsing / start_system.py
uppalmaurya's picture
Fix uvicorn runtime error by using import string
7434732
Raw
History Blame Contribute Delete
4.53 kB
#!/usr/bin/env python3
"""
Complete system startup script for PDF Parser MCP Server
"""
import os
import sys
import subprocess
import asyncio
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def check_dependencies():
"""Check if all required dependencies are installed"""
try:
import fitz
import tiktoken
import anthropic
import fastapi
import uvicorn
import mcp
print("βœ… All dependencies are installed!")
return True
except ImportError as e:
print(f"❌ Missing dependency: {e}")
return False
def check_env_vars():
"""Check if required environment variables are set"""
required_vars = ["ANTHROPIC_API_KEY"]
missing_vars = []
for var in required_vars:
if not os.getenv(var):
missing_vars.append(var)
if missing_vars:
print(f"❌ Missing environment variables: {', '.join(missing_vars)}")
print("Please create a .env file with your ANTHROPIC_API_KEY")
return False
print("βœ… All environment variables are set!")
return True
def create_env_file():
"""Create .env file if it doesn't exist"""
env_file = Path(".env")
if not env_file.exists():
print("Creating .env file...")
with open(env_file, "w") as f:
f.write("""ANTHROPIC_API_KEY=your_anthropic_api_key_here
UPLOAD_DIR=uploads
MAX_FILE_SIZE=50000000
CHUNK_SIZE=8000
MAX_TOKENS=180000
PORT=8000
HOST=0.0.0.0
""")
print("βœ… .env file created! Please add your ANTHROPIC_API_KEY")
return False
return True
def start_fastapi_server():
"""Start FastAPI server"""
print("πŸš€ Starting FastAPI server...")
try:
import uvicorn
from main import app
port = int(os.getenv("PORT", "8000"))
host = os.getenv("HOST", "0.0.0.0")
uvicorn.run("main:app", host=host, port=port, log_level="info")
except Exception as e:
print(f"❌ Error starting FastAPI server: {e}")
return False
def start_mcp_server():
"""Start MCP server"""
print("πŸš€ Starting MCP server...")
try:
from mcp_server import mcp
asyncio.run(mcp.run())
except Exception as e:
print(f"❌ Error starting MCP server: {e}")
return False
def print_instructions():
"""Print setup instructions"""
print("""
πŸŽ‰ PDF Parser MCP Server Setup Complete!
πŸ“‹ SETUP INSTRUCTIONS:
1. πŸ“ Create .env file with your Anthropic API key:
ANTHROPIC_API_KEY=your_actual_api_key_here
2. πŸš€ Start the FastAPI server:
python start_system.py fastapi
3. πŸ”§ Start the MCP server (for Claude Desktop):
python start_system.py mcp
4. πŸ–₯️ Configure Claude Desktop:
- Copy the contents of claude_desktop_config.json
- Add to your Claude Desktop config file:
Windows: %APPDATA%\\Claude\\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Linux: ~/.config/claude/claude_desktop_config.json
5. πŸ“„ Upload PDFs:
curl -X POST "http://localhost:8000/upload-pdf/" -F "file=@your_document.pdf"
6. πŸ’¬ Use with Claude Desktop:
- "List all my documents"
- "What is the summary of document [file_id]?"
- "Search for 'keyword' in all documents"
πŸ”— API Documentation: http://localhost:8000/docs
""")
def main():
"""Main function"""
print("πŸ”§ PDF Parser MCP Server Setup")
print("=" * 50)
# Check if .env file exists
if not create_env_file():
return
# Check dependencies
if not check_dependencies():
print("Please install dependencies with: uv sync")
return
# Check environment variables
if not check_env_vars():
return
# Create upload directory
upload_dir = Path(os.getenv("UPLOAD_DIR", "uploads"))
upload_dir.mkdir(exist_ok=True)
print(f"βœ… Upload directory created: {upload_dir}")
# Handle command line arguments
if len(sys.argv) > 1:
if sys.argv[1] == "fastapi":
start_fastapi_server()
elif sys.argv[1] == "mcp":
start_mcp_server()
else:
print("Usage: python start_system.py [fastapi|mcp]")
else:
print_instructions()
if __name__ == "__main__":
main()