Skip to main content
AG2 is an open-source Python framework for multi-agent systems. TwexAPI does not ship a native AG2 search toolkit yet. Connect AG2 agents to the TwexAPI MCP server with MCPToolkit so agents can call explore and twexapi_request while keeping API credentials inside your infrastructure. Preserve every tweet ID, user ID, and cursor the API returns.

Why use AG2 with TwexAPI?

AG2 gives you explicit control over tool exposure, delegation, and middleware. Pair that with TwexAPI MCP when you want multi-agent research without hardcoding every REST route. This suits research, monitoring, and reporting agents. Use the Python SDK or Prefect collection for deterministic jobs that need no model decisions.

Prerequisites

  • Python 3.10 or later
  • A TwexAPI API key
  • An LLM provider key supported by AG2
Public X reads need no X Developer credentials. Authenticate with TwexAPI.

Install AG2

Install your model provider extra as well.
Store secrets outside source control.

Connect TwexAPI MCP with MCPToolkit

Use client-side MCPToolkit when credentials must stay in your infrastructure. TwexAPI MCP exposes explore and twexapi_request.
headers with x-api-key is required. Unauthenticated requests to https://api.twexapi.io/mcp return 401.

Restrict tools by agent role

Give discovery-only agents access to explore. Give execution agents both tools.

Delegate search in a multi-agent team

Agent.as_tool() exposes an agent as a tool for another agent. The coordinator receives the delegate’s final answer, not its internal tool-call history.

Handoff checklist

Store durable fields from MCP responses so later workflow steps do not depend on chat history. See Agent MCP Handoff for the full checklist.

Pagination

When explore returns a paginated route, pass the documented cursor fields back through twexapi_request unchanged. Treat cursors as opaque strings. Deduplicate rows on tweet_id or user_id.

Combine with Docs MCP

Add the Docs MCP server when agents should search TwexAPI documentation before choosing routes.

Handle failures

MCP and REST errors surface through the toolkit as HTTP failures. Branch on status before retrying. Wrap MCPToolkit with AG2 tool middleware when you need retries, approval gates, or audit logging around every call.

Provider-side MCP (Anthropic only)

If you target Anthropic and accept forwarding credentials to the provider, use MCPServerTool instead of MCPToolkit. Prefer MCPToolkit for provider-agnostic deployments and when API keys must stay in your infrastructure.