DEV Community

xbill for Google Developer Experts

Posted on • Originally published at xbill999.Medium on

MCP Development with Fortran, Firestore, Cloud Run, and Gemini CLI

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

Fortran? Should I maybe bring an Abacus too?

The goal of this article is to provide a minimal viable basic working MCP stdio server in Fortran that can be run locally without any unneeded extra code or extensions.

So how does this Black Magic work?

It is theoretically possible to implement the stack needed with only valid Fortran language constructs. The key to this solution is that modern compiled Fortran supports importing bindings from C during the compilation process.

A fully functional C MCP library is here:

GitHub - micl2e2/mcpc: Cross-platform C SDK for Model Context Protocol (MCP), in modern🚀 C23.

What Is Fortran?

Fortran (FORmula TRANslation) is a high-level, compiled programming language, the first of its kind, created by IBM in the 1950s for scientific and engineering applications, known for its exceptional performance in numerical, computational, and high-performance computing (HPC) tasks like weather modeling, fluid dynamics, and physics simulations, with modern versions supporting parallel processing and object-oriented features while remaining relevant due to its speed and specialized libraries.

The main Fortran site is here:

The Fortran Programming Language - Fortran Programming Language

Installing Fortran

The step by step instructions vary by platform- for a basic Debian system here are the steps:

sudo apt update
sudo apt install build-essential
sudo apt install gcc
sudo apt install gfortran
gfortran --version
xbill@penguin:~/gemini-cli-codeassist/mcp-https-fortran$ gfortran --version
GNU Fortran (Debian 12.2.0-14+deb12u1) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Enter fullscreen mode Exit fullscreen mode

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 | Firebase

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
Enter fullscreen mode Exit fullscreen mode

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

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:

GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

C MCP Documentation

The official MCP C page provides samples and documentation for getting started:

GitHub - micl2e2/mcpc: Cross-platform C SDK for Model Context Protocol (MCP), in modern🚀 C23.

This C library is built and the low level bindings can be used from Fortran code.

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 Fortran MCP Server is built with HTTP transport using the C library bindings. 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 to Google Cloud Run. This remote endpoints is validated with the local copy of Gemini CLI.

Setup the Basic Environment

At this point you should have a working Fortran and C 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
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 gemini-cli-codeassist
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 gemini-cli-codeassist
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.

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 be in the same environment or distributed over the Internet.

The connection over HTTP will look similar to this:

! Initialize Server (TCP)
    server_ptr = mcpc_server_new_tcp()
    if (.not. c_associated(server_ptr)) then
        write(error_unit,*) "Failed to create server"
        error stop 1
    end if
Enter fullscreen mode Exit fullscreen mode

Package Information

The code depends on several standard libraries for MCP and logging:

program server
    use iso_c_binding
    use iso_fortran_env, only: error_unit
    use mcpc_interface
    implicit none
Enter fullscreen mode Exit fullscreen mode

Installing and Running the Code

Run the install make release target on the local system:

makexbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ make
gfortran -O2 -c server.f90
cc -std=c17 -Wall -Wextra -Imcpc -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c c_helpers.c
cc -std=c17 -Wall -Wextra -Imcpc -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c firestore_client.c
make -C mcpc 
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/firestore-https-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/firestore-https-fortran/mcpc/src'
cc -Dis_unix -std=c17 -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter -Wno-unused-label -Wno-error=unused-variable -Wno-error=unused-but-set-variable -O2 -Os -I.. -fPIC alloc.c log.c errcode.c anydata.c tool.c rsc.c prmpt.c server.c retbuf.c ucbr.c complt.c serlz.c mjson.c -c 
ar rcs libmcpc.a alloc.o log.o errcode.o anydata.o tool.o rsc.o prmpt.o server.o retbuf.o ucbr.o complt.o serlz.o mjson.o 
cc -s -o libmcpc.so alloc.o log.o errcode.o anydata.o tool.o rsc.o prmpt.o server.o retbuf.o ucbr.o complt.o serlz.o mjson.o -shared ../src/libmcpc.a 
make[2]: Leaving directory '/home/xbill/gemini-cli-codeassist/firestore-https-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/firestore-https-fortran/mcpc'
gfortran -o firestore-https-server server.o c_helpers.o firestore_client.o mcpc/src/libmcpc.a -lpthread
Enter fullscreen mode Exit fullscreen mode

