#!/usr/bin/env python3
"""
Chatlog MCP Server 使用示例
演示如何使用 MCP 工具访问聊天记录
"""
import asyncio
import json
from chatlog_mcp.server import server, call_api
async def main():
"""示例程序"""
print("=== Chatlog MCP Server 使用示例 ===\n")
# 1. 获取会话列表
print("1. 获取会话列表:")
sessions = await call_api("session", {"format": "text"})
print(sessions[:200] + "..." if len(sessions) > 200 else sessions)
print()
# 2. 搜索群聊
print("2. 搜索 'AI编程社团' 群聊:")
chatrooms = await call_api("chatroom", {
"keyword": "AI编程社团",
"format": "text"
})
print(chatrooms)
print()
# 3. 获取联系人
print("3. 搜索 '黄叔' 联系人:")
contacts = await call_api("contact", {
"keyword": "黄叔",
"format": "text"
})
print(contacts)
print()
# 4. 获取聊天记录
print("4. 获取 2026-01-12 的聊天记录:")
chatlog = await call_api("chatlog", {
"time": "2026-01-12",
"format": "text"
})
print(chatlog[:500] + "..." if len(chatlog) > 500 else chatlog)
print()
# 5. 多条件查询
print("5. 多条件查询 (指定群聊和时间):")
filtered_chatlog = await call_api("chatlog", {
"time": "2026-01-12",
"talker": "44156635321@chatroom",
"keyword": "AI",
"format": "text"
})
print(filtered_chatlog[:300] + "..." if len(filtered_chatlog) > 300 else filtered_chatlog)
if __name__ == "__main__":
asyncio.run(main())