模块 3:使用 AI 工具包进行高级 MCP 开发 Duration AI Toolkit Python MCP SDK Inspector 学习目标 完成本实验后,您将能够: ✅ 使用 AI 工具包创建自定义 MCP 服务器 ✅ 配置并使用最新的 MCP Python SDK(v1.9.3) ✅ 设置并利用 MCP Inspector 进行调试 ✅ 在 Agent Builder 和 Inspector 环境中调试 MCP 服务器 ✅ 理解高级 MCP 服务器开发流程 先决条件 完成实验 2(MCP 基础知识) 安装了 AI 工具包扩展的 VS Code Python 3.10 及以上环境 用于 Inspector 设置的 Node.
完成本实验后,您将能够:
在本实验中,您将创建一个 天气 MCP 服务器,演示:
Model Context Protocol Python SDK 是构建自定义 MCP 服务器的基础。您将使用带有增强调试功能的 1.9.3 版本。
一个强大的调试工具,提供:
WeatherAgent%E5%88%9D%E5%AD%A6%E8%80%85%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%8D%97/Agent.webp)
python-weather 模板weather_mcp%E5%88%9D%E5%AD%A6%E8%80%85%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%8D%97/Pythontemplate.webp)
weather_mcp/ ├── src/ │ ├── __init__.py │ └── server.py ├── inspector/ │ ├── package.json │ └── package-lock.json ├── .vscode/ │ ├── launch.json │ └── tasks.json ├── pyproject.toml └── README.md
** 为什么要升级?** 我们希望使用最新的 MCP SDK(v1.9.3)和 Inspector 服务(0.14.0),以获得更强的功能和更好的调试体验。
编辑 pyproject.toml: update ./code/weather_mcp/pyproject.toml
Edit inspector/package.json: update ./code/weather_mcp/inspector/package.json
Edit inspector/package-lock.json: update ./code/weather_mcp/inspector/package-lock.json
** Note:** This file contains extensive dependency definitions. Below is the essential structure - the full content ensures proper dependency resolution.
⚡ Full Package Lock: The complete package-lock.json contains ~3000 lines of dependency definitions. The above shows the key structure - use the provided file for complete dependency resolution.
Note: Please copy the file in the specified path to replace the corresponding local file
Edit .vscode/launch.json:
{ "version": "0.2.0", "configurations": [ { "name": "Attach to Local MCP", "type": "debugpy", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "presentation": { "hidden": true }, "internalConsoleOptions": "neverOpen", "postDebugTask": "Terminate All Tasks" }, { "name": "Launch Inspector (Edge)", "type": "msedge", "request": "launch", "url": "http://localhost:6274?timeout=60000&serverUrl=http://localhost:3001/sse#tools", "cascadeTerminateToConfigurations": [ "Attach to Local MCP" ], "presentation": { "hidden": true }, "internalConsoleOptions": "neverOpen" }, { "name": "Launch Inspector (Chrome)", "type": "chrome", "request": "launch", "url": "http://localhost:6274?timeout=60000&serverUrl=http://localhost:3001/sse#tools", "cascadeTerminateToConfigurations": [ "Attach to Local MCP" ], "presentation": { "hidden": true }, "internalConsoleOptions": "neverOpen" } ], "compounds": [ { "name": "Debug in Agent Builder", "configurations": [ "Attach to Local MCP" ], "preLaunchTask": "Open Agent Builder", }, { "name": "Debug in Inspector (Edge)", "configurations": [ "Launch Inspector (Edge)", "Attach to Local MCP" ], "preLaunchTask": "Start MCP Inspector", "stopAll": true }, { "name": "Debug in Inspector (Chrome)", "configurations": [ "Launch Inspector (Chrome)", "Attach to Local MCP" ], "preLaunchTask": "Start MCP Inspector", "stopAll": true } ] }
编辑 .vscode/tasks.json:
{ "version": "2.0.0", "tasks": [ { "label": "Start MCP Server", "type": "shell", "command": "python -m debugpy --listen 127.0.0.1:5678 src/__init__.py sse", "isBackground": true, "options": { "cwd": "${workspaceFolder}", "env": { "PORT": "3001" } }, "problemMatcher": { "pattern": [ { "regexp": "^.*$", "file": 0, "location": 1, "message": 2 } ], "background": { "activeOnStart": true, "beginsPattern": ".*", "endsPattern": "Application startup complete|running" } } }, { "label": "Start MCP Inspector", "type": "shell", "command": "npm run dev:inspector", "isBackground": true, "options": { "cwd": "${workspaceFolder}/inspector", "env": { "CLIENT_PORT": "6274", "SERVER_PORT": "6277", } }, "problemMatcher": { "pattern": [ { "regexp": "^.*$", "file": 0, "location": 1, "message": 2 } ], "background": { "activeOnStart": true, "beginsPattern": "Starting MCP inspector", "endsPattern": "Proxy server listening on port" } }, "dependsOn": [ "Start MCP Server" ] }, { "label": "Open Agent Builder", "type": "shell", "command": "echo ${input:openAgentBuilder}", "presentation": { "reveal": "never" }, "dependsOn": [ "Start MCP Server" ], }, { "label": "Terminate All Tasks", "command": "echo ${input:terminate}", "type": "shell", "problemMatcher": [] } ], "inputs": [ { "id": "openAgentBuilder", "type": "command", "command": "ai-mlstudio.agentBuilder", "args": { "initialMCPs": [ "local-server-weather_mcp" ], "triggeredFrom": "vsc-tasks" } }, { "id": "terminate", "type": "command", "command": "workbench.action.tasks.terminate", "args": "terminateAll" } ] }
完成配置更改后,运行以下命令:
安装 Python 依赖:
uv sync
安装 Inspector 依赖:
cd inspector npm install
输入类似以下提示
SYSTEM_PROMPT
You are my weather assistant
USER_PROMPT
How's the weather like in Seattle
%E5%88%9D%E5%AD%A6%E8%80%85%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%8D%97/Result.webp)
http://localhost:6274 打开 Inspector 界面%E5%88%9D%E5%AD%A6%E8%80%85%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%8D%97/Inspector.webp)
完成本实验后,您已:
| 功能 | 描述 | 使用场景 |
|---|---|---|
| MCP Python SDK v1.9.3 | 最新协议实现 | 现代服务器开发 |
| MCP Inspector 0.14.0 | 交互式调试工具 | 实时服务器测试 |
| VS Code 调试 | 集成开发环境 | 专业调试流程 |
| Agent Builder 集成 | 直接连接 AI 工具包 | 端到端代理测试 |
** 恭喜!** 您已成功完成实验 3,现在可以使用专业开发流程创建、调试和部署自定义 MCP 服务器。
准备将您的 MCP 技能应用到实际开发流程中吗?继续前往 模块 4:实用 MCP 开发 - 自定义 GitHub 克隆服务器,在这里您将:
免责声明:
本文件使用AI翻译服务Co-op Translator进行翻译。虽然我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议使用专业人工翻译。对于因使用本翻译而产生的任何误解或误释,我们不承担任何责任。