To lint the code:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ make lint
cc -std=c17 -Wall -Wextra -Imcpc -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -Wpedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -fsyntax-only c_helpers.c firestore_client.c
Enter fullscreen mode Exit fullscreen mode

To test the code:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ make test
python3 test_server_fortran.py
Starting server on port 8080...
Sending initialize...
✓ initialize successful
Sending initialized notification...
✓ notifications/initialized sent
Sending tools/list...
✓ tools/list successful (all tools found)
Testing 'greet' tool...
✓ tools/call (greet) successful
Testing 'check_db' tool...
✓ tools/call (check_db) successful: Database running: true (Project: comglitn)
Testing 'get_root' tool...
✓ tools/call (get_root) successful
Testing 'get_products' tool...
✓ tools/call (get_products) successful: Found 38 products

All tests passed!
Enter fullscreen mode Exit fullscreen mode

Gemini CLI settings.json

In this example — the 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-fortran": {
      "httpUrl": "http://127.0.0.1:8080"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Start the Local HTTP server

Open a new terminal window, and start the Local copy of the the HTTP MCP server:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ ./firestore-https-server 
{"asctime": "2026-01-29T15:20:17Z", "name": "root", "levelname": "INFO", "message": "Server starting..."}
Enter fullscreen mode Exit fullscreen mode

Validation with Gemini CLI

Next- leave the local server running, and open a new terminal window. Start Gemini CLI and the MCP connection over local HTTP to the Fortran Code is validated. The full Gemini CLI Session will start:

> /mcp list

Configured MCP servers:

🟢 firestore-https-fortran - Ready (8 tools)
  Tools:
  - check_db
  - get_product_by_id
  - get_products
  - get_root
  - greet
  - mcpc-info
  - reset
  - seed

> check_db

╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ check_db (firestore-https-fortran MCP Server) {} │
│ │
│ Database running: true (Project: comglitn) │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The database is running (Project: comglitn).

> get_products

✦ I've retrieved 38 products from the inventory. Here's a partial list:

   - Milk (ID: 27aSqcUEAeUxAjKcPnv3): $6.00, 128 in stock
   - Watermelon (ID: 2OSaK9NJHh1LzJ2hv3zh): $10.00, 262 in stock
   - Acai Smoothie Packs (ID: 2Q9K1iqQ7I8kAf93BDWZ): $4.00, 10 in stock
   - Sunflower Seeds (ID: 4ndC77SxseGRRXyKq63Z): $4.00, 350 in stock
   - Fresh Basil (ID: 6ZW5yHZiySMphtB1DDva): $3.00, 25 in stock
   - ... and 33 more products.

Enter fullscreen mode Exit fullscreen mode

The MCP tools can be used in the current Gemini CLI context:

> suggest a 1950's style dinner that a Fortran programmer might have come home to based on current inventory

✦ Based on the inventory and a 1950s culinary aesthetic (the decade FORTRAN was born), here is a suggested "Systematic Supper" for a weary
  programmer:

  The 1957 "Mainframe" Menu

   * Main Course: Roast Beef or Whole Chicken – Simple, hearty, and reliable, much like a well-structured DO loop.
   * Vegetable: Steamed Broccoli topped with melted Cheddar Cheese – A classic Mid-Century side that provides the necessary "input" for a long
     night of debugging.
   * Side: Jasmine Rice – While perhaps a bit exotic for the 50s, it serves as an excellent foundation for your main course.
   * Salad: Waldorf-style Apple & Walnut Salad – Using your fresh Apples and Walnuts, this was a high-society favorite of the era.
   * Dessert: Mint Chocolate Cookies served with a tall glass of cold Milk – The perfect "END" statement to your meal.
   * Beverage: A chilled Cola or a steaming cup of Coffee to keep the logic flowing.

  Enjoy your dinner! Just be careful not to spill any Apple Juice on your punch cards.

Enter fullscreen mode Exit fullscreen mode

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-fortran
Enter fullscreen mode Exit fullscreen mode

Deploy the project to Google Cloud Run with the pre-built cloudbuild.yaml and Dockerfile:

