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
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_toolthat 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 withpip install openai-agents and set your OPENAI_API_KEY.Step-by-step: give the agent a tweet tool
Get your OpenTweet API key
Create an ot_ key at /developer and connect your X account once. Keep both keys in environment variables.
pip install openai-agents requests
export OPENAI_API_KEY="sk-your_key"
export OPENTWEET_API_KEY="ot_your_key"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.
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)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.
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.
| Factor | Agents SDK + OpenTweet | Agents SDK + X API directly |
|---|---|---|
| Price | Flat $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 account | Not needed | Required, with billing enrollment |
| Auth in your tool | One ot_ Bearer key | OAuth2 user token, refreshed by you |
| Scheduling | Included via scheduled_date | Not supported by the X API |
| Tools available | 36 via MCP, or one function tool | Whatever 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.
Keep exploring
OpenTweet vs the X API
Post, schedule, and automate X without a developer account or the $200/mo minimum.
X DM outreach, human-approved
Find and qualify leads, AI-draft DMs, approve each one, and drip-send from your own account via API or MCP.
Post to X without an API
The clean, account-safe way to post to X from your code or an AI agent.
Twitter MCP Server
Give Claude, Cursor, and OpenClaw the ability to post to X. 36 tools included.
Developer docs
Quickstart, API keys, MCP setup, and the REST reference for posting to X from code or an agent.
XMCP vs OpenTweet
X's official MCP server bills per API call and cannot schedule. Compare it with the flat-fee hosted MCP.
MCP for AI agents
Connect your AI client to X in under two minutes, no X developer account.
Developer API and keys
REST endpoints, one bearer key, and usage tracking. Build on OpenTweet.
OpenTweet for AI agents
The posting layer for autonomous agents and automations that live on X.
Build an AI Twitter persona
Give your AI agent its own X account. Setup, cadence, and the rules that keep it safe.
Best MCP servers for social media
The 2026 ranked list. How the hosted OpenTweet MCP compares with the official X MCP and others.
Cheapest way to post to X via code
Flat fee vs pay-per-use. Why the $0.20 per-link fee flips the math past ~60 posts a month.
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 key7-day free trial. Cancel anytime.