Leveraging Gemini CLI and the underlying Gemini LLM to build Model Context Protocol (MCP) AI applications in Fortran 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.
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 Sorcery 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-stdio-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.
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.
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:
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 stdio 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 extended with Gemini CLI to add several new tools in standard code.
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
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:
! Ensure stdout is unbuffered
call set_stdout_unbuffered()
! Initialize Server
server_ptr = mcpc_server_new_iostrm(get_stdin(), get_stdout())
if (.not. c_associated(server_ptr)) then
write(error_unit,*) "Failed to create server"
error stop 1
end if
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
Installing and Running the Code
Run the install make release target on the local system:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-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-stdio-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-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-stdio-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-fortran/mcpc'
gfortran -o firestore-server server.o c_helpers.o firestore_client.o mcpc/src/libmcpc.a
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-fortran$
To lint the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-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
To test the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-fortran$ make test
python3 test_server_fortran.py
Testing 'greet' tool...
โ initialize successful
โ notifications/initialized sent
โ tools/list successful (all tools found)
โ tools/call (greet) successful
Testing 'get_root' tool...
โ tools/call (get_root) successful
Testing 'check_db' tool...
โ tools/call (check_db) successful: Database running: true (Project: comglitn)
All tests passed!
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-stdio-fortran": {
"command": "$HOME/gemini-cli-codeassist/firestore-stdio-fortran/firestore-server"
}
}
}
Validation with Gemini CLI
Finally- Gemini CLI is restarted and the MCP connection over stdio to the Fortran Code is validated, The full Gemini CLI Session will start:
> /mcp list
Configured MCP servers:
๐ข firestore-stdio-fortran - Ready (8 tools)
Tools:
- check_db
- get_product_by_id
- get_products
- get_root
- greet
- mcpc-info
- reset
- seed
> suggest some dishes with veggies from inventory
โฆ Based on the vegetables and other ingredients currently in your inventory, here are some dish suggestions:
Available Vegetables:
* Broccoli (402 in stock)
* Fresh Basil (25 in stock)
* Black Beans (170 in stock)
Dish Ideas:
1. Beef and Broccoli Stir-Fry
* Ingredients: Beef, Broccoli, Jasmine Rice (or regular Rice).
* Description: A classic savory dish using your fresh broccoli and beef supply over a bed of rice.
2. Creamy Broccoli Cheddar Soup
* Ingredients: Broccoli, Cheddar Cheese, Milk.
* Description: A warm, comfort food perfect for using up that large stock of broccoli and cheese.
3. Chicken with Basil & Broccoli
* Ingredients: Whole Chicken, Fresh Basil, Broccoli.
* Description: Roast the chicken with fresh basil and serve with steamed broccoli on the side.
4. Black Bean & Rice Bowl
* Ingredients: Black Beans, Rice, Cheddar Cheese (optional: add yogurt as a sour cream substitute).
* Description: A simple, protein-packed vegetarian meal.
5. Garlic Butter Shrimp & Broccoli
* Ingredients: Shrimp, Broccoli, Butter (substitute with Oil if needed, or use Milk/Cheese for a creamy sauce).
* Description: Quick and healthy seafood dinner.
Extending the MCP Server with Gemini CLI
Now that the basic MCP functionality has been tested and verified with Gemini CLI- the server code can be extended with new MCP tools:
> with the Fortran project add new MCP tools -
one that calculates optimum meals based on inventory
and that suggests high protein ingredients
Gemini CLI will get right to work:
โฆ I will begin by exploring the codebase, specifically server.f90 and firestore_client.c, to understand how to register new tools and handle
data fetching and processing.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ ReadFile server.f90 โ
โ โ
โ โ
โ โ ReadFile firestore_client.c โ
โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ โ โ
Once completedโโโthe changes will be summarized:
โฆ I have successfully implemented the new MCP tools.
Summary of Changes:
1. `firestore_client.c`: Added two new implementation functions:
* impl_suggest_meals: Fetches the inventory and suggests meals based on available ingredients (e.g., "Beef and Broccoli Stir Fry" if
Beef, Broccoli, and Rice are present).
* impl_get_high_protein: Fetches the inventory and filters for items matching high-protein keywords (Beef, Chicken, Shrimp, Beans, Eggs,
etc.), returning a JSON list.
* Fixed a string literal syntax error and added necessary includes for string manipulation.
2. `firestore_client.h`: Exposed the new C functions.
3. `server.f90`:
* Added interface bindings for the new C functions.
* Registered the new tools suggest_meals and get_high_protein with the MCP server.
* Implemented the Fortran callbacks to delegate to the C implementations.
4. `test_server_fortran.py`: Updated the integration tests to verify the presence and functionality of the new tools.
Verification:
Ran make firestore-server to rebuild and python3 test_server_fortran.py to verify all tools, including the new ones, are working correctly.
You can now use these tools via the MCP interface:
* suggest_meals: Returns a text list of meal ideas.
* get_high_protein: Returns a JSON list of high-protein products.
Restart Gemini CLI and check for the new MCP tools:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-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-stdio-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-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-stdio-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-fortran/mcpc'
gfortran -o firestore-server server.o c_helpers.o firestore_client.o mcpc/src/libmcpc.a
And list the new MCP Tools:
> /mcp list
Configured MCP servers:
๐ข firestore-stdio-fortran - Ready (10 tools)
Tools:
- check_db
- get_high_protein
- get_product_by_id
- get_products
- get_root
- greet
- mcpc-info
- reset
- seed
- suggest_meals
to run the tools:
> suggest_meals
โฆ The suggest_meals tool returned the following suggestions based on the current inventory:
- Beef and Broccoli Stir Fry (Beef, Broccoli, Rice)
- Chicken with Fresh Basil (Chicken, Fresh Basil)
- Cheesy Omelette (Eggs, Cheese)
- Garlic Shrimp with Broccoli (Shrimp, Broccoli)
- Egg Fried Rice (Rice, Eggs)
โฆ The get_high_protein tool returned the following high-protein ingredients from the inventory:
- Shrimp ($8.00, Quantity: 244)
- Coffee Beans ($4.00, Quantity: 430)
- Yogurt ($5.00, Quantity: 243)
- Beef ($7.00, Quantity: 91)
- Eggs ($9.00, Quantity: 21)
- Whole Chicken ($5.00, Quantity: 434)
- Black Beans ($7.00, Quantity: 170)
- Maple Almond Butter ($7.00, Quantity: 52)
- Walnuts ($7.00, Quantity: 417)
Summary
The strategy for using Fortran with MCP development with Gemini CLI was validated with an incremental step by step approach.
A minimal stdio transport MCP Server was started from Fortran 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 code with several MCP tools and use these tools inside the context for the underlying LLM.


Top comments (0)