Vercel AI SDK Social Media: Posta as a Tool

Add Posta as a tool to a Vercel AI SDK agent and post to eight social networks from any Next.js app, Edge Function, or Node server. MCP via experimental_createMCPClient, or a typed tool() wrapper around the REST API.

Why Posta + Vercel AI SDK?

First-class TypeScript
Define Posta calls with Zod schemas and let the AI SDK enforce parameter types end-to-end.
Stream from a chat UI
Use streamText to stream the agent's response to the browser while Posta executes the tool call server-side.
Edge or Node, your call
The REST wrapper runs anywhere fetch works. The MCP path uses Node stdio by default; SSE/HTTP transport works for Edge.
Closed loop via webhooks
HMAC callbacks fire on publish/fail. Pipe them into a Route Handler that triggers the next AI SDK turn.

Path 1 — Posta MCP via experimental_createMCPClient

The AI SDK exposes an experimental MCP client that loads any MCP server's tools and hands them to generateText or streamText.

// app/api/post/route.ts
import { anthropic } from '@ai-sdk/anthropic'
import { experimental_createMCPClient as createMCPClient, generateText } from 'ai'
import { Experimental_StdioMCPTransport as StdioTransport } from 'ai/mcp-stdio'

export async function POST(req: Request) {
  const { prompt } = await req.json()

  const mcp = await createMCPClient({
    transport: new StdioTransport({
      command: 'npx',
      args: ['-y', 'posta-mcp'],
      env: { POSTA_API_TOKEN: process.env.POSTA_API_TOKEN! },
    }),
  })
  const tools = await mcp.tools()

  try {
    const result = await generateText({
      model: anthropic('claude-sonnet-4-6'),
      tools,
      prompt,
    })
    return Response.json({ text: result.text })
  } finally {
    await mcp.close()
  }
}

Path 2 — Define a typed tool() around the REST API

Simpler when you want one deterministic call exposed to the model. Zod gives the AI SDK a typed schema; the model knows exactly what shape to call.

import { openai } from '@ai-sdk/openai'
import { generateText, tool } from 'ai'
import { z } from 'zod'

const POSTA = 'https://api.getposta.app'
const HEADERS = {
  'Authorization': `Bearer ${process.env.POSTA_API_TOKEN}`,
  'Content-Type': 'application/json',
}

const schedulePost = tool({
  description: 'Create a Posta draft on the given social accounts and schedule it.',
  parameters: z.object({
    caption: z.string().describe('Post caption'),
    socialAccountIds: z.array(z.number()).describe('Posta social_account IDs to publish to'),
    scheduledAt: z.string().describe('ISO 8601 timestamp'),
    mediaIds: z.array(z.string()).optional().describe('Optional Posta media IDs'),
  }),
  execute: async ({ caption, socialAccountIds, scheduledAt, mediaIds }) => {
    // Step 1 — create the draft
    const createRes = await fetch(`${POSTA}/v1/posts`, {
      method: 'POST',
      headers: HEADERS,
      body: JSON.stringify({
        caption,
        socialAccountIds,
        ...(mediaIds?.length ? { mediaIds } : {}),
      }),
    })
    if (!createRes.ok) throw new Error(`Posta create ${createRes.status}`)
    const { id } = await createRes.json()

    // Step 2 — schedule it
    const scheduleRes = await fetch(`${POSTA}/v1/posts/${id}/schedule`, {
      method: 'POST',
      headers: HEADERS,
      body: JSON.stringify({ scheduledAt }),
    })
    if (!scheduleRes.ok) throw new Error(`Posta schedule ${scheduleRes.status}`)
    return { id, scheduledAt }
  },
})

const result = await generateText({
  model: openai('gpt-4o'),
  tools: { schedulePost },
  prompt: 'Schedule a LinkedIn post about our v2 launch for tomorrow 9am CET.',
})

Common patterns

Chat-UI → Posta tool calls
A Next.js chat UI streams an agent's response with streamText; tool calls run server-side and publish via Posta.
Server Action campaign scheduler
A Server Action takes a high-level prompt, runs the agent, and returns the scheduled post IDs to the form.
Webhook Route Handler closes the loop
A Route Handler receives the Posta webhook, verifies the HMAC, and triggers the next AI SDK call.
Multi-step with stepCountIs / continueAfter
Use the AI SDK's multi-step loop control to let the agent draft, fetch images, and schedule across multiple platforms in one run.

Frequently asked questions

How do I let a Vercel AI SDK agent post to social media?
Load the Posta MCP server via experimental_createMCPClient, or define a typed tool() with Zod that POSTs to the Posta REST API. Code for both above.
Edge Functions or Node?
REST wrapper runs anywhere fetch works (Edge, Node, Route Handlers, Server Actions). MCP stdio transport needs Node; SSE/HTTP MCP transport works on Edge.
Does streamText work?
Yes — pass the Posta tools to streamText. Tool calls run server-side, the UI streams the conversation.
TypeScript or JavaScript?
Either; the AI SDK is TypeScript-first. Examples here are TypeScript.

Ship Posta in your next Vercel AI SDK app

14-day free trial. MCP server, Claude Code skill, and n8n node are free and open source.