Skip to content

Build a Read/Write X Agent Fresh

A worked pattern: an agent that searches X, summarizes what it finds, and posts a result - with write access deliberately scoped through an allow-list.

Architecture

flowchart TD
    User[Operator prompt] --> Agent[AI Agent / MCP client]
    Agent --> Docs[Docs MCP
search_x, get_page_x] Agent --> XMCP[XMCP
allow-listed tools] XMCP --> API[(X API v2)] subgraph Allow-list T1[searchPostsRecent] T2[getUsersByUsername] T3[getPostsById] T4[createPosts] end XMCP --- T1 XMCP --- T2 XMCP --- T3 XMCP --- T4

Step 1 - Scope the tools

Give the agent exactly what it needs and nothing more. In .env:

bash
X_API_TOOL_ALLOWLIST=searchPostsRecent,getUsersByUsername,getPostsById,createPosts

Restart XMCP so the allow-list takes effect. The agent can now read and create posts, but cannot delete, follow, message, or manage lists.

Step 2 - Wire both servers

json
{
  "mcpServers": {
    "xmcp": { "url": "http://127.0.0.1:8000/mcp" },
    "x-docs": { "url": "https://docs.x.com/mcp" }
  }
}

Step 3 - The task flow

sequenceDiagram
    participant Op as Operator
    participant Ag as Agent
    participant D as Docs MCP
    participant X as XMCP
    Op->>Ag: "Summarize what people say about MCP today, then post a takeaway"
    Ag->>D: search_x("recent search query operators")
    D-->>Ag: Query syntax + params
    Ag->>X: searchPostsRecent(query="MCP -is:retweet lang:en")
    X-->>Ag: Recent posts
    Ag->>Ag: Summarize themes
    Ag->>X: createPosts(text="Today's MCP takeaways: ...")
    X-->>Ag: New post ID
    Ag-->>Op: Done - posted, here is the link

Step 4 - Guardrails to add

ConcernMitigation
Agent posts something unintendedKeep createPosts out of the allow-list until you trust it; require human confirmation before write tools
Runaway loops hit rate limitsCache reads, respect x-rate-limit-reset, back off on 429 (see Rate Limits)
Credential exposureKeep .env gitignored; never echo tokens
ComplianceRespect Developer Guidelines and Display Requirements; do not automate prohibited behavior

Read-only variant

For a purely analytical agent, drop the write tool:

bash
X_API_TOOL_ALLOWLIST=searchPostsRecent,getUsersByUsername,getPostsById,getUsersPosts

Now the agent can research and summarize but cannot change anything on X.

See also