Complete guide for deploying the LangChain Agent MCP Server to Google Cloud Run.
- Google Cloud Account with billing enabled
- Google Cloud SDK (gcloud) installed and configured
- Docker installed and running
- OpenAI API Key ready to configure
Windows PowerShell (Recommended for Windows users):
.\deploy-cloud-run.ps1 -ProjectId "your-project-id" -Region "us-central1"📖 For detailed Windows instructions, see DEPLOY_CLOUD_RUN_WINDOWS.md
Linux/Mac:
chmod +x deploy-cloud-run.sh
./deploy-cloud-run.sh your-project-id us-central1Follow the step-by-step instructions below.
# Install gcloud CLI (if not already installed)
# See: https://cloud.google.com/sdk/docs/install
# Authenticate
gcloud auth login
# Set your project
gcloud config set project YOUR_PROJECT_IDgcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com# Set variables
PROJECT_ID="your-project-id"
SERVICE_NAME="langchain-agent-mcp-server"
IMAGE_NAME="gcr.io/${PROJECT_ID}/${SERVICE_NAME}"
# Build the image
docker build -t $IMAGE_NAME .
# Push to Container Registry
docker push $IMAGE_NAMEgcloud run deploy $SERVICE_NAME \
--image $IMAGE_NAME \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--memory 2Gi \
--cpu 2 \
--timeout 300 \
--max-instances 10 \
--min-instances 0 \
--set-env-vars "OPENAI_MODEL=gpt-4o-mini,MAX_ITERATIONS=10,VERBOSE=false" \
--port 8000Option A: Using Environment Variable (Quick but less secure)
gcloud run services update $SERVICE_NAME \
--set-env-vars OPENAI_API_KEY=your-key-here \
--region us-central1Option B: Using Secret Manager (Recommended for production)
- Create a secret:
echo -n "your-openai-api-key" | gcloud secrets create openai-api-key \
--data-file=- \
--replication-policy="automatic"- Grant Cloud Run access to the secret:
PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")
gcloud secrets add-iam-policy-binding openai-api-key \
--member="serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"- Update the service to use the secret:
gcloud run services update $SERVICE_NAME \
--update-secrets=OPENAI_API_KEY=openai-api-key:latest \
--region us-central1# Get the service URL
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME \
--platform managed \
--region us-central1 \
--format 'value(status.url)')
# Test health endpoint
curl $SERVICE_URL/health
# Test manifest endpoint
curl $SERVICE_URL/mcp/manifestAdjust based on your needs:
# For higher traffic or complex queries
--memory 4Gi \
--cpu 4 \
--max-instances 20
# For cost optimization
--memory 1Gi \
--cpu 1 \
--max-instances 5 \
--min-instances 0 # Scale to zero when not in useCloud Run has a maximum timeout of 300 seconds (5 minutes). For longer-running agent tasks:
--timeout 300 # Maximum allowedSet additional environment variables:
gcloud run services update $SERVICE_NAME \
--set-env-vars "OPENAI_MODEL=gpt-4,MAX_ITERATIONS=15,VERBOSE=true,API_KEY=your-api-key" \
--region us-central1If you need to allow specific origins:
gcloud run services update $SERVICE_NAME \
--set-env-vars "CORS_ORIGINS=https://yourdomain.com,https://app.yourdomain.com" \
--region us-central1The cloudbuild.yaml file is already included in the repository. It:
- Builds the Docker image
- Pushes to Container Registry
- Deploys to Cloud Run
# Create a trigger for GitHub
gcloud builds triggers create github \
--name="deploy-langchain-mcp" \
--repo-name="LangchainMCP" \
--repo-owner="mcpmessenger" \
--branch-pattern="^main$" \
--build-config="cloudbuild.yaml"# Store OpenAI API key as a secret
echo -n "your-openai-api-key" | gcloud secrets create openai-api-key \
--data-file=-
# Grant Cloud Build access
PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")
gcloud secrets add-iam-policy-binding openai-api-key \
--member="serviceAccount:${PROJECT_NUMBER}@cloudbuild.gserviceaccount.com" \
--role="roles/secretmanager.secretAccessor"Add this step before the deploy step:
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: 'bash'
args:
- '-c'
- |
gcloud run services update langchain-agent-mcp-server \
--update-secrets=OPENAI_API_KEY=openai-api-key:latest \
--region us-central1# View recent logs
gcloud run services logs read $SERVICE_NAME \
--platform managed \
--region us-central1
# Follow logs in real-time
gcloud run services logs tail $SERVICE_NAME \
--platform managed \
--region us-central1- Go to Cloud Console → Cloud Run → Your Service
- Click on "Monitoring" tab
- Set up alerts for:
- Request latency
- Error rate
- Memory usage
- CPU usage
--min-instances 0 # Scales down when not in useStart with minimal resources and scale up as needed:
- Development: 1 CPU, 1Gi memory
- Production: 2 CPU, 2Gi memory
- High Traffic: 4 CPU, 4Gi memory
--max-instances 10 # Limit concurrent instancesEstimate costs: https://cloud.google.com/run/pricing
- Check logs:
gcloud run services logs read $SERVICE_NAME --region us-central1- Verify environment variables:
gcloud run services describe $SERVICE_NAME --region us-central1- Test locally with Docker:
docker run -p 8000:8000 -e OPENAI_API_KEY=your-key gcr.io/$PROJECT_ID/$SERVICE_NAME- Increase memory/CPU:
gcloud run services update $SERVICE_NAME \
--memory 4Gi --cpu 4 --region us-central1- Check agent iteration limits:
gcloud run services update $SERVICE_NAME \
--set-env-vars MAX_ITERATIONS=5 --region us-central1If you need to restrict access:
# Remove --allow-unauthenticated and use IAM
gcloud run services update $SERVICE_NAME \
--no-allow-unauthenticated \
--region us-central1
# Grant access to specific users
gcloud run services add-iam-policy-binding $SERVICE_NAME \
--member="user:email@example.com" \
--role="roles/run.invoker" \
--region us-central1- Use Secret Manager for API keys (not environment variables)
- Enable VPC if accessing private resources
- Set up IAM policies for service access
- Enable Cloud Armor for DDoS protection
- Use HTTPS only (enabled by default)
- Set up API key authentication in the application
- Set up custom domain: https://cloud.google.com/run/docs/mapping-custom-domains
- Configure CDN: Use Cloud CDN with Cloud Run
- Set up monitoring: Configure alerts in Cloud Monitoring
- Enable tracing: Use Cloud Trace for request tracing
For issues or questions:
- Check Cloud Run logs
- Review Cloud Run documentation
- Check application logs in Cloud Console