登入開始免費試用
easyCoexistence 官方 API

為 LangChain 代理加入 Instagram、TikTok 與 WhatsApp 工具

LangChain 代理透過 easyCoexistence 的 MCP 伺服器(https://easycoexistence.com/api/mcp)取得 Instagram、TikTok、WhatsApp 以及更多渠道作為工具。這些工具由 LangChain 的 MCP 客戶端 MCPAdapter 載入,並由 FastMCP 負責 OAuth 登入。之後,create_agent 便可發佈及排程帖文、回覆留言與私人訊息,並透過各網絡的官方 API 讀取洞察數據。帶簽署的事件會把新留言和新訊息送到你的路由。

Beta

LangChain 用於 langchain.mcp 命名空間的標籤,需要 langchain[mcp] 1.4.0 或以上版本,並會引入 FastMCP。

In memory

FastMCP 會保存 OAuth 權杖,除非 token_storage 指定了後端,否則重新啟動後須再次登入。

127.0.0.1

FastMCP 為瀏覽器登入而啟動的臨時回調伺服器的回環地址,使用可用的連接埠或 callback_port。

60 days

easyCoexistence MCP 更新權杖的有效期;存取權杖有效一小時,每次更新都會發出一對新權杖。

8 seconds

easyCoexistence 等待你的路由回應已轉發的留言或私人訊息的時限;事件只會發送一次,不會重試。

LangChain 代理可從 easyCoexistence 取得哪些工具?

超過 50 個,全部都是 create_agent 可以調用的一般 LangChain 工具。社交工具需要 account_id,因此代理會先調用 list_social_accounts,再把帳戶 ID 傳給發佈、讀取、回覆留言、隱藏留言及讀取洞察數據的工具。屬於單一網絡的工具會帶上該網絡的名稱,例如 publish_tiktok_post 或 upload_youtube_video。WhatsApp 號碼則由 list_numbers 開始,它會返回 send_message 和 send_template 所需的 connection_id。每個工具的描述都載有該網絡本身的規則,例如模型會知道 TikTok 的回覆是公開的。排程發佈時再加一個參數:scheduled_at 會把帖文放入 easyCoexistence 的隊列。

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

代理如何登入 easyCoexistence?

使用 FastMCP 的 OAuth 輔助功能,MCPAdapter 以此進行驗證。首次執行時,它會探索 easyCoexistence 的授權伺服器,把自己註冊為客戶端,打開瀏覽器進入授權畫面(畫面會顯示其 client_name),然後經由本機 127.0.0.1 的回調接收授權碼。之後存取權杖可在無需瀏覽器的情況下更新。除非輔助功能獲得 token_storage 後端,否則權杖只會存放在記憶體中,因此範例會以 Fernet 金鑰加密後存入磁碟。請在 create_agent 接收工具清單之前先行篩選:工具持有 MCP 客戶端,所以 async with 區塊結束後,代理仍可繼續運作。

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)

留言和私人訊息如何送到 LangChain 代理?

以帶簽署的 POST 請求發送到你的路由,因為 MCP 只回應代理自己發出的調用。對每個帳戶使用 set_social_webhook_destination 將其指向路由,可讓代理執行一次,也可由 Claude 或 ChatGPT 執行,並保存它返回的 signing_secret。Instagram 和 Facebook 會轉發留言、提及及私人訊息,TikTok 則轉發留言;每個事件都帶有 platform、account_id、external_id 以及該網絡自己的條目。路由會核對 X-EC-Signature,並即時回應,因為 easyCoexistence 只等待 8 秒且不會重試,然後在背景把事件交給代理。為每位私人訊息發送者設定一個 thread_id 的 checkpointer,可保留對話紀錄。

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}]})

每個網絡允許代理做什麼?

