Examples
How an AI assistant uses Server4Agent to publish what it builds: whole apps, functions, scripts and existing repos. Every example is a sequence of tool calls the assistant makes itself.
The pattern
Whatever your assistant built, a full app, a script, or a one-off function, putting it on Server4Agent follows the same shape: get a server, make a project, write the code into it, run it, and publish it at a live URL when you are ready. The project stays private until then.
// The pattern every app follows. Your assistant writes the code;
// these calls run it and put it online.
create_server // a persistent box (or reuse one with room)
create_project // a folder on it with its own URL, private for now
write_file // the code the assistant wrote, one call per file
exec // install dependencies, build
start_app // run it on $PORT and keep it running
deploy // make it public and get the live URLPublish an app your assistant wrote
The assistant builds the app as it always does, then puts it on the server: a project for it, the files, an install, and start_app to keep it running. deploy then puts it online: it checks the app is listening, makes the project public, and waits for its URL to answer.
// Publish an app your assistant wrote. Each line is an MCP tool call.
create_server()
// → { "id": "srv_2k9", "tier": "small", "status": "running", "url": null }
// A new server takes about a minute to start the first time:
// get_server reads provisioning "ready" once it is up.
create_project({ server_id: "srv_2k9", name: "waitlist" })
// → { "id": "proj_9f2", "slug": "waitlist-9f2", "visibility": "private", "url": null }
write_file({
server_id: "srv_2k9", project_id: "proj_9f2", path: "package.json",
content: "{ \"type\": \"module\", \"dependencies\": { \"express\": \"^5\" } }"
})
write_file({
server_id: "srv_2k9", project_id: "proj_9f2", path: "server.js",
content: "import express from \"express\";\nconst app = express();\n…\napp.listen(process.env.PORT);"
})
exec({ server_id: "srv_2k9", project_id: "proj_9f2", command: "npm install" })
// → { "stdout": "added … packages" }
start_app({ project_id: "proj_9f2", command: "node server.js" })
// → { "status": "running", "listening": true, "url": null }
deploy({ server_id: "srv_2k9", project_id: "proj_9f2" })
// → { "url": "https://waitlist-9f2.apps.server4agent.app", "status": "live", "project_id": "proj_9f2" }Write and run a function
Not everything needs a URL. The assistant writes a file, runs it on the server, and gets the output back. No human, no plan, just the assistant and its API key.
// Write and run a function: no URL needed, just the output.
create_server()
// → { "id": "srv_2k9", "tier": "small", "status": "running", "url": null }
write_file({
server_id: "srv_2k9",
path: "fib.py",
content: "def fib(n):\n a, b = 0, 1\n for _ in range(n):\n a, b = b, a + b\n return a\n\nprint([fib(i) for i in range(10)])"
})
// → { "path": "fib.py", "written": true }
exec({ server_id: "srv_2k9", command: "python3 fib.py" })
// → { "stdout": "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]" }Run a script
Same primitives, longer code. The workspace is durable, so the files and anything installed are still there in the next session.
// A script that cleans a CSV, run on the server where the file is
write_file({
server_id: "srv_2k9",
path: "clean_csv.py",
content: "import csv, sys\n# dedupe rows, trim whitespace, write cleaned.csv\n..."
})
exec({ server_id: "srv_2k9", command: "python3 clean_csv.py leads.csv" })
// → { "stdout": "412 rows in, 389 out, wrote cleaned.csv" }Import and run an existing repo
Point a project at a git repo (or an uploaded archive) and the on-server agent installs, starts, and healthchecks it, so you can review, evaluate, or just run code you already have. The project reports init_phase from seeding to ready.
// Bring an existing codebase and let the server figure out how to run it.
create_server()
// → { "id": "srv_2k9", "tier": "small", "status": "running", "url": null }
create_project({
server_id: "srv_2k9",
name: "checkout-service",
source: { kind: "git", git_url: "https://github.com/your-org/checkout-service" }
})
// → { "id": "proj_9f2", "init_run_id": "tsk_c40", "init_phase": "seeding" }
// The on-server agent seeds the workspace, installs deps, starts the app, and
// healthchecks it. Poll get_project until the service is confirmed up:
get_project({ project_id: "proj_9f2" })
// → { "init_phase": "ready",
// "health": { "listening": true, "last_status": 200 },
// "url": null } // deploy it for a live URLHand the build to the agent on the server
Rather not write the code in your assistant? prompt hands a plain-language goal to an agent that runs on the server: it plans the project, writes it, installs, and starts it. It spends the account's AI budget, so an assistant reaches for it when you ask for that agent. deploy publishes the result, the same as code your assistant wrote.
// Hand a goal to the agent on the server. It spends the account's AI budget.
prompt({
server_id: "srv_2k9",
project_id: "proj_9f2",
prompt: "Catch new waitlist submissions, save each one, and alert our Slack channel. Serve it on $PORT."
})
// → { "run_id": "run_71a", "status": "running" }
// The agent on the server plans the project, writes the webhook endpoint and
// the event log, installs dependencies, and starts it. Poll until it is done:
get_run({ server_id: "srv_2k9", run_id: "run_71a" })
// → { "status": "completed", … }
// Then publish it, the same as code your assistant wrote.
deploy({ server_id: "srv_2k9", project_id: "proj_9f2" })
// → { "url": "https://waitlist-9f2.apps.server4agent.app", "status": "live", "project_id": "proj_9f2" }A whole project with start_build
For a whole project, start_build runs that agent through each stage in order (plan, scaffold, implement, install, build and test, start), and the assistant polls get_build for the result: the files the run changed, or why it failed. Deploy it when it is done.
// A whole project in one agent run on the server.
// start_build works through each stage (plan, scaffold, write, install,
// build and test, start); poll it with get_build.
start_build({ server_id: "srv_2k9", project_id: "proj_9f2",
goal: "Build a SaaS dashboard with sign-in." })
// → { "build_id": "bld_ep2mrm", "run_id": "tsk_c40", "status": "running", "project_id": "proj_9f2" }
get_build({ server_id: "srv_2k9", build_id: "bld_ep2mrm" })
// → { "status": "running", "run_id": "tsk_c40", "run_status": "running",
// "steps": [ { "key": "plan", "label": "Plan the project", "status": "running" }, … ] }
// The steps move together; get_run with the run_id shows what the agent is doing now.
// keep polling until status === "completed":
// → { "status": "completed",
// "url": null, // set once the project is public
// "artifacts": ["app/page.tsx", "app/login/page.tsx", "package.json"],
// "error": null }
// A failed run reads completed with every step "failed", and error says why.
deploy({ server_id: "srv_2k9", project_id: "proj_9f2" })
// → { "url": "https://dashboard-9f2.apps.server4agent.app", "status": "live", "project_id": "proj_9f2" }Prefer plain HTTP?
Every tool maps to a REST endpoint. Here's a whole Flask app put online with curl, from an empty account to a live URL.
# The same flow over plain HTTP: create a server and a project, write the
# app into the project's folder, install, start it, then deploy.
API=https://api.server4agent.com
AUTH="Authorization: Bearer $SERVER4AGENT_API_KEY"
JSON="Content-Type: application/json"
# 1. A server (no body; the plan behind the key sets the limits)
SRV=$(curl -s -X POST $API/servers -H "$AUTH" | jq -r .id)
# 2. A blank project on it. It starts private; deploy makes it public.
PROJ_JSON=$(curl -s -X POST $API/servers/$SRV/projects -H "$AUTH" -H "$JSON" \
-d '{ "name": "hello-flask" }')
PROJ=$(echo "$PROJ_JSON" | jq -r .id)
SLUG=$(echo "$PROJ_JSON" | jq -r .slug)
# 3. Files. Over REST, paths are relative to the server workspace
# (/home/node/workspace), and a project's folder is <slug>/ inside it.
curl -s -X PUT $API/servers/$SRV/files -H "$AUTH" -H "$JSON" -d '{
"path": "'$SLUG'/app.py",
"content": "import os\nfrom flask import Flask\napp = Flask(__name__)\n\n@app.get(\"/\")\ndef hi():\n return \"hello\"\n\nif __name__ == \"__main__\":\n app.run(host=\"0.0.0.0\", port=int(os.environ[\"PORT\"]))\n"
}'
# 4. One-off commands go through exec. With project_id it runs in the
# project folder, with PORT and the project's secrets in its env.
# A fresh server has python3 but no pip or venv, and an install outlasts
# exec's ~20 second window, so run it in the background with a log...
curl -s -X POST $API/servers/$SRV/exec -H "$AUTH" -H "$JSON" -d '{
"project_id": "'$PROJ'",
"command": "nohup sh -c \"sudo apt-get update -q && sudo apt-get install -y -q python3-venv && python3 -m venv .venv && .venv/bin/pip install -q flask; echo exit=\\$?\" > install.log 2>&1 &"
}'
# ...then read the log until its last line is exit=0
curl -s -X POST $API/servers/$SRV/exec -H "$AUTH" -H "$JSON" \
-d '{ "project_id": "'$PROJ'", "command": "tail -n 1 install.log" }'
# -> { "stdout": "exit=0\n" }
# 5. Start the app and keep it running (not exec: exec's session ends with the call)
curl -s -X POST $API/projects/$PROJ/start -H "$AUTH" -H "$JSON" \
-d '{ "command": ".venv/bin/python app.py" }'
# -> { "status": "running", "listening": true, ... }
# 6. Deploy: checks something is listening, makes the project public, waits for the URL
curl -s -X POST $API/servers/$SRV/deployments -H "$AUTH" -H "$JSON" \
-d '{ "project_id": "'$PROJ'" }'
# -> { "url": "https://hello-flask-<id>.apps.server4agent.app", "status": "live", "project_id": "..." }
# status "publishing" means the URL had not answered yet: poll the URL itself, or
# GET $API/servers/$SRV/deployments until this project's entry reads "live": true.What to know before you script it
- Where files live. The server workspace is
/home/node/workspace. Each project has its own folder there, named after itsslug. REST file paths are relative to the workspace, so a project's file is<slug>/app.py; the MCP file tools take aproject_idand resolve paths inside the project folder for you. - exec or start.
execis for one-off work (install, build, inspect). Its session ends with the call, and output comes back for about 20 seconds; a longer command keeps running on the server and other commands wait for it, so put long installs in the background with their output in a file.POST /projects/{id}/startis for the app: it runs the command in the project folder as a background process, brings it back after a restart, and waits about 10 seconds for it to answer. Deploy only checks that something is listening on the port;startis how you make that true and keep it true. - Port and address. Listen on the port in
$PORT, whichstartand project-scopedexecset to the project's routed port. The server's proxy connects to127.0.0.1:$PORT, so bind0.0.0.0or127.0.0.1, not an IPv6-only address. - What's installed. Servers come with Node.js 22,
python3,gitandcurl, and the workspace user has passwordlesssudo. Neither pip nor the venv module is preinstalled:sudo apt-get install -y python3-venvadds both, as in step 4. - JSON bodies. Send
Content-Type: application/jsonwith every request that has a body. - Exit codes.
execreturns one field,stdout, with stdout and stderr together. A failing command's error text and exit status come back in that same field. When a script needs the status, end the command with; echo exit=$?and read it from the output. - Waiting for a deploy. Deploy answers
liveonce the URL answered, orpublishingwhen it had not yet. There is no single-deployment endpoint: poll the URL, orGET /servers/{id}/deploymentsuntil the project's entry haslive: true. A409withnot_listeningmeans nothing is running on the port yet. - Bringing up your own code. A project created from a
gitorarchivesource is brought up by the on-server agent. It reads how the project says it runs (README, Makefile, Procfile, Dockerfile, the package manifest's scripts) before guessing from the source, so a Python app with a Procfile line or a README start command starts that way. Fix a wrong guess withPATCH /projects/{id}and arun_plan, thenPOST /projects/{id}/init. - Secrets during bring-up. If the code needs a value you did not provide, bring-up pauses at
awaiting_secrets. Set the values withPUT /projects/{id}/secrets, then callPOST /projects/{id}/initto resume: setting secrets alone does not restart it.
Connect an agent
Create a key, paste the MCP config, and your assistant can publish what it builds at a live URL that keeps running.