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.
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/mcp-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
make -C mcpc
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-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/mcp-stdio-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
gfortran -o server-fortran server.o c_helpers.o mcpc/src/libmcpc.a
xbill@penguin:~/gemini-cli-codeassist/mcp-stdio-fortran$
To lint the code:
xbill@penguin:~/gemini-cli-codeassist/mcp-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
xbill@penguin:~/gemini-cli-codeassist/mcp-stdio-fortran$
To test the code:
xbill@penguin:~/gemini-cli-codeassist/mcp-stdio-fortran$ make test
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
make -C mcpc
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-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/mcp-stdio-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
gfortran -o server-fortran server.o c_helpers.o mcpc/src/libmcpc.a
python3 test_server_fortran.py
Testing 'greet' tool...
โ initialize successful
โ notifications/initialized sent
โ tools/list successful
โ tools/call (greet) successful
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": {
"mcp-stdio-fortran": {
"command": "$HOME/gemini-cli-codeassist/mcp-stdio-fortran/server-fortran"
}
}
}
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:
๐ข mcp-stdio-fortran - Ready (2 tools)
Tools:
- greet
- mcpc-info
> greet Mr Backus
โฆ I will greet Mr Backus using the greet tool.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ ? greet (mcp-stdio-fortran MCP Server) {"param":"Mr Backus"} โ โ
โ โ
โ MCP Server: mcp-stdio-fortran โ
โ Tool: greet โ
โ โ
โ Allow execution of MCP tool "greet" from server "mcp-stdio-fortran"? โ
โ โ
โ 1. Allow once โ
โ 2. Allow tool for this session โ
โ โ 3. Allow all server tools for this session โ
โ 4. No, suggest changes (esc) โ
โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏโญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ greet (mcp-stdio-fortran MCP Server) {"param":"Mr Backus"} โ
โ โ
โ Hello, Mr Backus! โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ The greeting has been delivered: Hello, Mr Backus!
> greet Fortran and print the result in Backus Naur form
โฆ I will greet Fortran and format the output in Backus-Naur Form.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ greet (mcp-stdio-fortran MCP Server) {"param":"Fortran"} โ
โ โ
โ Hello, Fortran! โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ
1 <greeting> ::= "Hello, Fortran!"
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 gets detailled Fortran system information,
one that gets server information
and one that calculates x digits of pi
Gemini CLI will get right to work:
โ 279 + character(kind=c_char) :: c_info_str(256) โ
โ 280 + integer :: pid, i โ
โ 281 + โ
โ 282 + call log_info_c(c_char_"Executed server_info tool" // c_null_char) โ
โ 283 + โ
โ 284 + pid = get_pid() โ
โ 285 + write(info_str, '(A, I0)') "Server: mcp-stdio-fortran v0.1 | PID: ", pid โ
โ 286 + โ โ โ
Once completedโโโthe changes will be summarized:
โฆ I have successfully added the three requested MCP tools to the Fortran server.
Summary of Changes:
1. Modified `c_helpers.c`:
* Added helper_add_text_raw: A generic helper to send text results without the "Hello, " prefix.
* Added get_pid: A helper to retrieve the server's process ID.
2. Modified `server.f90`:
* Updated `mcpc_interface`: Added bindings for helper_add_text_raw and get_pid.
* Registered New Tools: Added registration logic in setup_tools for:
* system_info: Fetches compiler version (and attempts options).
* server_info: Returns server name and PID.
* calculate_pi: Calculates Pi to N digits using a spigot algorithm.
* Implemented Callbacks:
* system_info_cb: Uses iso_fortran_env to get compiler details. gracefully handles buffer limits by falling back to version-only
output.
* server_info_cb: Formats server name and PID.
* calculate_pi_cb: Implements a spigot algorithm to stream Pi digits into a string buffer.
3. Verification:
* Created test_new_tools.py which verifies all three new tools.
* Ran existing test_server_fortran.py to ensure no regressions.
* All tests passed.
Restart Gemini CLI and check for the new MCP tools:
xbill@penguin:~/gemini-cli-codeassist/mcp-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
make -C mcpc
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
make[2]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-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/mcp-stdio-fortran/mcpc/src'
make[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-fortran/mcpc'
gfortran -o server-fortran server.o c_helpers.o mcpc/src/libmcpc.a
xbill@penguin:~/gemini-cli-codeassist/mcp-stdio-fortran$
And list the new MCP Tools:
> /mcp list
Configured MCP servers:
๐ข mcp-stdio-fortran - Ready (5 tools)
Tools:
- calculate_pi
- greet
- mcpc-info
- server_info
- system_info
to run the tools:
> server_info
โฆ I will retrieve the server information for you.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ server_info (mcp-stdio-fortran MCP Server) {} โ
โ โ
โ Server: mcp-stdio-fortran v0.1 | PID: 2944 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ I have retrieved the server information. Please let me know your first command.
> system_info
โฆ I will retrieve the detailed Fortran system information for you.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ system_info (mcp-stdio-fortran MCP Server) {} โ
โ โ
โ Compiler Version: GCC version 12.2.0 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ I have retrieved the compiler version. Please let me know your next command.
> calculate_pi 42
โฆ I will calculate 42 digits of pi for you.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ calculate_pi (mcp-stdio-fortran MCP Server) {"digits":"42"} โ
โ โ
โ 3.14159265358979323846264338327950288419716 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ I have calculated 42 digits of pi: 3.14159265358979323846264338327950288419716. Please let me know if there's anything else I can help you
with!
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)