|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Run an MCP server in Google Cloud |
| 4 | +nav_order: 9 |
| 5 | +parent: Build your own Data Commons |
| 6 | +--- |
| 7 | + |
| 8 | +{:.no_toc} |
| 9 | +# Run an MCP server in Google Cloud |
| 10 | + |
| 11 | +If you have built a custom agent or Gemini CLI extension which you want to make publicly available, this page describes how to run the [Data Commons MCP server](https://pypi.org/project/datacommons-mcp/) in the cloud, using Google Cloud Run. |
| 12 | + |
| 13 | +Since setting up an MCP server is a simple, one-time setup, there's no need to use Terraform to manage it. Data Commons provides a prebuilt Docker image in the Artifact Registry, so you only need to set up a new Cloud Run service to point to it. |
| 14 | + |
| 15 | +## Prebuilt images |
| 16 | + |
| 17 | +There are several versions of the image available, viewable at <https://console.cloud.google.com/artifacts/docker/datcom-ci/us/gcr.io/datacommons-mcp-server>. We recommend that you choose a production version with a specific version number, to ensure that changes introduced by the Data Commons team don't break your application. |
| 18 | + |
| 19 | +## Before you start: decide on a hosting model |
| 20 | + |
| 21 | +There are several ways you can host the MCP server in Cloud Run, namely: |
| 22 | + |
| 23 | +- As a standalone service. In this case, any client simply connects to it over HTTP, including your own MCP agent running as a separate Cloud Run service or locally. You can choose whether to make the internal Cloud Run app URL publicly available, or whether to put a load balancer in front of the service and map a domain name. |
| 24 | +- As a ["sidecar"](https://docs.cloud.google.com/run/docs/deploying#sidecars){: target="_blank"} to an MCP client. If you are hosting your own MCP client in Cloud Run as well, this may be a useful option. In this case, the MCP server is not directly addressable; all external connections are managed by the client. |
| 25 | + |
| 26 | +In this page, we provide steps for running the Data Commons MCP server as a standalone container. If you want to go with the sidecar option, please see [Deploying multiple containers to a service (sidecars)](https://docs.cloud.google.com/run/docs/deploying#sidecars){: target="_blank"} for additional requirements and setup procedures. |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +The following procedures assume that you have set up the following Google Cloud Platform services, using the [Terraform scripts](deploy_cloud.md#terraform): |
| 31 | +- A service account and roles. |
| 32 | +- A Google Cloud Secret Manager secret for storing your Data Commons API key. |
| 33 | + |
| 34 | +## Create a Cloud Run Service for the MCP server |
| 35 | + |
| 36 | +The following procedure sets up a bare-bones container service. To set additional options, such as request timeouts, instance replication, etc., please see [Configure Cloud Run services](https://docs.cloud.google.com/run/docs/configuring){: target="_blank"} for details. |
| 37 | + |
| 38 | +<div class="gcp-tab-group"> |
| 39 | + <ul class="gcp-tab-headers"> |
| 40 | + <li class="active">Cloud Console</li> |
| 41 | + <li>gcloud CLI</li> |
| 42 | + </ul> |
| 43 | + <div class="gcp-tab-content"> |
| 44 | + <div class="active"> |
| 45 | + <ol> |
| 46 | + <li>Go to the <a href="https://console.cloud.google.com/run/services" target="_blank">https://console.cloud.google.com/run/services</a> page for your project.</li> |
| 47 | + <li>Click <b>Deploy container</b>.</li> |
| 48 | + <li>In the <b>Container image URL</b> field, click <b>Select</b>.</li> |
| 49 | + <li>In the Artifact Registry panel that appears in the right side of the window, that appears, click <b>Change</b>.</li> |
| 50 | + <li>In the project search bar, enter <code>datcom-ci</code> and click on the link that appears.</li> |
| 51 | + <li>Expand <b>gcr.io/datcom-ci</b> and expand <b>datacommons-mcp-server</b>.</li> |
| 52 | + <li>From the list of images, select a production image, e.g. <code>production-v1.1.4</code>.</li> |
| 53 | + <li>Under <b>Configure</b>, select the desired region for the service, e.g. <code>us-central1</code>.</li> |
| 54 | + <li>Expand <b>Containers, Networking, Security</b>.</li> |
| 55 | + <li>Click the <b>Variables & secrets</b> tab.</li> |
| 56 | + <li>Under <b>Environment variables</b>, click <b>Add variable</b> and set the following variables: |
| 57 | + <ul> |
| 58 | + <li>name: <code>DC_TYPE</code>, value: <code>custom</code></li> |
| 59 | + <li>name: <code>CUSTOM_DC_URL</code>, value: <code><var>YOUR_INSTANCE_URL</var></code></li> |
| 60 | + </ul></li> |
| 61 | + <li>Under <b>Secrets exposed as environment variables</b>, click <b>Reference a secret</b>.</li> |
| 62 | + <li>In the <b>Name</b> field, enter <code>DC_API_KEY</code>, and from the <b>Secret</b> field, select the secret previously created by the Terraform scripts. It is in the form <code><var>NAMESPACE</var>-datacommons-dc-api-key-<var>FINGERPRINT</var></code>.</li> |
| 63 | + <li>In the <b>Version</b> field, select the desired version, e.g. <b>latest</b>.</li> |
| 64 | + <li>Click <b>Done</b>.</li> |
| 65 | + <li>Click the <b>Security</b> tab. From the <b>Service account</b> field, select the service account for your namespace and project, previously created by the Terraform scripts.</li> |
| 66 | + <li>Click <b>Create</b>. If correctly configured, the service will deploy automatically. It may take several minutes to start up.</li></ol> |
| 67 | + </div> |
| 68 | + <div> |
| 69 | + <ol> |
| 70 | + <li>If you haven't recently refreshed your Google Cloud credentials, run <code>gcloud auth application-default login</code> and authenticate.</li> |
| 71 | + <li>From any local directory, run the following command: |
| 72 | + <pre>gcloud run deploy datacommons-mcp-server --image <var>CONTAINER_IMAGE_URL</var> \ |
| 73 | + --service-account <var>SERVICE_ACCOUNT</var> --region <var>REGION</var> \ |
| 74 | + --allow-unauthenticated \ |
| 75 | + --set-secrets="DC_API_KEY=<var>SECRET_NAME</var>:latest" \ |
| 76 | + --set-env-vars="DC_TYPE=custom" --set-env-vars="CUSTOM_DC_URL=<var>INSTANCE_URL</var>"</pre></li> |
| 77 | + </ol> |
| 78 | + <ul> |
| 79 | + <li>The container image URL is <code>gcr.io/datcom-ci/datacommons-mcp-server:<var>TAG</var></code>. The tag should be a production image with a version number, e.g. <code>production-v1.1.4</code>.</li> |
| 80 | + <li>The service account was created when you ran Terraform. It is in the form <code><var>NAMESPACE</var>-datacommons-sa@<var>PROJECT_ID</var>.iam.gserviceaccount.com</code>.</li> |
| 81 | + <li>The region is the Cloud region where you want to run the service, e.g. <code>us-central1</code>.</li> |
| 82 | + <li>The secret name is the one created when you ran the Terraform scripts, in the form <code><var>NAMESPACE</var>-datacommons-dc-api-key-<var>FINGERPRINT</var></code>. If you're not sure about the name or fingerprint, go to <a href="https://console.cloud.google.com/security/secret-manager" target="_blank">https://console.cloud.google.com/security/secret-manager</a> for your project and look it up.</li> |
| 83 | + </ul> |
| 84 | + To view the startup status, run the following command: |
| 85 | + <pre>gcloud run services logs tail datacommons-mcp-server --region <var>REGION</var></pre> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | +</div> |
| 89 | + |
| 90 | +<script src="/assets/js/customdc-doc-tabs.js"></script> |
| 91 | + |
| 92 | +## Connect to the server from a remote client |
| 93 | + |
| 94 | +For details, see the following pages: |
| 95 | +- [Connect to the server from a local Gemini CLI client](/mcp/run_tools.html#gemini-cli-remote) |
| 96 | +- [Connect to the server from a local agent](/mcp/run_tools.html#remote) |
| 97 | + |
| 98 | +The HTTP URL parameter is the Cloud Run App URL, if you are exposing the service directly, or a custom domain URL if you are using a load balancer and domain mapping. |
| 99 | + |
| 100 | +## Troubleshoot deployment issues |
| 101 | + |
| 102 | +### Container fails to start |
| 103 | + |
| 104 | +If you see this error message: |
| 105 | + |
| 106 | +``` |
| 107 | +The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout... |
| 108 | +``` |
| 109 | +This is a generic message that could indicate a number of configuration problems. Check all of these: |
| 110 | +- Be sure you have specified the `DC_API_KEY` environment variable. |
| 111 | +- Be sure you have specified the correct service account. |
| 112 | +- Try increasing the health check timeout. |
| 113 | + |
| 114 | + |
| 115 | + |
0 commit comments