5分鐘搭建一個能連線企業微信的AI Agent:MCP協議實戰教程
技术架构
為什麼選擇企業微信 + MCP?
企業微信覆蓋了80%以上的國內企業,是企業內部溝通的事實標準。將AI Agent接入企業微信,意味著:
- 零學習成本:員工在熟悉的聊天介面中就能使用AI
- 天然許可權體系:復用企業微信的組織架構和許可權
- 訊息觸達:AI可以主動推送訊息給員工和群組
- 審批整合:AI可以代員工發起審批流程
企業微信 + MCP = 企業級AI Agent的最短路徑
整體架構
┌──────────────┐ MCP協議 ┌──────────────────┐ HTTP API ┌──────────────┐
│ AI Agent │ ◄──────────────► │ MCP Server │ ◄──────────────► │ 企業微信API │
│ (Claude等) │ JSON-RPC │ (我們的實現) │ REST呼叫 │ (WeCom) │
└──────────────┘ └──────────────────┘ └──────────────┘
第一步:建立企業微信應用
登入企業微信管理後臺:
1. 應用管理 → 建立應用
2. 記錄以下憑證:
- corpId: ww1234567890abcdef
- agentId: 1000002
- secret: xxxxxxxxxxxxxxxxxxxxxxxxxx
3. 設定回撥URL: https://your-domain.com/wecom/callback
第二步:實現MCP Server
核心依賴
<dependencies>
<dependency>
<groupId>io.modelcontextprotocol</groupId>
<artifactId>mcp-spring-boot-starter</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
MCP Server核心實現
@SpringBootApplication
public class WecomMcpServerApplication {
public static void main(String[] args) {
SpringApplication.run(WecomMcpServerApplication.class, args);
}
}
傳送訊息Tool
@Component
public class SendMessageTool implements McpTool {
@Override
public String name() {
return "send_message";
}
@Override
public String description() {
return "透過企業微信傳送訊息給指定使用者或群組";
}
@Override
public McpToolResult execute(Map<String, Object> args) {
String receiverType = (String) args.get("receiverType");
String receiverId = (String) args.get("receiverId");
String messageType = (String) args.get("messageType");
String content = (String) args.get("content");
try {
String msgId = wecomClient.sendMessage(
receiverType, receiverId, messageType, content
);
return McpToolResult.success(
Map.of("msgId", msgId, "status", "sent")
);
} catch (WecomApiException e) {
return McpToolResult.error("傳送失敗: " + e.getMessage());
}
}
}
第三步:設定與啟動
application.yml
server:
port: 8081
wecom:
corpId: ${WECOM_CORP_ID}
agentId: ${WECOM_AGENT_ID}
secret: ${WECOM_SECRET}
mcp:
server:
name: wecom-mcp-server
version: 1.0.0
transport: sse
sseEndpoint: /mcp/sse
第四步:在Claude Desktop中設定
編輯 claude_desktop_config.json:
{
"mcpServers": {
"wecom": {
"url": "http://localhost:8081/mcp/sse",
"transport": "sse"
}
}
}
生產級部署
Docker部署
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY target/wecom-mcp-server.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "app.jar"]
安全注意事項
- Secret不要硬編碼:使用環境變數或Vault
- IP白名單:企業微信後臺設定API呼叫IP白名單
- 訊息審計:所有傳送的訊息記錄審計日誌
- 頻率限制:企業微信API有呼叫頻率限制,需要做限流
總結
5分鐘搭建的核心在於MCP協議的標準化——我們不需要為每個AI應用單獨寫企業微信整合程式碼,只需要實現一次MCP Server,所有支援MCP的AI工具都能直接使用。
這是企業級AI Agent落地的最短路徑——從「AI能聊天」到「AI能幹活」,MCP讓這一切變得簡單。
本站提供瀏覽器本地工具,免註冊即可試用 →
#MCP#企业微信#AI Agent#WeCom#工具调用#企业级