Developer Guide

How to Tweet from the
OpenAI Agents SDK

Give your agent a tweet tool two ways: a @function_tool that POSTs to the OpenTweet REST API, or the OpenTweet MCP server attached to the agent. Copy-ready Python below. No X developer account needed.

Last updated: July 2026

Get your API key

7-day free trial. Cancel anytime.

How does an OpenAI Agents SDK agent tweet?

An OpenAI Agents SDK agent tweets by calling a tool you give it. The SDK does not post to X by itself, so you attach a posting capability and the agent invokes it when its plan calls for a tweet. There are two clean ways to do that, and you pick one.

OpenTweet is a Twitter/X scheduler and posting API that holds your X connection, so the agent only ever talks to OpenTweet with one ot_ key.

  • Path A, a function tool: decorate a Python function with @function_tool that POSTs to the OpenTweet REST API.
  • Path B, MCP: attach the OpenTweet MCP server to the agent via the SDK MCP support, for the full set of 36 X tools.

Install the SDK

Both paths use the OpenAI Agents SDK for Python. Install it with pip install openai-agents and set your OPENAI_API_KEY.

Step-by-step: give the agent a tweet tool

1

Get your OpenTweet API key

Create an ot_ key at /developer and connect your X account once. Keep both keys in environment variables.

bash
pip install openai-agents requests

export OPENAI_API_KEY="sk-your_key"
export OPENTWEET_API_KEY="ot_your_key"
2

Path A: define a @function_tool

Decorate a function with @function_tool. The SDK reads its type hints and docstring to build the tool schema, so the agent knows when and how to call it. The body POSTs to OpenTweet.

agent_tweet_tool.py
import os, requests
from agents import Agent, Runner, function_tool

OPENTWEET = "https://opentweet.io/api/v1/posts"
HEADERS = {"Authorization": f"Bearer {os.environ['OPENTWEET_API_KEY']}"}

@function_tool
def post_tweet(text: str, scheduled_date: str | None = None) -> str:
    """Post a tweet to the user's connected X account.

    Args:
        text: The tweet text, up to 280 characters.
        scheduled_date: Optional ISO 8601 time to schedule instead of posting now.
    """
    body = {"text": text}
    if scheduled_date:
        body["scheduled_date"] = scheduled_date
    else:
        body["publish_now"] = True
    r = requests.post(OPENTWEET, headers=HEADERS, json=body, timeout=30)
    r.raise_for_status()
    data = r.json()
    return f"Posted, status={data.get('status')}, id={data.get('id')}"

agent = Agent(
    name="X poster",
    instructions="You post to X when asked. Keep tweets under 280 characters.",
    tools=[post_tweet],
)

result = Runner.run_sync(agent, "Tweet: shipped the OpenAI Agents SDK integration today.")
print(result.final_output)
3

Path B: attach the OpenTweet MCP server

The SDK ships MCP support. Connect the hosted server with MCPServerStreamableHttp and pass it in the agent mcp_servers list. The agent then sees all 36 X tools with no tool code to maintain.

agent_mcp.py
import os, asyncio
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

async def main():
    async with MCPServerStreamableHttp(
        name="opentweet",
        params={
            "url": "https://mcp.opentweet.io/mcp",
            "headers": {"Authorization": f"Bearer {os.environ['OPENTWEET_API_KEY']}"},
        },
    ) as opentweet:
        agent = Agent(
            name="X poster",
            instructions="You post and schedule on X using the OpenTweet tools.",
            mcp_servers=[opentweet],
        )
        result = await Runner.run(agent, "Schedule a tweet for tomorrow 9am about our launch.")
        print(result.final_output)

asyncio.run(main())

The one URL you paste is https://mcp.opentweet.io/mcp, a Streamable HTTP endpoint.

What does it cost, and why no X developer account?

OpenTweet is a flat monthly fee with no per-post charge. Building against the X API directly means a developer app and pay-per-use billing on every post.

FactorAgents SDK + OpenTweetAgents SDK + X API directly
PriceFlat $11.99/mo (Pro)X API billing, tiered per project
Per-post fee$0 per post~$0.015 per post, ~$0.20 with a link
X developer accountNot neededRequired, with billing enrollment
Auth in your toolOne ot_ Bearer keyOAuth2 user token, refreshed by you
SchedulingIncluded via scheduled_dateNot supported by the X API
Tools available36 via MCP, or one function toolWhatever you hand-build

Model cost is separate

The $11.99/mo OpenTweet fee covers posting and scheduling. Your OpenAI model usage is billed separately by OpenAI. The tweet tool itself adds no per-post fee.

Draft or schedule for safety

For an autonomous agent, have the tool schedule or save a draft instead of publishing now, then review in the OpenTweet dashboard. Omit publish_now and set scheduled_date to keep a human in the loop.

Frequently asked questions

How do I make an OpenAI Agents SDK agent tweet?

Give the agent a tweet tool. The simplest path is a @function_tool: decorate a Python function whose body POSTs to https://opentweet.io/api/v1/posts with your ot_ Bearer key, then pass it to your Agent in the tools list. When the agent decides to tweet, the SDK calls your function and posts to your connected X account.

Can I use an MCP server with the OpenAI Agents SDK instead of a function tool?

Yes. The OpenAI Agents SDK supports MCP servers. Attach the OpenTweet hosted server with an MCPServerStreamableHttp connection pointing at https://mcp.opentweet.io/mcp, pass it in the mcp_servers list of your Agent, and the agent gets 36 X tools, including post and schedule, with no custom tool code.

Do I need an X developer account to tweet from the Agents SDK?

No. OpenTweet holds the X connection, so the agent only ever calls the OpenTweet API with your ot_ key. You never create an X developer app or enroll in X API billing. Building against the X API directly instead would require a developer app, OAuth, and pay-per-use billing.

Which is better, the function_tool path or the MCP path?

Use @function_tool when you want a single tightly scoped tweet tool with full control over the request. Use the MCP path when you want the full set of 36 X tools, including scheduling, threads, analytics, and media, without writing or maintaining tool code. Both use the same ot_ key.

How much does it cost to tweet from the OpenAI Agents SDK?

OpenTweet is a flat $11.99/mo on the Pro plan, which includes the REST API and MCP access with no per-post fee. That is separate from your OpenAI model usage. Posting through the X API directly instead adds about $0.015 per post, or about $0.20 per post if the tweet contains a link.

Ship an agent that tweets

Get your ot_ API key and let your OpenAI Agents SDK agent post to X over a function tool or MCP. No X developer account, no per-post fees.

Get your API key

7-day free trial. Cancel anytime.