Muhammad Fari Madyan
Hire me
MCP Server · part 5 of 6

Building a Remote MCP Server on GCP Part 5: Deploying to App Engine — app.yaml, Building Outside, Scale to Zero & the /healthz That Never Arrived

Part five of a Building a Remote MCP Server on GCP series that opens with a quiz: why App Engine Standard, building locally with .gcloudignore and a no-op gcp-build, app.yaml line by line, PORT, 0.0.0.0 and the real client IP, Google's reserved paths ending in z, and esbuild failing on node_modules from another OS.

Muhammad Fari Madyan

· 13 min read

Baca dalam Bahasa Indonesia →

Opening: A 404 That Never Reached My Code

The deploy finished without errors. The App Engine URL answered GET / with the server's JSON. Then I checked the health endpoint:

TEXT
$ curl -i https://my-project.et.r.appspot.com/healthz
HTTP/2 404
...
The requested URL /healthz was not found on this server. That's all we know.

That is Google's 404 page, not mine. When Express does not know a path, it answers with a tiny page that says Cannot GET /healthz. This was a different page from a different server: the request had been answered before it ever reached my app.

The other surprise came before the deploy even started: the build tool refused to run on my Mac. Both stories are in this part, together with the less dramatic but more important work: app.yaml line by line, building outside the platform, scaling to zero, and getting the real client IP.


Warm-Up Quiz: Do You Still Remember?

Answer first. All correct? Jump to the Summary. Missed one? Let's refresh the material.

Question 1. Why did /healthz return Google's 404 page on App Engine?

  • A. Express does not support the letter z
  • B. Google's front end reserves some paths that end in z and answers them itself, so the request never reaches the app
  • C. The health check was not deployed
Show the answer to question 1

B. App Engine's documentation lists reserved paths: paths starting with /_ah/, paths ending with /eventlog, and some paths ending with z. Section 5 tells the story.

Question 2. Can an App Engine application be moved to another region after it is created?

  • A. Yes, with gcloud app update --region
  • B. No, the region of an App Engine application is permanent for that project
  • C. Only within the same continent
Show the answer to question 2

B. Choose the region carefully. Moving means a new project. I chose asia-southeast2 (Jakarta).

Question 3. My server needs the website's source to build its index, but only the mcp-server folder is uploaded. What did I do?

  • A. Upload the whole website repository
  • B. Build dist/ and data/ locally before deploying, upload them, and make the platform's build step a no-op
  • C. Fetch the articles from the website at startup
Show the answer to question 3

B. .gcloudignore uploads dist/ and data/ even though Git ignores them, and the gcp-build script does nothing. Section 2 shows both files.

Question 4. Why does the server listen on 0.0.0.0 on App Engine but on 127.0.0.1 on a VPS?

  • A. App Engine forwards traffic to the container from outside it, so the app must listen on all interfaces. On a VPS, only Nginx on the same machine should reach the port
  • B. 127.0.0.1 is slower
  • C. It does not matter
Show the answer to question 4

A. The same code picks the right address from the environment: GAE_SERVICE is set on App Engine.

Question 5. A node_modules folder installed on Linux is used on a Mac. What breaks first?

  • A. Nothing, JavaScript is portable
  • B. Packages with native binaries, such as esbuild, because the binary is built for one operating system and CPU
  • C. Only TypeScript types
Show the answer to question 5

B. My tsx failed with "Host version does not match binary version". Section 6 has the exact error and the fix.


1. Why App Engine Standard

For a new stateless HTTP service on GCP today, Cloud Run is usually my default (I compared the options in GCP for Backend Engineers Part 1). I chose App Engine Standard for this project on purpose, because I wanted to learn how it works with an external load balancer. It also fits the job well:

NeedApp Engine Standard
No container to maintainUpload code, the platform builds and runs it
Scale to zeromin_instances: 0, no instance runs while idle
Small free allowance28 free F-class instance hours per day per project
Cost capmax_instances: 2
Logsstdout goes to Cloud Logging

Two things to know before the first gcloud app create:

  • The region is permanent. An App Engine application cannot move region. I used asia-southeast2 (Jakarta), which gives URLs ending in .et.r.appspot.com.
  • The first service must be called default. Other services can have any name, but only after default exists.

2. Build Outside, Upload the Result

The search index is built from the website's Markdown and TypeScript constants, which live outside the mcp-server folder. App Engine only receives the folder I deploy. So the index cannot be built on the platform.

The solution is to build everything locally, upload the result, and tell the platform not to build:

BASH
npm run bundle     # = npm run index && tsc → data/articles.json and dist/
gcloud app deploy app.yaml

.gcloudignore decides what is uploaded. dist/ and data/ are ignored by Git, so they must not be ignored here:

