笔记所对应活动链接:https://activity.csdn.net/writing?id=11045&spm=1011.2124.3001.10637
作为一名嵌入式开发者,我一直关注智能音箱的二次开发潜力——市面上大多数智能音箱的功能相对固定,而小智AI音箱支持的MCP开发模式,恰好提供了自定义扩展的可能。恰逢CSDN发起“如何让小智AI成为你的第二大脑”征文活动,今天就带来一篇保姆级MCP开发实战教程,教你从零搭建“办公场景语音助手”技能,轻松实现日程提醒、快速记笔记、单位换算等自定义功能,同时满足活动创作要求,助力大家赢取小智AI音箱奖励!

打开终端执行以下命令,完成基础环境配置:
# 安装核心依赖库
pip install requests pyyaml flask
# 安装小智MCP SDK(需从官方渠道下载后本地安装)
pip install ./secretflow-mcp-sdk-1.0.0.tar.gzAppID与AppSecret(后续接口调用需用到)。创建测试脚本test_env.py,验证SDK是否正常工作:
from secretflow_mcp import MCPClient
# 初始化客户端
client = MCPClient(
app_id="你的AppID",
app_secret="你的AppSecret",
device_sn="你的音箱SN码"
)
# 测试设备连接
try:
response = client.get_device_status()
print("设备状态:", response["status"])
print("环境搭建成功!")
except Exception as e:
print("环境搭建失败:", str(e))运行脚本后若输出“设备状态:online”,则说明环境配置无误。
本次开发聚焦“办公效率提升”,实现3个核心功能:语音记笔记、日程提醒设置、单位换算,以下是详细实现步骤。

采用“意图识别+功能分发”架构,核心流程如下:

创建intent_recognizer.py,通过关键词匹配实现基础意图识别(实际项目可接入AI分词模型优化):
def recognize_intent(text):
"""
识别用户指令意图
返回:intent(意图)、params(参数)
"""
text = text.lower().strip()
if any(keyword in text for keyword in ["笔记", "记录", "写下"]):
# 提取笔记内容(例:“记录会议要点:明天下午3点评审”→“会议要点:明天下午3点评审”)
content = text.replace("笔记", "").replace("记录", "").replace("写下", "").strip()
return "note", {"content": content}
elif any(keyword in text for keyword in ["提醒", "闹钟", "记得"]):
# 提取提醒时间与内容(例:“明天上午10点提醒提交报告”→时间:明天上午10点,内容:提交报告)
import re
time_pattern = r"(\d+[点分])([上下]午)?"
time_match = re.search(time_pattern, text)
time = time_match.group() if time_match else ""
content = re.sub(time_pattern, "", text).replace("提醒", "").replace("闹钟", "").replace("记得", "").strip()
return "reminder", {"time": time, "content": content}
elif any(keyword in text for keyword in ["换算", "转换", "等于"]):
# 提取换算参数(例:“100美元等于多少人民币”→100,美元,人民币)
import re
num_pattern = r"\d+(\.\d+)?"
num_match = re.search(num_pattern, text)
num = num_match.group() if num_match else ""
from_unit = "美元" if "美元" in text else "人民币" if "人民币" in text else ""
to_unit = "人民币" if "人民币" in text and "美元" in text else "美元" if "美元" in text and "人民币" in text else ""
return "conversion", {"num": num, "from_unit": from_unit, "to_unit": to_unit}
else:
return "unknown", {"text": text}
创建function_modules.py,实现各意图对应的业务逻辑:
import json
import time
from datetime import datetime
# 笔记模块:存储笔记到本地文件
def handle_note(params):
content = params["content"]
if not content:
return "请告诉我要记录的内容~"
# 按日期创建笔记文件
date_str = datetime.now().strftime("%Y-%m-%d")
with open(f"notes_{date_str}.txt", "a", encoding="utf-8") as f:
timestamp = datetime.now().strftime("%H:%M:%S")
f.write(f"[{timestamp}] {content}\n")
return f"笔记已记录:{content}"
# 提醒模块:存储提醒到JSON文件(实际可对接系统闹钟)
def handle_reminder(params):
time_str = params["time"]
content = params["content"]
if not time_str or not content:
return "请告诉我提醒时间和内容,例如:明天上午10点提醒提交报告"
# 存储提醒
reminder = {
"time": time_str,
"content": content,
"create_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"status": "pending"
}
with open("reminders.json", "a", encoding="utf-8") as f:
json.dump(reminder, f, ensure_ascii=False)
f.write("\n")
return f"已设置提醒:{time_str} {content}"
# 换算模块:美元-人民币简单换算(实际可对接汇率API)
def handle_conversion(params):
try:
num = float(params["num"])
from_unit = params["from_unit"]
to_unit = params["to_unit"]
exchange_rate = 7.2 # 示例汇率,实际可通过API获取实时汇率
if from_unit == "美元" and to_unit == "人民币":
result = num * exchange_rate
return f"{num}美元等于{result:.2f}人民币"
elif from_unit == "人民币" and to_unit == "美元":
result = num / exchange_rate
return f"{num}人民币等于{result:.2f}美元"
else:
return "目前仅支持美元与人民币换算~"
except Exception as e:
return "换算失败,请检查输入格式,例如:100美元等于多少人民币"
创建app.py,用Flask搭建后端服务,对接MCP接口:
from flask import Flask, request, jsonify
from intent_recognizer import recognize_intent
from function_modules import handle_note, handle_reminder, handle_conversion
from secretflow_mcp import MCPClient
app = Flask(__name__)
# 初始化MCP客户端
client = MCPClient(
app_id="你的AppID",
app_secret="你的AppSecret",
device_sn="你的音箱SN码"
)
@app.route("/mcp/intent", methods=["POST"])
def handle_intent():
data = request.json
user_text = data.get("text", "")
if not user_text:
return jsonify({"response": "请再说一遍~"})
# 识别意图
intent, params = recognize_intent(user_text)
# 分发处理
if intent == "note":
response = handle_note(params)
elif intent == "reminder":
response = handle_reminder(params)
elif intent == "conversion":
response = handle_conversion(params)
else:
response = "我还没学会这个功能,试试让我记笔记、设提醒或换算货币吧~"
# 通过音箱语音反馈
client.send_voice_response(response)
return jsonify({"response": response})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
语音指令 | 预期结果 | 实际反馈 |
|---|---|---|
“办公助手,记录会议要点:明天评审” | 记录笔记并语音确认 | “笔记已记录:会议要点:明天评审” |
“办公助手,明天上午10点提醒提交报告” | 设置提醒并语音确认 | “已设置提醒:明天上午10点 提交报告” |
“办公助手,100美元等于多少人民币” | 计算并反馈结果 | “100美元等于720.00人民币” |


小智AI音箱的MCP开发模式,让开发者无需关注底层硬件细节,就能快速实现自定义技能,极大降低了智能设备二次开发的门槛。本次开发的办公语音助手,虽然功能不算复杂,但完全贴合实际办公场景,真正实现了“让AI成为第二大脑”的目标。
在开发过程中,我深刻体会到“技术落地”的价值——相比于复杂的算法,解决用户真实痛点的小功能更具意义。后续我还计划扩展更多办公功能,如语音转文字、邮件快速发送等,让小智AI音箱成为更强大的办公助手。
如果你在开发过程中遇到环境搭建、接口调用等问题,欢迎在评论区留言交流;也期待看到更多开发者分享创意十足的MCP开发项目,一起在这次活动中交流成长,赢取丰厚奖励!