Skip to main content
Build your agent on Neon and provision backends you fully control. See how Ideavo does it
Function templatesFirst Party

MCP server backed by Postgres

A Hono MCP server exposing create, list, update, and delete tools backed by a Postgres database.

About this template

This template is a Model Context Protocol server that runs as a Neon Function. It uses Hono and the MCP TypeScript SDK to serve streamable HTTP at /mcp. It stores a todo list in Lakebase Postgres through a pg pool that it registers with attachDatabasePool from @neon/functions.

The server exposes four tools:

ToolDescription
create_todoInsert a todo. title is required; notes is optional.
list_todosList todos, optionally filtered by completion or by a search of the title or notes. limit defaults to 20, with a maximum of 50.
update_todoUpdate the fields you pass for a todo id, including completed. You can't clear the title or notes.
delete_todoDelete a todo by id and return the deleted row.

Every request to /mcp must send Authorization: Bearer <MCP_API_KEY>.

Scaffold it

Install the latest Neon CLI, then create the function from the registry:

npm install -g neon@latest
neon login
neon link
neon functions new mcp-postgres --slug mcppostgres

The CLI writes functions/mcppostgres/index.ts, declares the function in neon.ts, and adds MCP_API_KEY to .env.example.

neon.ts points the function at the module and passes the API key through as a function env variable:

import { defineConfig } from "@neon/config/v1";

export default defineConfig({
  functions: {
    mcppostgres: {
      name: "MCP server backed by Postgres",
      source: "./functions/mcppostgres/index.ts",
      env: { MCP_API_KEY: { value: process.env.MCP_API_KEY, required: true } },
    },
  },
});

The function reads DATABASE_URL from the linked branch, so run neon link before you start or deploy it.

Set the API key

Generate a token and store it in .env.local:

export MCP_API_KEY="$(openssl rand -hex 32)"
printf 'MCP_API_KEY=%s\n' "$MCP_API_KEY" >> .env.local
VariableDescriptionRequired
MCP_API_KEYRandom token (for example, from openssl rand -hex 32) that MCP clients send as Authorization: Bearer <token>; required because the function URL is public.Yes

neon dev loads .env.local, but neon deploy needs --env .env.local to resolve the value when it evaluates neon.ts. A missing key and a wrong key both return 401. If every request fails, check that MCP_API_KEY was set in the environment you deployed with.

Run locally

neon dev

neon dev serves every function declared in neon.ts and prints its URL (the default is http://localhost:8787). The MCP endpoint is /mcp:

curl http://localhost:8787/
# MCP server. Connect an MCP client to /mcp with an Authorization: Bearer token.

curl -i http://localhost:8787/mcp
# HTTP/1.1 401 Unauthorized

Connect an MCP client

Any client that speaks the streamable HTTP transport can connect to /mcp and send the bearer token. For example, with mcporter:

npm install -g mcporter

mcporter list http://localhost:8787/mcp --schema --allow-http \
  --header "Authorization=Bearer $MCP_API_KEY"

mcporter call "http://localhost:8787/mcp.create_todo" --allow-http \
  --header "Authorization=Bearer $MCP_API_KEY" \
  title="Write the rollback notes" notes="Include the migration step"

You only need --allow-http for local plain-HTTP URLs. To write the config for Cursor, Claude Code, or another supported client instead, use add-mcp:

npx add-mcp http://localhost:8787/mcp \
  --header "Authorization: Bearer $MCP_API_KEY" \
  --name mcp-postgres -a cursor

add-mcp writes the token into the client config.

Deploy

neon deploy --env .env.local

--env .env.local lets neon.ts resolve MCP_API_KEY during deployment, and neon functions get prints the deployed function's URL:

neon functions get mcppostgres

Then connect a client to https://<function-url>/mcp with the same bearer token, or install it with add-mcp:

npx add-mcp https://<function-url>/mcp \
  --header "Authorization: Bearer $MCP_API_KEY" \
  --name mcp-postgres -a cursor

A Neon Function URL is public, so this template protects /mcp with a static bearer token.

Data model

The first tool call creates the table, so there is no migration step. A single cached promise runs the CREATE TABLE IF NOT EXISTS once per function instance:

CREATE TABLE IF NOT EXISTS mcp_todos (
  id serial PRIMARY KEY,
  title text NOT NULL,
  notes text,
  completed boolean NOT NULL DEFAULT false,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);