Document objects.
Prerequisites
- Python 3.10+
- A Twexapi API key
- Haystack installed in your retrieval pipeline
Install
MCP collection step
- Call Twexapi MCP with a focused task, such as searching tweets or loading a thread.
- Convert returned rows into
Documentobjects. - Add metadata fields like
tweet_id,author_username,created_at,route_used, andnext_cursor. - Write documents into your Haystack store.
Example handoff shape
Convert to documents
Tweet search
Use MCP for endpoint discovery and collection, then hand the normalized JSON into your Haystack pipeline.User timeline
For timeline retrieval, ask the agent to preserve a stable user identity and cursor.Pagination
Storehas_more and next_cursor separately from the embedded documents. Do not embed cursors into vector content; keep them in pipeline state so the next run can resume without changing indexed text.
Document mapping
| Haystack field | Twexapi source |
|---|---|
Document.content | Tweet text, article Markdown, or profile description |
meta.tweet_id | tweet_id |
meta.author_username | author_username or author_screen_name |
meta.created_at | created_at |
meta.url | Public X URL when returned, or a URL built from tweet_id |
meta.route_used | MCP selected route |
meta.next_cursor | Cursor checkpoint for the next collection run |
Agent prompt
Async usage
When your pipeline has async stages, run the MCP collection outside the synchronous Haystack pipeline and pass the resulting documents into the async branch.Error handling
Handle Twexapi errors before creating documents.| Status | Handling |
|---|---|
401 | Check the MCP x-api-key header or REST Bearer token. |
403 | Check account access, credits, and write permissions. |
429 | Store the cursor and retry after the rate-limit window. |
5xx | Retry the collection step, not the embedding step. |