Build a classifieds newsletter adapter
Register the classifieds subtype, process one queued issue into articles, and give the reader a view for those articles. The example uses the existing newsletter and article tables and needs no network or model call to parse an issue.
1. Define one listing line
Section titled “1. Define one listing line”The sample bulletin sends a title and a stable URL on each plain-text line. The local fixture accepts this two-listing issue.
Reading desk | https://example.test/listings/desk-7Desk lamp | https://example.test/listings/lamp-2The parser accepts only the synthetic publisher’s listing path, collapses duplicate keys within an issue and ignores other lines. A body over 16 KiB, an HTML-only issue or an issue with no valid lines produces no listings.
api/src/adapters/examples/classifieds/parse.ts
export interface Listing { key: string; title: string;}
// The synthetic publisher sends one "title | https://example.test/listings/key" per line.export function parseListings(body: string): Listing[] { if (Buffer.byteLength(body, "utf8") > 16_384) return []; const listings = new Map<string, Listing>(); for (const line of body.split(/\r?\n/)) { const match = /^([^|\p{Cc}]{1,120}) \| https:\/\/example\.test\/listings\/([a-z0-9-]{1,64})$/u.exec(line.trim()); if (!match) continue; const [, title, key] = match; if (title.trim()) listings.set(key, { key, title: title.trim() }); } return [...listings.values()];}2. Process one queued issue
Section titled “2. Process one queued issue”The core webhook verifies the signature, matches the receiving address, checks whether the feed is paused and writes a pending row to newsletter_raw_emails. The adapter joins that row to its owning feed and locks one issue per call. Concurrent calls skip a locked issue. In a single transaction it inserts each listing as an article and records the number of distinct keys seen in the issue. The article URL identifies the listing within its feed, so receiving the same key again preserves the first article. The article body links to the publisher’s URL.
api/src/adapters/examples/classifieds/worker.ts
import type { Pool } from "pg";import { escapeHtml } from "../../../lib/html.js";import { parseListings } from "./parse.js";
export const subtype = "classifieds";
export async function processNextIssue(pool: Pool) { const client = await pool.connect(); try { await client.query("BEGIN"); const { rows: [issue] } = await client.query( `SELECT r.id, r.user_id, r.feed_id, r.email_date, left(r.text_body, 16385) AS body FROM newsletter_raw_emails r JOIN feeds f ON f.id = r.feed_id AND f.user_id = r.user_id WHERE r.status = 'pending' AND f.feed_type = 'newsletter' AND f.feed_subtype = $1 AND (f.extraction_config->'paused') IS DISTINCT FROM 'true'::jsonb ORDER BY r.created_at, r.id LIMIT 1 FOR UPDATE OF r SKIP LOCKED`, [subtype]); if (!issue) { await client.query("COMMIT"); return { processed: false }; } const listings = parseListings(issue.body ?? ""); for (const { key, title } of listings) { const content = `<a href="https://example.test/listings/${key}">${escapeHtml(title)}</a>`; await client.query( `INSERT INTO articles (user_id, feed_id, url, title, content, published_at, content_source) VALUES ($1, $2, $3, $4, $5, COALESCE($6, now()), 'newsletter') ON CONFLICT (user_id, url) DO NOTHING`, [issue.user_id, issue.feed_id, `newsletter://${issue.feed_id}/${key}`, title, content, issue.email_date]); } await client.query( `UPDATE newsletter_raw_emails SET status = $2, listing_count = $3, error = $4, processing_finished_at = now() WHERE id = $1`, [issue.id, listings.length ? "done" : "failed", listings.length, listings.length ? null : "Expected a plain-text issue with listing lines within 16 KiB."]); await client.query("COMMIT"); return { processed: true, listings: listings.length }; } catch (error) { await client.query("ROLLBACK"); throw error; } finally { client.release(); }}An issue with no accepted listings is marked failed. A database error rolls back both the inserts and queue update, leaving the issue eligible for another call.
3. Register the worker and an optional MCP collection
Section titled “3. Register the worker and an optional MCP collection”The API adapter supplies the newsletter’s discovery labels and mounts a cron-protected worker at POST /api/content/classifieds/process-next. Each authorized call processes at most one issue. To drain a queue automatically, the installation must arrange repeated authenticated calls.
api/src/adapters/examples/classifieds/index.ts
import type { FeatureAdapter } from "../../types.js";import { getPool } from "../../../lib/db.js";import { processNextIssue, subtype } from "./worker.js";
export const classifieds: FeatureAdapter = { newsletter: { subtype, icon: "🏷️", sourceLabel: "classifieds newsletters", libraryLabel: "listing articles", toolHelp: "Use feed_subtype=classifieds for plain-text listing newsletters.", }, contentRoutes(router, { requireCron }) { router.post("/classifieds/process-next", requireCron, async c => c.json(await processNextIssue(getPool()))); }, library: [{ resource: { kind: "classified_listing", description: "Articles from your classifieds newsletters.", scopes: ["articles:read"], from: "articles r JOIN feeds f ON f.id=r.feed_id AND f.user_id=$1", guard: "f.feed_type='newsletter' AND f.feed_subtype='classifieds'", fields: "r.id AS article_id,r.title,r.content,r.published_at", title: "r.title", parents: { article_id: "r.id", feed_id: "f.id" }, searchable: false, }, searchColumns: [], }],};In api/src/adapters/registered.ts, import the example and include it in the array. This registers it as the installation’s only API adapter.
import type { FeatureAdapter } from "./types.js";import { classifieds } from "./examples/classifieds/index.js";
export const adapters: readonly FeatureAdapter[] = [classifieds];The optional library entry gives clients with articles:read a classified_listing collection. Call list_library_records with that collection and the feed ID, then pass a returned URI to fetch. The projection checks ownership of the article and feed. It supports listing and fetching by ID. For full-text search on a new collection, supply indexed expressions and enable search in its registration.
4. Register the reader view
Section titled “4. Register the reader view”The reader reuses the newsletter creation dialog with the same subtype. Its feed view gets the current articles, the selection callback and pagination from the article list. Selecting a title opens the article in the existing reader.
src/adapters/examples/classifieds/index.tsx
import { lazy } from "react";import { Tags } from "lucide-react";import type { UiAdapter } from "../../types";
const AddNewsletter = lazy(() => import("@/components/AddNewsletter"));const subtype = "classifieds";
export const classifieds: UiAdapter = { newsletter: { subtype, nav: { key: "listing_bulletins", label: "Classifieds", emoji: "🏷️" }, Icon: Tags, emptyText: "No classifieds feeds yet.", Add: props => <AddNewsletter {...props} feedSubtype={subtype} />, View: ({ articles, loading, onSelectArticle, hasMore, loadingMore, onLoadMore }) => { if (loading) return <p>Loading listings…</p>; return <section aria-label="Classified listings"> {articles.length === 0 && <p>No listings received yet.</p>} <ul>{articles.map(article => <li key={article.id}><button onClick={() => onSelectArticle(article.id)}>{article.title}</button></li> )}</ul> {hasMore && <button disabled={loadingMore} onClick={onLoadMore}>Load more listings</button>} </section>; }, catalog: { id: subtype, name: "Classifieds", appliesTo: "Plain-text classifieds newsletters", description: "Saves one article per listing.", hasDedicatedSchema: false, codePath: "api/src/adapters/examples/classifieds/worker.ts", }, prompt: { id: "classifieds_summary", label: "Listing summary", description: "Suggested listing summary.", fallback: "Summarize the available items.", help: "The deterministic listing parser uses no model.", }, },};In src/adapters/registered.ts, add the corresponding reader contribution.
import type { UiAdapter } from "./types";import { classifieds } from "./examples/classifieds";
export const adapters: readonly UiAdapter[] = [classifieds];Rebuild both sides after registering the adapter. In the reader, add a feed under Classifieds and copy the receiving address. With newsletter intake configured, a matching issue enters the queue. After the worker runs, open the feed and select Reading desk.
5. Verify locally
Section titled “5. Verify locally”From the repository root, run the example tests. The API integration test requires MCP_TEST_DATABASE_URL pointing to a disposable PostgreSQL database named mcp_test on localhost with a role allowed to create fixture databases. It creates its own schema from tools/dev/schema.sql. The tests select the example registration in isolation.
npm --prefix api test -- src/adapters/examples/classifiedsnpm test -- src/adapters/examples/classifiedsThe API test delivers a signed synthetic issue, calls the worker concurrently, repeats a listing and verifies the owning reader’s MCP records. The reader tests exercise creation, article selection and loading the next page. Adapter interface reference