@neon/tools
v2.20.1
Published
Agent tools for the Neon SDK ergonomic client, compatible with MCP, Eve, and Mastra.
Readme
@neon/tools
Agent tools for the @neon/sdk ergonomic client. Select the methods an agent needs, then use the descriptors directly or adapt them for MCP, Eve, and Mastra.
npm install @neon/toolsCreate tools
Selectors are SDK paths, plus the custom tools listed in customToolIds (see Custom tools). The returned record is keyed by those selectors. Each tool's published id is the last path segment, then the resource, in snake_case (projects.list → list_projects, postgres.roles.resetPassword → reset_password_postgres_roles, postgres.connectionString → connection_string_postgres). publishedId derives that string. toolIds lists every published selector.
import { createNeonTools, publishedId } from "@neon/tools";
publishedId("projects.list"); // "list_projects"
publishedId("postgres.roles.resetPassword"); // "reset_password_postgres_roles"
const apiKey = process.env.NEON_API_KEY;
if (!apiKey) throw new Error("NEON_API_KEY is required");
const tools = createNeonTools({
apiKey,
tools: [
"projects.list",
"projects.createAndConnect",
"branches.createAndConnect",
"branches.resetFromParent",
"branches.compareSchema",
],
});
const listed = await tools["projects.list"].execute({ limit: 10 });
const created = await tools["projects.createAndConnect"].execute({
name: "agent-project",
region_id: "aws-us-east-1",
});
await tools["branches.resetFromParent"].execute({
project_id: "project-id",
branch_id: "br-feature",
preserve_under_name: "feature-before-reset",
});
const compared = await tools["branches.compareSchema"].execute({
project_id: "project-id",
branch_id: "br-feature",
database_name: "neondb",
});Trigger create keeps the discriminator nested as body. type is "schedule" or "storage_object_created":
const triggerTools = createNeonTools({
apiKey,
tools: ["triggers.create"],
});
const createdTrigger = await triggerTools["triggers.create"].execute({
project_id: "project-id",
branch_id: "br-feature",
body: {
type: "schedule",
function_slug: "worker",
name: "daily-refresh",
schedule: { cron: "0 9 * * *" },
},
});
const createdStorageTrigger = await triggerTools["triggers.create"].execute({
project_id: "project-id",
branch_id: "br-feature",
body: {
type: "storage_object_created",
function_slug: "ingest",
name: "uploads",
storage_object_created: { bucket_name: "uploads", prefix: "incoming/" },
},
});limit on a list tool caps how many items come back.
MCP and Mastra publish tool.id (list_projects), not the record key.
apiKey is a Bearer credential: a Neon API key or a Neon OAuth access token. A function is called on every request, which is how short-lived OAuth tokens get refreshed. A credential is required when a tool executes — at construction, on execute(), or from MCP authInfo — and an empty value is rejected rather than ignored.
const tools = createNeonTools({
apiKey: () => oauth.getAccessToken(),
tools: ["projects.list"],
});
await tools["projects.list"].execute(
{ limit: 10 },
{ apiKey: oauthAccessToken },
);Each tool includes its Zod 4 inputSchema and outputSchema, published id, title, description, safety annotations, stability metadata, and execute(). Inputs are snake_case at the tool boundary. execute() strictly validates the input, rejects unknown fields, and returns typed, JSON-safe { data }. Neon SDK errors remain typed and are thrown to the caller.
Paginated lists call .all() and return the item array. Do not pass a cursor; those fields are omitted from the input schema.
const createBranch = createNeonTool("branches.createAndConnect", { apiKey });incidents.list (list_incidents) is the exception to the credential rule. It reads neonstatus.com, needs no credential, and never sends one, so it works for anonymous agents:
const incidents = createNeonTool("incidents.list", {});
const { data } = await incidents.execute({});
// { status: "operational", incidents: [], maintenance: [], affected_components: [], ... }Custom tools
Custom tools combine several requests into one call, or call Postgres or neon.com, and are not a single NeonClient method. They are selected, adapted, and executed like every other tool. customToolIds lists them.
| Selector | Published id | Returns |
| --- | --- | --- |
| auth.getConfig | get_config_auth | The branch's Neon Auth integration and settings: trusted origins, localhost access, email and password, OAuth providers, email provider, the organization, magic link, and phone number plugins, and the webhook. branch_id defaults to the project's default branch. |
| resources.search | search_resources | { results: [{ id, title, url }] } for organizations, projects (including projects shared with the user), branches, compute endpoints, and snapshots whose name or id contains query. A compute endpoint also matches its host, with or without -pooler, so a host from a connection string finds it. The id prefix (org:, project:, branch:<project>/<branch>, endpoint:<project>/<endpoint>, snapshot:<project>/<snapshot>) names the kind. |
| resources.fetch | fetch_resources | { id, title, text, url, metadata } for an id from resources.search: markdown details in text, the console URL, and the organization with members and projects, the project with branches, the branches.describe result, the endpoint, or the snapshot in metadata. |
| sql.run | run_sql | { rows, totalRowCount, truncated, note? } for one statement. rows holds at most max_rows (default 100, at most 10,000). |
| sql.runTransaction | run_transaction_sql | One { rows, totalRowCount, truncated, note? } per statement, run in one transaction. |
| sql.explain | explain_sql | The EXPLAIN (FORMAT JSON) rows. analyze: true executes the statement. |
| schema.listTables | list_tables_schema | { table_schema, table_name, table_type } for every table outside pg_catalog and information_schema. |
| schema.describeTable | describe_table_schema | { raw, formatted }: columns, indexes, constraints, and sizes. A missing table, or a name that matches two tables, throws NeonClientError. |
| branches.describe | describe_branches | { branch, console_url, text, database_structure }, where database_structure is [[], rows]: one { tree_structure } row per database except Neon's reserved postgres, which the other SQL tools reject, then the connected database's schemas and objects, read from the system catalogs in a READ ONLY transaction. When that query fails it is null and database_structure_error says why. |
| queries.listSlow | list_slow_queries | { slow_queries, total_queries_found } from pg_stat_statements, slowest mean time first. Without the extension it throws NeonClientError with the same message database.inspect gives: ask the user, then run the CREATE EXTENSION statement it names. |
| database.inspect | inspect_database | One neon inspect db check: { check, describe, projectId, branchId, databases, fields, totalRowCount, rows, truncated, note? }. |
| migrations.prepare | prepare_migrations | Runs migration_sql on a new temporary branch and returns { complete, temporary_branch, migration_result }. complete holds exactly the fields migrations.complete takes (migration_id, migration_sql, database_name, project_id, temporary_branch_id, parent_branch_id); temporary_branch is { id, name, parent_id }; migration_result holds each statement's rows. |
| migrations.complete | complete_migrations | Applies the migration to the parent unless apply_changes is false, deletes the temporary branch, and returns { applied, deletedBranchId?, cleanupError?, migrationResult? }. |
| queryTuning.prepare | prepare_query_tuning | Runs EXPLAIN ANALYZE on a new temporary branch and returns { complete, execution_plan, table_schemas, sql, baseline_metrics }. complete holds tuning_id, database_name, project_id, temporary_branch_id, and role_name when one was passed; baseline_metrics holds time, cost, rows, and buffer counters of the root plan node. |
| queryTuning.complete | complete_query_tuning | Applies suggested_sql_statements when apply_changes is true, deletes the temporary branch unless should_delete_temporary_branch is false, and returns { appliedChanges?, results?, deletedBranches?, message }. |
| docs.search | search_docs | { hits: [{ title, url, slug, category, breadcrumb, heading, excerpt }] } from neon.com site search. |
| docs.get | get_docs | { url, markdown } for one neon.com page slug, without the agent skills note and feedback footer neon.com adds for agents that browse the site. |
| api.search | search_api | { endpoints: [{ method, path, summary, operation_id, body_required, content_type?, fields }] }: the endpoints of the released Neon OpenAPI spec that match query, best first, with each one's path, query, header, and body fields (body fields as dotted paths, descriptions cut to one sentence). Needs no credential. |
| api.call | call_api | The response body of one request to any Neon API path, from neon.request. Not read-only safe; withheld under secrets: "none"; under secrets: "setup" it refuses paths with an auth segment, because Neon Auth routes return stored secrets. |
| feedback.send | send_feedback | Posts { feedback, source } to the Neon feedback service. |
const tools = createNeonTools({
apiKey,
tools: ["auth.getConfig", "resources.search", "resources.fetch", "sql.run"],
});
const { data: config } = await tools["auth.getConfig"].execute({
project_id: "project-id",
});
config.oauth_providers; // [{ id: "google", type: "standard", client_id: "…", client_secret: "****abcd" }]
const { data: found } = await tools["resources.search"].execute({
query: "checkout",
});
found.results; // [{ id: "project:project-id", title: "checkout-service", url: "https://console.neon.tech/app/projects/project-id" }]
const { data: project } = await tools["resources.fetch"].execute({
id: "project:project-id",
});
project.text; // "**Project Details**\n\n**Basic Information:**\n- Name: checkout-service\n…"
project.metadata; // { project: { id: "project-id", … }, branches: [ … ] }
const migrationTools = createNeonTools({
apiKey,
tools: ["migrations.prepare", "migrations.complete"],
});
const prepared = await migrationTools["migrations.prepare"].execute({
project_id: "project-id",
migration_sql: "ALTER TABLE users ADD COLUMN plan text",
});
await migrationTools["migrations.complete"].execute({
...prepared.data.complete,
apply_changes: true,
});
prepared.data.temporary_branch; // { id: "br-…", name: "mcp-migration-2026-11-06T20-05-00", parent_id: "br-main-…" }
const tuningTools = createNeonTools({
apiKey,
tools: ["queryTuning.prepare", "queryTuning.complete"],
});
const tuning = await tuningTools["queryTuning.prepare"].execute({
project_id: "project-id",
database_name: "neondb",
sql: "SELECT * FROM orders WHERE customer_id = 42",
});
await tuningTools["queryTuning.complete"].execute({
...tuning.data.complete,
suggested_sql_statements: ["CREATE INDEX ON orders (customer_id)"],
apply_changes: true,
});
const { data: result } = await tools["sql.run"].execute({
project_id: "project-id",
sql: "SELECT id, email FROM users ORDER BY id",
max_rows: 2,
});
result; // { rows: [{ id: 1, email: "[email protected]" }, { id: 2, email: "[email protected]" }], totalRowCount: 40, truncated: true, note: "Showing the first 2 of 40 rows. …" }auth.getConfig returns the OAuth client secrets and SMTP password the API returns to the caller; they are stored secrets, so secrets: "setup" or "none" hides them (see Secrets). Trusted origins, localhost access, email and password, and OAuth providers are required reads: an error on any of them fails the call, and a branch without Neon Auth throws NeonNotFoundError. The email provider, plugins, and webhook reads are reported under _errors when they fail; an email provider that was never configured is null.
user.organizations takes an optional search that filters by a case-insensitive part of the name or id. An organization API key gets its own organization.
resources.search leaves out a resource whose list the API refuses with 403 or 404, and fails on any other error, including a 5xx, an exhausted 429, and an aborted signal. It reads up to 400 projects per organization and 400 shared projects, and lists each project's branches, compute endpoints, and snapshots in parallel. Resources that belong to a branch (databases, roles, functions, buckets, triggers, credentials) are not searched, since each would cost one request per branch. resources.fetch reads the first page of an organization's members and projects and skips either when the API refuses it with 403 or 404; other errors fail the call. An id without a known prefix throws NeonClientError. Console URLs follow baseUrl: https://console.neon.tech/api/v2 links to https://console.neon.tech.
SQL tools
The SQL tools resolve a connection from project_id (required), then branch_id (default branch), database_name (neondb, else the first database), role_name (that database's owner), and compute_id (the branch's read-write compute). Their metadata.method is "SQL" and metadata.path is /projects/{project_id}/connection_uri, so inject.projectId fills project_id. Whether a tool can change data is in annotations, not metadata.method. postgres.connectionString resolves the same way and returns the URI, with endpoint_id as the compute and pooled defaulting to true.
A Postgres error rejects with the NeonDbError from @neondatabase/serverless, carrying code (SQLSTATE), position, detail, hint, and where. database.inspect throws InspectDiagnosticError (exported from @neon/tools, with check and the NeonDbError as cause) when Postgres rejects one of its own diagnostic queries, which is a bug in this package rather than in the input. A caller-side problem, such as a missing extension, a missing database, or a refused connection, keeps its own class.
inspectChecks lists every database.inspect check from the catalog the tool runs, as frozen objects with id, scope (database or compute), requiresExtension, fields, sqlLimit, and the emptyNote an empty result carries (emptyNoteAllDatabases when database_name is omitted).
migrations.prepare and queryTuning.prepare delete the temporary branch when anything after its creation fails, including the readiness wait (a wait timeout, a failed operation, or an aborted signal). Cleanup waits up to 60 seconds for the branch to finish provisioning before deleting it, regardless of wait and signal. migrations.complete applies first; if that fails the temporary branch stays so the call can be retried, and a failed delete after a successful apply is reported as cleanupError.
Execution context
execute(input, context) takes these fields in addition to signal and apiKey:
await tools["sql.run"].execute(
{ project_id: "project-id", sql: "SELECT count(*) FROM orders" },
{
readOnly: true,
postgresRole: "app_reader",
caller: { source: "my_host", userAgent: "my-agent/1.0", client: "cursor" },
},
);| Field | Effect |
| --- | --- |
| readOnly | A tool whose metadata.readOnlySafe is false throws NeonClientError without a request (see Read-only). sql.run and sql.explain run the statement in a READ ONLY transaction, so Postgres rejects writes. sql.runTransaction, migrations.*, and queryTuning.* are not read-only safe, because a caller statement such as COMMIT or SET TRANSACTION READ WRITE can leave a read-only transaction. schema.* behave like sql.run; branches.describe, queries.listSlow, and database.inspect always use a READ ONLY transaction. |
| postgresRole | Every SQL connection logs in as this role, and postgres.connectionString returns a URI for it. A role_name that differs throws NeonClientError. |
| caller | Sent with docs.search as x-caller-source, x-caller-user-agent, and x-caller-client. Without it, or when source cannot be a header value, x-caller-source is neon_tools. The search service records website, cli, mcp, and monitor by name and counts any other source as rest. |
Docs and feedback
docs.search, docs.get, and feedback.send call neon.com and the Neon feedback service, not the Neon API, and run without apiKey. Each request times out after 10 seconds. docs.get rejects slugs that contain .. or :// or start with /. Configure the endpoints on createNeonTools:
const tools = createNeonTools({
tools: ["docs.search", "docs.get", "feedback.send"],
siteSearchUrl: "https://example.com/site-search", // default: neon.com site search
docsBaseUrl: "https://neon.com", // docs.get reads <docsBaseUrl>/<slug>.md
feedbackUrl: "https://feedback.neon.tech/",
feedbackSource: "my_host", // default "neon_tools"
});
const { data } = await tools["docs.search"].execute({ query: "reset a branch" });
const { data: page } = await tools["docs.get"].execute({ slug: data.hits[0].slug });API search and passthrough
api.search fetches the released spec once an hour per URL and shares the fetch between concurrent calls; a failed fetch throws and is retried on the next call. apiSpecUrl on createNeonTools points it elsewhere. api.call sends what neon.request sends, so the path rules and typed errors are the SDK's:
const tools = createNeonTools({ apiKey, tools: ["api.search", "api.call"] });
const { data } = await tools["api.search"].execute({ query: "create branch" });
// data.endpoints[0]: { method: "POST", path: "/projects/{project_id}/branches", fields: [...] }
await tools["api.call"].execute({
method: "POST",
path: "/projects/old-flower-123/branches",
body: { branch: { name: "dev" } },
});Neon's feedback reporting counts manual_test, neon_agent, neon_cli, neon_mcp, and neon_website by name and any other feedbackSource, including the default, as other.
Read-only
metadata.readOnlySafe is true when a tool changes no Neon resource and returns no connection string or role password: a GET that needs no approval, logs.query, the SQL tools that run one statement in a READ ONLY transaction, and the docs, feedback, search, and incidents tools. postgres.connectionString is a GET but not read-only safe, because its URI carries a role password that works against the read-write compute.
readOnly: true on createNeonTools, createNeonTool, or execute refuses every tool that is not read-only safe. The execution context wins over the factory:
const tools = createNeonTools({
apiKey,
tools: ["projects.list", "projects.delete", "sql.run"],
readOnly: true,
});
await tools["projects.list"].execute({}); // runs
await tools["sql.run"].execute({ project_id: "project-id", sql: "SELECT 1" }); // READ ONLY transaction
await tools["projects.delete"].execute({ project_id: "project-id" });
// throws NeonClientError: delete_projects is unavailable in read-only mode ...
await tools["projects.delete"].execute(
{ project_id: "project-id" },
{ readOnly: false },
); // runsThe refusal happens inside onExecute, so a tracking hook sees it as a failed call, and before inject resolves a project or branch id.
Select tools by metadata
tools also takes a predicate. It sees every published tool without its execute (operationId, id, title, description, annotations, requiresApproval, metadata) and keeps the ones it returns true for, in toolIds order. A predicate that matches nothing throws.
const readTools = createNeonTools({
apiKey,
readOnly: true,
tools: (tool) =>
tool.metadata.readOnlySafe && tool.metadata.tags.includes("Branch"),
});
await readTools["branches.list"]?.execute({ project_id: "project-id" });The result is typed as a partial record, since which tools the predicate keeps is known only at runtime. toMastraTools keeps that optionality, also when the record mixes in tools from a list. CreateNeonToolsPredicateOptions types predicate options declared ahead of the call. Pass a list of selectors when you need exact keys.
Secrets
Tools return secrets as the API returns them. secrets on createNeonTools, createNeonTool, or execute chooses which ones results keep, and the execution context wins over the factory:
| secrets | Results keep |
| --- | --- |
| "all" (default) | every secret |
| "setup" | setup secrets: credentials Neon issues for an app, CI job, or agent to use |
| "none" | no secret |
A hidden secret becomes REDACTED_SECRET ("***redacted***"). An absent, null, or empty value stays as it is.
import { createNeonTools, REDACTED_SECRET } from "@neon/tools";
const tools = createNeonTools({
apiKey,
tools: ["credentials.create", "auth.emailProvider.get", "realtime.secret"],
secrets: "setup",
});
const { data: credential } = await tools["credentials.create"].execute({
project_id: "project-id",
branch_id: "br-main",
scopes: ["ai_gateway:invoke"],
principal_type: "user",
});
credential.api_token; // "nt_live_..."
const { data: smtp } = await tools["auth.emailProvider.get"].execute({
project_id: "project-id",
branch_id: "br-main",
});
smtp.password === REDACTED_SECRET; // true
await tools["realtime.secret"].execute(
{ project_id: "project-id", branch_id: "br-main" },
{ secrets: "none" },
);
// throws NeonClientError: secret_realtime is unavailable with secrets: "none" because its result is a secret.metadata.secretOutputFields lists where a tool's result carries a secret, as { path, kind }. A path is $ (the whole value), .key, or [*] (every array item). kind is setup for a credential Neon issues, or stored for a secret you gave Neon to call another service. metadata.returnsOnlySecrets is true when the result is the secret itself; such a tool refuses before it sends a request when secrets would hide its result.
| Tool | secretOutputFields | kind | returnsOnlySecrets |
| --- | --- | --- | --- |
| postgres.connectionString | $ | setup | yes |
| projects.createAndConnect, branches.createAndConnect | $.connectionString | setup | no |
| postgres.roles.list | $[*].password | setup | no |
| postgres.roles.get, postgres.roles.create | $.password | setup | no |
| postgres.roles.resetPassword | $.password | setup | yes |
| credentials.create | $.api_token, $.s3_secret_access_key | setup | no |
| credentials.rotate | $.api_token, $.s3_secret_access_key | setup | yes |
| realtime.secret | $.secret | setup | yes |
| storage.objects.presign | $.url | setup | yes |
| auth.create | $.secret_server_key | setup | no |
| auth.oauthClients.create | $.client_secret | setup | no |
| auth.oauthClients.rotateSecret | $.client_secret | setup | yes |
| apiKeys.create, organizations.apiKeys.create | $.key | setup | no |
| auth.oauthProviders.list | $[*].client_secret | stored | no |
| auth.oauthProviders.add, auth.oauthProviders.update | $.client_secret | stored | no |
| auth.emailProvider.get, auth.emailProvider.update | $.password | stored | no |
| auth.plugins.get | $.email_provider.password, $.oauth_providers[*].client_secret | stored | no |
| auth.getConfig | $.oauth_providers[*].client_secret, $.email_provider.password | stored | no |
withheldUnderSecrets(tool.metadata, mode) tells whether a tool refuses under a mode, so a host can leave those tools out: (tool) => !withheldUnderSecrets(tool.metadata, "none"). hiddenSecretFields(tool.metadata, mode) returns the fields a mode hides.
A rotation that returns the replacement (credentials.rotate, postgres.roles.resetPassword, auth.oauthClients.rotateSecret) counts as only a secret, because hiding it would discard the new value after the old one stopped working. realtime.rotateSecret returns nothing and stays available. Creates still run when their secret is hidden, so a secret the API returns only once, such as apiKeys.create's key or a confidential client's client_secret, is lost. readOnly does not change secrets.
Redaction runs inside onExecute, so a tracking hook sees the redacted result.
Writes and waiting
Tools run with waitForReadiness: true. When a mutation response includes an operations array, the call waits until those operations finish. The default deadline is five minutes (wait.timeoutMs). Pass wait: { timeoutMs: 30_000 } on createNeonTools or createNeonTool to bound that. Set it below the host's own tool-call timeout, otherwise the host gives up first.
An abort signal on execute or a wait timeout stops the poll, not the create: the branch or project may already exist, and the error does not include its id. List before retrying.
functions.deploy can still return pending. Its response has no operations array, so the tool does not poll.
realtime.enable, realtime.disable, and realtime.rotateSecret have no operations either. They poll the branch's Realtime state until pending is false, under the same wait budget.
metadata.method and metadata.path name the first request; extra readiness GETs are not listed there.
These public client methods are not tools: operations.waitFor, postgres.roles.password, storage.objects.get, credentials.reveal, logs.loki.labels, and logs.loki.labelValues. Waiting is what the write tools already do. Loki labels are the log fields, so logs.fields and logs.fieldValues are the discovery tools for both logs.query and logs.loki.queryRange; the latter returns Loki's response, with lines grouped by stream labels, and nanosecond timestamp strings. realtime.secret is a GET that requires approval: it returns the branch's Realtime secret, which an application backend seals queries with. After realtime.rotateSecret, it returns the new value once pending is false. projects.create and branches.create return the created resource without a connection string; createAndConnect returns a URI. postgres.dataApi.get returns { enabled: false } for a database without a Data API, where the SDK method throws NeonNotFoundError, and adds enabled: true to a configured one; a missing branch or database still throws. triggers.create and triggers.update take the OpenAPI discriminator as a nested body field. credentials.rotate requires approval and is not idempotent: a lost success already replaced the secret; create a replacement and revoke the rotated credential.
Roles
postgres.roles.create (create_postgres_roles) defaults to access: "admin", which joins neon_superuser. Pass read_only or read_write for a least-privilege credential. Scoped roles are created with SQL as the database owner; see @neon/sdk's neon.postgres.roles for the grants and limits.
const tools = createNeonTools({ apiKey, tools: ["postgres.roles.create"] });
const { data: role } = await tools["postgres.roles.create"].execute({
project_id: "project-id",
branch_id: "br-main",
name: "app_reader",
access: "read_only",
database_name: "neondb",
schemas: ["public"],
});
// role.password is the generated passwordOptional host add-ons
Descriptions
Every tool and every top-level input has a description. A description that names another tool uses the name that tool publishes under, after names and name. names and name run once per selected tool. A tool you did not select is named by its default id, without calling them.
Pass descriptions to replace the package's copy: a map keyed by SDK path or the current published id, or a function that receives the package's text. A key that matches neither is ignored. Write {{selector}} to name another tool the same way; an unknown selector throws.
const tools = createNeonTools({
apiKey,
tools: ["projects.create", "postgres.connectionString", "projects.delete"],
names: { "postgres.connectionString": "get_connection_string" },
descriptions: {
"postgres.connectionString":
"Get a connection string. Call it after {{projects.create}}.",
delete_projects:
"Delete a Neon project and all its data. NEVER run autonomously: run it only when the user's own message asks for it and unambiguously identifies its target; otherwise ask first.",
},
});
tools["projects.create"].description;
// "Creates a project with a default branch and read-write compute and waits until it is ready. Returns no connection string; use get_connection_string."
tools["postgres.connectionString"].description;
// "Get a connection string. Call it after create_projects."Tracking
onExecute wraps the call. The host must call event.execute(). That inner call performs getter resolution, path injection, original schema validation, auth, and the API request, so tracking and spans see those failures:
const tools = createNeonTools({
apiKey,
tools: ["projects.list"],
onExecute: async ({ id, execute }) => {
return execute();
},
});This package does not send analytics. Mutating event.input does not change a grant-locked project or branch id.
Names
name rewrites every published tool id. names overrides one tool first, keyed by SDK path or the generated snake-case id:
const tools = createNeonTools({
apiKey,
tools: ["branches.createAndConnect", "projects.list"] as const,
names: { "branches.createAndConnect": "create_branch" },
name: (id) => `neon_${id}`,
});Those tools publish as neon_create_branch and neon_list_projects. The record is still keyed by SDK path (tools["branches.createAndConnect"]). MCP and Mastra publish tool.id. Eve uses the filename as the model-facing name, so name the file after the published id. Duplicate or non-snake-case ids throw, and a names key that matches no selected tool throws.
Project and branch injection
Tools take path parameters as project_id and branch_id. A host that already knows those values can inject them, including on branches.createAndConnect. Without omitFromSchema, the published field becomes optional and a caller-supplied value wins. With omitFromSchema: true, the field is removed from the published schema and the injector is the only source:
const tools = createNeonTools({
apiKey,
tools: ["projects.get", "branches.delete"] as const,
inject: {
projectId: "project-id",
omitFromSchema: true,
},
});
await tools["projects.get"].execute({});
await tools["branches.delete"].execute({ branch_id: "br-id" });Use a getter when the value is request-scoped. The getter can read the host's own AsyncLocalStorage (this package does not export one):
import { AsyncLocalStorage } from "node:async_hooks";
const grant = new AsyncLocalStorage<{ projectId: string }>();
const tools = createNeonTools({
tools: ["projects.get"] as const,
inject: {
projectId: () => grant.getStore()?.projectId,
omitFromSchema: true,
},
});
await grant.run({ projectId: "project-id" }, () =>
tools["projects.get"].execute({}),
);Injectors only apply to tools that have that path key. projects.list is unchanged. Empty inject values fail closed. Invalid ids still fail the original path schema before fetch.
Injection reads the URL template, so it fills path project_id and branch_id only. Query and body fields with those names stay caller-supplied, including postgres.connectionString's branch_id. omitFromSchema: true does not hide those fields.
Request schemas
Generated request schemas are available independently:
import {
zCreateProjectBody,
zListProjectsQuery,
} from "@neon/tools/schemas";
const query = zListProjectsQuery.parse({ limit: 10 });
const body = zCreateProjectBody.parse({
project: { name: "agent-project" },
});These are the raw OpenAPI request shapes, not the tool input. zCreateProjectBody still wraps fields in project. projects.createAndConnect does not.
These schemas are strict. If a newly added API field is not recognized, upgrade @neon/tools; use @neon/sdk directly until a matching tools release is available.
MCP
Use @neon/tools/mcp with MCP 2.x:
import { McpServer } from "@modelcontextprotocol/server";
import { createNeonTools } from "@neon/tools";
import { registerNeonTools } from "@neon/tools/mcp";
const apiKey = process.env.NEON_API_KEY;
if (!apiKey) throw new Error("NEON_API_KEY is required");
const server = new McpServer({ name: "neon", version: "1.0.0" });
const tools = createNeonTools({
apiKey,
tools: ["projects.list", "projects.createAndConnect"] as const,
});
registerNeonTools(server, tools);registerNeonTools publishes that catalog. MCP 2 inputSchema is JSON Schema without $schema. Generated fields have types, enums, required, and constraints, and no OpenAPI property docs. Date-time fields publish format: "date-time" with pattern: "Z$" in place of Zod's full ISO pattern; validate and execution still check the full format. Fields this package described with .describe() (pooled, finalize, zip) keep that copy.
Every tool also publishes an outputSchema for its structuredContent, { data }:
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"default": { "type": "boolean" },
"parent_id": { "type": ["string", "null"] },
"created_by": { "type": ["object", "null"] }
},
"required": ["id", "name", "default"]
}
}
},
"required": ["data"]
}That is an excerpt of list_branches. The schema types the fields of data, or of each list item, and stops there: a nested object is { "type": "object" }. Enums are strings and extra fields are allowed, so a new API value or field still validates. Descriptions appear only where this package wrote them. The server checks every successful result against this schema, the same check a validating MCP client runs; a result that breaks it becomes an isError result reading Output validation error. Failures return { error } and are not checked.
tool.outputSchema is the full Zod schema of data. Hosts that register tools themselves can publish the same compact form:
import { toMcpOutputJsonSchema, toMcpOutputSchema } from "@neon/tools/mcp";
const json = toMcpOutputJsonSchema(tool.outputSchema); // JSON Schema of { data }
const standard = toMcpOutputSchema(tool.outputSchema); // Standard Schema for MCP 2 registerTool
// A host that removes fields from results before returning them
const stripped = toMcpOutputSchema(tool.outputSchema, {
omit: ["connectionString", "password"],
});omit drops those names from the schema at every level, including from required.
Hosts that convert Zod themselves:
import { compactJsonSchema } from "@neon/tools/mcp";
import * as z from "zod";
import { createNeonTool } from "@neon/tools";
const tool = createNeonTool("projects.update", { apiKey });
const inputSchema = compactJsonSchema(
z.toJSONSchema(tool.inputSchema, { io: "input" }),
);That keeps Zod's full date-time patterns. toMcpInputSchema(tool.inputSchema) from @neon/tools/mcp returns the Standard Schema registerNeonTools registers, with pattern: "Z$" in its JSON Schema.
For a remote MCP server that already authenticated the client, omit apiKey at construction. registerNeonTools sends authInfo.token as the Bearer credential: MCP 2.x http.authInfo.token, MCP 1.x authInfo.token. The host must put a Neon API key or Neon OAuth access token there. A present authInfo with an empty token is an error, not a fall back to a constructor key.
const tools = createNeonTools({
tools: ["projects.list", "projects.createAndConnect"] as const,
});
registerNeonTools(server, tools);This package does not implement an OAuth authorization server. That is mcp-server-neon at mcp.neon.tech.
Existing MCP 1.x servers can use the version-specific entry point:
import { registerNeonTools } from "@neon/tools/mcp-v1";MCP 1.x still receives Zod input schemas, including their .describe() copy. Generated Zod carries only the first sentence of each OpenAPI field description. Use compactJsonSchema if you convert those schemas yourself and need $schema removed.
The adapter returns both text content and object-valued structuredContent. Execution failures use isError: true with { error: { message, name, kind, status, code, source, timeoutMs, requestId, reason, operationId } } when those fields exist on the thrown error. A wait timeout has kind: "timeout" and source: "wait".
MCP annotations are advisory; the protocol does not enforce approval. Tools expose neon/requiresApproval in MCP _meta. Hosts must read that value and enforce their own approval policy before execution.
Eve
Eve requires Node.js 24 or later.
create_and_connect_projects.ts:
import { defineTool } from "eve/tools";
import { createNeonTool } from "@neon/tools";
import { toEveTool } from "@neon/tools/eve";
const apiKey = process.env.NEON_API_KEY;
if (!apiKey) throw new Error("NEON_API_KEY is required");
export default defineTool(
toEveTool(
createNeonTool("projects.createAndConnect", {
apiKey,
}),
),
);Eve uses the filename as the model-facing tool name, so name the file after the published id. The adapter maps approval requirements to Eve's approval hook and forwards its abort signal. abortSignal is optional. Credentials are the constructor apiKey (a string or a callback invoked on every request).
Mastra
Mastra requires Node.js 22.13 or later.
import { createTool } from "@mastra/core/tools";
import { createNeonTools } from "@neon/tools";
import { toMastraTools } from "@neon/tools/mastra";
const apiKey = process.env.NEON_API_KEY;
if (!apiKey) throw new Error("NEON_API_KEY is required");
const neonTools = createNeonTools({
apiKey,
tools: ["projects.list", "projects.createAndConnect"] as const,
});
const configs = toMastraTools(neonTools);
const listProjects = createTool(configs.list_projects);
const createProject = createTool(configs.create_and_connect_projects);The adapter maps approval requirements to Mastra's requireApproval field and forwards its abort signal. Credentials are the constructor apiKey (a string or a callback invoked on every request).
Effect
@neon/tools/effect runs the same tools as Effect v4 Effects. Install the optional peer dependencies effect and @neon/effect; the other entry points don't need them.
import { createNeonTools } from "@neon/tools";
import { retryReadOnlyNetworkFailures, toEffectTools } from "@neon/tools/effect";
import { Effect } from "effect";
const tools = toEffectTools(
createNeonTools({ tools: ["projects.list", "projects.get"] as const }),
{ retry: retryReadOnlyNetworkFailures },
);
const project = tools["projects.get"]
.execute({ project_id: "my-project-id" }, { apiKey })
.pipe(Effect.catchTag("NeonNotFoundError", () => Effect.succeed(undefined)));toEffectTools keeps every tool field and replaces execute, which returns Effect<NeonToolResult, NeonToolError> with a span named NeonTools.<id>. The execution context is the one above without signal: interrupting the fiber cancels the call.
The span records every failure's tag as error.type and fails only for the server's: invalid input, a statement Postgres rejected, and any 4xx from the Neon API keep it ok. NeonClientError fails it, since the SDK uses it for malformed responses as well as its own validation. A NeonSqlError whose SQLSTATE class is 08, 40, 53, 55, 57, 58, F0, or XX (connection, rollback, resources, operator, system, or internal errors) fails it. neon.tool.attempts counts the attempts, and neon.tool.retried_error.type names the failure a retry recovered from.
| Error / _tag | When |
| --- | --- |
| NeonEffectError members (NeonAuthError, NeonNotFoundError, NeonNetworkError, …) | The Neon API or SDK failed; see @neon/effect |
| NeonToolInputError | The input failed the tool's schema; issues lists why |
| NeonSqlError | Postgres rejected the statement; code is the SQLSTATE, with position, detail, hint, where |
| NeonSqlConnectionError | The SQL endpoint failed before Postgres answered |
Anything else a tool throws becomes a defect. toNeonToolError is the mapping, for promise code you wrap yourself.
Retries are off unless you pass retry. The SDK already retries 423, 429, and 503 responses and honours Retry-After, and its ergonomic client resends a GET whose connection dropped before a response. retryReadOnlyNetworkFailures retries a read-only tool that got no response, twice with jittered exponential backoff from 200 ms; mutations are never retried, since one may have reached the API. A retry runs the whole tool again, including onExecute hooks and every page of a list. Pass your own NeonToolRetryPolicy (schedule, times, shouldRetry) for a different policy. Whatever it says, the adapter only retries read-only tools and never a 423, 429, or 503 response.
Safety and binary data
Every non-read operation requires approval. Generated non-read operations are conservatively marked as potentially destructive; migrations.prepare and queryTuning.prepare (which write only to a new temporary branch) and feedback.send are not. Reads that return connection credentials or Neon Auth secrets also require approval.
Binary request fields accept base64 strings:
import { createNeonTool } from "@neon/tools";
const apiKey = process.env.NEON_API_KEY;
if (!apiKey) throw new Error("NEON_API_KEY is required");
const deployFunction = createNeonTool("functions.deploy", { apiKey });
await deployFunction.execute({
project_id: "project-id",
branch_id: "branch-id",
slug: "hello",
zip: "UEsDBA==",
});@neon/tools supports Node.js 20.19 or later. Framework integrations also require the Node.js version supported by that framework.
