小智ai+mcp+n8n的智能组合
小智ai+mcp+n8n的智能组合
- 1 小智ai的版本
- 2 n8n的配置
- 3 mcp的demo
- 4 工作流json
之前有写过小智ai的介绍,它提供了流畅且丰富的用户语音交互能力。n8n提供了灵活且稳定的后台工作流的能力,如果这两个工具进行组合,可以打造一个好玩又好用的智能ai应用。
我这里使用了官方提供的mcp服务来联动这两个工具,主要是借用小智ai与n8n的开源优越性。
1 小智ai的版本
我之前有写过介绍了的(虾哥小智AI助手python电脑端初体验_虾哥 小智-CSDN博客 ),我目前使用的是黄俊森之前的一版,目前作者已经在github版本更新了,把之前外挂mcp服务的形式,改为新版本集成了。不过我这里还是使用的老版本,我使用的版本地址,提取码: phil。
2 n8n的配置
这里使用的是Webhook节点、Respond to Webhook节点,分别作为post、response的对象。其中,Webhook节点的节点配置如下:
- HTTP Method: POST
- Authentication: Header Auth 这里在header中加入了自己定义的api_key,这一步也可以不添加。
- Respond: Using ‘Respond to Webhook’ Node 这里需要选择使用Respond to Webhook节点响应。
- Options: data 这里接收小智ai客户端post来的参数信息。
Respond to Webhook节点这里,我配置的信息有:
Respond With: JSON
Response Body :
{
“task_id”: “{{ $(‘Webhook’).item.json.body.task_id }}”,
“workflow_id”: "{{ KaTeX parse error: Expected 'EOF', got '}' at position 15: workflow.name }̲}", "status":…json.output.toJsonString()}}
}Options: Response Code 200
Response Headers: 自定义配置吧,也可以不配置
AI Agent这里的System Message,需要好好打磨和调试,因为它是工具调用的灵魂,我这里提供一个demo
你是一位用户需求智能响应管家,你必须使用中文回复,你必须根据用户的需求信息进行分析,然后进行反馈。
这里是用户的输入信息:{{ $(‘Webhook’).item.json.body.user_input}}
你的任务1:
如果用户的输入信息中含有"百度热点"关键词,你必须运行获取百度热点工具,并把该工具的输出信息反馈给用户,按照这里的格式输出:获取了6条信息:1.热点信息1、2.热点信息2、你的任务2:
如果用户的输入信息中明确提到不知道你的能力,可以把你的工具介绍给用户,只介绍工具名称。
n8n除了可以添加tool工具外,还支持工作流调用子工作流,需要准确的System Message进行调度识别。
3 mcp的demo
这里是一个requests的demo,添加在mcp的server服务中即可。
@mcp.tool()
def start_n8n_workflow(user_input: str) -> dict:"""Open n8n workflow with the given user_input.then return the result of the workflow."""import requestsimport jsonimport time# 从配置文件读取n8n相关配置BASE_WEBHOOK_URL = "你的n8n中的webhook地址"X_N8N_API_KEY = "你自定义的api_key"# 请求头设置headers = {"Content-Type": "application/json","X-N8N-API-KEY": X_N8N_API_KEY}# 生成时间戳作为task_idtask_id = str(int(time.time()))# 请求数据data = {"user_input": user_input,"task_id": task_id}try:# 发送POST请求response = requests.post(BASE_WEBHOOK_URL + WEBHOOK_ID,headers=headers,data=json.dumps(data))# 处理响应# 检查请求是否成功if response.status_code == 200:logger.info(f"n8n workflow 请求成功,状态码: 200 ok ")data = response.json()result = data.get('result')else:logger.info(f"n8n workflow 请求失败,状态码: {response.status_code}")result = f"n8n workflow 请求失败,状态码: {response.status_code}"return {"success": True, "result": result}except Exception as e:logger.info(f"发送请求时发生错误: {str(e)}")return {"success": False, "result": str(e)}
4 工作流json
保存下面的文本为json文件,在n8n工作流编辑器页面点击从文件导入即可。
{"name": "xz_n8n_assistant","nodes": [{"parameters": {"httpMethod": "POST","path": "6eb7f60e-23e3-4074-963a-6f42dee63e69","authentication": "headerAuth","responseMode": "responseNode","options": {"binaryPropertyName": "data"}},"type": "n8n-nodes-base.webhook","typeVersion": 2,"position": [-896,-384],"id": "d4752292-416d-4bef-8de2-60be70d66cdb","name": "Webhook","webhookId": "6eb7f60e-23e3-4074-963a-6f42dee63e69","credentials": {"httpHeaderAuth": {"id": "9FNAq5gcbSRzHtBQ","name": "Header Auth account 2"}}},{"parameters": {"promptType": "define","text": "根据用户输入的需求信息,调用工具,最后用中文输出","options": {"systemMessage": "=你是一位用户需求智能响应管家,你必须使用中文回复,你必须根据用户的需求信息进行分析,然后进行反馈。\n\n这里是用户的输入信息:{{ $('Webhook').item.json.body.user_input}}\n\n你的任务1:\n如果用户的输入信息中含有\"百度热点\"关键词,你必须运行获取百度热点工具,并把该工具的输出信息反馈给用户,按照这里的格式输出:获取了6条信息:1.热点信息1、2.热点信息2、\n\n你的任务2:\n如果用户的输入信息中明确提到不知道你的能力,可以把你的工具介绍给用户,只介绍工具名称。\n\n\n\n\n"}},"type": "@n8n/n8n-nodes-langchain.agent","typeVersion": 2.1,"position": [-704,-384],"id": "d34bbdfd-e881-41b6-a184-513987862e7c","name": "AI Agent","retryOnFail": false,"maxTries": 2,"executeOnce": true,"alwaysOutputData": false},{"parameters": {"model": {"__rl": true,"value": "=openai/gpt-oss-120b","mode": "id"},"options": {}},"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi","typeVersion": 1.2,"position": [-704,-176],"id": "ba7a8eb0-0d48-4872-92f0-75c9863ed48e","name": "OpenAI Chat Model","credentials": {"openAiApi": {"id": "Hm8gyQkgPUkOVBi6","name": "OpenAi account"}}},{"parameters": {"respondWith": "json","responseBody": "={\n \"task_id\": \"{{ $('Webhook').item.json.body.task_id }}\",\n \"workflow_id\": \"{{ $workflow.name }}\",\n \"status\": \"success\",\n \"result\": {{$json.output.toJsonString()}}\n}","options": {"responseCode": 200,"responseHeaders": {"entries": [{"name": "X-N8N-API-KEY","value": "n8n_12345"},{"name": "Content-Type","value": "application/json; charset=utf-8"}]}}},"type": "n8n-nodes-base.respondToWebhook","typeVersion": 1.4,"position": [-304,-384],"id": "ab850bdd-6ef4-46b8-be98-818a6683bdb4","name": "Respond to Webhook"},{"parameters": {"toolDescription": "一个获取百度热点的工具","url": "https://api.1314.cool/getbaiduhot/","options": {}},"type": "n8n-nodes-base.httpRequestTool","typeVersion": 4.2,"position": [-512,-160],"id": "5ca81a09-cd74-45c2-97d7-ce656fd01d76","name": "HTTP-百度热点","alwaysOutputData": false}],"pinData": {},"connections": {"Webhook": {"main": [[{"node": "AI Agent","type": "main","index": 0}]]},"AI Agent": {"main": [[{"node": "Respond to Webhook","type": "main","index": 0}]]},"OpenAI Chat Model": {"ai_languageModel": [[{"node": "AI Agent","type": "ai_languageModel","index": 0}]]},"HTTP-百度热点": {"ai_tool": [[{"node": "AI Agent","type": "ai_tool","index": 0}]]}},"active": false,"settings": {"executionOrder": "v1"},"versionId": "b5a76a30-1562-4353-b29f-4d7da92a2b86","meta": {"templateCredsSetupCompleted": true,"instanceId": "1f16bc26aa41d68046a6cd33330705e84dc6e662ed18adf783908c69e188965b"},"id": "F17dzJC0t32JuQX3","tags": []
}