TEXT
# Only the built server, the article index and package files go to App Engine.
# dist/ and data/ are in .gitignore but MUST be uploaded: run `npm run bundle` before deploying.
.gcloudignore
.gitignore
node_modules/
src/
test/
scripts/
deploy/
*.md
ecosystem.config.cjs
tsconfig.json

And package.json has a build script that does nothing:

JSON
"scripts": {
  "bundle": "npm run index && tsc",
  "start": "node dist/server.js",
  "gcp-build": "echo"
}

When a Node.js app is deployed, the platform's build step runs the gcp-build script if there is one. Without the no-op, it would try to build, and the build would fail because the website's source is not there. With it, the platform still installs dependencies from package-lock.json, then runs the files I uploaded as they are.

Note that node_modules/ is never uploaded. The platform installs dependencies itself, on Linux. That detail matters in Section 6.

The deploy pipeline: npm run bundle builds data/articles.json and dist locally, gcloud app deploy uploads only what .gcloudignore allows, the platform installs dependencies and skips the build, and a new App Engine version starts serving


3. app.yaml, Line by Line

YAML
runtime: nodejs22
service: default
instance_class: F1
entrypoint: node dist/server.js

automatic_scaling:
  min_instances: 0          # scale to zero when idle (pay nothing for instances)
  max_instances: 2          # cost cap: at most two F1 instances
  target_cpu_utilization: 0.65
  max_concurrent_requests: 40

env_variables:
  # App Engine puts the real client IP in this header; the app rate limits on it.
  CLIENT_IP_HEADER: x-appengine-user-ip
  RATE_LIMIT_PER_MINUTE: "120"
LineWhy
runtime: nodejs22Same major version as my local tests
service: defaultRequired for the first service
instance_class: F1The smallest class: 384 MB memory, 600 MHz CPU limit. Enough for 4.2 MB of data and a handful of calls, but Part 4 showed memory reaching 293 MB under a local load test
entrypointExplicit instead of relying on npm start. One less layer between the platform and my process
min_instances: 0No instance while idle. The price is a cold start on the first request
max_instances: 2The bill cannot grow past two instances, whatever the traffic does
target_cpu_utilization, max_concurrent_requestsWhen to add the second instance. 40 matches the concurrency where Part 4's local test was already saturated
CLIENT_IP_HEADERWhich header holds the real client IP. Section 4 explains

On scaling to zero: when the first request arrives after a quiet period, App Engine starts an instance, Node.js starts, and the server parses 4.2 MB of JSON and builds the index (37 ms and 394 ms on my machine, more on an F1). I did not measure cold starts on App Engine precisely. For a tool called from a chat, where the model itself takes seconds to answer, a cold start on the first call is acceptable. For a user-facing API with a latency target, min_instances: 1 would be the fix, at the cost of an instance running all day.


4. Three Small Code Changes for the Platform

The same code runs on my laptop, on App Engine and on a VPS. The differences come from the environment:

TS
const port = Number(process.env.PORT || 3400);
// App Engine needs 0.0.0.0. On a VPS, stay on loopback so only Nginx can reach the port.
const host = process.env.HOST || (process.env.GAE_SERVICE ? "0.0.0.0" : "127.0.0.1");
  • PORT is set by App Engine. The app must listen on it.
  • The host must be 0.0.0.0 on App Engine, because the platform forwards traffic into the instance from outside. On a VPS, 127.0.0.1 means only Nginx on the same machine can reach the Node process, which is exactly what I want there.

The real client IP. Behind App Engine's front end, the TCP connection comes from Google, not from the user. App Engine puts the user's IP in the X-AppEngine-User-IP header, and the rate limiter from Part 4 reads it through CLIENT_IP_HEADER. Without that variable, every user would share one rate limit counter, and one busy client would lock everyone out.

Graceful shutdown. App Engine stops instances when it scales down or deploys a new version, and sends SIGTERM first:

TS
const shutdown = (signal: string) => {
  jsonLogger("INFO", "shutting down", { signal });
  server.close(() => process.exit(0));
  setTimeout(() => process.exit(1), 8000).unref();
};
process.on("SIGTERM", () => shutdown("SIGTERM"));

server.close() stops accepting new connections and lets requests in flight finish. The timer is a safety net so the process never hangs. (Node.js & TypeScript Part 4 covers this pattern in depth.)


5. The /healthz That Never Arrived

Back to the opening. The health endpoint was called /healthz, a habit from Kubernetes. On App Engine:

  • GET / reached my app and returned its JSON.
  • GET /healthz returned Google's own 404 page, "The requested URL /healthz was not found on this server. That's all we know."
  • Express would have answered an unknown path with its own Cannot GET /healthz. This page was not from Express.

When the answer does not come from your app, the problem is in front of the app. App Engine's documentation has a short list of reserved URL paths: paths that start with /_ah/, paths that end with /eventlog, and "some paths ending with z". Google's front end answers those itself. /healthz is one of them.

