DEV Community

xbill for Google Developer Experts

Posted on • Originally published at xbill999.Medium on

MCP Development with Rust and Gemini CLI

Leveraging Gemini CLI and the underlying Gemini LLM to build Model Context Protocol (MCP) AI applications in the Rust 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.

What is this Tutorial Trying to Do?

Traditionally, ML and AI tools have been deployed in interpreted languages like Python, and Java. One of the key goals of this tutorial is to validate that a compiled language like Rust can be used for AI software development beyond the traditional interpreted languages.

What is Rust?

Rust is a high performance, memory safe, compiled language:

Rust

Rust provides memory safe operations beyond C/C++ and also can provide exceptional performance gains as it is compiled directly to native binaries.

Initial Environment Setup

The environment is meant to be run from a Bash like shell. You can run this from a Linux VM, ChromeOS Linux VM, Firebase Studio environment, or any environment that provides a basic shell. You will also need a working Docker environment.

Rust Setup

Instructions to install Rust are available here:

Getting started

For a Linux like environment the command looks like this:

curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

Rust also depends on a working C compiler and OpenSSL setup. For a Debian 12 system — install the basic tools for development:

sudo apt install build-essential
sudo apt install libssl-dev
sudo apt install pkg-config
sudo apt-get install libudev-dev
sudo apt install make
sudo apt install git
Enter fullscreen mode Exit fullscreen mode

Gemini CLI

If not pre-installed you can download the Gemini CLI to interact with the source files and provide real-time assistance:

sudo npm install -g @google/gemini-cli
Enter fullscreen mode Exit fullscreen mode

Note- if you are an a non standard environment — you will need to make sure to have at least Node version 20 available in order to run 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
Enter fullscreen mode Exit fullscreen mode

Getting Started with Rust and MCP

When MCP was first released, there were several competing Rust frameworks that provided support for the protocol. Eventually, one official supported SDK was consolidated to provide a standard package for building MCP applications with Rust. This SDK is more like a toolbox that provides many options- clients/servers, different transports, and even more advanced integration options.

The official MCP Rust SDK (rmcp) is available here:

GitHub - modelcontextprotocol/rust-sdk: The official Rust SDK for the Model Context Protocol

Where do I start?

The strategy for validating Rust for 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.

A command line version of the System Information tool is built with Gemini CLI.

Then, a minimal Rust MCP Server is built with the stdio transport working directly with Gemini CLI in the local environment. This validates the connection from Gemini CLI to the local compiled Rust process via MCP. The MCP client (Gemini CLI) and the Rust MCP compiled binary Server both run in the same environment.

Setup the Basic Environment

At this point you should have a working Rust compiler 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/iap-https-rust
Enter fullscreen mode Exit fullscreen mode

Then run init.sh from the cloned directory.

The script will attempt to determine your shell environment and set the correct variables:

cd iap-https-rust
source init.sh
Enter fullscreen mode Exit fullscreen mode

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 iap-https-rust
source set_env.sh
Enter fullscreen mode Exit fullscreen mode

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.

Minimal System Information Tool Build

The first step is to build the basic tool directly with Rust. This allows the tool to be debugged and tested locally before adding the MCP layer.

First build the tool locally:

xbill@penguin:~/iap-https-rust/stdio$ make
Building the Rust project...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
xbill@penguin:~/iap-https-rust/stdio$ 
Enter fullscreen mode Exit fullscreen mode

then lint check the code:

xbill@penguin:~/iap-https-rust/stdio$ make lint
Linting code...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.09s
xbill@penguin:~/iap-https-rust/stdio$ 
Enter fullscreen mode Exit fullscreen mode

and run local tests:

xbill@penguin:~/iap-https-rust/stdio$ make test
Running tests...
   Compiling sysutils-stdio-rust v0.2.0 (/home/xbill/iap-https-rust/stdio)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.69s
     Running unittests src/main.rs (target/debug/deps/sysutils_stdio_rust-43a31a19ab207fb9)

