Log inStart Free Trial
easyCoexistence Official APIs

Give a LangChain agent Instagram, TikTok and WhatsApp tools

A LangChain agent gets Instagram, TikTok, WhatsApp and more channels as tools from easyCoexistence's MCP server at https://easycoexistence.com/api/mcp, loaded by MCPAdapter, LangChain's MCP client, with FastMCP running the OAuth sign-in. create_agent can then publish and schedule posts, reply to comments and DMs and read insights through each network's official API, while signed events bring new comments and messages to your route.

Beta

LangChain's label for its langchain.mcp namespace, which needs langchain[mcp] 1.4.0 or later and brings in FastMCP.

In memory

Where FastMCP keeps OAuth tokens unless token_storage names a backend, so a restart means signing in again.

127.0.0.1

The loopback address of the temporary callback server FastMCP starts for the browser sign-in, on a free port or callback_port.

60 days

How long an easyCoexistence MCP refresh token lasts; access tokens last an hour, and each refresh issues a new pair.

8 seconds

How long easyCoexistence waits for your route to answer a forwarded comment or DM, sent once and never retried.

Which tools does a LangChain agent get from easyCoexistence?

More than 50, as ordinary LangChain tools that create_agent can call. The social tools take an account_id, so the agent starts with list_social_accounts and passes its IDs on to publish, read and reply to comments, hide them and read insights. Tools that belong to one network carry its name, such as publish_tiktok_post or upload_youtube_video, and a WhatsApp number starts from list_numbers, which returns the connection_id that send_message and send_template take. Each tool's description carries the network's own rules, so the model learns, for instance, that a TikTok reply is public. Posting later takes one more argument: scheduled_at puts the post in easyCoexistence's queue.

Instagrampublish_instagram_post, reply_to_comment, send_social_dm, get_social_insights
TikTokpublish_tiktok_post, get_tiktok_publish_status, reply_to_comment with post_id
WhatsApplist_numbers, send_message, send_template, get_number_health
Facebookpublish_facebook_post, reply_to_comment, send_social_dm for Messenger
YouTube and LinkedInupload_youtube_video, reply_to_youtube_comment, publish_linkedin_post, get_linkedin_stats
The queuelist_scheduled_posts, update_scheduled_post, cancel_scheduled_post, retry_scheduled_post

How does the agent sign in to easyCoexistence?

With FastMCP's OAuth helper, which MCPAdapter uses for authentication. On the first run it discovers easyCoexistence's authorization server, registers itself as a client, opens the browser on the consent screen, where its client_name is shown, and receives the code on a local callback at 127.0.0.1. After that, access tokens refresh without a browser. Tokens sit in memory unless the helper gets a token_storage backend, so the example keeps them on disk, encrypted with a Fernet key. Filter the list before create_agent sees it: the tools hold the MCP client, so the agent keeps working after the async with block closes.

A reply agent's tools, with a sign-in that survives restarts
import os

from cryptography.fernet import Fernet
from fastmcp import Client
from fastmcp.client.auth import OAuth
from key_value.aio.stores.disk import DiskStore
from key_value.aio.wrappers.encryption import FernetEncryptionWrapper
from langchain.agents import create_agent
from langchain.mcp import MCPAdapter

URL = "https://easycoexistence.com/api/mcp"

# Kept on disk and encrypted, so the browser sign-in happens once
oauth = OAuth(
    client_name="Acme reply agent",  # shown on easyCoexistence's consent screen
    callback_port=8765,
    token_storage=FernetEncryptionWrapper(
        key_value=DiskStore(directory="~/.easycoexistence/oauth"),
        fernet=Fernet(os.environ["TOKEN_KEY"]),
    ),
)

REPLY_TOOLS = {"list_social_accounts", "list_social_comments", "reply_to_comment",
               "hide_comment", "send_social_dm"}

RULES = (
    "You reply to comments and DMs for Acme, on the account_id in the event. "
    "TikTok replies are public and need the video ID as post_id. "
    "Never reply to the account's own messages or replies."
)

async def build_reply_agent(model: str):
    async with MCPAdapter(Client(URL, auth=oauth)) as adapter:
        tools = [t for t in await adapter.list_tools() if t.name in REPLY_TOOLS]
    return create_agent(model, tools, system_prompt=RULES)

How do comments and DMs reach a LangChain agent?

As signed POST requests to a route of yours, because MCP only answers the calls the agent makes. Point each account at the route with set_social_webhook_destination, asked of the agent once or run from Claude or ChatGPT, and keep the signing_secret it returns. Instagram and Facebook forward comments, mentions and DMs, and TikTok forwards comments; each event carries platform, account_id, external_id and the network's own entry. The route checks X-EC-Signature, responds at once, since easyCoexistence waits 8 seconds and does not retry, and hands the event to the agent in the background. A checkpointer with a thread_id per DM sender keeps a conversation's history.

A FastAPI route that verifies the event, then runs the agent
import hashlib, hmac, json, time

from fastapi import BackgroundTasks, FastAPI, HTTPException, Request

