DEV Community

xbill for Google Developer Experts

Posted on • Originally published at Medium on

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

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

Swift? Isn’t that just IOS Only?

The goal of this article is to provide a minimal viable basic working MCP server in Swift that can be run locally without any unneeded extra code or extensions. Swift can target Linux systems and as an official MCP SDK.

The official Swift MCP SDK is here:

GitHub - modelcontextprotocol/swift-sdk: The official Swift SDK for Model Context Protocol servers and clients.

What Is Swift?

Swift is a powerful, general-purpose programming language developed by Apple Inc. and the open-source community.

Key Aspects:

  • Purpose: It is designed for building apps for Apple platforms (iOS, macOS, watchOS, tvOS) and is also available for Linux and Windows.
  • Features: Swift is known for its speed, modern features, and focus on safety, which helps developers write more reliable code by eliminating common programming errors.
  • Modern alternative: It was introduced as a successor to the older Objective-C language and is used with frameworks like SwiftUI for building user interfaces

The full details of the language can be found here:

Swift.org

Installing Swift

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

curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz && \
tar zxf swiftly-$(uname -m).tar.gz && \
./swiftly init --quiet-shell-followup && \
. "${SWIFTLY_HOME_DIR:-$HOME/.local/share/swiftly}/env.sh" && \
hash -r
Enter fullscreen mode Exit fullscreen mode

You can validate the installation with the — version command:

xbill@penguin:~$ swift --version
Swift version 6.2.3 (swift-6.2.3-RELEASE)
Target: x86_64-unknown-linux-gnu
xbill@penguin:~$
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

Swift MCP Documentation

The official MCP GitHub Repo provides samples and documentation for getting started:

GitHub - modelcontextprotocol/swift-sdk: The official Swift SDK for Model Context Protocol servers and clients.

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 Swift MCP Server is built with HTTP transport. 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 Swift 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 configuration is validated with the local Gemini CLI installation.

Setup the Basic Environment

At this point you should have a working Swift 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 client and server to be on the same host or distributed over the Internet.

The connection over HTTP will look similar to this:

func createSession(logger: Logger) -> SSEServerTransport {
    let id = UUID().uuidString
    let transport = SSEServerTransport(sessionId: id, logger: logger)
    sessions[id] = transport
    return transport
  }
Enter fullscreen mode Exit fullscreen mode

Swift Package Information

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

name: "firestore-https-swift",
    platforms: [
        .macOS(.v13),
    ],
    dependencies: [
        .package(url: "https://github.com/modelcontextprotocol/swift-sdk", from: "0.10.2"),
        .package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.0"),
        .package(url: "https://github.com/apple/swift-log.git", from: "1.6.0"),
        .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
    ],
Enter fullscreen mode Exit fullscreen mode

Installing and Running the Swift Code

Run the install make release target on the local system:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make release
Building in release mode...
[1/1] Planning build
Building for production...
[1/1] Write swift-version-6FE34157E209550C.txt
Build complete! (3.96s)
xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$
Enter fullscreen mode Exit fullscreen mode

To format/check the code:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make lint
Linting code...
Enter fullscreen mode Exit fullscreen mode

To test the code:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make test
Running tests...
[1/1] Planning build
Building for debugging...
[1/1] Write swift-version-6FE34157E209550C.txt
Build complete! (4.04s)
Test Suite 'All tests' started at 2026-01-11 12:19:39.959
Test Suite 'debug.xctest' started at 2026-01-11 12:19:39.960
Test Suite 'HandlersTests' started at 2026-01-11 12:19:39.960
Test Case 'HandlersTests.testCallToolGreet' started at 2026-01-11 12:19:39.960
Test Case 'HandlersTests.testCallToolGreet' passed (0.102 seconds)
Test Case 'HandlersTests.testCallToolMissingParam' started at 2026-01-11 12:19:40.062
Test Case 'HandlersTests.testCallToolMissingParam' passed (0.001 seconds)
Test Case 'HandlersTests.testListTools' started at 2026-01-11 12:19:40.063
Test Case 'HandlersTests.testListTools' passed (0.001 seconds)
Test Suite 'HandlersTests' passed at 2026-01-11 12:19:40.064
         Executed 3 tests, with 0 failures (0 unexpected) in 0.104 (0.104) seconds
Test Suite 'debug.xctest' passed at 2026-01-11 12:19:40.064
         Executed 3 tests, with 0 failures (0 unexpected) in 0.104 (0.104) seconds
Test Suite 'All tests' passed at 2026-01-11 12:19:40.064
         Executed 3 tests, with 0 failures (0 unexpected) in 0.104 (0.104) seconds
◇ Test run started.
↳ Testing Library Version: 6.2.3 (48a471ab313e858)
↳ Target Platform: x86_64-unknown-linux-gnu
✔ Test run with 0 tests in 0 suites passed after 0.001 seconds.
Enter fullscreen mode Exit fullscreen mode