running 3 tests
test tests::test_schema_generation ... ok
test tests::test_disk_usage ... ok
test tests::test_local_system_info ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.13s

xbill@penguin:~/iap-https-rust/stdio$ 
Enter fullscreen mode Exit fullscreen mode

The last step is to build the production version:

xbill@penguin:~/iap-https-rust/stdio$ make release
Building Release...
    Finished `release` profile [optimized] target(s) in 0.07s
xbill@penguin:~/iap-https-rust/stdio$ 
Enter fullscreen mode Exit fullscreen mode

Running the Tool Locally

Once the release version has been built- the resulting binary can be executed directly in the local environment.

The quick summary of local system info can be run right from the Makefile:

xbill@penguin:~/iap-https-rust/stdio$ make info
System Information Report
=========================

System Information
------------------
System Name: Debian GNU/Linux
Kernel Version: 6.6.99-09121-g16665fbb817c
OS Version: 12
Host Name: penguin

CPU Information
---------------
Number of Cores: 16

Memory Information
------------------
Total Memory: 6364 MB
Used Memory: 258 MB
Total Swap: 0 MB
Used Swap: 0 MB

Network Interfaces
------------------
br-e70a18428e21 : RX: 168 bytes, TX: 746 bytes (MAC: 5a:9e:42:3d:d0:71)
docker0 : RX: 0 bytes, TX: 0 bytes (MAC: 9a:a4:a2:9f:cc:14)
eth0 : RX: 4471564 bytes, TX: 1101720 bytes (MAC: 00:16:3e:07:39:7b)
lo : RX: 200 bytes, TX: 200 bytes (MAC: 00:00:00:00:00:00)
veth3987ef4 : RX: 126 bytes, TX: 1866 bytes (MAC: 6e:93:06:a7:e6:4b)
vethec1877b : RX: 126 bytes, TX: 1908 bytes (MAC: e6:91:8c:43:bb:60)

Enter fullscreen mode Exit fullscreen mode

and also local disk information:

xbill@penguin:~/iap-https-rust/stdio$ make disk
Disk Usage Report
=================

/ btrfs 79333 / 400384 MB used (19.8%)
/mnt/chromeos 9p 1 / 3805 MB used (0.0%)
/opt/google/cros-containers ext4 74 / 74 MB used (100.0%)
/usr/share/fonts/chromeos virtiofs 1928 / 2287 MB used (84.3%)
/dev/kmsg btrfs 79333 / 400384 MB used (19.8%)
/dev/kvm btrfs 79333 / 400384 MB used (19.8%)
/dev/snd/controlC0 btrfs 79333 / 400384 MB used (19.8%)
/dev/snd/pcmC0D0c btrfs 79333 / 400384 MB used (19.8%)
/dev/snd/pcmC0D0p btrfs 79333 / 400384 MB used (19.8%)
/dev/snd/seq btrfs 79333 / 400384 MB used (19.8%)
/dev/snd/timer btrfs 79333 / 400384 MB used (19.8%)
/dev/wl0 btrfs 79333 / 400384 MB used (19.8%)
/dev/fuse btrfs 79333 / 400384 MB used (19.8%)
/dev/net/tun btrfs 79333 / 400384 MB used (19.8%)
/var/lib/docker/overlay2/34ccc5bb1661a6ae2ba599e81915ec2db9771ff7aa4d7054b14adc2e0a58aab7/merged overlay 0 / 0 MB used (0.0%)
/var/lib/docker/overlay2/a81a6d9b5d9973892ed4727cd01e8138c470018c4e6f6dab22279c31e78d6f9d/merged overlay 0 / 0 MB used (0.0%)
Enter fullscreen mode Exit fullscreen mode

System Information with MCP STDIO Transport

One of the key features that the Rust rmcp SDK provides is abstracting various transport methods.

The high level tool MCP 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.

First- switch the directory with the Rust stdio sample code:

xbill@penguin:~/iap-https-rust/stdio$ make release
Building Release...
    Finished `release` profile [optimized] target(s) in 0.07s
