@markdown-for-agents/fastify
Fastify plugin for markdown-for-agents — a runtime-agnostic HTML to Markdown converter built for AI agents.

markdown-for-agents converts HTML to clean, token-efficient Markdown for AI agents — typically saving 80–90% of tokens. This package adds automatic content negotiation to your Fastify app via
Accept: text/markdown. Try the playground to see the core conversion in action.
Add one line to your Fastify app and AI agents get clean, token-efficient Markdown instead of HTML. Normal browser requests pass through untouched.
How it works
The plugin uses content negotiation. When a client sends Accept: text/markdown, HTML responses are automatically converted to Markdown. The response includes:
Content-Type: text/markdown; charset=utf-8x-markdown-tokensheader with the token countETagheader with a content hash for cache validationVary: Acceptheader so CDNs cache HTML and Markdown separatelycontent-signalheader with publisher consent signals (when configured)
Install
npm install @markdown-for-agents/fastify markdown-for-agents
markdown-for-agentsis a peer dependency — you may already have it installed.
Usage
import Fastify from 'fastify';
import { markdown } from '@markdown-for-agents/fastify';
const fastify = Fastify();
fastify.register(markdown({ extract: true }));
fastify.get('/', async (request, reply) => {
reply.type('text/html');
return '<h1>Hello</h1>';
});
fastify.listen({ port: 3000 });# Normal HTML response
curl http://localhost:3000
# Markdown response for AI agents
curl -H "Accept: text/markdown" http://localhost:3000Full working example: See
examples/fastify/for a complete Fastify app with integration tests.
Options
Accepts all markdown-for-agents options:
fastify.register(
markdown({
// Strip nav, ads, sidebars, cookie banners
extract: true,
// Resolve relative URLs
baseUrl: 'https://example.com',
// Remove duplicate content blocks
deduplicate: true,
// Custom token counter (e.g. tiktoken)
tokenCounter: text => ({ tokens: enc.encode(text).length, characters: text.length, words: text.split(/\s+/).filter(Boolean).length }),
// Publisher consent signal header
contentSignal: { aiTrain: true, search: true, aiInput: true }
})
);Other frameworks
| Package | Framework |
|---|---|
@markdown-for-agents/express | Express |
@markdown-for-agents/hono | Hono |
@markdown-for-agents/nextjs | Next.js |
@markdown-for-agents/web | Web Standard (Cloudflare Workers, Deno, Bun) |
License
MIT