# Which Orders Are Stuck Between Paid and Shipped? A Back-Office App's Morning Check of Each Marketplace's PlanetScale Database

A back-office app for online marketplaces makes one agent per marketplace that runs read-only SQL on its PlanetScale database and lists orders paid but not shipped.

[Run this on Rush](https://cloud.getrush.ai/run/find-orders-stuck-between-paid-and-shipped/planetscale/). Opens the console with this prompt ready to send. Connect PlanetScale there first.

## What the agent does

This is for a software company that sells a back-office app to online marketplaces: a handmade-goods marketplace, a used-equipment exchange, a regional food-delivery platform. Each marketplace runs its own product on its own PlanetScale database, and every morning its operations lead wants the same list: orders the buyer paid for that no seller has shipped, and how long each has waited. The software company saves one agent as a template that names the PlanetScale MCP server, with instructions to run read-only queries and show each one, and makes one agent from it per marketplace with `POST /o/{org}/agents {"template": "<template id>", "name": "stuck-orders-<marketplace id>"}`. Before saving the template, the company connects PlanetScale to its own account with `POST /me/connections {"type": "mcp", "name": "planetscale", "url": "https://mcp.pscale.dev/mcp/planetscale", "auth": {"mode": "oauth"}}`.

A marketplace connects PlanetScale once from the app's settings. The backend calls `POST /o/{org}/agents/{agent}/connections {"type": "mcp", "name": "planetscale"}` and opens the `authorize_url` in a popup. On PlanetScale's own consent page the marketplace's engineer picks the organization, the one database, and read-only access to it, and no access to the payment method. PlanetScale's docs say an organization admin can also set MCP query access to read-only for the whole organization, which the app's setup guide recommends.

During setup the marketplace says where its orders live: the table, the status column and the values that mean paid and shipped, and the timestamp of payment. On a run the agent calls `planetscale_get_branch_schema` on the production branch to confirm those columns still exist, and stops with a note if one was renamed. It then runs `planetscale_execute_read_query`, which accepts only `SELECT`, `SHOW`, `DESCRIBE` and `EXPLAIN` and reads from a replica when the database has one: first a count of paid, unshipped orders by how long they have waited, then the oldest 50 with order id, seller id, paid time and hours waiting. It selects no buyer name, email or address.

It returns JSON: the counts by age band, the oldest orders, the SQL that produced each, and any column it could not find. PlanetScale tags each query with `source=planetscale-mcp`, so the marketplace's engineers can find these queries in Insights and see what they cost the database.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "<agent id>",
  "input": "Find paid orders not yet shipped in database market-prod, branch main. From the marketplace's setup: orders table is orders; status column is status; paid means status = 'paid'; shipped means status in ('shipped', 'delivered'); payment time is paid_at (UTC). First call planetscale_get_branch_schema and confirm those columns exist; if one is missing, stop and say which. With planetscale_execute_read_query run: (1) the count of orders with status = 'paid' grouped by hours since paid_at in bands 0-24, 24-48, 48-96, over 96; (2) the 50 oldest such orders with id, seller_id, paid_at and hours waiting. Select no buyer name, email, phone or address columns. Run only SELECT. Return only JSON: {bands: [{band, orders}], oldest: [{order_id, seller_id, paid_at, hours_waiting}], sql: [string], missing_columns: [string]}."
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The answer is JSON in the session's `result` and in the `run.completed` webhook. The app shows the age bands at the top of the marketplace's morning page and the oldest orders as a list, each linked to the order in the marketplace's own admin, with the SQL behind a small link.

What the operations lead checks: that "paid" still means what it did at setup (a new status such as `on_hold` changes the picture), and that the oldest orders really are stuck and not pre-orders or made-to-order items with a longer promised time. Contacting a seller is the lead's call; the agent names seller ids and writes to no one. The transcript shows each tool call and the rows it returned.

## Run it on every marketplace morning

The app's scheduler sends one `POST /o/{org}/sessions` per connected marketplace at 6 a.m. in that marketplace's time zone, with its agent id as `agent`, its database and setup in `input`, and a `request_id` that is a UUID v5 of the marketplace id and the date, so a retried job starts no second run.

A lead can press Check now in the app during a busy day, which starts the same request for that marketplace at once. Runs past the key's concurrent limit wait `queued` and start as earlier ones finish.

## Why run it on Rush?

The usual way to build this asks every marketplace for a database password and keeps it on the software company's servers. Here each marketplace signs in on PlanetScale's own page, picks one database and read-only access, and that sign-in belongs to its own agent only: one marketplace's run cannot reach another's database. PlanetScale creates short-lived credentials for each query and deletes them after, so no password exists for anyone to keep.

The company keeps one template. Adding a new number, such as orders refunded after shipping, is one `PATCH` to the template that reaches every marketplace's next run. `GET /o/{org}/sessions?agent=<agent id>` lists one marketplace's runs and their `total_cost_usd`, so the company can see what the feature's model calls cost per customer.

## Frequently asked questions

### Can the agent change a marketplace's data?

Not when the marketplace grants read-only access on PlanetScale's consent page; the write tool then has no permission to run. The template and the prompt also allow only `SELECT`. PlanetScale's server separately blocks `UPDATE` and `DELETE` without a `WHERE` and every `TRUNCATE`.

### Will the query slow down the marketplace's production database?

Reads go to a replica when the database has one, which is PlanetScale's default for this tool. The queries are a count and a 50-row list, and the marketplace can see them in Insights by their `source=planetscale-mcp` tag.

### Our marketplace restricts database access by IP. Does that matter?

For a Postgres database with IP restrictions, PlanetScale's docs list four addresses its hosted server queries from, and all four must be allowed or queries fail. Vitess databases need no change.

### What if a Postgres table uses row-level security?

PlanetScale runs reads with a role that does not bypass row-level security, so a policy can hide rows. When a query returns nothing, the server can warn that RLS is active, and the agent reports that warning rather than calling the backlog empty.

## More PlanetScale jobs

- [Which Queries Got Slower This Month? A Database Consultancy's PlanetScale Review for Each Client](https://getrush.ai/run/review-each-clients-slow-queries/planetscale/)

## More jobs like this: Run an agent per customer task

- [Flag Subcontractors' Expiring Insurance Certificates in Each Contractor's OneDrive](https://getrush.ai/run/flag-expiring-insurance-certificates/onedrive/)
- [Flag Products in Each Online Store's Airtable Catalog That Are Not Ready to List](https://getrush.ai/run/flag-incomplete-product-listings/airtable/)
- [Generate a Typed API Client from an ASP.NET Core Swagger Document](https://getrush.ai/run/generate-api-clients/aspnet-core/)
- [Generate a Typed API Client for a FastAPI Service](https://getrush.ai/run/generate-api-clients/fastapi/)
- [Generate a Typed API Client for a NestJS Service](https://getrush.ai/run/generate-api-clients/nestjs/)

## Keep reading

- [Connect PlanetScale to Rush once, then name it on any run](https://getrush.ai/integrations/planetscale/)
- [Start your first run from the developer API quickstart](https://getrush.ai/docs/quickstart/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