xbill@penguin:~/iap-https-rust/stdio$
Enter fullscreen mode Exit fullscreen mode

You can validate the final result of the build by checking the compiled Rust binary:

xbill@penguin:~/iap-https-rust/stdio/target/release$ file sysutils-stdio-rust
sysutils-stdio-rust: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=49a2c7ea0b8b90647016b77de212409a5a85fb7a, stripped
xbill@penguin:~/iap-https-rust/stdio/target/release$ 
Enter fullscreen mode Exit fullscreen mode

Connecting Gemini CLI to the MCP STDIO Server

To configure Gemini CLI as the MCP client- a sample settings.json is provided in the .gemini config directory:

{
  "mcpServers": {
    "sysutils-stdio-rust": {
        "command": "$HOME/iap-https-rust/stdio/target/release/sysutils-stdio-rust",
        "args": ["--prebuilt","--stdio"],
        "env": {
          "RUST_LOG": "trace"
        }
      }
    }
  }
Enter fullscreen mode Exit fullscreen mode

The default setup uses the final compiled binary directly with Gemini CLI. It is also possible to trigger the Rust cargo command. A sample Gemini setup is provided for this scenario as well:

{
  "mcpServers": {
    "sysutils-stdio-rust": {
      "command": "cargo",
      "args": [
        "run",
        "--quiet",
        "--release"
      ],
      "env": {
        "RUST_LOG": "info,sysutils_stdio_rust=debug"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Next Gemini CLI is used to check the MCP connection settings:

 > /mcp list
Configured MCP servers:

🟢 sysutils-stdio-rust - Ready (2 tools)
  Tools:
  - disk_usage
  - local_system_info

Enter fullscreen mode Exit fullscreen mode

The local MCP Server (sysutils-stdio-rust) can now be used directly using Gemini CLI as a MCP client. This is the same Rust binary that was tested locally as a standalone build:

> run mcp tool local_system_info
✦ I will execute the local_system_info MCP tool to retrieve the system information.

╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ local_system_info (sysutils-stdio-rust MCP Server) {} │
│ │
│ System Information Report │
│ ========================= │
│ │
│ System Information │
│ --- │
│ System Name: Debian GNU/Linux │
│ Kernel Version: 6.6.99-09121-g16665fbb817c │
│ OS Version: 12 │
│ Host Name: penguin │
│ │
│ CPU Information │
│ --- │
│ Number of Cores: 16 │
│ │
│ Memory Information │
│ --- │
│ Total Memory: 6364 MB │
│ Used Memory: 640 MB │
│ Total Swap: 0 MB │
│ Used Swap: 0 MB │
│ │
│ Network Interfaces │
│ --- │
│ br-e70a18428e21 : RX: 168 bytes, TX: 746 bytes (MAC: 5a:9e:42:3d:d0:71) │
│ docker0 : RX: 0 bytes, TX: 0 bytes (MAC: 9a:a4:a2:9f:cc:14) │
│ vethec1877b : RX: 126 bytes, TX: 1908 bytes (MAC: e6:91:8c:43:bb:60) │
│ veth3987ef4 : RX: 126 bytes, TX: 1866 bytes (MAC: 6e:93:06:a7:e6:4b) │
│ eth0 : RX: 5103921 bytes, TX: 7929688 bytes (MAC: 00:16:3e:07:39:7b) │
│ lo : RX: 200 bytes, TX: 200 bytes (MAC: 00:00:00:00:00:00) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The local_system_info tool has been executed, providing a detailed report of the system's current state,
  including CPU, memory, and network interface details.
Enter fullscreen mode Exit fullscreen mode

Project Package Details

The stdio project has been published to crates.io:

crates.io: Rust Package Registry

Summary

The potential for using Rust for MCP development with Gemini CLI was validated with a incremental step by step approach.

A minimal stdio transport MCP Server was built from Rust source code and validated with Gemini CLI running as a MCP client in the same local environment.

This approach can be extended to more complex deployments using other MCP transports and Cloud based options.

Top comments (0)