Leveraging Gemini CLI and the underlying Gemini LLM to build Model Context Protocol (MCP) AI applications in COBOL 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.
COBOL? For AI? What century are you from, Time Traveler?
The goal of this article is to provide a minimal viable basic working MCP stdio server in COBOL 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 COBOL language constructs. The key to this solution is that modern compiled COBOL 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 COBOL?
COBOL (COmmon Business-Oriented Language) is a high-level, English-like programming language developed in the 1950s, designed for business, finance, and administrative systems, especially on mainframe computers, handling large-scale data processing. Despite its age, it remains crucial for critical infrastructure like banking and government systems, powering applications that process massive transaction volumes, due to its reliability and self-documenting syntax.
There are some legacy sites at IBM, but for the modern compiler version- here is the GNU COBOL site:
Installing COBOL
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 gnucobol4
cobc -V
xbill@penguin:~$ cobc -V
cobc (GnuCOBOL) 4.0-early-dev.0
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built Sep 19 2022 04:28:17
Packaged Jun 06 2020 20:56:36 UTC
C version "12.2.0"
xbill@penguin:~$
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 COBOL code.
Where do I start?
The strategy for starting MCP development with COBOL 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 COBOL 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 COBOL 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:
CALL "mcpc_server_new_iostrm" USING BY VALUE stdin-ptr
BY VALUE stdout-ptr
RETURNING server-ptr.
Package Information
The code depends on several standard libraries for MCP and logging:
>>SOURCE FORMAT FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. server.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
DATA DIVISION.
Installing and Running the Code
Run the install make release target on the local system:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-cobol$ make
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c c_helpers.c
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c cob_helpers.c
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c firestore_client.c
make -C mcpc/src
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-cobol/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[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/firestore-stdio-cobol/mcpc/src'
cobc -x -free -o firestore-server server.cob c_helpers.o cob_helpers.o firestore_client.o mcpc/src/libmcpc.a -lm
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-cobol$
To lint the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-cobol$ make lint
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -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
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -Wpedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -fsyntax-only cob_helpers.c
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -Wpedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -fsyntax-only firestore_client.c
To test the code:
xbill@penguin:~/gemini-cli-codeassist/firestore-stdio-cobol$ make test
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c cob_helpers.c
cobc -x -free -o firestore-server server.cob c_helpers.o cob_helpers.o firestore_client.o mcpc/src/libmcpc.a -lm
python3 test_server_cobol.py
Testing Firestore COBOL Server...
โ initialize successful
โ notifications/initialized sent
โ tools/list successful
โ greet successful
โ check_db successful: Database running: true (Project: comglitn)
โ get_root successful
โ get_products successful (got 4767 bytes)
Seeding database...
โ seed successful
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-cobol": {
"command": "$HOME/gemini-cli-codeassist/firestore-stdio-cobol/firestore-server"
}
}
}
Validation with Gemini CLI
Finally- Gemini CLI is restarted and the MCP connection over stdio to the COBOL Code is validated. The full Gemini CLI Session will start:
> /mcp list
Configured MCP servers:
๐ข firestore-stdio-cobol - Ready (8 tools)
Tools:
- check_db
- get_product_by_id
- get_products
- get_root
- greet
- mcpc-info
- reset
- seed
> suggest a lunch with current inventory that I can eat in the 1960's cafeteria
โฆ Based on the current inventory, here is a quintessential 1960s-style "Blue Plate Special" for your cafeteria lunch:
* Main: Roast Beef Sandwich on Whole Wheat Bread.
* Side: A portion of steamed Broccoli.
* Fruit: A crisp Red Apple or a slice of fresh Watermelon.
* Beverage: A cold glass of Milk or a classic Cola.
Enjoy your vintage meal!
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 @server.cob add a new tool exposed by
MCP that generates a sample inventory report
Gemini CLI will get right to work:
โ 186 + *> Tool: generate_report โ
โ 187 + CALL "mcpc_tool_new2" USING BY REFERENCE tool-name-report โ
โ 188 + BY REFERENCE tool-desc-report โ
โ 189 + RETURNING tool-ptr. โ
โ 190 + IF tool-ptr NOT = NULL THEN โ
โ 191 + SET cb-ptr TO ENTRY "generate_report_cb_wrapper" โ
โ 192 + CALL "mcpc_tool_set_call_cb" USING BY VALUE tool-ptr โ
โ 193 + BY VALUE cb-ptr โ
โ 194 + CALL "mcpc_server_add_tool" USING BY VALUE server-ptr โ
โ 195 + BY VALUE tool-ptr โ
โ 196 + END-IF. โ
โ 197 + โ โ โ
Once completedโโโthe changes will be summarized:
โฆ I've added the generate_report tool to the COBOL-based MCP server. This tool fetches the current inventory from Firestore and formats it into
a human-readable text table.
Restart Gemini CLI and check for the new MCP tool:
xbill@penguin:~/gemini-cli-codeassist/mcp-stdio-cobol$ make
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c c_helpers.c
cc -std=c17 -Wall -Wextra -Imcpc/src -Imcpc -Dis_unix -DMCPC_C23PTCH_KW1 -DMCPC_C23PTCH_CKD1 -DMCPC_C23PTCH_UCHAR1 -DMCPC_C23GIVUP_FIXENUM -O2 -c cob_helpers.c
make -C mcpc/src
make[1]: Entering directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-cobol/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[1]: Leaving directory '/home/xbill/gemini-cli-codeassist/mcp-stdio-cobol/mcpc/src'
cobc -x -free -o server-cobol server.cob c_helpers.o cob_helpers.o mcpc/src/libmcpc.a -lm
And run the new MCP tool:
> /mcp list
Configured MCP servers:
๐ข firestore-stdio-cobol - Ready (9 tools)
Tools:
- check_db
- generate_report
- get_product_by_id
- get_products
- get_root
- greet
- mcpc-info
- reset
- seed
This information can then be used inside the Gemini CLI context:
> generate_report
โฆ I will now generate a summary report of the current inventory.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ generate_report (firestore-stdio-cobol MCP Server) {} โ
โ โ
โ INVENTORY REPORT โ
โ ================ โ
โ Name | Price | Quantity โ
โ --- โ
โ Milk | 6.00 | 128 โ
โ Watermelon | 10.00 | 262 โ
โ Acai Smoothie Packs | 4.00 | 10 โ
โ Sunflower Seeds | 4.00 | 350 โ
โ Apples | 2 | 100 โ
โ Fresh Basil | 3.00 | 25 โ
โ Apples | 9.00 | 103 โ
โ Apples | 2 | 100 โ
โ Bananas | 4 | 100 โ
โ Jasmine Rice | 2.00 | 126 โ
โ Whole Wheat Bread | 7.00 | 437 โ
โ Broccoli | 6.00 | 402 โ
โ Shrimp | 8.00 | 244 โ
โ Coffee Beans | 4.00 | 430 โ
โ Mint Chocolate Cookies | 3.00 | 1 โ
โ Cheese | 12 | 100 โ
โ Cola | 7.00 | 362 โ
โ Cinnamon | 6.00 | 178 โ
โ Yogurt | 5.00 | 243 โ
โ Beef | 7.00 | 91 โ
โ White Chocolate Caramel Corn | 7.00 | 69 โ
โ Eggs | 9.00 | 21 โ
โ Bananas | 4 | 100 โ
โ Apples | 2 | 100 โ
โ Smores Cereal | 6.00 | 31 โ
โ Peanut Butter and Jelly Cups | 9.00 | 21 โ
โ Cheddar Cheese | 1.00 | 289 โ
โ Bottled Water | 9.00 | 187 โ
โ Whole Chicken | 5.00 | 434 โ
โ Cheese | 12 | 100 โ
โ Apple Juice | 1.00 | 187 โ
โ Parmesan Crisps | 6.00 | 79 โ
โ Bananas | 4 | 100 โ
โ Cheese | 12 | 100 โ
โ Green Tea | 9.00 | 442 โ
โ Black Beans | 7.00 | 170 โ
โ Rice | 7.00 | 314 โ
โ Maple Almond Butter | 7.00 | 52 โ
โ Bananas | 8.00 | 51 โ
โ Walnuts | 7.00 | 417 โ
โ Pineapple Kombucha | 1.00 | 58 โ
โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ The inventory report has been generated, showing a variety of products including Milk, Watermelon, and multiple entries for Apples, Bananas,
and Cheese.
> what items should I re-order?
โฆ Based on the inventory report, you should consider re-ordering the following items that are low in stock (quantity 25 or less):
* Mint Chocolate Cookies: 1 remaining
* Acai Smoothie Packs: 10 remaining
* Eggs: 21 remaining
* Peanut Butter and Jelly Cups: 21 remaining
* Fresh Basil: 25 remaining
and finally the key COBOL question:
> can I get my CICS on route 66?
โฆ I'll search the codebase for any references to "CICS" or "Route 66" to see if they're relevant to this project.
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ SearchText 'CICS|Route 66' within ./ โ
โ โ
โ No matches found โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฆ While "Route 66" is a classic, and CICS is the backbone of many COBOL
systems, this particular project is a modern Firestore MCP server
running on GnuCOBOL. It's more "Cloud Native" than "Mainframe Classic,"
so you won't find any CICS regions here!
Summary
The strategy for using COBOL with MCP development with Gemini CLI was validated with an incremental step by step approach.
A minimal stdio transport MCP Server was started from COBOL 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 a new MCP tool and use the tool inside the context for the underlying LLM.


Top comments (0)