send_message
Send messages to Feishu group chats by providing chat ID and text. Requires disabling read-only mode for execution.
Instructions
发送消息到飞书群聊(需要关闭只读模式)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chat_id | Yes | 群聊ID | |
| text | Yes | 消息内容 |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/feishu_mcp_server/server.py:109-125 (handler)The MCP tool handler for 'send_message'. Decorated with @mcp.tool(), it accepts chat_id and text, checks if messaging is enabled, calls the client's send_message, and returns the result as JSON. Handles PermissionError (read-only mode) and generic exceptions.
@mcp.tool() def send_message(chat_id: str, text: str) -> str: """发送消息到飞书群聊(需要关闭只读模式) Args: chat_id: 群聊ID text: 消息内容 """ if not config.enable_message: return json.dumps({"error": "消息功能未启用"}, ensure_ascii=False) try: result = client.send_message(chat_id, text) return json.dumps(result, ensure_ascii=False, indent=2, default=str) except PermissionError as e: return json.dumps({"error": str(e)}, ensure_ascii=False) except Exception as e: return json.dumps({"error": str(e)}, ensure_ascii=False) - The FeishuClient.send_message() method that actually makes the API call. Checks read_only mode and allowed_chats permissions, then POSTs to /im/v1/messages with receive_id_type=chat_id to send a text message to the specified chat.
def send_message( self, chat_id: str, text: str, msg_type: str = "text" ) -> dict[str, Any]: """发送消息到群聊""" if self.config.read_only: raise PermissionError("只读模式下禁止发送消息") if self.config.allowed_chats and chat_id not in self.config.allowed_chats: raise PermissionError(f"群聊 '{chat_id}' 不在允许发送的列表中") return self._request( "POST", "/im/v1/messages", params={"receive_id_type": "chat_id"}, json_data={ "receive_id": chat_id, "msg_type": msg_type, "content": f'{{"text":"{text}"}}' if msg_type == "text" else text, }, ) - src/feishu_mcp_server/server.py:109-110 (registration)The @mcp.tool() decorator on line 109 registers 'send_message' as an MCP tool with the FastMCP server.
@mcp.tool() def send_message(chat_id: str, text: str) -> str: - src/feishu_mcp_server/config.py:8-28 (schema)FeishuConfig schema defines the settings relevant to send_message: enable_message toggle, read_only flag, and allowed_chats whitelist.
class FeishuConfig(BaseSettings): """飞书应用配置""" app_id: str = Field(default="", description="飞书应用App ID") app_secret: str = Field(default="", description="飞书应用App Secret") # 功能开关 enable_doc: bool = Field(default=True, description="启用文档读取") enable_message: bool = Field(default=True, description="启用消息发送") enable_calendar: bool = Field(default=True, description="启用日历查询") enable_sheet: bool = Field(default=True, description="启用电子表格") # 安全配置 read_only: bool = Field(default=True, description="只读模式(禁止发送消息等写操作)") allowed_chats: Optional[list[str]] = Field( default=None, description="允许发送消息的群聊ID列表" ) # API配置 base_url: str = Field(default="https://open.feishu.cn/open-apis", description="飞书API基础URL") token_cache_seconds: int = Field(default=3600, description="Token缓存时间(秒)") - tests/test_server.py:36-41 (helper)Test class TestMessagePermission verifying that send_message is blocked in read-only mode and for unallowed chats.
class TestMessagePermission: def test_send_blocked_in_readonly(self): config = FeishuConfig(app_id="test", app_secret="secret", read_only=True) client = FeishuClient(config) with pytest.raises(PermissionError, match="只读"): client.send_message("chat_id", "hello")