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:
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:
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
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:~$
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:
Swift MCP Documentation
The official MCP GitHub Repo provides samples and documentation for getting started:
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
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 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
}
Swift Package Information
The Swift code depends on several standard Swift libraries for MCP and logging:
name: "mcp-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"),
],
Installing and Running the Swift Code
Run the install make release target on the local system:
xbill@penguin:~/gemini-cli-codeassist/mcp-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/mcp-https-swift$
To format/check the code:
xbill@penguin:~/gemini-cli-codeassist/mcp-https-swift$ make lint
Linting code...
To test the code:
xbill@penguin:~/gemini-cli-codeassist/mcp-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.
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": {
"hello-https-swift": {
"url": "http://127.0.0.1:8080/mcp"
}
}
}
Start the Local HTTP Server
Open a new terminal window and start up the local HTTP Server:
xbill@penguin:~/gemini-cli-codeassist/mcp-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)
The local server will start logging:
{"label":"mcp-http-server","level":"debug","metadata":{"sl-gracefulShutdownSignals":"[SIGTERM, SIGINT]","sl-services":"[Application, mcp_https_swift.SessionCleanupService(sessionManager: mcp_https_swift.SessionManager)]","sl-cancellationSignals":"[]"},"timestamp":"2026-01-11T18:17:40Z","message":"Starting service lifecycle"}
{"label":"mcp-http-server","level":"debug","metadata":{"sl-service":"Application"},"timestamp":"2026-01-11T18:17:40Z","message":"Starting service"}
{"label":"mcp-http-server","level":"debug","metadata":{"sl-service":"SessionCleanupService(sessionManager: mcp_https_swift.SessionManager)"},"timestamp":"2026-01-11T18:17:40Z","message":"Starting service"}
{"timestamp":"2026-01-11T18:17:40Z","label":"mcp-http-server","metadata":{"sl-services":"[Hummingbird.DateCache, PreludeService<Server<HTTP1Channel>>]","sl-cancellationSignals":"[]","sl-gracefulShutdownSignals":"[]"},"level":"debug","message":"Starting service lifecycle"}
{"timestamp":"2026-01-11T18:17:40Z","metadata":{"sl-service":"Hummingbird.DateCache"},"label":"mcp-http-server","level":"debug","message":"Starting service"}
{"timestamp":"2026-01-11T18:17:40Z","metadata":{"sl-service":"PreludeService<Server<HTTP1Channel>>"},"label":"mcp-http-server","level":"debug","message":"Starting service"}
{"timestamp":"2026-01-11T18:17:40Z","label":"mcp-http-server","level":"trace","message":"Server State: starting"}
{"message":"Server started and listening on 0.0.0.0:8080","timestamp":"2026-01-11T18:17:40Z","label":"mcp-http-server","level":"info"}
{"message":"Server State: running","timestamp":"2026-01-11T18:17:40Z","label":"mcp-http-server","level":"trace"}
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
🟢 hello-https-swift - Ready (1 tool)
Tools:
- greet
> greet Tay-Tay
✦ I will call the greet tool with the name "Tay-Tay" as requested.
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ? greet (hello-https-swift MCP Server) {"param":"Tay-Tay"} ← │
│ │
│ MCP Server: hello-https-swift │
│ Tool: greet │
│ │
│ Allow execution of MCP tool "greet" from server "hello-https-swift"? │
│ │
│ 1. Allow once │
│ 2. Allow tool for this session │
│ ● 3. Allow all server tools for this session │
│ 4. No, suggest changes (esc) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ greet (hello-https-swift MCP Server) {"param":"Tay-Tay"} │
│ │
│ Tay-Tay │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The server responded with: Tay-Tay
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/mcp-https-swift
Deploy the project to Google Cloud Run with the pre-built cloudbuild.yaml and Dockerfile:
cd ~/gemini-cli-codeassist/mcp-https-swift
xbill@penguin:~/gemini-cli-codeassist/mcp-https-swift$ make deploy
The Cloud Build will start:
> make deploy
✦ I will read the Makefile to understand the deployment process and then execute make deploy.
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ ReadFile Makefile │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I will read cloudbuild.yaml to verify the build configuration.
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ ReadFile cloudbuild.yaml │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I will execute make deploy to submit the build to Google Cloud Build.
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ⊶ Shell make deploy [current working directory /home/xbill/gemini-cli-codeassist/mcp-https-swift] (Submits the build to Googl… (ctrl+f to focus) │
│ │
│ Copying gs://comglitn_cloudbuild/source/1768155802.353972-9c22dc9c12f94a978c1ce32d1becf9fc.tgz#1768155802740790... │
│ / [1 files][10.6 KiB/ 10.6 KiB] │
│ Operation completed over 1 objects/10.6 KiB. │
│ BUILD │
│ Starting Step #0 │
│ Step #0: Already have image (with digest): gcr.io/cloud-builders/docker │
│ Step #0: Sending build context to Docker daemon 47.1kB │
│ Step #0: Step 1/10 : FROM swift:6.2 AS builder │
│ Step #0: 6.2: Pulling from library/swift │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
⠸ Planning Execution and Safety (esc to cancel, 26s)
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:
│ Step #2: Deploying container to Cloud Run service [mcp-https-swift] in project [comglitn] region [us-central1] │
│ Step #2: Deploying... │
│ Step #2: Setting IAM Policy..............done │
│ Step #2: Creating Revision..........................................................................done │
│ Step #2: Routing traffic.....done │
│ Step #2: Done. │
│ Step #2: Service [mcp-https-swift] revision [mcp-https-swift-00002-z2v] has been deployed and is serving 100 percent of traffic. │
│ Step #2: Service URL: https://mcp-https-swift-1056842563084.us-central1.run.app │
│ Finished Step #2
The service endpoint in this example is :
https://mcp-https-swift-1056842563084.us-central1.run.app
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://mcp-https-swift-1056842563084.us-central1.run.app
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
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": {
"hello-cloudrun-swift": {
"url": "https://mcp-https-swift-1056842563084.us-central1.run.app/mcp"
}
}
}
Copy the Cloud Run version of the Gemini CLI configuration file:
xbill@penguin:~/gemini-cli-codeassist/mcp-https-swift$ cd .gemini
cp settings.json.cloudrun settings.json
xbill@penguin:~/gemini-cli-codeassist/mcp-https-swift/.gemini$
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
🟢 hello-cloudrun-swift - Ready (1 tool)
Tools:
- greet
> greet Swifties!
✦ I will call the greet tool with the parameter "Swifties!".
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ? greet (hello-cloudrun-swift MCP Server) {"param":"Swifties!"} ← │
│ │
│ MCP Server: hello-cloudrun-swift │
│ Tool: greet │
│ │
│ Allow execution of MCP tool "greet" from server "hello-cloudrun-swift"? │
│ │
│ 1. Allow once │
│ 2. Allow tool for this session │
│ ● 3. Allow all server tools for this session │
│ 4. No, suggest changes (esc) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ greet (hello-cloudrun-swift MCP Server) {"param":"Swifties!"} │
│ │
│ Swifties! │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Swifties
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)