app = FastAPI()  # at startup: app.state.agent = await build_reply_agent("<MODEL>")
SECRETS: dict[str, str] = {}  # account_id -> signing_secret, kept in your database

def signed(raw: bytes, header: str, secret: str) -> bool:
    parts = dict(p.split("=", 1) for p in header.split(",") if "=" in p)
    t, v1 = parts.get("t", ""), parts.get("v1", "")
    if not t.isdigit() or abs(time.time() - int(t)) > 300:
        return False
    mac = hmac.new(secret.encode(), t.encode() + b"." + raw, hashlib.sha256).hexdigest()
    return hmac.compare_digest(mac.encode(), v1.encode())

@app.post("/hooks/social")
async def social_event(request: Request, background: BackgroundTasks):
    raw = await request.body()
    event = json.loads(raw)  # platform, account_id, external_id, entry
    secret = SECRETS.get(event.get("account_id", ""))
    if not secret or not signed(raw, request.headers.get("x-ec-signature", ""), secret):
        raise HTTPException(status_code=401)
    background.add_task(reply, event)  # runs after the response is sent
    return {"received": True}

async def reply(event: dict) -> None:
    prompt = json.dumps(event)  # the agent reads the platform, the account and the entry
    await app.state.agent.ainvoke({"messages": [{"role": "user", "content": prompt}]})

What does each network allow the agent to do?

What the network's API allows the account, which the agent's instructions should state plainly:

  • Instagram and Messenger DMs: send_social_dm replies to people who wrote in the last 24 hours, and Instagram caps a DM at 1,000 bytes of UTF-8.
  • Comment replies: reply_to_comment replies in public, or with private set to true sends one DM per Instagram or Facebook comment within 7 days of it.
  • TikTok: replies are always public and need the video's ID as post_id, and there are no DMs on TikTok.
  • WhatsApp: send_message works within 24 hours of the customer's last message, and send_template with an approved template outside it.
  • Media: each file sits at a public HTTPS link; Instagram takes JPEG images, and TikTok photo posts take JPEG or WebP.

When should code call the networks without the agent?

When a step has to happen the same way each time, or with no one there to sign in. get_social_credentials hands the agent an account's API base, its ID on the network and, on TikTok, YouTube and LinkedIn, a token that is valid now, and the Partner API returns the same from GET /api/v1/channels/:id?credentials=1 with an ec_live_ key, so a Python job can call TikTok, Google or LinkedIn directly. Replies that have to go out at any hour without the agent, such as fixed text for a keyword, fit comment rules and automations, which easyCoexistence runs itself.

Common mistakes

  • Replying to events the account caused. Instagram echoes the account's own DMs with is_echo set to true and returns its public replies as comments, so drop those before the agent sees them, or it ends up replying to itself.
  • Running the agent before the route responds. easyCoexistence waits 8 seconds for a 2xx and never retries, so a handler that waits on the model loses the event; respond first, work in the background.
  • Calling reply_to_comment for a TikTok comment without post_id. TikTok's reply names the video as well as the comment, so pass the event's video_id as post_id.
  • Giving a reply agent the publishing tools. Filter the list by name before create_agent, so a misread comment cannot publish a post, delete one or change an automation.
Doing this with EasyCoexistence

easyCoexistence gives your LangChain agents reach to customers on WhatsApp, Instagram, Facebook, TikTok, YouTube, LinkedIn and more channels, each through its official API: one MCP server behind an OAuth sign-in, signed comment and DM events, and a queue that posts up to 90 days ahead, with every connection monitored 24/7. US$ 9 to US$ 0.75 per connection per month, falling as you add connections, with the first 3 days free.

easyCoexistence is a multichannel API and MCP server that connects AI agents such as Claude and ChatGPT to WhatsApp, Instagram, TikTok, Facebook and more. On WhatsApp it uses Coexistence: the business keeps using its number in the WhatsApp Business app while the AI works through Meta's official API.

Frequently asked questions

Does the agent need its own Meta, TikTok or Google developer app?

No. Accounts connect through easyCoexistence's own app on each network, in the dashboard or through a Partner API connect link, and the agent reaches them all through one easyCoexistence sign-in.

Can the agent schedule posts instead of publishing at once?

Yes. publish_instagram_post, publish_facebook_post, publish_tiktok_post and publish_linkedin_post take scheduled_at, an ISO 8601 time with a timezone from 1 minute to 90 days ahead. YouTube schedules natively with upload_youtube_video and publish_at.

Which models can run the agent?

Any chat model create_agent can use with tools. The MCP tools do not depend on the model, and the model's provider bills its own usage; easyCoexistence charges per connection, never per tool call.

How much does easyCoexistence cost for a LangChain agent?

US$ 9 to US$ 0.75 per connection per month, falling as you add connections, and the same on each network. The MCP server, the forwarded events and the Partner API are included, and the first 3 days are free.

Keep reading

Connect your first channel in two minutes.

No platform reviews to wait for. Connect WhatsApp, Instagram, TikTok or another channel, then ask your AI to post, reply or report, and watch it work.

  • 3 days free
  • Cancel any time
  • Official APIs only
  • Monitored 24/7

Verified on