From ce1804b35a48ace9cc5be6d57c7c79c87bf3c3ec Mon Sep 17 00:00:00 2001 From: cvcat <992822653@qq.com> Date: Thu, 17 Apr 2025 21:40:51 +0800 Subject: [PATCH] =?UTF-8?q?Lark=20AI=20Agent=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 20 + .gitignore | 32 ++ Dockerfile | 29 ++ README.md | 266 ++++++++++ app/__init__.py | 4 + app/api/__init__.py | 3 + app/api/auth.py | 25 + app/api/lark_client.py | 170 +++++++ app/config/__init__.py | 3 + app/config/settings.py | 38 ++ app/core/__init__.py | 3 + app/core/llm_service.py | 53 ++ app/core/mcp_server.py | 179 +++++++ app/core/message_service.py | 108 ++++ app/db/__init__.py | 3 + app/db/models.py | 23 + app/db/session.py | 67 +++ app/utils/__init__.py | 3 + app/utils/lark_utils.py | 85 ++++ builder/__init__.py | 0 builder/header.py | 257 ++++++++++ builder/params.py | 76 +++ builder/proto.py | 276 +++++++++++ docker-compose.yml | 45 ++ extension/__init__.py | 0 extension/weather_api/GEOIP.db | Bin 0 -> 91136 bytes extension/weather_api/api.py | 74 +++ extension/weather_api/db.py | 178 +++++++ main.py | 73 +++ requirements.txt | 14 + static/__init__.py | 0 static/lark_decrypt.js | 183 +++++++ static/proto.proto | 855 ++++++++++++++++++++++++++++++++ static/proto_pb2.py | 244 +++++++++ static/resource/back_end.png | Bin 0 -> 97447 bytes static/resource/front_end.png | Bin 0 -> 16243 bytes static/resource/front_end_1.png | Bin 0 -> 16243 bytes static/resource/front_end_2.png | Bin 0 -> 16542 bytes static/resource/functions.png | Bin 0 -> 14800 bytes 39 files changed, 3389 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 app/__init__.py create mode 100644 app/api/__init__.py create mode 100644 app/api/auth.py create mode 100644 app/api/lark_client.py create mode 100644 app/config/__init__.py create mode 100644 app/config/settings.py create mode 100644 app/core/__init__.py create mode 100644 app/core/llm_service.py create mode 100644 app/core/mcp_server.py create mode 100644 app/core/message_service.py create mode 100644 app/db/__init__.py create mode 100644 app/db/models.py create mode 100644 app/db/session.py create mode 100644 app/utils/__init__.py create mode 100644 app/utils/lark_utils.py create mode 100644 builder/__init__.py create mode 100644 builder/header.py create mode 100644 builder/params.py create mode 100644 builder/proto.py create mode 100644 docker-compose.yml create mode 100644 extension/__init__.py create mode 100644 extension/weather_api/GEOIP.db create mode 100644 extension/weather_api/api.py create mode 100644 extension/weather_api/db.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 static/__init__.py create mode 100644 static/lark_decrypt.js create mode 100644 static/proto.proto create mode 100644 static/proto_pb2.py create mode 100644 static/resource/back_end.png create mode 100644 static/resource/front_end.png create mode 100644 static/resource/front_end_1.png create mode 100644 static/resource/front_end_2.png create mode 100644 static/resource/functions.png diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..a94ab12 --- /dev/null +++ b/.env.example @@ -0,0 +1,20 @@ +# Database configuration +DB_HOST=localhost +DB_PORT=3306 +DB_USER=YOUR_MYSQL_USERNAME +DB_PASSWORD=YOUR_MYSQL_PASSWORD +DB_NAME=lark_messages + +# Lark API configuration +LARK_COOKIE="YOUR LARK Cookie" + +# Function trigger flag +FUNCTION_TRIGGER_FLAG="/run" + +# AI Bot message prefix (preferably longer) +AI_BOT_PREFIX="Lark AI Bot:" + +# OpenAI API configuration +OPENAI_API_KEY="Your api key" +OPENAI_API_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1" +OPENAI_API_MODEL="qwen-plus" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2cfdc22 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +__pycache__ +*node_modules/* +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +MANIFEST +*.manifest +*.spec +.cache +*.log +local_settings.py +db.sqlite3 +__pypackages__/ +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +.env +test.py \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9b2f92d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM python:3.10-slim + +WORKDIR /app + +RUN apt-get update && apt-get install -y \ + curl \ + gnupg \ + build-essential \ + git \ + && rm -rf /var/lib/apt/lists/* + +RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ + && apt-get install -y nodejs \ + && rm -rf /var/lib/apt/lists/* + +RUN python --version && node --version && npm --version + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 5000 + +ENV PYTHONUNBUFFERED=1 +ENV NODE_ENV=production + +CMD ["python", "main.py"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..629b77a --- /dev/null +++ b/README.md @@ -0,0 +1,266 @@ +# Lark MCP - 飞书全场景 Function Call AI 助手 🚀 + +[](https://www.python.org/) +[](https://nodejs.org/zh-cn/) + +一个基于飞书(Lark)的全场景MCP Function Call AI Agent,实现大模型通过飞书进行函数调用和消息处理。**无需配置飞书机器人,你的飞书账号即是AI助手。** + + +**无需配置飞书机器人,你的飞书账号即是AI助手。** + + +**只需定义函数和注释,你的飞书机器人会自动根据场景调用。** + + +## 项目概述 🌟 + +Lark MCP是一个现代化的Python应用程序,能够: + +- 📊 逆向飞书Protobuf格式传输的Websockets和API,监听并记录消息 +- 🤖 提供自定义函数供大模型调用 +- 🔄 实现基于MCP (Model Context Protocol) 的函数调用框架 +- 💾 使用SQLAlchemy将消息存储到MySQL数据库 + +## 效果图🧸 + +
+
+
+
+ qzp(#Bm%aZ*%Ie_nSz*v#SH{AxMMjpF%iZ>b=!QPO<_~
z?=U}J*HP#xAWh(O7O>b)cU|HEoIM>z>zwR$5#;tGTJih3vAKd5M4FXvn4|GD-!8Yl
z)le?H5b?NR-f40Ur=DoY0v|s*9^aG<8g(XmLOd461x#;(N2gWBo;D_@r?L0m-5oDB
zOeuFs%$>In8`rM6X%$oRu6_$lb<1kic^FN6$nW+jEZfxdi|6I0(KwkEzV~!ja~^oI
zE@2+`AqD(7cK0q;5lL~3_MeR()kXT36?MHakKg^`OCnLR&Bqz0FA^9Q{9 Gh5ORxam6e=Sc)Z}(>cvqAZsd?RG3VRcgJK!_fwI^MB1&h$iC-PED%
zU}#i@OdfHc9Ftzc>}fBTRo`Gtea_KP!^g6Z(&+cmQe)ooVJpU@YJM&!qB<%u6lHoY
zB<09)FFi0ew?Vk=E*}G