Skip to content

Next.js and React apps

With output: 'export', add the script in app/layout.tsx, then index the exported site:

app/layout.tsx
import Script from 'next/script';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script src="https://cdn.jsdelivr.net/npm/agentprose@0/dist/agentprose.min.js" strategy="afterInteractive" data-subscribe="/newsletter" />
</body>
</html>
);
}
Terminal window
next build && npx agentprose build out --base-url https://example.com/

Dynamic sites: the npm API with your own source

Section titled “Dynamic sites: the npm API with your own source”

When posts come from your API, give agentprose an adapter that calls it, and create the tools once on the client:

components/AgentTools.tsx
'use client';
import { useEffect } from 'react';
import { createAgentTools } from 'agentprose';
import { defineAdapter } from 'agentprose/adapters/generic';
const api = defineAdapter({
name: 'next-api',
list: async ({ limit = 20, offset = 0 } = {}) => fetch(`/api/posts?limit=${limit}&offset=${offset}`).then((r) => r.json()),
get: async ({ slug, url }) => fetch(`/api/posts/${slug ?? url?.split('/').filter(Boolean).pop()}`).then((r) => (r.ok ? r.json() : null)),
});
export function AgentTools() {
useEffect(() => {
const tools = createAgentTools({ adapter: api, links: { subscribe: '/newsletter' } });
return () => tools.destroy(); // unregisters on unmount
}, []);
return null;
}

Render <AgentTools /> once in your root layout. Tools read location and the DOM when they run, so get_current_article follows client-side navigation without re-registering.

list must return { items, total? } with at least url and title per item; get returns a post with markdown or html. See Adapters.

The same pattern works anywhere: call createAgentTools() once when the app mounts on the client, and destroy() when it unmounts. Don’t call it during server-side rendering (it needs document); pass register: false if you want to build the tools without registering them, for example in tests.