diff --git a/mcp_server_main.py b/mcp_server_main.py index 0ace2b9..35f2025 100644 --- a/mcp_server_main.py +++ b/mcp_server_main.py @@ -6,37 +6,12 @@ import os import httpx import json import time -import subprocess -import sys from collections import defaultdict from pydantic import BaseModel, HttpUrl, Field from typing import Optional, Dict, List, Literal, Any, Union import urllib.parse from fastmcp.server.middleware import Middleware, MiddlewareContext -# --- Install Playwright Chromium at startup (for cloud deployments) --- -def _install_playwright_chromium(): - """Install Playwright Chromium browser if not already installed.""" - try: - # Check if running in a cloud environment (no display) - result = subprocess.run( - [sys.executable, "-m", "playwright", "install", "chromium"], - capture_output=True, - text=True, - timeout=300 # 5 minute timeout - ) - if result.returncode == 0: - print("Playwright Chromium installed successfully") - else: - print(f"Playwright Chromium installation warning: {result.stderr}") - except subprocess.TimeoutExpired: - print("Warning: Playwright Chromium installation timed out") - except Exception as e: - print(f"Warning: Could not install Playwright Chromium: {e}") - -# Run installation at module load time -_install_playwright_chromium() - # Optional tiktoken import for token counting try: import tiktoken diff --git a/requirements.txt b/requirements.txt.bak similarity index 100% rename from requirements.txt rename to requirements.txt.bak diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..af9e87d --- /dev/null +++ b/setup.py @@ -0,0 +1,47 @@ +""" +Setup script with post-install hook for Playwright Chromium installation. +This runs during `pip install .` to ensure browser binaries are available. +""" +import subprocess +import sys +from setuptools import setup +from setuptools.command.install import install +from setuptools.command.develop import develop + + +def install_playwright_chromium(): + """Install Playwright Chromium browser.""" + print("Installing Playwright Chromium browser...") + try: + subprocess.run( + [sys.executable, "-m", "playwright", "install", "chromium"], + check=True + ) + print("Playwright Chromium installed successfully!") + except subprocess.CalledProcessError as e: + print(f"Warning: Playwright Chromium installation failed: {e}") + except Exception as e: + print(f"Warning: Could not install Playwright Chromium: {e}") + + +class PostInstallCommand(install): + """Post-installation command to install Playwright browsers.""" + def run(self): + install.run(self) + install_playwright_chromium() + + +class PostDevelopCommand(develop): + """Post-develop command to install Playwright browsers.""" + def run(self): + develop.run(self) + install_playwright_chromium() + + +# Minimal setup - main config is in pyproject.toml +setup( + cmdclass={ + 'install': PostInstallCommand, + 'develop': PostDevelopCommand, + }, +)