cli.pyā¢3.5 kB
"""CLI interface for the GCP MCP server."""
import asyncio
import sys
from pathlib import Path
from typing import Optional
import click
from .server import main as server_main
@click.command()
@click.option(
"--config",
"-c",
type=click.Path(path_type=Path),
help="Path to configuration file (default: config.local.json for local dev)",
)
@click.option(
"--project",
"-p",
help="GCP project ID to use as default",
)
@click.option(
"--debug",
is_flag=True,
help="Enable debug logging",
)
@click.option(
"--local",
is_flag=True,
help="Use local development configuration",
)
@click.option(
"--credentials",
type=click.Path(exists=True, path_type=Path),
help="Path to GCP service account credentials JSON file",
)
def main(config: Optional[Path], project: Optional[str], debug: bool, local: bool, credentials: Optional[Path]) -> None:
"""Start the GCP MCP server for integration with AI assistants like Claude Code.
For local development, use --local flag or set config to config.local.json
"""
import os
# Handle credentials first
if credentials:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(credentials)
click.echo(f"š Using credentials: {credentials}")
# Try to extract project ID from credentials file
if not project:
try:
import json
with open(credentials) as f:
creds_data = json.load(f)
project = creds_data.get("project_id")
if project:
click.echo(f"šÆ Extracted project from credentials: {project}")
except:
pass
# Set up local development defaults
if local or (not config and Path("config.local.json").exists()):
config = Path("config.local.json")
if not project:
# Try to read project from config file
try:
import json
with open(config) as f:
cfg = json.load(f)
project = cfg.get("default_project")
except:
pass
if debug:
os.environ["LOG_LEVEL"] = "DEBUG"
click.echo("š Debug mode enabled")
if project:
os.environ["GCP_PROJECT"] = project
click.echo(f"šÆ Using GCP project: {project}")
if config:
click.echo(f"āļø Using config file: {config}")
# Check for credentials if not already set
if not credentials:
creds_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
if creds_path and Path(creds_path).exists():
click.echo(f"š Using credentials: {creds_path}")
elif not creds_path:
click.echo("ā ļø No GOOGLE_APPLICATION_CREDENTIALS set - will try application default credentials")
click.echo("š Starting GCP MCP Server...")
click.echo("š” Use Ctrl+C to stop the server")
try:
# Pass config to server_main if we have one
if config:
os.environ["GCP_MCP_CONFIG"] = str(config)
asyncio.run(server_main())
except KeyboardInterrupt:
click.echo("\nš Server stopped by user")
sys.exit(0)
except Exception as e:
click.echo(f"ā Error: {e}", err=True)
if debug:
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()