Leveraging Gemini CLI and the underlying Gemini LLM to build Model Context Protocol (MCP) AI applications in the Zig programming language deployed to Google Cloud Run.
Why not just use Python?
Python has traditionally been the main coding language for ML and AI tools. One of the strengths of the MCP protocol is that the actual implementation details are independent of the development language. The reality is that not every project is coded in Python- and MCP allows you to use the latest AI approaches with other coding languages.
Zig? What else do you want me to do? Zag?
The goal of this article is to provide a minimal viable basic working MCP stdio server in Zig that can be run locally without any unneeded extra code or extensions.
The Zig MCP library is here:
What Is Zig?
Zig is a modern, open-source, general-purpose systems programming language and toolchain designed for robustness, optimality, and clarity. Created by Andrew Kelley, it aims to be a “better C,” offering manual memory management, no hidden allocations, and powerful compile-time code execution (comptime). Zig also functions as a C/C++ compiler and build system.
The main site for Zig is here:
Home ⚡ Zig Programming Language
Installing Zig
The step by step instructions vary by platform. Here is a site with starter steps for a Debian based system:
How to Install Zig on Debian: The Easy Way with debian.griffo.io
What is Firestore?
Google Firestore, also known as Cloud Firestore is a part of the Google Firebase application development platform. It is fundamentally a cloud-hosted NoSQL database for storing and syncing data. Firestore can be directly accessed by mobile and web applications through native SDKs.
Firestore Options
There are two options for Firestore integration. Google provides a full SDK for specific languages:
SDKs and client libraries | Firestore | Firebase
Or an endpoint version:
Use the Cloud Firestore REST API | Firebase
For this project- the full SDK is not available. The endpoint version of the Firestore SDK was used for the integration.
Gemini CLI
If not pre-installed you can download the Gemini CLI to interact with the source files and provide real-time assistance:
npm install -g @google/gemini-cli
Testing the Gemini CLI Environment
Once you have all the tools and the correct Node.js version in place- you can test the startup of Gemini CLI. You will need to authenticate with a Key or your Google Account:
gemini
Node Version Management
Gemini CLI needs a consistent, up to date version of Node. The nvm command can be used to get a standard Node environment:
Zig MCP Documentation
This Zig MCP page provides samples and documentation for getting started:
mcp.zig | Model Context Protocol for Zig
Where do I start?
The strategy for starting MCP development is a incremental step by step approach.
First, the basic development environment is setup with the required system variables, and a working Gemini CLI configuration.
Then, a minimal Hello World Style Zig MCP Server is built with HTTP transport. This server is validated with Gemini CLI in the local environment.
This setup validates the connection from Gemini CLI to the local process via MCP. The MCP client (Gemini CLI) and the MCP server both run in the same local environment.
Next- the basic MCP server is wrapped in a container and deployed remotely to Google Cloud Run. This remote MCP server running on a Cloud Run endpoint is validated with a local copy of Gemini CLI.
Setup the Basic Environment
At this point you should have a working Zig build environment and a working Gemini CLI installation. The next step is to clone the GitHub samples repository with support scripts:
cd ~
git clone https://github.com/xbill9/gemini-cli-codeassist
Then run init.sh from the cloned directory.
The script will attempt to determine your shell environment and set the correct variables:
cd gemini-cli-codeassist
source init.sh
If your session times out or you need to re-authenticate- you can run the set_env.sh script to reset your environment variables:
cd gemini-cli-codeassist
source set_env.sh
Variables like PROJECT_ID need to be setup for use in the various build scripts- so the set_env script can be used to reset the environment if you time-out.
Hello World with HTTP Transport
One of the key features that the standard MCP libraries provide is abstracting various transport methods.
The high level MCP tool implementation is the same no matter what low level transport channel/method that the MCP Client uses to connect to a MCP Server.
The simplest transport that the SDK supports is the stdio (stdio/stdout) transport — which connects a locally running process. Both the MCP client and MCP Server must be running in the same environment.
The HTTP transport allows the MCP client and server to run in the same environment or deployed remotely over the Internet.
The connection over HTTP will look similar to this:
logInfo("Server starting on 0.0.0.0:8080...");
var http_transport = try HttpServerTransport.init(allocator, 8080);
defer http_transport.deinit();
try server.runWithTransport(http_transport.transport());
Zig Package Information
The code depends on several standard Ziglibraries for MCP and logging:
const std = @import("std");
const mcp = @import("mcp");
the Firestore access layer has been wrapped in a separate include:
const fs = @import("firestore.zig");
Installing and Running the Zig Code
Run the install make release target on the local system:
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ make
zig build
Linked zig-out/bin/firestore-https-zig to server
To lint the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ make lint
zig fmt --check .
zig build -Doptimize=ReleaseFast
To test the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ make test
zig build
Linked zig-out/bin/firestore-https-zig to server
zig build test
python3 test_server.py
Testing server tools (HTTP)...
✓ initialize
✓ notifications/initialized
✓ tools/list
✓ tools/call (greet)
All tests passed!
Gemini CLI settings.json
In this example — the C source code uses a compiled binary that can be called directly from Gemini CLI.
The default Gemini CLI settings.json has an entry for the source:
{
"mcpServers": {
"firestore-https-zig": {
"httpUrl": "http://127.0.0.1:8080/mcp"
}
}
}
Start the Local MCP Server
Open a terminal window and kick off the local MCP server:
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ ./server
{"asctime":"1770049307","name":"root","levelname":"INFO","message":"Firestore client initialized successfully."}
{"asctime":"1770049307","name":"root","levelname":"INFO","message":"Server starting on 0.0.0.0:8080..."}
Validation with Gemini CLI
Next- open another window and start Gemini CLI. The local MCP connection over HTTP to the Zig Code is validated and the full Gemini CLI session will start:
> /mcp list
Configured MCP servers:
🟢 firestore-https-zig - Ready (7 tools)
Tools:
- check_db
- get_product_by_id
- get_products
- get_root
- greet
- reset
- seed
> check_db
✦ I will check if the inventory database is running.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ check_db (firestore-https-zig MCP Server) {} │
│ │
│ Database running: true │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The inventory database is running.
> do I have coffee in inventory?
✦ I will check the inventory for coffee.
✦ Yes, you have Coffee Beans in your inventory. There are two listings:
- 430 units at $4 each
- 218 units at $2 each
Deploying to Cloud Run
After the HTTP version of the MCP server has been tested locally — it can be deployed remotely to Google Cloud Run.
First- switch to the directory with the HTTP MCP sample code:
cd ~/gemini-cli-codeassist/firestore-https-zig
Deploy the project to Google Cloud Run with the pre-built cloudbuild.yaml and Dockerfile:
cd ~/gemini-cli-codeassist/firestore-https-zig
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ make deploy
The Cloud Build will start:
echo "Submitting build to Google Cloud Build..."
Submitting build to Google Cloud Build...
gcloud builds submit . --config cloudbuild.yaml
Creating temporary archive of 17 file(s) totalling 51.6 KiB before compression.
Some files were not included in the source upload.
Check the gcloud log [/home/xbill/.config/gcloud/logs/2026.02.02/11.25.12.014870.log] to see which files and the contents of the
default gcloudignore file used (see `$ gcloud topic gcloudignore` to learn
more).
Uploading tarball of [.] to [gs://comglitn_cloudbuild/source/1770049512.219689-0a3463728b0040db9ff42ec4341f1c0f.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/comglitn/locations/global/builds/387d2c89-72d8-472a-b6de-0f70c5b8d43d].
Logs are available at [https://console.cloud.google.com/cloud-build/builds/387d2c89-72d8-472a-b6de-0f70c5b8d43d?project=1056842563084].
Waiting for build to complete. Polling interval: 1 second(s).
------------------------------------------------------------ REMOTE BUILD OUTPUT -------------------------------------------------------------
starting build "387d2c89-72d8-472a-b6de-0f70c5b8d43d" │
It can take 15–30 minutes to complete the build.
The cloud build needs to pull in all the Zig libraries in the build environment and generate the entire package from scratch:
Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #1: Deploying container to Cloud Run service [firestore-https-zig] in project [comglitn] region [us-central1]
Step #1: Deploying...
Step #1: Setting IAM Policy..........done
Step #1: Creating Revision...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................done
Step #1: Routing traffic.....done
Step #1: Done.
Step #1: Service [firestore-https-zig] revision [firestore-https-zig-00003-69c] has been deployed and is serving 100 percent of traffic.
Step #1: Service URL: https://firestore-https-zig-1056842563084.us-central1.run.app
Finished Step #1
When the build is complete- an endpoint will be returned. The service endpoint in this example is :
https://firestore-https-zig-1056842563084.us-central1.run.app
The actual endpoint will vary based on your project settings.
Review Service in Cloud Run
Navigate to the Google Cloud console and search for Cloud Run -
and then you can detailed information on the Cloud Run Service:
Cloud Logging
The remote server writes logs to stderr in standard JSON format. These logs are available from the deployed Cloud Run Service:
Validate HTTP connection
Once you have the Endpoint — you can attempt a connection- navigate to in your browser:
https://firestore-https-zig-1056842563084.us-central1.run.app
You will need to adjust the exact URL to match the URL returned from Cloud Build.
You will get an error- this connection is expecting a message in the MCP format:
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}
Gemini CLI settings.json.cloudrun
Replace the default Gemini CLI configuration file — settings.json with a pre-configured sample- settings.json.cloudrun to use the Cloud Run version of the connection:
{
"mcpServers": {
"firestore-cloudrun-zig": {
"httpUrl": "https://firestore-https-zig-1056842563084.us-central1.run.app/mcp"
}
}
}
Copy the Cloud Run version of the Gemini CLI configuration file:
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig$ cd .gemini
cp settings.json.cloudrun settings.json
xbill@penguin:~/gemini-cli-codeassist/firestore-https-zig/.gemini$
Validation with Gemini CLI
The final connection test uses Gemini CLI as a MCP client with the deployed Cloud Run Service in Zig providing the MCP server. Startup Gemini CLI with the updated settings :
> /mcp list
Configured MCP servers:
🟢 firestore-cloudrun-zig - Ready (7 tools)
Tools:
- check_db
- get_product_by_id
- get_products
- get_root
- greet
- reset
- seed
> check_db
✦ I will check if the inventory database is running.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ? check_db (firestore-cloudrun-zig MCP Server) {} ← │
│ │
│ MCP Server: firestore-cloudrun-zig │
│ Tool: check_db │
│ │
│ Allow execution of MCP tool "check_db" from server "firestore-cloudrun-zig"? │
│ │
│ 1. Allow once │
│ 2. Allow tool for this session │
│ 3. Allow all server tools for this session │
│ ● 4. Allow tool for all future sessions │
│ 5. No, suggest changes (esc) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
> do I have twizzlers in inventory?
✦ I will check the current inventory to see if Twizzlers are available.
✦ No, Twizzlers are not currently in the inventory.
> suggest a snack for Carmen the Zig mascot based on what is in stock
✦ Based on the current inventory, I suggest Mint Chocolate Cookies paired with some Green Tea for Carmen. If she's looking for something more
"crunchy" (fitting for a Zig mascot), the White Chocolate Caramel Corn or Parmesan Crisps would also be excellent choices!
Code Review the Zig Project
Gemini CLI is used to review the Zig Project:
> what does the project do?
✦ This project is a Model Context Protocol (MCP) server implemented in Zig. It enables LLMs to interact with a Firestore database and other
local tools via an HTTP-based transport.
Key Features:
* Firestore Integration: Connects to Google Cloud Firestore to manage a product inventory.
* Custom HTTP Transport: Implements a custom MCP transport layer that listens for JSON-RPC 2.0 messages over HTTP (port 8080).
* Tooling: Provides several MCP tools that an AI agent can call:
* get_products: List all products in the inventory.
* get_product_by_id: Retrieve details for a specific product.
* seed: Populate the database with sample products.
* reset: Clear the inventory.
* check_db: Verify if the database connection is active.
* greet: A simple greeting tool for testing.
* Structured Logging: Outputs JSON-formatted logs to stderr for easy monitoring.
Technical Details:
* Language: Zig (v0.15.2+)
* Library: Uses the mcp.zig package for protocol implementation.
* Dependencies: Uses curl and gcloud (via shell execution) for Firestore API requests and authentication.
Summary
MCP development with Zig using Gemini CLI was validated with an incremental step by step approach.
A minimal streaming HTTP transport MCP Server was built from source code and validated with Gemini CLI running as a MCP client in the same local environment.
Then — the MCP server was wrapped in a container and submitted to Google Cloud Build for deployment to Google Cloud Run. The remote MCP server was validated with a standard browser, and Gemini CLI.
Finally- remote MCP operations were performed from the local Gemini CLI installation to the Zig MCP server hosted in Google Cloud Run.
This approach can be extended to more complex deployments and Cloud based options.





Top comments (0)