模块3:使用AI工具包进行高级MCP开发


文档摘要

模块 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.

模块 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.js 和 npm

️ 您将构建的内容

在本实验中,您将创建一个 天气 MCP 服务器,演示:

  • 自定义 MCP 服务器的实现
  • 与 AI 工具包 Agent Builder 的集成
  • 专业的调试流程
  • 现代 MCP SDK 的使用模式

核心组件概述

MCP Python SDK

Model Context Protocol Python SDK 是构建自定义 MCP 服务器的基础。您将使用带有增强调试功能的 1.9.3 版本。

MCP Inspector

一个强大的调试工具,提供:

  • 实时服务器监控
  • 工具执行可视化
  • 网络请求/响应检查
  • 交互式测试环境

逐步实现

第 1 步:在 Agent Builder 中创建 WeatherAgent

  1. 通过 AI 工具包扩展在 VS Code 中启动 Agent Builder
  2. 创建一个新代理,配置如下:
    • 代理名称:WeatherAgent

Agent Creation

第 2 步:初始化 MCP 服务器项目

  1. 在 Agent Builder 中导航到 工具 → 添加工具
  2. 选择“ MCP 服务器”
  3. 选择“创建新的 MCP 服务器”
  4. 选择 python-weather 模板
  5. 为服务器命名: weather_mcp

Python Template Selection

第 3 步:打开并检查项目

  1. 在 VS Code 中打开生成的项目
  2. 查看项目结构:
    weather_mcp/ ├── src/ │ ├── __init__.py │ └── server.py ├── inspector/ │ ├── package.json │ └── package-lock.json ├── .vscode/ │ ├── launch.json │ └── tasks.json ├── pyproject.toml └── README.md

第 4 步:升级到最新 MCP SDK

** 为什么要升级?** 我们希望使用最新的 MCP SDK(v1.9.3)和 Inspector 服务(0.14.0),以获得更强的功能和更好的调试体验。

4a. 更新 Python 依赖

编辑 pyproject.toml: update ./code/weather_mcp/pyproject.toml

4b. Update Inspector Configuration

Edit inspector/package.json: update ./code/weather_mcp/inspector/package.json

4c. Update Inspector Dependencies

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.

Step 5: Configure VS Code Debugging

Note: Please copy the file in the specified path to replace the corresponding local file

5a. Update Launch Configuration

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" } ] }

运行和测试您的 MCP 服务器

第 6 步:安装依赖

完成配置更改后,运行以下命令:

安装 Python 依赖:

uv sync

安装 Inspector 依赖:

cd inspector npm install

第 7 步:使用 Agent Builder 调试

  1. 按 F5 键或使用**“在 Agent Builder 中调试”**配置
  2. 从调试面板选择复合配置
  3. 等待服务器启动并打开 Agent Builder
  4. 使用自然语言查询测试您的天气 MCP 服务器

输入类似以下提示

SYSTEM_PROMPT

You are my weather assistant

USER_PROMPT

How's the weather like in Seattle

Agent Builder Debug Result

第 8 步:使用 MCP Inspector 调试

  1. 使用“在 Inspector 中调试”配置(Edge 或 Chrome)
  2. http://localhost:6274 打开 Inspector 界面
  3. 探索交互式测试环境:
    • 查看可用工具
    • 测试工具执行
    • 监控网络请求
    • 调试服务器响应

MCP Inspector Interface

关键学习成果

完成本实验后,您已:

  • 使用 AI 工具包模板创建了自定义 MCP 服务器
  • 升级到最新 MCP SDK(v1.9.3),实现增强功能
  • 配置了专业的调试流程,适用于 Agent Builder 和 Inspector
  • 设置了 MCP Inspector,实现交互式服务器测试
  • 掌握了 VS Code 的 MCP 开发调试配置

探索的高级功能

功能 描述 使用场景
MCP Python SDK v1.9.3 最新协议实现 现代服务器开发
MCP Inspector 0.14.0 交互式调试工具 实时服务器测试
VS Code 调试 集成开发环境 专业调试流程
Agent Builder 集成 直接连接 AI 工具包 端到端代理测试

额外资源

** 恭喜!** 您已成功完成实验 3,现在可以使用专业开发流程创建、调试和部署自定义 MCP 服务器。

继续下一模块

准备将您的 MCP 技能应用到实际开发流程中吗?继续前往 模块 4:实用 MCP 开发 - 自定义 GitHub 克隆服务器,在这里您将:

  • 构建一个生产级 MCP 服务器,实现 GitHub 仓库操作自动化
  • 通过 MCP 实现 GitHub 仓库克隆功能
  • 将自定义 MCP 服务器与 VS Code 和 GitHub Copilot Agent Mode 集成
  • 在生产环境中测试和部署自定义 MCP 服务器
  • 学习面向开发者的实用工作流自动化技巧

免责声明
本文件使用AI翻译服务Co-op Translator进行翻译。虽然我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议使用专业人工翻译。对于因使用本翻译而产生的任何误解或误释,我们不承担任何责任。


作者与出处
原作者: microsoft
来源:microsoft
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: microsoft 转发
评论区 (0)
U