Gemini CLI settings.json

In this example — the Swift source code compiles to a platform native binary that can be called directly from Gemini CLI.

The default Gemini CLI settings.json has an entry for the Swift source:

{
  "mcpServers": {
    "firestore-https-swift": {
      "url": "http://127.0.0.1:8080/mcp"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Start the Local HTTP Server

Open a new terminal window and start up the local HTTP Server:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make run
Running the application...
[1/1] Planning build
Building for debugging...
[1/1] Write swift-version-6FE34157E209550C.txt
Build of product 'mcp-https-swift' complete! (3.63s)
Enter fullscreen mode Exit fullscreen mode

The local server will start logging:

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make run
Running the application...
[1/1] Planning build
Building for debugging...
[1/1] Write swift-version-6FE34157E209550C.txt
Build of product 'firestore-https-swift' complete! (5.11s)
{"label":"firestore-https-swift","level":"debug","timestamp":"2026-01-11T21:32:06.442Z","message":"Fetching project ID from Metadata Server..."}
{"timestamp":"2026-01-11T21:32:11.454Z","level":"warning","message":"GOOGLE_APPLICATION_CREDENTIALS not set and Metadata Server not available. Firestore will run in in-memory mode.","label":"firestore-https-swift"}
{"label":"firestore-https-swift","level":"debug","timestamp":"2026-01-11T21:32:11.460Z","message":"Starting service lifecycle","metadata":{"sl-services":"[Application, firestore_https_swift.SessionCleanupService(sessionManager: firestore_https_swift.SessionManager, logger: Logging.Logger(_storage: Logging.Logger.Storage))]","sl-cancellationSignals":"[]","sl-gracefulShutdownSignals":"[SIGTERM, SIGINT]"}}
{"label":"firestore-https-swift","level":"debug","timestamp":"2026-01-11T21:32:11.463Z","message":"Starting service","metadata":{"sl-service":"Application"}}
Enter fullscreen mode Exit fullscreen mode

Validation with Gemini CLI

Leave the HTTP server running — and open a new terminal window for the MCP client. Start Gemini CLI and the MCP connection over HTTP to the Swift Code is validated, The full Gemini CLI Session will start:

> /mcp list

 firestore-https-swift - Ready (8 tools)
  Tools:
  - check_db
  - get_product_by_id
  - get_products
  - get_root
  - greet
  - reset
  - search
  - seed

> check_db

✦ I will check the database status for you.

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

> get_products

✦ I will retrieve the list of all products from the inventory database.

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ get_products (firestore-https-swift MCP Server) {} │
│ │
│ [] │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The inventory database is currently empty.

> seed

✦ I will seed the inventory database with sample products.

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ seed (firestore-https-swift MCP Server) {} │
│ │
│ Database seeded successfully. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The database has been seeded successfully.

> get_products

✦ I will retrieve the list of products from the database for you.

✦ The database has been seeded, and here are the products:

   - Acai Smoothie Packs ($6, 78 in stock)
   - Apple Juice ($4, 325 in stock)
   - Apples ($5, 429 in stock)
   - Bananas ($5, 163 in stock)
   - Beef ($6, 181 in stock)
   - Black Beans ($6, 257 in stock)
   - Bottled Water ($8, 191 in stock)

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-swift
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-swift
xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ make deploy
Enter fullscreen mode Exit fullscreen mode

The Cloud Build will start:

> make deploy

✦ I will deploy the application to Google Cloud Run using the make deploy command.

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ⊶ Shell make deploy [current working directory /home/xbill/gemini-cli-codeassist/firestore-https-swift] (Deploy the application using make depl… │
│ │
│ │
│ FETCHSOURCE │
│ Fetching storage object: gs://comglitn_cloudbuild/source/1768167380.235536-3a505b7a6263499d8db91e193b15c0e2.tgz#1768167382188177 │
│ Copying gs://comglitn_cloudbuild/source/1768167380.235536-3a505b7a6263499d8db91e193b15c0e2.tgz#1768167382188177... │
│ / [1 files][17.8 KiB/ 17.8 KiB] │
│ Operation completed over 1 objects/17.8 KiB. │
│ BUILD │
│ Starting Step #0 │
│ Step #0: Already have image (with digest): gcr.io/cloud-builders/docker │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Enter fullscreen mode Exit fullscreen mode

It can take 15–30 minutes to complete the build.

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

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

Starting Step #2 │
│ Step #2: Already have image (with digest): gcr.io/cloud-builders/gcloud │
│ Step #2: Deploying container to Cloud Run service [firestore-https-swift] in project [comglitn] region [us-central1] │
│ Step #2: Deploying... │
│ Step #2: Setting IAM Policy.........done │
│ Step #2: Creating │
│ Step #2: Routing traffic.....done │
│ Step #2: Done. │
│ Step #2: Service [firestore-https-swift] revision [firestore-https-swift-00004-j47] has been deployed and is serving 100 percent of traffic. │
│ Step #2: Service URL: https://firestore-https-swift-1056842563084.us-central1.run.app │
│ Finished Step #2 │
Enter fullscreen mode Exit fullscreen mode

The service endpoint in this example is :

https://firestore-https-swift-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-swift-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:

Not Found
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-swift": {
      "url": "https://firestore-https-swift-1056842563084.us-central1.run.app/mcp"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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

xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift$ cd .gemini
cp settings.json.cloudrun settings.json
xbill@penguin:~/gemini-cli-codeassist/firestore-https-swift/.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 Swift providing the MCP server. Startup Gemini CLI with the updated settings :

gemini

🟢 firestore-cloudrun-swift - Ready (8 tools)
  Tools:
  - check_db
  - get_product_by_id
  - get_products
  - get_root
  - greet
  - reset
  - search
  - seed

> check_db

✦ I will check if the inventory database is running.

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ check_db (firestore-cloudrun-swift MCP Server) {} │
│ │
│ Database running: true │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

> get_products

✦ I've retrieved 34 products from the inventory. Here are a few examples:

   * Green Tea (ID: 2OpxVhxhPsxrNLoPB7na) - $6.00 (Qty: 474)
   * Jasmine Rice (ID: 2qqnVY6qNdWp73LYbDnc) - $4.00 (Qty: 167)
   * Coffee Beans (ID: 37kWbZHTwg5nbrSD13PB) - $6.00 (Qty: 375)

  Would you like to see the full list or search for a specific product?

Enter fullscreen mode Exit fullscreen mode

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

> suggest a menu for the big game

✦ Based on the current inventory, here is a suggested menu for the big game that utilizes your available high-stock items:

  🏈 Big Game Victory Menu

  Appetizers (Warm-up)
   * Crunch Zone: Bowls of Sunflower Seeds and Parmesan Crisps for classic snacking.
   * Sweet & Salty: White Chocolate Caramel Corn to satisfy those cravings.
   * Healthy Defense: Fresh Broccoli florets served with a Yogurt dip (seasoned with Fresh Basil if you like).

  The Main Event (MVPs)
   * Surf & Turf Sliders:
       * Cheddar Beef Melts: Ground Beef patties topped with melted Cheddar Cheese on Whole Wheat Bread.
       * Shrimp Skewers: Grilled or sautéed Shrimp.
   * Sideline Sides:
       * Black Beans & Jasmine Rice bowl.
       * Roast Chicken: A classic Whole Chicken as a centerpiece option.

  Halftime Refreshments
   * The Cooler: Ice-cold Cola and Bottled Water.
   * Specialty Drink: Pineapple Kombucha or Iced Green Tea.

  Post-Game Sweets
   * Victory Cookies: Mint Chocolate Cookies.
   * Fruit Platter: Fresh slices of Watermelon, Apples, and Bananas.

  Note: I excluded items like Wasabi Party Mix (out of stock) and Walnuts/Apple Juice (very low stock).

> suggest some snacks for listening to songs about bad boyfriends

✦ Here are some snack pairings from the inventory to go with your "bad boyfriend" playlist, categorized by the mood of the song:

  💔 The "Comfort & Indulgence" Mix
  For when you just need something sweet to get through the sad songs.
   * Mint Chocolate Cookies: Classic therapy food. Good for eating by the sleeve.
   * White Chocolate Caramel Corn: Sweet, salty, and a little chaotic—perfect for the drama.
   * Smores Cereal: Nostalgic comfort in a bowl (milk optional, dry snacking encouraged).

  🍋 The "Bitter & Sour" Sips
  For the angry, high-energy anthems.
   * Pineapple Kombucha: Fizzy, tart, and a little punchy.
   * Green Tea: Hot or iced, for calming down after belting out the bridge.

  💢 The "Crunch Away the Rage" Platter
  Aggressively crunchy snacks for venting.
   * Parmesan Crisps: Sharp, cheesy, and loud.
   * Sunflower Seeds: Requires work to eat, good for nervous energy.
   * Crisp Apples: The loudest crunch available in the fruit aisle.

  ✨ The "I'm Better Off" Glow-Up
  Healthy treats for when you realize you're the prize.
   * Acai Smoothie Packs: Treat yourself to something fancy and refreshing.
   * Yogurt Parfait: Layer Yogurt with Bananas and Cheddar Cheese... wait, definitely not the cheese. Just the fruit.

  (Note: We are out of Wasabi Party Mix, so you'll have to bring your own heat!)
Enter fullscreen mode Exit fullscreen mode

Summary

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

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

Top comments (0)