init.sh

Overview

The init.sh script is a foundational initialization utility designed to prepare a Google Cloud environment for deploying the MCP server to Google Cloud Run. It automates critical setup tasks such as configuring the Google Cloud project, enabling essential APIs, assigning IAM roles, managing authentication contexts, and exporting environment variables required for subsequent deployment and runtime operations.

This script ensures a consistent, reproducible, and authenticated environment, acting as a prerequisite for the deployment automation workflow detailed under Cloud Run Deployment Automation and the environment setup described in Environment Initialization.


Detailed Explanation

Error Handling Function

handle_error() {
  echo "Error: $1"
  exit 1
}

Main Functional Segments

1. Change Directory to Project Root

cd ~/mcp-on-cloudrun

2. Google Cloud Project ID Setup

PROJECT_FILE="$HOME/project_id.txt"
echo "--- Setting Google Cloud Project ID File ---"

read -p "Please enter your Google Cloud project ID: " user_project_id

if [[ -z "$user_project_id" ]]; then
  handle_error "No project ID was entered."
fi

echo "You entered: $user_project_id"
echo "$user_project_id" > "$PROJECT_FILE"

if [[ $? -ne 0 ]]; then
  handle_error "Failed saving your project ID: $user_project_id."
fi

3. Source Environment Configuration Script

source ./set_env.sh

4. Set Active Google Cloud Project with gcloud

gcloud config set project $(cat ~/project_id.txt)
echo "Successfully saved project ID."

5. Enable Required Google Cloud Services

echo "Enabling Services"
gcloud services enable \
  run.googleapis.com \
  artifactregistry.googleapis.com \
  cloudbuild.googleapis.com

6. Add IAM Roles for Cloud Run Invocation

echo "Adding IAM Roles"

export GOOGLE_CLOUD_PROJECT=$(gcloud config get project)

if [[ -z "$CLOUD_SHELL" ]] && curl -s -i metadata.google.internal | grep -q "Metadata-Flavor: Google"; then
  echo "This VM is running on GCP Defaults to Service Account."
else
  gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member=user:$(gcloud config get-value account) \
    --role='roles/run.invoker' \
    --quiet \
    --condition=None
fi

7. Authentication Context Handling

if [ "$CLOUD_SHELL" = "true" ]; then
  echo "Running in Google Cloud Shell."
else
  if curl -s -i metadata.google.internal | grep -q "Metadata-Flavor: Google"; then
    echo "This VM is running on Google Cloud."
  else
    echo "Not running in Google Cloud VM or Shell."
    echo "Setting ADC Credentials"
    gcloud auth application-default login
  fi
fi

8. Environment-Specific Checks and Messages

if [ -n "$FIREBASE_DEPLOY_AGENT" ]; then
  echo "Running in Firebase Studio terminal"
else
  echo "Not running in Firebase Studio terminal"
fi

if [ -d "/mnt/chromeos" ] ; then
  echo "Running on ChromeOS"
else
  echo "Not running on ChromeOS"
fi

9. Export Identity Token

export ID_TOKEN=$(gcloud auth print-identity-token)

Interaction with Other Parts of the System


Important Implementation Details


Usage Example

$ ./init.sh
--- Setting Google Cloud Project ID File ---
Please enter your Google Cloud project ID: my-gcp-project-123
You entered: my-gcp-project-123
Successfully saved project ID.
Enabling Services
...
Adding IAM Roles
This VM is running on GCP Defaults to Service Account.
Running in Google Cloud Shell.
Not running in Firebase Studio terminal
Not running on ChromeOS
--- Initial Setup complete ---

Visual Diagram: Workflow of init.sh

flowchart TD
A[Start] --> B[Prompt for Project ID]
B --> C{Project ID entered?}
C -- No --> D[Error: No project ID, exit]
C -- Yes --> E[Save Project ID to file]
E --> F[Source set_env.sh]
F --> G[Set gcloud active project]
G --> H[Enable required Cloud APIs]
H --> I[Check environment]
I --> J{Running on GCP VM & not Cloud Shell?}
J -- Yes --> K[Use default service account]
J -- No --> L[Add IAM role for user]
K & L --> M{Running in Cloud Shell?}
M -- Yes --> N[Skip ADC login]
M -- No --> O{Running on GCP VM?}
O -- Yes --> N
O -- No --> P[Run gcloud auth application-default login]
N & P --> Q[Check Firebase Studio terminal]
Q --> R[Check ChromeOS environment]
R --> S[Export ID_TOKEN]
S --> T[Initialization complete]

Summary of Key Variables and Environment Effects

Variable

Description

PROJECT_FILE

File path to save the project ID (~/project_id.txt)

user_project_id

User-entered Google Cloud project ID

GOOGLE_CLOUD_PROJECT

Active Google Cloud project configured in gcloud

ID_TOKEN

OIDC identity token exported for authentication

CLOUD_SHELL

Environment variable indicating if running in Cloud Shell

FIREBASE_DEPLOY_AGENT

Indicates if running in Firebase Studio terminal


Reference to Related Topics


This documentation provides a comprehensive understanding of the init.sh script's role, structure, and integration within the MCP server deployment process.