The fix was a rename, with a comment so nobody renames it back:

TS
// Not /healthz: Google's front end (App Engine, Cloud Run) reserves paths that end in "z"
// and answers them itself with a 404, so the request never reaches the app.
app.get("/health", (_req, res) => {
  res.json({ ok: true, articles: search.index.count, generatedAt: search.index.generatedAt, version: SERVER_INFO.version });
});

The rename touched the code, the tests, the README, the deploy script and the Nginx config. A health check path appears in more places than you expect, which is a good reason to choose it carefully the first time.

The general lesson is worth more than the specific one: if an answer does not look like it came from your app, look at everything between the user and the app before you debug the app.


6. The Build Tool That Came from Another Operating System

Before any of that, npm run bundle failed on my Mac:

TEXT
Error: Host version "0.28.2" does not match binary version "0.25.11"

The script runs TypeScript through tsx, which uses esbuild. esbuild ships a native binary for each operating system and CPU. The node_modules in that folder had been installed from a Linux VM that shares the folder with my Mac. The JavaScript part of esbuild and the native binary in the folder no longer matched what the Mac needed.

The fix:

BASH
rm -rf node_modules dist data
npm ci

npm ci installs exactly what package-lock.json says, with the right binaries for the machine running it.

Two lessons:

  • node_modules belongs to one machine. Never share it between operating systems, through a shared folder, a Docker volume, or a copied directory.
  • This is also why node_modules/ is in .gcloudignore. If I uploaded my Mac's node_modules, App Engine's Linux instances would get macOS binaries. Letting the platform install dependencies itself avoids the whole class of problem.

7. Deploying, Checking, and Cleaning Up Versions

The deploy script wraps it all:

BASH
PROJECT=my-project ./deploy/gcp.sh app
BASH
g services enable appengine.googleapis.com cloudbuild.googleapis.com compute.googleapis.com
g app describe >/dev/null 2>&1 || g app create --region="$REGION"
npm run bundle
g app deploy app.yaml --quiet
echo "App Engine URL: https://$(g app describe --format='value(defaultHostname)')/health"

g is a small wrapper for gcloud --project "$PROJECT", so a command can never run against the wrong project. gcloud app create runs only if the application does not exist yet, so the script is safe to run again.

After the deploy, two checks:

BASH
curl -s https://my-project.et.r.appspot.com/health
gcloud app logs tail -s default --project my-project

The first log line after a cold start is the server's own:

JSON
{"severity":"INFO","message":"listening","host":"0.0.0.0","port":"(from PORT)","articles":194,"generatedAt":"..."}

host: 0.0.0.0 and articles: 194 confirm that the platform branch of the code ran and that the index went out with it.

Every deploy creates a new version, named by date and time, and moves traffic to it. Old versions stay. With automatic scaling and no traffic, they scale to zero, but they still clutter the console and count toward a version limit. List them with gcloud app versions list and delete the ones you will not roll back to.


8. Three App Engine Traps

Trap one: Kubernetes habits on a managed platform. /healthz is a reserved path here. Check the platform's reserved paths and headers before you choose names.

Trap two: building on the platform when the build needs files outside the upload. Build locally or in CI, upload the result, and make gcp-build a no-op. Make sure .gcloudignore does not drop the very files you built.

Trap three: forgetting the region is forever. Choose the region before gcloud app create, based on your users and the other services you will use.


Summary

  • Why App Engine Standard: no container, scale to zero, 28 free F-class hours per day, a hard cap with max_instances. The region is permanent, and the first service must be default.
  • Build outside: npm run bundle creates data/ and dist/ locally, .gcloudignore uploads them, gcp-build is a no-op, and the platform installs dependencies itself.
  • app.yaml: nodejs22, F1 (384 MB, 600 MHz), explicit entrypoint, min_instances: 0 and max_instances: 2, and CLIENT_IP_HEADER: x-appengine-user-ip for the rate limiter.
  • Code for the platform: PORT, 0.0.0.0 on App Engine and loopback on a VPS, the real client IP from a header, and a SIGTERM handler.
  • /healthz is reserved by Google's front end and never reaches the app. Renamed to /health. If an answer did not come from your app, look in front of it.
  • esbuild "Host version does not match binary version": node_modules from another OS. Fix with rm -rf node_modules dist data && npm ci, and never upload node_modules.

Next

The server now runs on an appspot.com URL. In Part 6, the final part, we put it behind an External Application Load Balancer with a serverless NEG and a Google-managed certificate on my own domain, lock App Engine so only the load balancer can reach it, watch bots arrive within minutes, look at what it all costs, compare it with a plain VPS, and connect it to Claude.

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 4Testing and Observabilitypart 6 →Load Balancer, Domain, Bots, and Cost

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan