为 LangChain 智能体提供 Instagram、TikTok 和 WhatsApp 工具
LangChain 智能体通过 https://easycoexistence.com/api/mcp 上的 easyCoexistence MCP 服务器,获取 Instagram、TikTok、WhatsApp 及更多渠道的工具。这些工具由 LangChain 的 MCP 客户端 MCPAdapter 加载,OAuth 登录则由 FastMCP 负责。随后 create_agent 可以发布和排期帖子、回复评论和私信,并通过各网络的官方 API 读取洞察数据;已签名的事件会把新评论和消息送到您的路由。
LangChain 为其 langchain.mcp 命名空间提供的标签,需要 langchain[mcp] 1.4.0 或更高版本,并会引入 FastMCP。
除非 token_storage 指定后端,FastMCP 会将 OAuth 令牌保存在内存中,因此重启后需要重新登录。
FastMCP 为浏览器登录临时启动的回调服务器所用的环回地址,使用空闲端口,或由 callback_port 指定的端口。
easyCoexistence MCP 刷新令牌的有效期;访问令牌有效一小时,每次刷新都会签发新的一对令牌。
easyCoexistence 等待您的路由响应转发评论或私信的时长。该事件只发送一次,从不重试。
easyCoexistence 为 LangChain 智能体提供哪些工具?
超过 35 个,作为普通的 LangChain 工具供 create_agent 调用。社交工具需要 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 的队列。
智能体如何登录 easyCoexistence?
使用 FastMCP 的 OAuth 辅助工具,MCPAdapter 用它进行身份验证。首次运行时,它会发现 easyCoexistence 的授权服务器,将自己注册为客户端,打开浏览器显示授权页面(其中会显示 client_name),并通过 127.0.0.1 上的本地回调接收授权码。此后,访问令牌无需浏览器即可刷新。除非辅助工具获得 token_storage 后端,否则令牌保存在内存中,因此示例会使用 Fernet 密钥将令牌加密后保存到磁盘。请在 create_agent 看到工具列表之前先过滤:这些工具持有 MCP 客户端,因此 async with 代码块结束后,智能体仍能继续工作。
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,可以保留对话历史。
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,则会对每条 Instagram 或 Facebook 评论在其发布后 7 天内发送一条私信。 - 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 则通过带 ec_live_ 密钥的 GET /api/v1/channels/:id?credentials=1 返回相同内容,因此 Python 任务可以直接调用 TikTok、Google 或 LinkedIn。若回复必须在智能体不在场时随时发出,例如针对某个关键词的固定文本,则适合使用评论规则和自动化功能,这些功能由 easyCoexistence 自行运行。
常见错误
- 回复账户自身触发的事件。Instagram 会以 is_echo 设为 true 回显账户自己发出的私信,并把其公开回复作为评论返回,因此需要在智能体看到之前将它们过滤掉,否则它会回复自己。
- 在路由响应之前运行智能体。easyCoexistence 等待 8 秒以获得 2xx 响应且从不重试,因此若处理程序等待模型结果,就会丢失事件。应先响应,再在后台处理。
- 对 TikTok 评论调用 reply_to_comment 时未提供 post_id。TikTok 的回复既要指明视频,也要指明评论,因此应将事件中的 video_id 作为 post_id 传入。
- 为回复智能体提供了发布工具。请在 create_agent 之前按名称过滤列表,以免误读的评论导致发布或删除帖子,或更改自动化。
easyCoexistence 让您的 LangChain 智能体通过各网络的官方 API 触达 WhatsApp、Instagram、Facebook、TikTok、YouTube、LinkedIn 等渠道的客户。它提供一个 MCP 服务器、一次 OAuth 登录、已签名的评论与私信事件,以及可提前 90 天排期的发布队列,并对每个连接进行 24/7 监控。每个连接每月 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 监控
验证时间