Connect to a database
Reach Postgres, MySQL, Redis, MongoDB and other TCP services from a Glxymesh function tool, with the address approved in tool.yml and the password passed in from the vault.
A function tool can open TCP connections, so it can talk to a database or any other service that
doesn’t speak HTTPS: Postgres, MySQL, Redis, MongoDB and the like. Like requests, every connection
goes through the egress gateway, which holds it to the addresses in tool.yml, refuses private
addresses and logs each connection with its bytes and duration.
Declare the address and the password
Section titled “Declare the address and the password”spec: glxymesh/tool@v1name: list-orderskind: functiondescription: Lists a customer's orders from the shop's database, newest first.read_only: trueinputs: customer: {type: string, description: "The customer's email, e.g. ada@example.com"}network: connect: ["db.example.com:5432"]secrets: PG_URL: hosts: [db.example.com] mode: pass-innetwork.connectlistshost:portpairs the code may connect to, up to 10. The host is a name or an IP address. A person approves each new one before it goes live, as with hosts.- The password, or a whole connection string, is a passed-in key:
mode: pass-in. Unlike other keys, its value reaches your code, so a person approves it explicitly before the code can read it. It is fetched for each call and never stored in the function’s configuration. - Never take a connection string or password as an input: store it on the Secrets page.
For a database on your own server, keep its address in the vault, as you would for an API on your own server:
network: connect: ["{PG_HOST}:5432"]secrets: PG_URL: {hosts: ["{PG_HOST}"], mode: pass-in}Store the server’s name or public IP as PG_HOST and the connection string as PG_URL. The
connection string can name the server itself: a connection to the address PG_HOST holds is allowed
too.
Connect from code
Section titled “Connect from code”Give the driver the platform’s connection instead of letting it open its own.
package listorders
import ( "github.com/jackc/pgx/v5" "glxymesh.com/sdk/glxy")
func Run(ctx *glxy.Ctx, in Input) (any, error) { dsn, err := ctx.SecretValue("PG_URL") if err != nil { return nil, err } cfg, err := pgx.ParseConfig(dsn) if err != nil { return nil, glxy.Errorf("PG_URL isn't a Postgres connection string: %v", err) } cfg.DialFunc = ctx.DialContext conn, err := pgx.ConnectConfig(ctx, cfg) if err != nil { return nil, err } defer conn.Close(ctx) rows, _ := conn.Query(ctx, "SELECT id, total FROM orders WHERE customer = $1 ORDER BY created_at DESC LIMIT 20", in.Customer) return pgx.CollectRows(rows, pgx.RowToMap)}ctx.DialContext has the signature drivers take as a dial hook: pgx’s DialFunc, go-redis’s
Dialer, and others. Imports resolve when the platform builds, so go.mod needs no extra lines.
import pg from 'pg';import type { Ctx } from '@glxymesh/sdk';import type { Input } from './tool.gen';
export async function run(ctx: Ctx, input: Input) { const { host, port } = await ctx.forward('db.example.com:5432'); const client = new pg.Client({ connectionString: await ctx.secretValue('PG_URL'), host, port, ssl: { servername: 'db.example.com' }, }); await client.connect(); try { const { rows } = await client.query('SELECT id, total FROM orders WHERE customer = $1 LIMIT 20', [input.customer]); return rows; } finally { await client.end(); }}ctx.forward returns a local { host, port } that carries each connection made to it, for
drivers that open their own sockets, like pg. ctx.connect(address) returns a connected socket,
for code that speaks the protocol itself.
import psycopgfrom glxymesh import Ctxfrom .tool_gen import Input
def run(ctx: Ctx, input: Input) -> list[dict]: host, port = ctx.forward("db.example.com:5432") with psycopg.connect(ctx.secret_value("PG_URL"), host=host, port=port) as conn: rows = conn.execute( "SELECT id, total FROM orders WHERE customer = %s LIMIT 20", (input["customer"],) ).fetchall() return [{"id": r[0], "total": float(r[1])} for r in rows]ctx.forward gives drivers that open their own sockets, such as psycopg and asyncpg, a local
address to connect to. ctx.connect(address) returns a socket, for drivers that take one, such
as pg8000’s sock. Pin the driver in requirements.txt.
Connections end with the call: open one per call and close it.
Keep TLS on. With Go’s ctx.DialContext the driver still knows the real server name, so
sslmode=verify-full works. With ctx.forward the driver connects to 127.0.0.1: tell it the
server’s name for verification where it can (pg’s ssl.servername), or use sslmode=require.
Run it on your machine
Section titled “Run it on your machine”gxmesh call and gxmesh dev connect the same way, with the keys from .env.local. Private
addresses are refused locally too; for a database on your machine or your network, add
--allow-private:
gxmesh call list-orders '{"customer":"ada@example.com"}' --allow-privateBuild it with AI
Section titled “Build it with AI”Describe the tools and say where the connection string lives, for example “Postgres tools for my
own database: read orders by customer and mark one shipped. Keep the connection string in the
vault.” Glxymesh writes function tools with network.connect and a passed-in key, and you approve
both before anything goes live. In a Go project the driver is ready at once; in TypeScript and
Python, pin the driver it names in your lock file.
What isn’t possible
Section titled “What isn’t possible”UDP, interactive SSH sessions and listening for incoming connections aren’t available. Use an HTTPS API where the service offers one.