Instagram MCP Server + CrewAI Integration
This project enables automated posting to Instagram (including carousels) using a local Model Context Protocol (MCP) server and a CrewAI agent. It is designed for advanced automation, leveraging the Instagram Graph API and CrewAI's agent/task orchestration.
Table of Contents
Overview
This system allows you to:
- Upload single images or carousels (multi-image posts) to Instagram via API.
- Use a CrewAI agent to automate the process, including caption formatting and media management.
- Integrate with other tools or workflows using the MCP protocol.
How It Works
Instagram MCP Server
The MCP server (mcp_servers/instagram_server.py) exposes Instagram API operations as tools, including:
- refresh_instagram_access_token: Refreshes your long-lived access token.
- upload_image_without_caption: Uploads an image and returns a media container ID (not published yet).
- upload_image_with_caption: Uploads an image with a caption and returns a media container ID.
- upload_carousel_post: Creates a carousel container from a list of media container IDs and a caption.
- publish_media_container: Publishes a previously uploaded media container (single image or carousel) to your Instagram feed.
Workflow for Posting a Carousel:
- Upload each image: Each image is uploaded to Instagram, returning a media container ID.
- Create a carousel container: The list of media container IDs is used to create a carousel post, along with a caption.
- Publish the carousel: The carousel container is published to Instagram, making the post live.
Workflow for Single Image:
- Upload the image: The image is uploaded (with or without a caption), returning a media container ID.
- Publish the image: The media container is published to Instagram.
CrewAI Agent Orchestration
The CrewAI agent (see main.py) automates the Instagram posting process:
- Agent Setup: The agent is initialized with access to the MCP server tools.
- Task Description: The agent receives a detailed task, including:
- How to format the caption (Markdown-like, with line breaks).
- The list of image URLs to use.
- The order of operations for uploading and publishing.
- Logic for handling carousels vs. single-image posts.
- Execution: The agent:
- Uploads all images and collects their media IDs.
- Depending on user input, creates a carousel or single image post.
- Publishes the post to Instagram.
Setup Instructions
Environment Variables
Create a .env file in your project root with the following variables:
INSTAGRAM_ACCESS_TOKEN=your_long_lived_access_token
INSTAGRAM_ACCOUNT_ID=your_instagram_business_account_id
HOST_URL=graph.facebook.com
LATEST_API_VERSION=v22.0
- INSTAGRAM_ACCESS_TOKEN: Obtain a long-lived access token via the Instagram Graph API.
- INSTAGRAM_ACCOUNT_ID: Your Instagram Business Account ID (see Meta docs).
- HOST_URL: Usually
graph.facebook.com.
- LATEST_API_VERSION: Use the latest supported version, e.g.,
v22.0.
Dependencies
Step-by-Step Usage
Clone the repository and navigate to the project directory.
Install dependencies:
pip install -r requirements.txt
Set up your environment variables:
- Copy
.env.example to .env and fill in your credentials.
The MCP Instagram server starts automatically:
main.py launches mcp_servers/instagram_server.py over stdio through MCPServerAdapter, so you don't start it yourself. The path is relative, so run everything from the repository root.
Run the CrewAI Instagram agent (from the repository root):
python main.py
- You will be prompted to enter the type of post (
carousel or single image publish).
- The agent will:
- Format the caption using the latest YouTube transcription.
- Upload images.
- Create and publish the post as specified.
Check the output:
- The script will print the result of the Instagram API call (success or error).
Troubleshooting & Tips
- Access Token Expiry: If you get authentication errors, refresh your long-lived access token using the provided tool or via the Meta API.
- Image URLs: Ensure all image URLs are publicly accessible (no authentication required).
- Account Type: Only Instagram Business or Creator accounts linked to a Facebook Page can use the API.
- API Limits: Instagram enforces rate limits (e.g., 100 posts per 24 hours). Carousels count as a single post.
- Error Handling: Check the printed API responses for error messages and consult the Meta error code reference.
References
Instagram API Setup: Step-by-Step
Follow these steps to obtain the required credentials for Instagram API access. For more details, see the Meta for Developers documentation.
Create a Facebook App
- Go to the Meta for Developers portal.
- Log in and click "My Apps" > "Create App".
- Choose "Business" as the app type.
- Fill in the required details (App Name, Contact Email, etc.).
- Complete the app creation process.
Configure Instagram Graph API
- In your app dashboard, click "Add Product" in the left sidebar.
- Find Instagram Graph API and click "Set Up".
- Follow the prompts to add the product to your app.
Set Up Instagram Business Account and Facebook Page
- Instagram Account:
- Make sure your Instagram account is a Business or Creator account.
- In the Instagram app:
- Go to Settings > Account > Switch to Professional Account.
- Choose Business or Creator.
- Link to Facebook Page:
- In Instagram:
- Go to Settings > Account > Linked Accounts > Facebook.
- Link your Instagram account to a Facebook Page you manage.
Get Required Permissions
- In your app dashboard, go to App Review > Permissions and Features.
- Request the following permissions:
instagram_basic
instagram_content_publish
pages_read_engagement
- For production use, your app will need to go through App Review.
Get a User Access Token
- Go to the Graph API Explorer.
- Select your app from the dropdown.
- Click "Get User Access Token".
- Select the permissions you need (see above).
- Click "Generate Access Token" and log in with your Facebook account.
- Copy the short-lived access token.
Exchange for a Long-Lived Access Token
Get Your Instagram Business Account ID
- Use the following cURL command (replace placeholders):
curl -X GET "https://graph.facebook.com/v22.0/me/accounts?fields=instagram_business_account&access_token=LONG_LIVED_ACCESS_TOKEN"
- The response will look like:
{
"data": [
{
"instagram_business_account": {
"id": "17841400000000000"
},
...
}
]
}
- The value of
"id" is your Instagram Business Account ID. Use this as your INSTAGRAM_ACCOUNT_ID.
Add to Your .env File
INSTAGRAM_ACCESS_TOKEN=your_long_lived_access_token
INSTAGRAM_ACCOUNT_ID=your_instagram_business_account_id
HOST_URL=graph.facebook.com
LATEST_API_VERSION=v22.0