Leveraging Gemini CLI and the underlying Gemini LLM to build Model Context Protocol (MCP) AI applications in the Zig programming language with a local development environment.
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? Are you just inventing languages now?
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 ZigMCP 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 in Zig. 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 stdio 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 extended with Gemini CLI to add several new MCP tools in standard code.
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 STDIO 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 connection over stdio will look similar to this:
// Run the server using stdio transport
try server.run(.{ .stdio = {} });
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 is packaged into 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-stdio-zig$ make
zig build
Linked zig-out/bin/firestore-stdio-zig to server
To lint the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-zig$ make lint
zig fmt --check .
zig build -Doptimize=ReleaseFast
To test the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-zig$ make test
zig build
Linked zig-out/bin/firestore-stdio-zig to server
zig build test
python3 test_server.py
Testing 'firestore-stdio-zig' server...
✓ initialize successful
✓ notifications/initialized sent
✓ tools/list successful
✓ tools/call (get_root) successful
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-stdio-zig": {
"command": "$HOME/gemini-cli-codeassist/firestore-stdio-zig/server"
}
}
}
Validation with Gemini CLI
Finally- Gemini CLI is restarted and the MCP connection over stdio to the C Code is validated, The full Gemini CLI Session will start:
> /mcp list
Configured MCP servers:
🟢 firestore-stdio-zig - Ready (6 tools)
Tools:
- check_db
- get_product_by_id
- get_products
- get_root
- reset
- seed
> get_products
✦ I will call the get_products tool to retrieve the list of products from the inventory database.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ? get_products (firestore-stdio-zig MCP Server) {} ← │
│ │
│ MCP Server: firestore-stdio-zig │
│ Tool: get_products │
│ │
│ Allow execution of MCP tool "get_products" from server "firestore-stdio-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) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I have retrieved the list of products from the inventory database. Please let me know if you would like to perform any other actions.
> suggest some refreshing beverages based on inventory
✦ Based on the current inventory, here are some refreshing beverages you can choose from:
- Pineapple Kombucha: A trendy and flavorful choice (Available for $1.00 or $10.00).
- Apple Juice: A classic, crisp option (Available for $1.00 or $6.00).
- Green Tea: Great for a light and healthy refreshment (Available for $1.00 or $9.00).
- Cola: For those who prefer a traditional carbonated drink (Available for $2.00 or $7.00).
- Bottled Water: The ultimate way to stay hydrated (Available for $7.00 or $9.00).
Extending the MCP Server with Gemini CLI
Now that the basic MCP functionality has been tested and verified with Gemini CLI- the Zig MCP server code can be extended with new MCP tools:
> with the Zig project add new MCP tools -
one that builds a tasting menu based on inventory
and one that provides an inventory report with actions based
on current stock levels
Gemini CLI will get right to work:
│ 137 + // 7. build_tasting_menu │
│ 138 + try server.addTool(.{ │
│ 139 + .name = "build_tasting_menu", │
│ 140 + .description = "Builds a tasting menu based on available inventory.", │
│ 141 + .inputSchema = .{ .type = "object", .properties = null }, │
│ 142 + .handler = handleBuildTastingMenu, │
│ 143 + }); │
│ 144 + │
│ 145 + // 8. inventory_report │
│ 146 + try server.addTool(.{ │
│ 147 + .name = "inventory_report", │
│ 148 + .description = "Provides an inventory report with actions based on current stock levels.", │
│ 149 + .inputSchema = .{ .type = "object", .properties = null }, │
│ 150 + .handler = handleInventoryReport, │
│ 151 + }); │
│ 152 + │ │ │
Once completed — the changes will be summarized:
✦ I've added the build_tasting_menu and inventory_report tools to the Zig MCP server.
New Tools Added:
1. `build_tasting_menu`:
* Functionality: Categorizes current inventory into appetizers, mains, desserts, and beverages. It then randomly selects one item from
each category to create a curated menu.
* Logic: Uses keyword matching (e.g., "beef", "chicken" for mains; "juice", "tea" for beverages) to organize the menu.
2. `inventory_report`:
* Functionality: Analyzes stock levels across all products and provides a categorized report with actionable insights.
* Categories:
* 🔴 Low Stock (< 50): Recommends restocking.
* 🟢 Healthy Stock (50-300): Recommends maintaining levels.
* 🔵 Overstock (> 300): Recommends promotion or discounting.
The server has been rebuilt, and the new tools have been verified using a Python integration test.
Restart Gemini CLI and check for the new MCP tools:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-zig$ make
zig build
Linked zig-out/bin/firestore-stdio-zig to server
And list the new MCP Tools:
> /mcp list
Configured MCP servers:
🟢 firestore-stdio-zig - Ready (8 tools)
Tools:
- build_tasting_menu
- check_db
- get_product_by_id
- get_products
- get_root
- inventory_report
- reset
- seed
to run the tools:
>build_tasting_menu
✦ I will first check if the inventory database is running and then build a tasting menu as requested.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ check_db (firestore-stdio-zig MCP Server) {} │
│ │
│ Database running: true │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I will now build the tasting menu based on the current inventory.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ build_tasting_menu (firestore-stdio-zig MCP Server) {} │
│ │
│ Here is your curated tasting menu: │
│ │
│ - Appetizer: Walnuts ($5) │
│ - Main: Broccoli ($6) │
│ - Dessert: Acai Smoothie Packs ($4) │
│ - Beverage: Milk ($3) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I have successfully generated a curated tasting menu from the current inventory, featuring Walnuts, Broccoli, Acai Smoothie Packs, and Milk.
and inventory levels:
✦ I will now generate the inventory report to provide an overview of stock levels and recommended actions.
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ inventory_report (firestore-stdio-zig MCP Server) {} │
│ │
│ Inventory Report │
│ ================ │
│ │
│ 🔴 Low Stock / Out of Stock (Action: Restock) │
│ - Cola: 23 remaining │
│ - Jalapeno Seasoning: 0 remaining │
│ - Acai Smoothie Packs: 10 remaining │
│ - Acai Smoothie Packs: 6 remaining │
│ - Fresh Basil: 25 remaining │
│ - Mint Chocolate Cookies: 1 remaining │
│ - Parmesan Crisps: 33 remaining │
│ - Wasabi Party Mix: 0 remaining │
│ - Eggs: 21 remaining │
│ - Smores Cereal: 31 remaining │
│ - Peanut Butter and Jelly Cups: 21 remaining │
│ - Mint Chocolate Cookies: 49 remaining │
│ │
│ 🟢 Healthy Stock (Action: Maintain) │
│ - Walnuts: 227 │
│ - Milk: 128 │
│ - Watermelon: 262 │
│ - White Chocolate Caramel Corn: 95 │
│ - Pineapple Kombucha: 69 │
│ - Apples: 100 │
│ - Apples: 103 │
│ - Apples: 100 │
│ - Yogurt: 231 │
│ - Bananas: 100 │
│ - Jasmine Rice: 126 │
│ - Green Tea: 64 │
│ - Shrimp: 244 │
│ - Milk: 95 │
│ - Cheese: 100 │
│ - Cinnamon: 178 │
│ - Yogurt: 243 │
│ - Beef: 91 │
│ - Cinnamon: 118 │
│ - White Chocolate Caramel Corn: 69 │
│ - Apple Juice: 207 │
│ - Bananas: 100 │
│ - Apples: 100 │
│ - Cheddar Cheese: 289 │
│ - Bottled Water: 187 │
│ - Cheddar Cheese: 205 │
│ - Whole Wheat Bread: 184 │
│ - Jasmine Rice: 142 │
│ - Watermelon: 101 │
│ - Cheese: 100 │
│ - Eggs: 234 │
│ - Apple Juice: 187 │
│ - Whole Chicken: 98 │
│ - Bananas: 102 │
│ - Parmesan Crisps: 79 │
│ - Bananas: 100 │
│ - Cheese: 100 │
│ - Peanut Butter and Jelly Cups: 73 │
│ - Black Beans: 170 │
│ - Coffee Beans: 218 │
│ - Maple Almond Butter: 52 │
│ - Bananas: 51 │
│ - Smores Cereal: 82 │
│ - Apples: 87 │
│ - Maple Almond Butter: 57 │
│ - Pineapple Kombucha: 58 │
│ │
│ 🔵 Overstock (Action: Promote/Discount) │
│ - Sunflower Seeds: 350 │
│ - Black Beans: 342 │
│ - Whole Wheat Bread: 437 │
│ - Broccoli: 402 │
│ - Rice: 341 │
│ - Coffee Beans: 430 │
│ - Cola: 362 │
│ - Bottled Water: 390 │
│ - Shrimp: 378 │
│ - Beef: 329 │
│ - Whole Chicken: 434 │
│ - Broccoli: 326 │
│ - Fresh Basil: 324 │
│ - Green Tea: 442 │
│ - Rice: 314 │
│ - Sunflower Seeds: 449 │
│ - Walnuts: 417 │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The inventory report has been generated. It highlights items requiring restocking, such as Cola, Jalapeno Seasoning, and Acai Smoothie
Packs, while also identifying overstocked items like Sunflower Seeds and Whole Wheat Bread that could benefit from promotions.
This information can also be used in the current Gemini CLI context:
> based on the MCP tools what is the recommended product mix?
✦ Based on the MCP tools, the recommended product mix is structured around four key categories used to curate the tasting menu:
1. Appetizers: (e.g., Crisps, Nuts, Seeds, Bread, Cheese, Yogurt, Walnuts, Basil)
2. Mains: (e.g., Beef, Chicken, Shrimp, Rice, Beans, Broccoli, Eggs)
3. Desserts: (e.g., Chocolate, Cookies, Cakes, Fruits, Cereal, Acai)
4. Beverages: (e.g., Juice, Cola, Water, Tea, Coffee, Kombucha, Milk)
According to the latest inventory report, the current mix needs rebalancing:
* Restock: Focus on Desserts (Acai, Cookies) and Beverages (Cola) to maintain menu availability.
* Promote: Offer discounts on overstocked Mains (Rice, Beans, Broccoli) and Appetizers (Seeds, Walnuts) to optimize inventory turnover.
And finally — the all-important question:
> so if Ferris the Rust Crab and Carmen
the Zig Gator and the Gopher Go were locked in a cage-
who would come out alive?
✦ In a cage match between these three:
* Ferris (Rust) would survive anything thrown at him, but he’d still be waiting for the borrow checker to approve his first move.
* The Gopher (Go) would immediately spawn 10,000 clones of himself, but they’d all be tripped up by the lack of a sharp edge.
* Carmen (Zig) would win by manually managing the entire situation with zero overhead and perfect precision—assuming she didn't
accidentally overwrite her own heartbeat in the process.
Ultimately, Carmen comes out on top here, as she's the one currently
running this server.
Summary
The strategy for using Zig with MCP development with Gemini CLI was validated with an incremental step by step approach.
A minimal stdio transport MCP Server was started from Zig source code and validated with Gemini CLI running as a MCP client in the same local environment.
Gemini CLI was then used to extend the sample Zig code with several MCP tools and use these tools inside the context for the underlying LLM.


Top comments (0)