具有模型上下文协议的AI代理-Python代码示例
此存储库包含Coursera课程“具有模型上下文协议的AI代理”的Python代码示例
先决条件
- Python 3.10+
- 点
- 来自OpenAI、Anthropic或Google的API密钥(设置于
.env文件)
设置
每个模块都有自己的 python-code/ 目录。要设置任何模块,请执行以下操作:
cd /python-code
pip install -r requirements.txt
cp .env.example .env # Then add your API key(s)______________________________________________________________________
课程结构和代码映射
模块1:人工智能代理模型上下文协议(MCP)入门
| 项目 | 类型 | 代码文件 |
|---|---|---|
| 为什么我们需要模型上下文协议? | 视频 | -- |
| 模型上下文协议与人工智能问题解决工具 | 视频 | -- |
| MCP允许AI与计算机通信 | 视频 | -- |
| AI@Work-我与AI的合作是什么样子 | 视频 | -- |
| 我的人工智能工具 | 阅读 | -- |
| 了解更多并保持联系 | 阅读 | -- |
*模块1是概念介绍,没有代码练习。*
______________________________________________________________________
模块2:AI代理循环和模型上下文协议
| 项目 | 类型 | 代码文件 | 运行命令 |
|---|---|---|---|
| 模型上下文协议:语法、语义、时序 | 视频 | -- | -- |
| 模型上下文协议和AI代理 | 视频 | -- | -- |
| 什么是MCP服务器? | 视频 | -- | -- |
| 工具规格 | 视频 | -- | -- |
| 构建您的第一台MCP服务器 | 未分级插件 | server.py | python server.py |
| 构建您的第一个MCP AI代理 | 未分级插件 | agent.py, llm.py | python agent.py "question" |
| 代理与工具对话与人工智能工具 | 视频 | -- | -- |
代码位置: 01-intro-to-mcp-agents/python-code/
关键文件:
| 文件 | 描述 |
|---|---|
server.py | 基本MCP工具服务器 list_files 和 read_file 工具 |
agent.py | 代理循环实现(PERCEIVE→决定→ACT→观察) |
llm.py | LLM集成工具(OpenAI/Anthropic/GGemini) |
test_server.py | 无需代理即可直接测试服务器工具 |
server_with_resources.py | 带有MCP资源的服务器,用于教学代理 |
agent_with_learning.py | 在执行操作之前读取资源的代理 |
命令:
cd 01-intro-to-mcp-agents/python-code
pip install -r requirements.txt
python server.py # Start the basic MCP server
python agent.py "question" # Run the basic agent
python test_server.py # Test server tools directly______________________________________________________________________
模块3:使用模型上下文协议构建AI代理
| 项目 | 类型 | 代码文件 | 运行命令 |
|---|---|---|---|
| 资源 | 视频 | -- | -- |
| 教代理人使用工具 | 未分级插件 | workspace_server.py | python workspace_server.py |
| 教导代理人寻求帮助 | 未分级插件 | workspace_server.py | python workspace_server.py |
| 帮助代理商找到指导 | 未分级插件 | workspace_server.py | python workspace_server.py |
| 帮助AI代理在飞行中 | 未分级插件 | workspace_server.py | python workspace_server.py |
| 帮助AI代理发现与工作空间相关的指导 | 未分级插件 | workspace_agent.py | python workspace_agent.py "question" |
代码位置: 01-intro-to-mcp-agents/python-code/
关键文件:
| 文件 | 描述 |
|---|---|
workspace_server.py | 具有工作区感知工具和上下文发现的增强服务器 |
workspace_agent.py | 在具有上下文感知的定义工作空间内工作的代理 |
workspace/ | 示例工作空间 .context.md 测试文件 |
命令:
cd 01-intro-to-mcp-agents/python-code
python workspace_server.py # Start workspace-aware server
python workspace_agent.py "question" # Run workspace agent with context discovery______________________________________________________________________
模块4:AI代理的稳健错误处理技术
| 项目 | 类型 | 代码文件 | 运行命令 |
|---|---|---|---|
| 响应不仅仅是数据 | 未分级插件 | expense_server.py | python expense_server.py |
| 设计错误以帮助AI代理 | 未分级插件 | expense_server.py | python expense_server.py |
| 复杂工作流中的错误 | 未分级插件 | expense_server.py | python expense_server.py |
| 从错误恢复中最小化人工智能代理的认知负担 | 未分级插件 | expense_server.py | python expense_server.py |
| 帮助AI代理找到修复错误的替代路径 | 未分级插件 | expense_server.py | python expense_server.py |
代码位置: 02-failing-forward/python-code/
关键文件:
| 文件 | 描述 |
|---|---|
expense_server.py | MCP服务器演示所有故障转发模式 |
expense_agent.py | 从错误中学习以完成支出任务的代理 |
test_failing_forward.py | 对所有失败的正向模式进行全面测试 |
命令:
cd 02-failing-forward/python-code
pip install -r requirements.txt
python expense_server.py # Start the expense server
python expense_agent.py "request" # Run the expense agent
python test_failing_forward.py # Run failing forward tests模式映射:
| Coursera项目 | 模式 | 实现 |
|---|---|---|
| 响应不仅仅是数据 | 响应是指令 | next_action, hint 响应中的字段 |
| 设计错误以帮助人工智能代理 | 错误作为课程 | submit_expense 指导下的验证 |
| 复杂工作流中的错误 | 错误链 | request_late_expense_approval → check_approval_status |
| 最小化AI代理认知负担 | 预填充参数 | suggested_params 在错误响应中 |
| 帮助AI代理找到替代路径 | 替代行动 | alternatives 响应中的数组 |
______________________________________________________________________
模块5:更快、更可预测、更有能力的人工智能代理
| 项目 | 类型 | 代码文件 | 运行命令 |
|---|---|---|---|
| 管理AI代理认知负荷 | 未分级插件 | agent_heavy_server.py | |
tool_heavy_server.py | python agent_heavy_server.py | ||
python tool_heavy_server.py | |||
| 可预测性、低成本、速度:脚本编排 | 未分级插件 | scripted_orchestration_server.py | python scripted_orchestration_server.py |
| 提示和MCP | 视频 | -- | -- |
| 自我提示:在工具中添加推理 | 未分级插件 | self_prompting_server.py | python self_prompting_server.py |
| 常见错误的人工智能代理工具设计 | 未分级插件 | validate_at_source_server.py | python validate_at_source_server.py |
| AI代理、MCP和身份/安全 | 视频 | -- | -- |
| 结束 | 视频 | -- | -- |
| 最终评估 | 作业 | -- | -- |
代码位置: 03-intelligence-budget/python-code/
关键文件:
| 文件 | 描述 |
|---|---|
agent_heavy_server.py | 最小化工具方法-代理执行大部分处理 |
tool_heavy_server.py | 丰富的工具方法-工具为代理预处理数据 |
hybrid_server.py | 结合两种策略的平衡方法 |
scripted_orchestration_server.py | 允许代理编写批处理操作脚本的工具 |
self_prompting_server.py | 使孤立LLM调用语义推理的工具 |
validate_at_source_server.py | 具有分层验证的工具(格式→ 商业→ 语义) |
database.py | 所有服务器示例使用的共享模拟数据库 |
test_all.py | 比较所有方法的综合测试套件 |
命令:
cd 03-intelligence-budget/python-code
pip install -r requirements.txt
python agent_heavy_server.py # Start agent-heavy server
python tool_heavy_server.py # Start tool-heavy server
python hybrid_server.py # Start hybrid server
python test_all.py # Run comparison tests模式映射:
| Coursera项目 | 模式 | 实现 |
|---|---|---|
| 管理人工智能代理认知负荷 | 智能预算 | 代理重与工具重的方法 |
| 可预测性、更低成本、更快 | 脚本编排 | execute_script 批量操作工具 |
| 自我提示:添加推理 | 自我提示 | 工具内孤立的LLM调用 |
| 常见错误的AI代理工具设计 | 源验证 | 分层验证堆栈(格式→ 商业→ 语义) |
______________________________________________________________________
快速参考:Coursera项目→ 代码文件
| 模块 | Coursera项目 | 代码文件 |
|---|---|---|
| 2 | 构建您的第一个MCP服务器 | 01-intro-to-mcp-agents/python-code/server.py |
| 2 | 构建您的第一个MCP AI代理 | 01-intro-to-mcp-agents/python-code/agent.py |
| 3 | 教导代理人使用工具 | 01-intro-to-mcp-agents/python-code/workspace_server.py |
| 3 | 教导代理人寻求帮助 | 01-intro-to-mcp-agents/python-code/workspace_server.py |
| 3 | 帮助代理商找到指导 | 01-intro-to-mcp-agents/python-code/workspace_server.py |
| 3 | 帮助AI代理在飞行中 | 01-intro-to-mcp-agents/python-code/workspace_server.py |
| 3 | 帮助AI代理发现与工作空间相关的指导 | 01-intro-to-mcp-agents/python-code/workspace_agent.py |
| 4 | 响应不仅仅是数据 | 02-failing-forward/python-code/expense_server.py |
| 4 | 设计错误以帮助AI代理 | 02-failing-forward/python-code/expense_server.py |
| 4 | 复杂工作流中的错误 | 02-failing-forward/python-code/expense_server.py |
| 4 | 从错误恢复中最小化AI代理的认知负担 | 02-failing-forward/python-code/expense_server.py |
| 4 | 帮助AI代理找到修复错误的替代路径 | 02-failing-forward/python-code/expense_server.py |
| 5 | 管理AI代理认知负荷 | 03-intelligence-budget/python-code/agent_heavy_server.py |
03-intelligence-budget/python-code/tool_heavy_server.py | ||
| 5 | 可预测性、低成本、速度:脚本编排 | 03-intelligence-budget/python-code/scripted_orchestration_server.py |
| 5 | 自我提示:在工具中添加推理 | 03-intelligence-budget/python-code/self_prompting_server.py |
| 5 | 常见错误的AI代理工具设计 | 03-intelligence-budget/python-code/validate_at_source_server.py |
______________________________________________________________________
环境设置
创建一个 .env 每个模块中的文件 python-code/ 目录:
OPENAI_API_KEY=your-openai-key-here
ANTHROPIC_API_KEY=your-anthropic-key-here
GOOGLE_API_KEY=your-google-key-here您只需要为计划使用的LLM提供程序提供API密钥。
______________________________________________________________________
项目结构
code-repo/
├── README.md
├── .gitignore
│
├── 01-intro-to-mcp-agents/
│ └── python-code/
│ ├── server.py # Module 2: Building Your First MCP Server
│ ├── agent.py # Module 2: Building Your First MCP AI Agent
│ ├── llm.py # LLM integration utilities
│ ├── workspace_server.py # Module 3: All workspace/context items
│ ├── workspace_agent.py # Module 3: Workspace-related Guidance
│ ├── server_with_resources.py # Server with MCP resources
│ ├── agent_with_learning.py # Agent with active learning
│ ├── test_server.py # Server testing
│ ├── test_resources.py # Resource testing
│ ├── requirements.txt # Python dependencies
│ ├── README.md # Setup instructions
│ └── workspace/ # Sample workspace with .context.md files
│
├── 02-failing-forward/
│ └── python-code/
│ ├── expense_server.py # Module 4: All error handling items
│ ├── expense_agent.py # Agent for expense workflows
│ ├── test_failing_forward.py # Pattern tests
│ ├── requirements.txt # Python dependencies
│ └── README.md # Setup instructions
│
└── 03-intelligence-budget/
└── python-code/
├── agent_heavy_server.py # Module 5: Managing Cognitive Load
├── tool_heavy_server.py # Module 5: Managing Cognitive Load
├── hybrid_server.py # Balanced approach
├── scripted_orchestration_server.py # Module 5: Scripted Orchestration
├── self_prompting_server.py # Module 5: Self-Prompting
├── validate_at_source_server.py # Module 5: Tool Design for Errors
├── database.py # Shared mock database
├── test_all.py # Comparison tests
├── requirements.txt # Python dependencies
└── README.md # Setup instructions______________________________________________________________________
许可证
麻省理工学院
