main.py•2.81 kB
#!/usr/bin/env python3
"""
PostgreSQL MCP Server - Main entry point
"""
import argparse
import asyncio
import sys
import logging
from pathlib import Path
try:
from src.mcp_server import PostgreSQLMCPServer, main as server_main
except ImportError as e:
print(f"Warning: MCP dependencies not installed. Install with: pip install -r requirements.txt")
print(f"Import error: {e}")
PostgreSQLMCPServer = None
server_main = None
def main():
"""Main CLI function"""
parser = argparse.ArgumentParser(
description="PostgreSQL MCP Server - Model Context Protocol server for PostgreSQL databases"
)
parser.add_argument(
"--version",
action="version",
version="%(prog)s 1.0.0"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Enable verbose logging"
)
parser.add_argument(
"--config",
type=str,
help="Path to configuration file"
)
parser.add_argument(
"--test",
action="store_true",
help="Test database connection and exit"
)
args = parser.parse_args()
# Set up logging level
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
print("Verbose logging enabled")
if args.test:
print("Testing database connection...")
return test_connection()
# Run the MCP server
if server_main is None:
print("❌ Cannot start server: MCP dependencies not installed")
print("Install dependencies with: pip install -r requirements.txt")
return 1
try:
print("Starting PostgreSQL MCP Server...")
asyncio.run(server_main())
return 0
except KeyboardInterrupt:
print("\nServer stopped by user")
return 0
except Exception as e:
print(f"Server failed: {e}")
return 1
def test_connection():
"""Test database connection"""
try:
from src.config import load_config
from src.database import PostgreSQLManager
async def _test():
db_config, _ = load_config()
db_manager = PostgreSQLManager(db_config)
info = await db_manager.test_connection()
print(f"✅ Connection successful!")
print(f"Database: {info['database']}")
print(f"User: {info['user']}")
print(f"Version: {info['version']}")
await db_manager.close()
asyncio.run(_test())
return 0
except ImportError as e:
print(f"❌ Dependencies not installed: {e}")
print("Install dependencies with: pip install -r requirements.txt")
return 1
except Exception as e:
print(f"❌ Connection failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())