Skip to main content
Build a Mastra AI Twitter MCP agent through Xquik’s remote MCP server. This Mastra Twitter agent searches tweets, profiles, replies, followers, and monitors. It preserves every tweet ID, profile ID, cursor, and job ID. This Mastra AI tutorial combines agent tools, one typed workflow, and a strict handoff. Xquik provides the Twitter MCP server and documented API endpoints. Mastra controls the AI model, tool call, approval, and agent workflows.

Why use the Mastra AI agent framework with a Twitter API?

Mastra is an open-source TypeScript framework for building AI applications. The Mastra AI agent framework adds tools, routing, memory, and workflows. Use an X API agent when a model must choose related operations. Use REST for fixed routes, scheduled exports, or predictable latency.

Mastra Twitter API prerequisites

  • Node.js 22.13 or later
  • An Xquik API key beginning with xq_
  • A model-provider key supported by Mastra
  • A connected X account for private reads or writes
Public reads need no X Developer credentials. Authenticate through Xquik.

Install the Mastra AI SDK and MCP support

Install the tested Mastra AI SDK packages and Zod.
Store secrets outside source control.
.env
.gitignore
Run npm run dev in a generated Mastra project. Then inspect agent calls in Mastra Studio. Define the final handoff before creating the agent.
listTools() suits static agent construction. listToolsets() groups tools by server for each call. The client tries Streamable HTTP for URL servers. The MCP runtime returns normalized snake_case fields through xquik.request(). It normalizes createdAt to the Unix-second field created. Keep the Zod schema aligned with that contract.

Run a Mastra AI workflow for tweet research

The inputSchema: z.object(...) declaration validates every query. The outputSchema protects the final handoff. Mastra core workflows chain steps with .then() and finish with .commit(). createRun() creates isolated state. Store each successful handoff immediately. A registered agent can also run the workflow through a Mastra instance.

Search tweets with precise operators

Keep the exact q in every checkpoint. Use queryType=Latest for time-ordered research. Pass next_cursor unchanged. Stop when has_more becomes false. Also stop when a cursor repeats.

Require approval for Twitter actions

Xquik exposes one xquik execution tool. It can run authorized reads and writes. Require human-in-the-loop approval when the key permits writes.
Show the route, method, arguments, and X account before approval. Never treat MCP annotations as an authorization boundary. They are server-provided hints.
@mastra/core 1.55.0 predates the declined-call fix in PR #20487. Do not enable writes on that release. Upgrade after a stable fix. Then prove declined calls never execute.
Use a guest paid_reads key for a firm read-only boundary. See guest wallets for its exact scope.

Expose discovery without execution

Filter listTools() when an agent should only inspect endpoint schemas.
The explore tool returns routes, methods, parameters, and response fields. It does not call X. Adding xquik enables every key-authorized operation.

Use a Mastra AI MCP client for each user

Create one client after resolving the tenant identity.
Never share keys across tenants. Keep keys outside prompts, traces, and handoffs. Supply a unique client id for otherwise identical settings.

Forward dynamic headers safely

Use custom fetch when request context selects a tenant key.
The secretStore object represents your existing secret manager. Never send its key to the model. Keep forwardInstructions disabled for untrusted servers.

Store a resumable Twitter agent handoff

Store validated identifiers outside the model transcript.

Tweet search rows

Store tweet_id, text, author_username, created, url, has_more, next_cursor, and the original q.

Follower exports

A Twitter follower scraper API handoff keeps user_id, username, followers, has_more, and next_cursor.

Monitor events

Store monitor_id, event_id, type, occurred_at, and replay cursors.

Extraction jobs

Store extraction_id, status, poll, and export_after_complete.

Approved X actions

Store tweet_id, write_action_id, status, and poll. Never resend pending writes.

Webhook deliveries

Store webhook_id, delivery_id, and stream_event_id. Protect the secret.
Persist the validated result.object, not free-form result.text.

Handle Twitter API errors and rate limits

Match each documented status before retrying. Keep onToolError: "throw". It preserves MCP isError failures. Never restart pagination after 429. Read each route before designing retry logic.

Verify Mastra package compatibility

These stable versions were checked on August 3, 2026. @mastra/mcp 1.15.0 predates the concurrent reconnect fix in PR #20530. Avoid parallel recovery through one client. Test reconnects before every upgrade.

Mastra AI MCP frequently asked questions

What are the main Mastra AI MCP features?

The integration combines typed agent tools, workflows, approval, and MCP. It supports tweets, profiles, followers, monitors, webhooks, and writes.

How does Mastra AI MCP compare with other agent frameworks?

Choose Mastra for TypeScript-first agent workflows and Zod contracts. Choose direct REST for fixed routes. Xquik supports both paths.

What must a Twitter agent system enforce?

Require bounded queries, typed outputs, exact IDs, cursor checkpoints, and rate limiting. Isolate credentials and approve every write.

Which Mastra AI examples does this tutorial include?

It covers MCP discovery, tweet search, workflows, approval, errors, tenant isolation, and durable handoffs.

What is a Mastra AI MCP client?

The Mastra AI MCP client uses Xquik’s remote tools. A Mastra AI MCP server publishes local tools.

How do I search tweets with TypeScript?

Call GET /api/v1/x/tweets/search. Supply q, queryType, and limit. Preserve tweet_id, created, and next_cursor.

Can a Mastra agent export Twitter followers?

The Twitter follower scraper API returns profiles and cursors. Use the followers API for bounded pages. Use extraction jobs for CSV, JSON, or XLSX exports.

Can Mastra monitor tweets in real time?

Monitors store matching events between agent calls. Replay events by cursor. Use signed webhooks when the receiver needs immediate delivery.

Can a Mastra AI agent post tweets and replies?

Yes, after connecting X. Do not enable writes on affected Mastra releases. Test approval rejection before production.

How should a Mastra agent handle Twitter rate limits?

Treat 429 separately from dependency failures. Save the cursor and completed tweet IDs. Resume after reset guidance.