Build an AI Agent Connected to WeCom in 5 Minutes: MCP Protocol Practical Tutorial
技术架构
Why WeCom + MCP?
WeCom (Enterprise WeChat) covers 80%+ of Chinese enterprises and is the de facto standard for internal communication. Connecting AI Agent to WeCom means:
- Zero learning curve: Employees use AI in their familiar chat interface
- Built-in permissions: Reuse WeCom's organizational structure and access control
- Message delivery: AI can proactively push messages to employees and groups
- Approval integration: AI can initiate approval workflows on behalf of employees
WeCom + MCP = the shortest path to enterprise AI Agent
Architecture Overview
┌──────────────┐ MCP Protocol ┌──────────────────┐ HTTP API ┌──────────────┐
│ AI Agent │ ◄──────────────────► │ MCP Server │ ◄──────────────► │ WeCom API │
│ (Claude) │ JSON-RPC │ (Our impl) │ REST calls │ │
└──────────────┘ └──────────────────┘ └──────────────┘
MCP Server Tools:
├── send_message Send message to user/group
├── search_contacts Search address book
├── get_department Get department info
├── create_approval Create approval request
├── upload_file Upload file to WeCom
└── get_chat_history Get group chat history
Step 1: Create WeCom Application
1.1 Get WeCom Credentials
Log in to WeCom Admin Console:
1. App Management → Create App
2. Record credentials:
- corpId: ww1234567890abcdef
- agentId: 1000002
- secret: xxxxxxxxxxxxxxxxxxxxxxxxxx
3. Set callback URL: https://your-domain.com/wecom/callback
Step 2: Implement MCP Server
2.1 Project Structure
wecom-mcp-server/
├── src/main/java/com/example/wecom/
│ ├── WecomMcpServerApplication.java
│ ├── config/WecomConfig.java
│ ├── mcp/
│ │ ├── WecomMcpServer.java
│ │ └── tools/
│ │ ├── SendMessageTool.java
│ │ ├── SearchContactsTool.java
│ │ ├── GetDepartmentTool.java
│ │ └── CreateApprovalTool.java
│ └── wecom/
│ ├── WecomApiClient.java
│ └── WecomTokenManager.java
└── pom.xml
2.2 Core Dependencies
<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>
2.3 MCP Server Implementation
@SpringBootApplication
public class WecomMcpServerApplication {
public static void main(String[] args) {
SpringApplication.run(WecomMcpServerApplication.class, args);
}
}
@Configuration
@ConfigurationProperties(prefix = "wecom")
@Data
public class WecomConfig {
private String corpId;
private String agentId;
private String secret;
private String token;
private String encodingAesKey;
}
2.4 Send Message Tool
@Component
public class SendMessageTool implements McpTool {
private final WecomApiClient wecomClient;
@Override
public String name() {
return "send_message";
}
@Override
public String description() {
return "Send a message to a specified user or group via WeCom";
}
@Override
public JsonNode inputSchema() {
return objectMapper.readTree("""
{
"type": "object",
"properties": {
"receiverType": {
"type": "string",
"enum": ["user", "group"],
"description": "Receiver type: user or group"
},
"receiverId": {
"type": "string",
"description": "Receiver ID"
},
"messageType": {
"type": "string",
"enum": ["text", "markdown", "file"],
"description": "Message type"
},
"content": {
"type": "string",
"description": "Message content"
}
},
"required": ["receiverType", "receiverId", "messageType", "content"]
}
""");
}
@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("Send failed: " + e.getMessage());
}
}
}
2.5 Token Manager (Auto-refresh)
@Component
public class WecomTokenManager {
private final WecomConfig config;
private final RestTemplate restTemplate;
private String accessToken;
private long expiresAt;
public synchronized String getAccessToken() {
if (accessToken != null && System.currentTimeMillis() < expiresAt) {
return accessToken;
}
refreshAccessToken();
return accessToken;
}
private void refreshAccessToken() {
String url = String.format(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
config.getCorpId(), config.getSecret()
);
JsonNode json = restTemplate.getForObject(url, JsonNode.class);
this.accessToken = json.path("access_token").asText();
int expiresIn = json.path("expires_in").asInt();
this.expiresAt = System.currentTimeMillis() + (expiresIn - 300) * 1000L;
}
}
Step 3: Configuration
application.yml
server:
port: 8081
wecom:
corpId: ${WECOM_CORP_ID}
agentId: ${WECOM_AGENT_ID}
secret: ${WECOM_SECRET}
token: ${WECOM_CALLBACK_TOKEN}
encodingAesKey: ${WECOM_ENCODING_AES_KEY}
mcp:
server:
name: wecom-mcp-server
version: 1.0.0
transport: sse
sseEndpoint: /mcp/sse
Step 4: Configure in Claude Desktop
Edit claude_desktop_config.json:
{
"mcpServers": {
"wecom": {
"url": "http://localhost:8081/mcp/sse",
"transport": "sse"
}
}
}
Now you can use WeCom features directly in Claude:
User: Send a message to the product group notifying about tomorrow's 3pm review meeting
Claude: I'll send that message for you.
[Calling send_message tool]
→ receiverType: "group"
→ receiverId: "product_group_id"
→ messageType: "text"
→ content: "Notice: Review meeting tomorrow at 3pm. Please attend."
Message sent successfully! msgId: msg_20260611_001
Production Deployment
Docker
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY target/wecom-mcp-server.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "app.jar"]
version: '3.8'
services:
wecom-mcp:
build: .
ports:
- "8081:8081"
environment:
- WECOM_CORP_ID=${WECOM_CORP_ID}
- WECOM_AGENT_ID=${WECOM_AGENT_ID}
- WECOM_SECRET=${WECOM_SECRET}
restart: unless-stopped
Security Notes
- Never hardcode secrets: Use environment variables or Vault
- IP whitelist: Set API call IP whitelist in WeCom admin
- Message audit: Log all sent messages for compliance
- Rate limiting: WeCom API has rate limits — implement throttling
Summary
The "5-minute" setup is possible because MCP standardizes the integration — implement the MCP Server once, and all MCP-compatible AI tools can use it:
- Implement once, use everywhere: Claude, Cursor, custom Agents all connect
- Spring Boot ecosystem: WeCom SDK, auth, audit out of the box
- Production-ready: Auto token refresh, rate limiting, audit logs, Docker deployment
This is the shortest path from "AI that chats" to "AI that works" — MCP makes it simple.
Try these browser-local tools — no sign-up required →
#MCP#企业微信#AI Agent#WeCom#工具调用#企业级