Microsoft Agent Framework Skills 实行 Scripts(实战指南)

[复制链接]
发表于 2026-3-24 22:06:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 Microsoft Agent Framework 中,Agent Skills 是一个非常紧张但轻易被忽略的本领。它可以让你的 Agent 拥有“插件化本领”,乃至可以实行当地脚本(如 Python),实现真正的自动化。
本文团结完备示例,带你从 原理 → 结构 → 实战 → 坑点 全面把握怎样通过 Skills + 自界说 Tool,实现实行 Python 脚本。
一、什么是 Agent Skills?

官方界说:
Agent Skills 是一组可移植的指令、脚本和资源,用于为 Agent 提供特定本领。
简单明白就是给 Agent 安装“技能包”,好比:

  • 数据分析 skill
  • 报销考核 skill
  • 集会纪要 skill(本文示例)
二、Skill 目次结构

一个标准 Skill 结构如下:
  1. skills/
  2. └── meeting-notes/
  3.     ├── SKILL.md
  4.     └── scripts/
  5.         └── GetMeetingNotes.py
复制代码
1. SKILL.md(核心)
  1. ---
  2. name: meeting-notes
  3. description: Various python scripts for meeting-notes
  4. ---
复制代码
  1. # The meeting notes
  2. [GetMeetingNotes](meeting-notes/scripts/GetMeetingNotes.py)
  3. Give above script-path to a 'execute_python' tool
复制代码
关键点:

  • name 必须和目次划一
  • description 用于让 Agent 判断是否使用该 skill
  • markdown 内容 = 利用阐明
2. Python Script
  1. def main():
  2.     return "Chester completed the create user api, and need implement the update user api"
  3. if __name__ == "__main__":
  4.     print(main())
复制代码
阐明:

  • 界说 main()
  • 末了 print 输出
三、关键标题:C# 现在不支持自动实行 scripts

官方文档明白阐明:
Script execution is not yet supported in C#
以是:

  • ❌ Agent 不会自动实行 .py
  • ✅ 须要本身提供 Tool
四、核心实现:自界说 Python 实行器

PythonRunner.cs
  1. public static class PythonRunner
  2. {
  3.     public static string RunPhytonScript(string pythonFilePath)
  4.     {
  5.         if (string.IsNullOrWhiteSpace(pythonFilePath))
  6.             throw new ArgumentException("File path cannot be empty.", nameof(pythonFilePath));
  7.         pythonFilePath = Path.Combine(AppContext.BaseDirectory, "skills", pythonFilePath);
  8.         if (!File.Exists(pythonFilePath))
  9.             throw new FileNotFoundException("Python file not found.", pythonFilePath);
  10.         if (!string.Equals(Path.GetExtension(pythonFilePath), ".py", StringComparison.OrdinalIgnoreCase))
  11.             throw new ArgumentException("File must have a .py extension.", nameof(pythonFilePath));
  12.         ProcessStartInfo startInfo = new ProcessStartInfo
  13.         {
  14.             FileName = "python",
  15.             Arguments = """ + pythonFilePath + """,
  16.             UseShellExecute = false,
  17.             RedirectStandardOutput = true,
  18.             RedirectStandardError = true,
  19.             CreateNoWindow = true,
  20.             StandardOutputEncoding = Encoding.UTF8,
  21.             StandardErrorEncoding = Encoding.UTF8
  22.         };
  23.         using (Process process = new Process { StartInfo = startInfo })
  24.         {
  25.             process.Start();
  26.             string output = process.StandardOutput.ReadToEnd();
  27.             string error = process.StandardError.ReadToEnd();
  28.             process.WaitForExit();
  29.             if (process.ExitCode != 0 || !string.IsNullOrWhiteSpace(error))
  30.                 throw new InvalidOperationException(
  31.                     "Python script failed. ExitCode=" + process.ExitCode + Environment.NewLine + error.Trim());
  32.             return output.TrimEnd();
  33.         }
  34.     }
  35. }
复制代码
五、把 Python 实行本领酿成 Agent Tool

Program.cs
  1. Tools = [
  2.     AIFunctionFactory.Create(
  3.         PythonRunner.RunPhytonScript,
  4.         name: "execute_python"
  5.     )
  6. ]
复制代码
关键点:

  • 方法袒露为 Tool
  • 名字必须和 SKILL.md 里的阐明划一
六、Skills + Tool 协作流程
  1. 用户输入
  2.    ↓
  3. Agent 发现 skills(Advertise)
  4.    ↓
  5. 判断匹配 meeting-notes skill
  6.    ↓
  7. load_skill(加载 SKILL.md)
  8.    ↓
  9. 理解指令:"调用 execute_python"
  10.    ↓
  11. 调用 Tool
  12.    ↓
  13. 执行 Python
  14.    ↓
  15. 返回结果
复制代码
七、运行结果

用户输入:
  1. 帮我获取会议纪要
复制代码
Agent 内部活动:

  • 发现 meeting-notes
  • 读取 SKILL.md
  • 找到实行阐明:
  1. Give above script-path to a 'execute_python' tool
复制代码

  • 调用:
  1. execute_python("meeting-notes/scripts/GetMeetingNotes.py")
复制代码
终极输出:
  1. Chester completed the create user api, and need implement the update user api
复制代码
八、关键筹划模式

Pattern:Skill 负责“决定”,Tool 负责“实行”

层级作用Skill告诉 Agent 做什么Tool真正实行阐明:

  • Skill ≠ 代码实行
  • Skill = Prompt + 指南
九、安全发起

实行脚本属于高风险利用,须要注意:
1. 限定路径
  1. skills/xxx
复制代码
克制实行体系敏感目次,如:
  1. ../../../../system32
复制代码
2. 白名单机制

只答应实行:
  1. scripts/*.py
复制代码
3. 沙箱运行


  • Docker
  • 限定权限
  • 克制网络访问(须要时)
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表