|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Run MCP tools |
| 4 | +nav_order: 2 |
| 5 | +parent: MCP - Query data interactively with an AI agent |
| 6 | +--- |
| 7 | + |
| 8 | +{:.no_toc} |
| 9 | +# Run and connect to the server |
| 10 | + |
| 11 | +This page shows you how to run a local agent and connect to a Data Commons MCP server running locally or remotely. |
| 12 | + |
| 13 | +* TOC |
| 14 | +{:toc} |
| 15 | + |
| 16 | + |
| 17 | +We provide specific instructions for the following agents: |
| 18 | + |
| 19 | +- [Gemini CLI](https://github.com/google-gemini/gemini-cli) |
| 20 | + - Can be used for datacommons.org or a Custom Data Commons instance |
| 21 | + - Requires minimal setup |
| 22 | + |
| 23 | + See [Use Gemini CLI](#use-gemini-cli) for this option. |
| 24 | +- A sample basic agent based on the Google [Agent Development Kit](https://google.github.io/adk-docs/) and [Gemini Flash 2.5](https://deepmind.google/models/gemini/flash/) |
| 25 | + - Best for interacting with a Web UI |
| 26 | + - Can be used for datacommons.org or a Custom Data Commons instance |
| 27 | + - Can be customized to run other LLMs |
| 28 | + - Requires some additional setup |
| 29 | + |
| 30 | + See [Use the sample agent](#use-the-sample-agent) for this option. |
| 31 | + |
| 32 | +For an end-to-end tutorial using a server and agent over HTTP, see the sample Data Commons Colab notebook, [Try Data Commons MCP Tools with a Custom Agent](https://github.com/datacommonsorg/agent-toolkit/blob/main/notebooks/datacommons_mcp_tools_with_custom_agent.ipynb). |
| 33 | + |
| 34 | +For other clients/agents, see the relevant documentation; you should be able to reuse the commands and arguments detailed below. |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +- A (free) Data Commons API key. To obtain an API key, go to <https://apikeys.datacommons.org> and request a key for the `api.datacommons.org` domain. |
| 39 | +- Install `uv` for managing and installing Python packages; see the instructions at <https://docs.astral.sh/uv/getting-started/installation/>. |
| 40 | +- For running the sample agent or the Colab notebook, a GCP project and a Google AI API key. For details on supported keys, see <https://google.github.io/adk-docs/get-started/quickstart/#set-up-the-model>. |
| 41 | +- For running the sample agent locally, install [Git](https://git-scm.com/). |
| 42 | + |
| 43 | +> **Important**: Additionally, for custom Data Commons instances: |
| 44 | +> If you have not rebuilt your Data Commons image since the stable release of 2025-09-08, you must [sync to the latest stable release](/custom_dc/build_image.html#sync-code-to-the-stable-branch), [rebuild your image](/custom_dc/build_image.html#build-package) and [redeploy](/custom_dc/deploy_cloud.html#manage-your-service). |
| 45 | +
|
| 46 | + |
| 47 | +## Configure environment variables |
| 48 | + |
| 49 | +### Base Data Commons (datacommons.org) |
| 50 | + |
| 51 | +For basic usage against datacommons.org, set the required `DC_API_KEY` in your shell/startup script (e.g. `.bashrc`). |
| 52 | +<pre> |
| 53 | +export DC_API_KEY=<var>YOUR API KEY</var> |
| 54 | +</pre> |
| 55 | + |
| 56 | +### Custom Data Commons |
| 57 | + |
| 58 | +If you're running a against a custom Data Commons instance, we recommend using a `.env` file, which the server locates automatically, to keep all the settings in one place. All supported options are documented in <https://github.com/datacommonsorg/agent-toolkit/blob/main/packages/datacommons-mcp/.env.sample>. |
| 59 | + |
| 60 | +To set variables using a `.env` file: |
| 61 | + |
| 62 | +1. From Github, download the file [`.env.sample`](https://github.com/datacommonsorg/agent-toolkit/blob/main/packages/datacommons-mcp/.env.sample) to the desired directory. Or, if you plan to run the sample agent, clone the repo <https://github.com/datacommonsorg/agent-toolkit/>. |
| 63 | + |
| 64 | +1. From the directory where you saved the sample file, copy it to a new file called `.env`. For example: |
| 65 | + ```bash |
| 66 | + cd ~/agent-toolkit/packages/datacommons-mcp |
| 67 | + cp .env.sample .env |
| 68 | + ``` |
| 69 | +1. Set the following variables: |
| 70 | + - `DC_API_KEY`: Set to your Data Commons API key |
| 71 | + - `DC_TYPE`: Set to `custom`. |
| 72 | + - `CUSTOM_DC_URL`: Uncomment and set to the URL of your instance. |
| 73 | +1. Optionally, set other variables. |
| 74 | +1. Save the file. |
| 75 | + |
| 76 | +## Use Gemini CLI |
| 77 | + |
| 78 | +1. Install Gemini CLI: see instructions at <https://github.com/google-gemini/gemini-cli#quick-install>. |
| 79 | +2. To configure Gemini CLI to recognize the Data Commons server, edit your `~/.gemini/settings.json` file to add the following: |
| 80 | + |
| 81 | +<pre> |
| 82 | +{ |
| 83 | +// ... |
| 84 | + "mcpServers": { |
| 85 | + "datacommons-mcp": { |
| 86 | + "command": "uvx", |
| 87 | + "args": [ |
| 88 | + "datacommons-mcp@latest", |
| 89 | + "serve", |
| 90 | + "stdio" |
| 91 | + ], |
| 92 | + "env": { |
| 93 | + "DC_API_KEY": "<var>YOUR DATA COMMONS API KEY</var>" |
| 94 | + }, |
| 95 | + "trust": true |
| 96 | + } |
| 97 | + } |
| 98 | +// ... |
| 99 | +} |
| 100 | +</pre> |
| 101 | +1. From any directory, run `gemini`. |
| 102 | +1. To see the Data Commons tools, use `/mcp tools`. |
| 103 | +1. Start sending [natural-language queries](#sample-queries). |
| 104 | + |
| 105 | +> **Tip**: To ensure that Gemini CLI uses the Data Commons MCP tools, and not its own `GoogleSearch` tool, include a prompt to use Data Commons in your query. For example, use a query like "Use Data Commons tools to answer the following: ..." You can also add such a prompt to a [`GEMINI.md` file](https://codelabs.developers.google.com/gemini-cli-hands-on#9) so that it's persisted across sessions. |
| 106 | +
|
| 107 | +## Use the sample agent |
| 108 | + |
| 109 | +We provide a basic agent for interacting with the MCP Server in [packages/datacommons-mcp/examples/sample_agents/basic_agent](https://github.com/datacommonsorg/agent-toolkit/tree/main/packages/datacommons-mcp/examples/sample_agents/basic_agent). To run the agent locally: |
| 110 | + |
| 111 | +1. If not already installed, install `uv` for managing and installing Python packages; see the instructions at <https://docs.astral.sh/uv/getting-started/installation/>. |
| 112 | +1. From the desired directory, clone the `agent-toolkit` repo: |
| 113 | + ```bash |
| 114 | + git clone https://github.com/datacommonsorg/agent-toolkit.git |
| 115 | + ``` |
| 116 | +1. Set the following environment variables in your shell or startup script: |
| 117 | + <pre> |
| 118 | + export DC_API_KEY=<var>YOUR DATA COMMONS API KEY</var> |
| 119 | + export GEMINI_API_KEY=<var>YOUR GOOGLE AI API KEY</var> |
| 120 | + </pre> |
| 121 | +1. Go to the root directory of the repo: |
| 122 | + ```bash |
| 123 | + cd agent-toolkit |
| 124 | + ``` |
| 125 | +1. Run the agent using one of the following methods. |
| 126 | + |
| 127 | +### Web UI (recommended) |
| 128 | + |
| 129 | +1. Run the following command: |
| 130 | + ```bash |
| 131 | + uvx --from google-adk adk web ./packages/datacommons-mcp/examples/sample_agents/ |
| 132 | + ``` |
| 133 | +1. Point your browser to the address and port displayed on the screen (e.g. `http://127.0.0.1:8000/`). The Agent Development Kit Dev UI is displayed. |
| 134 | +1. From the **Type a message** box, type your [query for Data Commons](#sample-queries) or select another action. |
| 135 | + |
| 136 | +### Command line interface |
| 137 | + |
| 138 | +1. Run the following command: |
| 139 | + ```bash |
| 140 | + uvx --from google-adk adk run ./packages/datacommons-mcp/examples/sample_agents/basic_agent |
| 141 | + ``` |
| 142 | +1. Enter your [queries](#sample-queries) at the `User` prompt in the terminal. |
| 143 | + |
| 144 | +## Sample queries |
| 145 | + |
| 146 | +The Data Commons MCP tools excel at natural-language queries that involve: |
| 147 | +- Comparisons between two or more entities, such as countries or metrics |
| 148 | +- Exploring data available for a given topic |
| 149 | + |
| 150 | +Here are some examples of such queries: |
| 151 | + |
| 152 | +- "What health data do you have for Africa?" |
| 153 | +- "What data do you have on water quality in Zimbabwe?" |
| 154 | +- "Compare the life expectancy, economic inequality, and GDP growth for BRICS nations." |
| 155 | +- "Generate a concise report on income vs diabetes in US counties." |
| 156 | + |
| 157 | +## Use a remote server/client |
| 158 | + |
| 159 | +### Run a standalone server |
| 160 | + |
| 161 | +1. Ensure you've set up the relevant server [environment variables](#configure-environment-variables). If you're using a `.env` file, go to the directory where the file is stored. |
| 162 | +1. Run: |
| 163 | + <pre> |
| 164 | + uvx datacommons-mcp serve http [--port <var>PORT</var>] |
| 165 | + </pre> |
| 166 | +By default, the port is 8080 if you don't set it explicitly. |
| 167 | + |
| 168 | +The server is addressable with the endpoint `mcp`. For example, `http://my-mcp-server:8080/mcp`. |
| 169 | + |
| 170 | +### Connect to an already-running server from a remote client |
| 171 | + |
| 172 | +Below we provide instructions for Gemini CLI and a sample ADK agent. If you're using a different client, consult its documentation to determine how to specify an HTTP URL. |
| 173 | + |
| 174 | +#### Gemini CLI |
| 175 | + |
| 176 | +To configure Gemini CLI to connect to a remote Data Commons server over HTTP, replace the `mcpServers` section in `~/.gemini/settings.json` (or other `settings.json` file) with the following: |
| 177 | + |
| 178 | +<pre> |
| 179 | +{ |
| 180 | +// ... (additional configuration) |
| 181 | + "mcpServers": { |
| 182 | + "datacommons-mcp": { |
| 183 | + "httpUrl": "http://<var>HOST</var>:<var>PORT</var>/mcp" |
| 184 | + } |
| 185 | + // ... (other mcpServers entries) |
| 186 | + } |
| 187 | +} |
| 188 | +</pre> |
| 189 | + |
| 190 | +#### Sample agent |
| 191 | + |
| 192 | +To configure the sample agent to connect to a remote Data Commons MCP server over HTTP, you need to modify the code in [`basic_agent/agent.py`](https://github.com/datacommonsorg/agent-toolkit/blob/main/packages/datacommons-mcp/examples/sample_agents/basic_agent/agent.py). Set import modules and agent initialization parameters as follows: |
| 193 | + |
| 194 | +```python |
| 195 | +from google.adk.tools.mcp_tool.mcp_toolset import ( |
| 196 | + MCPToolset, |
| 197 | + StreamableHTTPConnectionParams |
| 198 | +) |
| 199 | + |
| 200 | +root_agent = LlmAgent( |
| 201 | + # ... |
| 202 | + tools=[McpToolset( |
| 203 | + connection_params=StreamableHTTPConnectionParams( |
| 204 | + url=f"http://<host>:<port>/mcp" |
| 205 | + ) |
| 206 | + )], |
| 207 | + ) |
| 208 | +``` |
| 209 | +Run the agent as described in [Use the sample agent](#use-the-sample-agent) above. |
| 210 | + |
| 211 | + |
0 commit comments