Vercel AI SDK Integration

Post to X from the
Vercel AI SDK

Write one tool() that calls the OpenTweet API, or point createMCPClient at our MCP server for 36 tools. Your agent posts, schedules, and threads on X. No X developer account, no OAuth, no per-post billing.

Get your API key

7-day free trial. No X developer account needed.

What your AI SDK agent can do on X

Write tools for the actions you need, or get all of them at once through MCP.

Post now

One execute call with publish_now: true and the tweet is live.

Schedule

Send scheduled_date instead and the post queues for later.

Threads

Set is_thread with a thread_tweets array to publish a connected thread.

MCP native

createMCPClient pulls all 36 OpenTweet tools with zero tool code.

Analytics

MCP analytics tools let the agent read its results and adapt.

No X API

No developer account, no OAuth, no token refresh to build.

Option 1: a postTweet tool() in 20 lines

The tool() helper takes a zod inputSchema and an execute function. This one POSTs to the OpenTweet API with your ot_ key and returns the live tweet URL to the model. Works the same in AI SDK v5 and v6, with generateText or streamText.

import { streamText, tool, stepCountIs } from 'ai';
import { z } from 'zod';

const postTweet = tool({
  description: 'Publish a tweet to X (Twitter) immediately.',
  inputSchema: z.object({
    text: z.string().max(280).describe('The tweet text, max 280 characters'),
  }),
  execute: async ({ text }) => {
    const res = await fetch('https://opentweet.io/api/v1/posts', {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${process.env.OPENTWEET_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ text, publish_now: true }),
    });
    if (!res.ok) throw new Error(`OpenTweet API error ${res.status}`);
    const data = await res.json();
    return { posted: true, url: data.posts[0].url };
  },
});

const result = streamText({
  model: 'anthropic/claude-sonnet-4.5',
  tools: { postTweet },
  stopWhen: stepCountIs(5),
  prompt: 'Write and post a short tweet announcing that our v2 API is live.',
});

for await (const chunk of result.textStream) process.stdout.write(chunk);

To schedule instead of posting now, send scheduled_date (ISO 8601) in place of publish_now. For threads, add is_thread: true and a thread_tweets array.

Option 2: MCP client, all 36 tools, zero tool code

AI SDK 6 ships a stable createMCPClient with an HTTP transport. Point it at the hosted OpenTweet MCP server and mcpClient.tools() returns create_tweet, create_thread, schedule_tweet, batch_schedule, create_article, evergreen and analytics tools, ready to pass to generateText.

import { generateText, stepCountIs } from 'ai';
import { createMCPClient } from '@ai-sdk/mcp';

const mcpClient = await createMCPClient({
  transport: {
    type: 'http',
    url: 'https://mcp.opentweet.io/mcp',
    headers: { Authorization: 'Bearer ot_your_api_key' },
  },
});

try {
  const tools = await mcpClient.tools();

  const { text } = await generateText({
    model: 'anthropic/claude-sonnet-4.5',
    tools,
    stopWhen: stepCountIs(10),
    prompt: 'Draft a 4-tweet thread about our launch and schedule it for tomorrow at 9am UTC.',
  });

  console.log(text);
} finally {
  await mcpClient.close();
}

On AI SDK v5, import experimental_createMCPClient from ai instead. Same transport, same tools. Full tool list in the MCP docs.

Which one should you use?

Both hit the same OpenTweet backend with the same ot_ key. The difference is how much surface your agent needs.

Write a tool() when

  • Your agent needs one or two actions and you want full control over the schema the model sees.
  • You are deploying to an edge or serverless route and want zero extra dependencies beyond fetch.
  • You want to post-process the API response before it reaches the model.

Use the MCP client when

  • The agent should schedule, thread, batch, publish articles, or read analytics, not just post.
  • You want new OpenTweet tools to appear without redeploying your app.
  • You already run other MCP servers and want one consistent integration pattern.

Building with a different framework? The same two patterns work in the OpenAI Agents SDK and every major agent framework. See post to X from AI agent frameworks for the full matrix.

Perfect for

Next.js apps with agent routes

Add the postTweet tool to an existing streamText route handler and your product can tweet on behalf of users of your own X account.

Autonomous content agents

A cron-triggered agent drafts from your changelog or RSS and schedules the week through batch_schedule.

Launch and ops bots

A deploy hook runs generateText with the tool and posts release notes or incident updates the moment they happen.

Multi-step agent loops

With stopWhen and the MCP toolset, one run can research, draft a thread, schedule it, and report back.

Frequently asked questions

How does a Vercel AI SDK agent post to Twitter?

Define a tool() with a zod inputSchema and an execute function that POSTs to the OpenTweet API with your ot_ bearer key, then pass it to streamText or generateText. When the model decides to post, the tweet goes live. There is no X developer account or OAuth involved.

Should I write a tool() or use the MCP client?

Write a tool() when your agent only needs one or two actions, like posting a tweet. Use the MCP client when you want the full surface: threads, scheduling, batch scheduling, articles, evergreen queues, and analytics arrive as 36 ready-made tools with zero tool code.

Does this work with AI SDK v5 and v6?

Yes. The tool() helper with inputSchema is identical in both. For MCP, AI SDK 6 ships a stable createMCPClient in the @ai-sdk/mcp package, while v5 exposes the same thing as experimental_createMCPClient from the ai package. Both accept the HTTP transport used on this page.

Do I need the X (Twitter) API or a developer account?

No. OpenTweet replaces the X developer account, OAuth 2.0 flow, and token refresh with a single bearer key. Your agent posts to X through OpenTweet, so you never touch the pay-per-use X API.

Can the agent schedule tweets instead of posting immediately?

Yes. Send scheduled_date (ISO 8601) instead of publish_now to the REST API, or let the model call the schedule_tweet and batch_schedule MCP tools. Threads work the same way with is_thread and thread_tweets.

What does it cost?

OpenTweet plans start at $11.99/month with a 7-day free trial. There is no per-post charge at normal volumes, unlike the pay-per-use X API. See the pricing page for current plans.

Ship an AI SDK agent that posts to X

Connect X, copy your key, and paste the tool into your agent today.

Start your 7-day free trial