網絡 API 允許該帳戶做的事,代理的指示應清楚說明:

  • Instagram 和 Messenger 私人訊息:send_social_dm 只能回覆過去 24 小時內發訊息來的人,而 Instagram 的私人訊息上限為 1,000 位元組(UTF-8)。
  • 留言回覆:reply_to_comment 會公開回覆;若 private 設為 true,則會在留言後 7 天內,為每條 Instagram 或 Facebook 留言各發送一則私人訊息。
  • TikTok:回覆一定是公開的,需要以影片的 ID 作為 post_id,而 TikTok 沒有私人訊息。
  • WhatsApp:send_message 只能在客戶最後一則訊息後 24 小時內使用,超過時限則須以已批准的範本透過 send_template 發送。
  • 媒體:每個檔案都必須位於公開的 HTTPS 連結;Instagram 接受 JPEG 圖片,TikTok 圖片帖文則接受 JPEG 或 WebP。

何時應由程式直接調用網絡,而非透過代理?

當某個步驟必須每次以相同方式執行,或無人在場登入時。get_social_credentials 會把帳戶的 API 基底、其在網絡上的 ID,以及在 TikTok、YouTube 及 LinkedIn 上目前有效的權杖交給代理。Partner API 則透過 GET /api/v1/channels/:id?credentials=1 並使用 ec_live_ 金鑰返回相同資料,因此 Python 作業可直接調用 TikTok、Google 或 LinkedIn。若回覆必須在代理不在場時隨時發出,例如某關鍵字的固定文字,則適合以留言規則及自動化處理,由 easyCoexistence 自行執行。

常見錯誤

  • 回覆帳戶自己觸發的事件。Instagram 會以 is_echo 設為 true 回傳帳戶自己的私人訊息,並將其公開回覆以留言形式返回,因此須在代理看到之前剔除這些事件,否則它會回覆自己。
  • 在路由回應之前就執行代理。easyCoexistence 等待 2xx 回應 8 秒且從不重試,因此若處理程式等待模型,就會遺失事件;應先回應,再於背景工作。
  • 為 TikTok 留言調用 reply_to_comment 時沒有提供 post_id。TikTok 的回覆同時指定影片和留言,所以應把事件的 video_id 作為 post_id 傳入。
  • 把發佈工具交給回覆代理。請在 create_agent 之前按名稱篩選清單,以免誤讀的留言發佈帖文、刪除帖文或更改自動化。
使用 EasyCoexistence 完成這項設定

easyCoexistence 讓你的 LangChain 代理透過各網絡的官方 API 觸及 WhatsApp、Instagram、Facebook、TikTok、YouTube、LinkedIn 等渠道的客戶:一個 MCP 伺服器、一次 OAuth 登入、帶簽署的留言與私人訊息事件,以及可提前 90 天排程的隊列,並以 24 小時監察每個連接。每個連接每月 US$ 9 至 US$ 0.75,連接越多越便宜,前 3 天免費試用。

常見問題

代理是否需要自己的 Meta、TikTok 或 Google 開發者應用程式?

不需要。帳戶透過 easyCoexistence 在各網絡上的自有應用程式連接,可在控制台中完成,或透過 Partner API 的連接連結完成。之後,代理只需一次 easyCoexistence 登入,即可觸及所有帳戶。

代理能否排程帖文,而非立即發佈?

可以。publish_instagram_post、publish_facebook_post、publish_tiktok_post 和 publish_linkedin_post 都接受 scheduled_at,即含時區的 ISO 8601 時間,範圍由 1 分鐘至 90 天後。YouTube 則以 upload_youtube_video 和 publish_at 原生排程。

哪些模型可以運行代理?

任何可搭配工具使用於 create_agent 的聊天模型皆可。MCP 工具與模型無關,模型供應商會就其自身用量收費;easyCoexistence 則按連接收費,從不按每次工具調用收費。

easyCoexistence 為 LangChain 代理收費多少?

每個連接每月 US$ 9 至 US$ 0.75,連接越多越便宜,各網絡的價格相同。MCP 伺服器、轉發的事件及 Partner API 均包括在內,前 3 天免費。

繼續閱讀

兩分鐘內連接您的第一個頻道。

無需等待平台審核。連接 WhatsApp、Instagram、TikTok 或其他頻道,然後請您的 AI 發佈貼文、回覆訊息或匯報數據,並看着它運作。

  • 免費試用 3 天
  • 隨時取消
  • 只用官方 API
  • 24/7 監察

驗證日期