Qwen3系列模型拥有卓越的Agent能力。然而,从模型到Agent,开发者们仍存在许多技术及工程适配等难题。
今天,我们推出3个基于Qwen-Agent框架的CookBook,演示如何通过这一框架让Qwen3丝滑调用MCP Server的全过程。不论是本地部署模型或是API调用模型,开发者均可通过Qwen-Agent来完成一个复杂的Agent任务,甚至轻松打造令人惊艳的Agent应用。
Qwen-Agent框架
Qwen-Agent是我们充分挖掘模型Agent能力的一个尝试,这一框架可最大程度地发挥Qwen模型在指令遵循、工具使用、规划及记忆等方面的强劲能力,并且内部封装了工具调用模板和解析器,原生支持 MCP 协议。通过使用 Qwen-Agent ,Qwen3模型可无缝调用成熟的MCP服务,Agent开发成本可大大降低。
在此,我们通过三个基于Qwen-Agent的CookBook示例,全面展示Qwen3的Agent能力:
1️⃣cookbook_database_manipulation实现自然语言驱动的数据库操作闭环(文件解析→数据入库→智能查询)
2️⃣cookbook_drive_guide深度融合云端高德API提供实时地理智能服务(路径规划/旅游推荐)
3️⃣cookbook_mind_map则能一键将文档转化为结构化思维导图。
仓库地址:
CookBook
「文档转思维导图」示例
Step1:环境配置
打开终端或jupyter notebook中执行shell命令,进行第三方包安装和检查环境配置:
pip3 install -U "qwen-agent[gui,rag,code_interpreter,mcp]" pip3 install -U uv npm --version #jupyter notebook使用命令: !pip3 install -U "qwen-agent[gui,rag,code_interpreter,mcp]" !pip3 install -U uv !npm --version
Step2:创建示例文档
创建一个简单的示例文档:
document = ''' # Document ## Introduction - Brief overview of the document's purpose and main topics. ## Background - Historical context and previous research. - Key theories and models discussed. ## Methodology - Detailed description of the methods used in the study. - Explanation of data collection and analysis techniques. ## Results - Presentation of the findings from the research. - Statistical analysis and graphical representations. ## Discussion - Interpretation of the results. - Comparison with previous studies. - Limitations andfuture research directions. ## Conclusion - Summary of the main points covered in the document. - Final thoughts and implications of the research. '''.strip() with open('example_document.md', 'w', encoding='utf-8') as f: f.write(document)
Step3: 创建智能Agent
创建一个能够执行以下任务的智能Agent:
- 列出、读取和写入文件(通过 MCP 服务器 @modelcontextprotocol/server-filesystem)。
- 制作思维导图(通过 MCP 服务器 mindmap-mcp-server)。
from qwen_agent.agents import Assistant from qwen_agent.utils.output_beautify import typewriter_print #typewriter_print打印流式消息
llm_cfg = { 'model': 'qwen3-32b', 'model_server': 'dashscope', 'api_key': '' # 填写百炼API-KEY # 使用与 OpenAI API 兼容的模型服务,例如 vLLM 或 Ollama: # 'model': 'Qwen3-8B', # 'model_server': 'http://localhost:8000/v1', # 'api_key': 'EMPTY' } tools = [{ "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", '.', ] }, "mindmap": { "command": "uvx", "args": ["mindmap-mcp-server", "--return-type", "filePath"] } } }] agent = Assistant( llm=llm_cfg, function_list=tools )
Step4:标题编号
我们在 Markdown 标题中添加数字:
messages = [] messages += [{"role": "user", "content": "Read the example_document.md file in the current folder. In your reply, show me the content with the second-level headings numbered. (example: ## heading -> ## 1. heading)"}]
response_plain_text = '' for ret_messages in agent.run(messages): # ret_messages 将包含所有后续消息,由助手消息和工具响应组成 response_plain_text = typewriter_print(ret_messages, response_plain_text)
messages += ret_messages #将 ret_messages 历史上下对话信息中。
Step5:创建思维导图
接下来,我们通过Agent 创建一个思维导图,并显示存储路径:
messages += [{'role': 'user', 'content': 'Create a mind map with your previous markdown output. Show me the saved path.'}]
response_plain_text = '' for ret_messages in agent.run(messages): response_plain_text = typewriter_print(ret_messages, response_plain_text)
更方便的Web UI可视化操作
我们已经探索了 Qwen-Agent 框架和 Qwen 模型对文档转换为思维导图的功能。我们不仅可以基于代码进行操作,也可以使用Web UI进行可视化操作。
我们通过下方的代码启动Web UI 服务:
from qwen_agent.gui import WebUI agent = Assistant( name="Qwen Assistant", description="I'm a digital assistant powered by Qwen-Agent, ask me anything!", llm=llm_cfg, function_list=tools ) WebUI(agent).run()
欢迎体验
通过以上示例,我们完整展现了 Qwen-Agent 如何通过Qwen3调用 MCP Server服务。从文件读取、智能编号到思维导图转换,Qwen3展现出强大的对复杂任务的理解与执行能力。
我们还准备了另外两个示例,欢迎大家前往前文地址查看。更多MCP服务,也可通过魔搭社区MCP广场获取。
Qwen-Agent 的意义,不止于“能用”,更在于“好用”:它封装了模型调用、工具集成的关键环节,让开发者真正可以把更多精力放在“做什么”上,而不是“怎么做”。依托 Qwen3 在指令遵循、思维链规划、工具调用等方面的优势,Qwen-Agent 将Agent的开发门槛大幅降低。
我们将继续探索 Qwen 在现实场景中的更多可能性,欢迎大家上手体验并给我们反馈。