The first time I asked Claude to make a carousel through our MCP server, it wrote seven solid slides. Then it had to tell me the honest truth: it had no idea what they looked like.

Fair enough. It had written text into a JSON blob. Somewhere else, a browser would turn that into images. The agent had no browser, so it was designing blind.

This post is about fixing that: rendering HTML slides on the server so an AI agent can look at its own work, catch its own mistakes, and only then schedule the post. The stack is Rails, Sidekiq and a headless Chromium container, but the ideas work anywhere.

The problem: the renderer lived in the browser

Our carousel studio draws every slide as plain HTML and CSS: 1080 by 1350 pixels, one design system, around 80 themes. When a person clicks Export, the browser turns each slide into a JPEG with an html-to-image library and uploads it.

That works great for humans. It's useless for an agent. An MCP tool call runs on the server. There is no tab, no DOM, no click. So the agent could create and edit carousels, but every image it handed back was either missing or stale.

I had three options:

  1. Rewrite the renderer for the server (canvas, Satori, something else).
  2. Ask the user to open the editor and export.
  3. Run the exact same renderer on the server, in a real browser.

Option 1 means two renderers that drift apart forever. Option 2 defeats the point of an agent. So, option 3.

Same renderer, real Chromium, its own box

I didn't want Chromium inside the Rails image. It's big, it eats memory, and if it crashes I don't want it taking a web worker with it. So it runs as its own container: Gotenberg, which wraps headless Chromium in a small HTTP API.

renderer: image: gotenberg/gotenberg:8.39.0-chromium command: - gotenberg - --api-timeout=60s - --chromium-restart-after=50 - --chromium-allow-list=^file:///tmp/.*|^https://fonts\.(googleapis|gstatic)\.com/.* mem_limit: 1g cpus: 1 

No published ports, a memory cap, a restart every 50 renders to keep Chromium fresh, and an allow-list so the page can only load its own files and Google Fonts. Nothing else on the internet.

The flow for one render:

  1. The agent calls a tool like carousels_render. Rails answers right away with a job id.
  2. A Sidekiq job builds one self-contained HTML page: the same renderer JavaScript the browser uses, the same CSS, the slide data as JSON, and every image as a local file.
  3. The job posts that page to Gotenberg and gets one PNG back.
  4. ImageMagick cuts it into slides, and the job saves them as JPEGs.
  5. The agent polls the job id and gets image URLs back.

The render jobs run on their own Sidekiq queue, so a slow render never delays someone's scheduled post.

One screenshot, many slides

The naive version takes one screenshot per slide. With 7 slides that's 7 Chromium page loads, 7 font downloads and 7 chances to time out.

Instead, the page stacks every slide on top of each other, with no gaps:

body { margin: 0; width: 1080px; } .slide { width: 1080px; height: 1350px; overflow: hidden; } 

Then Gotenberg takes a single screenshot of the whole page, 1080 pixels wide and 1350 times the slide count tall. One page load, fonts fetched once, and every slide rendered in the same browser state.

Cutting it back into slides is one ImageMagick command:

magick page.png -crop 1080x1350 +repage -quality 92 slide_%02d.jpg 

The same trick made a second feature almost free. The agent can ask to see one slide in four different designs before choosing. That's just four copies of the same slide, each with a different theme, stacked on one page. One render, four previews, a few seconds.

Three bugs that cost me hours

1. Every image came back blank. In the browser export, images get crossOrigin = "anonymous" so html-to-image can read their pixels. On the server the page loads from file://, and Chromium treats a cross-origin request from a file page as a failure. Photos, logos, all gone, with no error. The fix was a one-liner: the server page is the screenshot, so it doesn't need to read pixels, so no crossOrigin there.

2. A missing image hung the render for a full minute. Gotenberg waits until an expression is true (window.renderReady === true) before taking the screenshot. When an image failed, my code threw an error before setting the flag, so Gotenberg sat there until its 60 second timeout. Now the page sets the flag first, then throws. With failOnConsoleExceptions on, Gotenberg answers with an error right away, and the agent gets "this image can't be used" in a second instead of a vague timeout.

3. A job released someone else's lock. Generation takes a Redis lock per account so nobody runs two at once. If a job ran longer than the lock's TTL, a second job could take the lock, and then the first job's DEL at the end would delete the second job's lock. The fix is to delete only if the value is still yours, atomically:

if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) end return 0 

None of these showed up in my own testing. A teammate's code review caught all three before any customer did, which is a good argument for code review.

Giving the agent eyes

Rendering images is only half of it. The agent has to actually look at them. A few things made the difference:

Return the images, not just URLs. MCP tool results can carry images. When the agent asks for a preview of four designs, it gets four small images back in the tool result, each labelled with the design's name, so it can compare them like a person would.

Tell it to look before it posts. The tool's answer includes a plain next step: look at every slide before showing or scheduling them, and fix anything that's off. Agents follow instructions in tool results surprisingly well. With the images in front of it, Claude can spot a crowded slide or a cover where the title fights the background, edit it and render again.

Hand it the boring fields. When a carousel is ready, the tool also returns a LinkedIn document title and alt text for every slide, built from the slides' own text. Before, the agent had to write these itself or skip them. Now they're just there, ready to pass along.

Keep status calls read-only. My first version of the status tool started a render if the images were missing. Helpful, I thought. In practice an agent polling every few seconds kicked off renders nobody asked for. A status call now only reports; starting work is always its own explicit tool.

The writing problem

Once the slides looked right, the next complaint was the words. A teammate tried it end to end and the first carousel opened with "Unlock the Power of AI in Coding!" Every slide had an exclamation mark. Two slides said the same thing.

A better prompt helped, but prompts are suggestions. So after the first draft, a small Ruby check looks for the things a reader notices first:

  • exclamation marks on slides
  • a list of hype phrases (unlock, game changer, supercharge, level up, and friends)
  • two slides making the same point

The repeat check is simple word overlap between slides:

words = slides.map { |text| text.downcase.scan(/\p{L}{4,}/).uniq } shared = (words[a] & words[b]).size all = (words[a] | words[b]).size repeat = all >= 6 && shared.fdiv(all) >= 0.5 

If the draft has problems, the AI gets exactly one revision, with the list of problems. The important part is what happens next: the revision is only kept if it has no more problems than the draft and at least as many usable slides. If the revision call fails, times out, or comes back broken, the user gets the first draft. The check can make a carousel better. It can never make one worse, and it can never fail the whole job.

What I'd tell anyone building tools for agents

  1. One renderer, two places. Running the browser's code in a headless browser beat writing a second renderer by a mile. The slides an agent makes look exactly like the ones a person exports.
  2. Let the agent check its own work. The biggest quality jump didn't come from a better model or prompt. It came from giving the agent the finished images and asking it to look.
  3. Fail fast and say why. "This image can't be used" in one second is worth more than a timeout after sixty. Agents recover well from clear errors and badly from vague ones.
  4. Make the AI's second try safe. Automatic revisions are great until one comes back worse. Only keep it when it's measurably better, and fall back to the first draft on any error.
  5. Isolate the heavy stuff. Chromium in its own container with a memory cap, render jobs on their own queue, and a kill switch in an env var. When something goes wrong, nothing else notices.

Where this lives

All of this runs in RobinReach, the social media scheduler I build. If you use Claude or ChatGPT, you can connect it over MCP and ask for a carousel in plain words. It writes it, renders it, looks at it, fixes it, and schedules it once you say yes.

The pattern isn't specific to carousels, though. Any time an agent makes something visual (a chart, a thumbnail, an email, a landing page), the same move works: render it the way a human would see it, and hand the agent the picture.