cd ~/gemini-cli-codeassist/firestore-https-fortran
xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ make deploy
Enter fullscreen mode Exit fullscreen mode

The Cloud Build will start:

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-fortran] 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-fortran] revision [firestore-https-fortran-00005-g8r] has been deployed and is serving 100 percent of traffic.
Step #1: Service URL: https://firestore-https-fortran-1056842563084.us-central1.run.app
Finished Step #1
Enter fullscreen mode Exit fullscreen mode

The cloud build needs to pull in all the Fortran and C libraries in the build environment and generate the entire package from scratch.

When the build is complete- an endpoint will be returned.

The service endpoint in this example is :

https://firestore-https-fortran-1056842563084.us-central1.run.app
Enter fullscreen mode Exit fullscreen mode

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-fortran-1056842563084.us-central1.run.app
Enter fullscreen mode Exit fullscreen mode

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","error":{"code":-32700,"message":""}}
Enter fullscreen mode Exit fullscreen mode

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-fortran": {
       "httpUrl": "https://firestore-https-fortran-$PROJECT_NUMBER.us-central1.run.app"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Copy the Cloud Run version of the Gemini CLI configuration file:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran$ cd .gemini
cp settings.json.cloudrun settings.json
xbill@penguin:~/gemini-cli-codeassist/firestore-https-fortran/.gemini$
Enter fullscreen mode Exit fullscreen mode

Validation with Gemini CLI

The final connection test uses Gemini CLI as a MCP client with the deployed Cloud Run Service in C providing the MCP server. Startup Gemini CLI with the updated settings :

> /mcp list

Configured MCP servers:

🟢 firestore-cloudrun-fortran - Ready (8 tools)
  Tools:
  - check_db
  - get_product_by_id
  - get_products
  - get_root
  - greet
  - mcpc-info
  - reset
  - seed
Enter fullscreen mode Exit fullscreen mode

You can also use the remote MCP server in the local Gemini CLI context:

> based on my current inventory what would be a power lunch at my 1950's mainframe shop

✦ For a high-stakes afternoon of debugging punch cards at your 1950s mainframe shop, here is the ultimate "Batch Processor's Power Lunch"
  sourced directly from your current inventory:

   * The Mainframe Sandwich: Slices of savory Beef and Cheddar Cheese piled high on Whole Wheat Bread.
   * The "Clean Room" Side: A serving of Broccoli (essential vitamins for staring at vacuum tubes).
   * Fuel for the Night Shift: A steaming cup of joe brewed from your Coffee Beans, or a refreshing Cola if you need that sugar rush.
   * The Operator's Reward: The absolute last Mint Chocolate Cookie in the building (Inventory: 1)—strictly reserved for whoever successfully
     clears the paper jam in the card reader.
   * The "Thinking" Snack: A pocketful of Sunflower Seeds to keep you occupied while the computer spends 45 minutes calculating a single
     trajectory.

  Warning: Please keep all Cola and Beef crumbs away from the magnetic tape drives!

Enter fullscreen mode Exit fullscreen mode

Final Code Review

Gemini CLI was used to review the project:

> what does this project do?

✦ I will read the README.md file to provide a comprehensive summary of the project's purpose and functionality.
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ ReadFile README.md │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ This project is a Model Context Protocol (MCP) server written in Fortran that acts as an inventory management system for Google Cloud
  Firestore.

  Key aspects of the project include:

   * Technology Stack: It uses Fortran (2008+) with iso_c_binding to interface with a C library called mcpc. It also uses C for Firestore HTTP
     interactions via curl and gcloud.
   * Purpose: It allows LLMs (via an MCP client) to interact with a product inventory database.
   * Core Functionality:
       * Inventory Management: Tools to list all products, find products by ID, seed the database with sample data, and reset (clear) the
         inventory.
       * Connectivity: It exposes an MCP endpoint over HTTP/SSE, making it deployable to services like Google Cloud Run.
       * Integration: It leverages the gcloud CLI for authentication and project identification.
   * Logging: It generates structured JSON logs to stderr for monitoring and debugging.

Enter fullscreen mode Exit fullscreen mode

Summary

MCP development with Fortran 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 Fortran MCP server hosted in Google Cloud Run.

This approach can be extended to more complex deployments and Cloud based options.

Top comments (0)