mcp-for-beginners 模块 3 实战:用 Microsoft Foundry Toolkit 构建并调试 Weather MCP 服务器 教程文档人工智能【免费下载链接】mcp-for-beginnersThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.项目地址https://gitcode.com/GitHub_Trending/mc/mcp-for-beginners点击查看免费下载本文基于 mcp-for-beginners 课程仓库的「模块 3使用 Microsoft Foundry Toolkit 进行高级 MCP 开发」实验文档展开完整覆盖从 Agent Builder 创建WeatherAgent、用python-weather模板生成 Weather MCP Server 项目到升级 MCP SDK 与 Inspector 依赖、配置 VS Code 双调试通道Agent Builder / MCP Inspector并实际运行测试的全过程。读完后你可以掌握一套可复制的 MCP 服务器开发调试工作流理解 FastMCP 服务入口、launch.json/tasks.json中各端口与任务的协作关系并在源码层面看懂每个配置项的作用。一、实验概述学习对象与环境要求该实验约 20 分钟的目标是让学习者具备以下能力使用 Microsoft Foundry Toolkit 创建自定义 MCP 服务器配置并使用 MCP Python SDK文档中锁定版本为 v1.9.3搭建 MCP Inspector 用于调试文档中锁定版本 0.14.0在 Agent Builder 与 Inspector 两套环境中调试 MCP 服务器理解高级 MCP 服务器开发工作流。前置要求包括已完成 Lab 2MCP 基础见 10-StreamliningAIWorkflowsBuildingAnMCPServerWithAIToolkit/lab2/README.mdVS Code 已安装 Microsoft Foundry Toolkit 扩展Python 3.10 环境用于 Inspector 搭建的 Node.js 与 npm。一个必须注意的版本提示原文档开头明确说明本实验中的 Inspector URL 使用旧版/sse端点并针对锁定的 MCP SDK1.9.3与 Inspector0.14.0依赖它们并不是当前2026-07-28Streamable HTTP 规范的示例。也就是说本实验的价值在于完整演示调试工作流的搭建方法而端点形态SSE属于历史方案。二、核心组件MCP Python SDK 与 MCP Inspector实验依赖两个核心组件MCP Python SDK构建自定义 MCP 服务器的基础库。从仓库源码看服务器通过mcp.server.fastmcp.FastMCP创建实例这是 SDK 面向开发者的高层 APIMCP Inspector交互式调试工具提供实时服务器监控、工具执行可视化、网络请求/响应检查与交互式测试环境。仓库中 代码目录 实际保留的依赖版本值得对照查看pyproject.toml 声明mcp1.26.0[project.optional-dependencies]中开发依赖为debugpy1.8.8锁文件 uv.lock 中实际解析到的mcp版本为1.28.1inspector/package.json 将modelcontextprotocol/inspector固定在2.0.0并通过overrides将shell-quote锁定为1.8.4。可以推断仓库代码目录随课程维护已更新到更新的依赖版本而实验文档正文中「升级到 v1.9.3 / Inspector 0.14.0」的表述对应的是撰写实验时所锁定的历史版本。实操时以仓库中当前pyproject.toml与package.json的声明为准执行安装即可。三、实施步骤步骤 1在 Agent Builder 中创建 WeatherAgent通过 VS Code 中的 Microsoft Foundry Toolkit 扩展启动 Agent Builder创建名为WeatherAgent的新 Agent。步骤 2生成 MCP Server 项目在 Agent Builder 中进入Tools → Add Tool选择MCP Server选择Create A new MCP Server选择python-weather模板服务器命名为weather_mcp。步骤 3查看生成的项目结构在 VS Code 中打开生成的项目其目录结构为weather_mcp/ ├── src/ │ ├── __init__.py │ └── server.py ├── inspector/ │ ├── package.json │ └── package-lock.json ├── .vscode/ │ ├── launch.json │ └── tasks.json ├── pyproject.toml └── README.md其中src/server.py定义工具逻辑src/__init__.py是传输层入口inspector/是 Inspector 的占位启动包.vscode/提供调试配置。仓库中的模板 READMEcode/weather_mcp/README.md还说明模板额外包含.aitk/目录用于 Microsoft Foundry Toolkit 配置。步骤 4升级 MCP SDK 与 Inspector 依赖按实验文档需更新三处文件此处按仓库根目录给出对应路径更新 Python 依赖10-StreamliningAIWorkflowsBuildingAnMCPServerWithAIToolkit/lab3/code/weather_mcp/pyproject.toml更新 Inspector 配置10-StreamliningAIWorkflowsBuildingAnMCPServerWithAIToolkit/lab3/code/weather_mcp/inspector/package.json更新 Inspector 锁文件10-StreamliningAIWorkflowsBuildingAnMCPServerWithAIToolkit/lab3/code/weather_mcp/inspector/package-lock.json。文档提示该锁文件包含约 3000 行的完整依赖定义仓库中实际为 4000 余行应整体使用仓库提供的文件以保证依赖解析一致。步骤 5配置 VS Code 调试实验文档要求将下面两份配置整体复制到本地项目的.vscode/目录替换模板生成的同名文件。.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?timeout60000serverUrlhttp://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?timeout60000serverUrlhttp://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 服务器监听3001SSE 端点为http://localhost:3001/sseInspector 客户端页面在6274Inspector 代理端口为6277debugpy 附加端口为5678dependsOn链条Start MCP Inspector与Open Agent Builder都依赖Start MCP Server保证服务器先于调试入口启动compound 编排Debug in Agent Builder只附加调试并前置打开 Agent Builder通过ai-mlstudio.agentBuilder命令并自动带上local-server-weather_mcp这个 MCPDebug in Inspector (Edge/Chrome)则先启动 Inspector再打开带serverUrlhttp://localhost:3001/sse#tools的浏览器页面并用cascadeTerminateToConfigurations级联终止 Python 附加会话background 任务判定Start MCP Server以Application startup complete|running作为完成标志Start MCP Inspector以Proxy server listening on port作为完成标志这是 debugpy attach 能准确等待服务器就绪的前提。步骤 6安装依赖完成上述配置变更后执行# 安装 Python 依赖 uv sync # 安装 Inspector 依赖 cd inspector npm installinspector/package.json中唯一的生产性内容其实是devDependencies里的modelcontextprotocol/inspectordev:inspector脚本只是调用mcp-inspector命令因此npm install后npm run dev:inspector即可启动 Inspector 服务。步骤 7用 Agent Builder 调试按F5或选择Debug in Agent Builder复合配置等待服务器启动、Agent Builder 面板打开用自然语言测试 Weather MCP 服务器。实验文档给出的测试提示词为SYSTEM_PROMPT You are my weather assistantUSER_PROMPT Hows the weather like in Seattle步骤 8用 MCP Inspector 调试选择Debug in Inspector (Edge)或(Chrome)配置启动调试Inspector 界面会在http://localhost:6274打开在交互式环境中可以查看可用工具、执行工具、监控网络请求、调试服务器响应。四、源码纵深Weather MCP 服务器是如何工作的结合仓库中保留的代码可以完整还原调试链路背后的实现。工具定义—— src/server.pyimport json import random from mcp.server.fastmcp import FastMCP # Initialize FastMCP server server FastMCP(weather_mcp) server.tool() async def get_weather(location: str) - str: Get weather for a location. Args: location: Location to get weather for, e.g., city name, state, or coordinates if not location: return Location is required. # mock weather data conditions [ Sunny, Rainy, Cloudy, Snowy ] weather { location: location, temperature: f{random.randint(10, 90)}°F, condition: random.choice(conditions), } return json.dumps(weather, ensure_asciiFalse)FastMCP(weather_mcp)注册了服务器名server.tool()装饰器把get_weather暴露为可供 LLM 调用的工具工具内部是随机模拟天气数据返回 JSON 字符串——这正是 Agent Builder 提示词“Hows the weather like in Seattle”能拿到结果的来源。入口与传输选择—— src/init.pyimport os import sys from server import server if __name__ __main__: Main entry point transport_type sys.argv[1] if len(sys.argv) 1 else None server.settings.log_level os.environ.get(LOG_LEVEL, DEBUG) if transport_type sse: port int(os.environ.get(PORT, 3001)) server.settings.port port server.settings.host 127.0.0.1 server.run(transportsse) elif transport_type stdio: server.run(transportstdio) else: print(Invalid transport type. Use sse or stdio.) sys.exit(1)这解释了tasks.json中各配置的取值命令python -m debugpy --listen 127.0.0.1:5678 src/__init__.py sse的第一个位置参数sse决定走 SSE 分支PORT环境变量默认 3001被读取后写入server.settings.port与launch.json里 Inspector 页面的serverUrlhttp://localhost:3001/sse精确对应LOG_LEVEL默认DEBUG。也就是说tasks.json的env、launch.json的 URL、__init__.py的分支逻辑三者在 3001/5678 这两个端口上形成闭环任何一处改动都需要同步另外两处。Inspector 占位包—— inspector/package.json 自称inspector-placeholder其dev:inspector脚本直接执行mcp-inspectoroverrides中固定shell-quote到1.8.4是为了在安装期间锁定该传递依赖的版本。五、实验成果与能力清单完成本实验后应达成使用 Microsoft Foundry Toolkit 模板创建了自定义 MCP 服务器将 MCP SDK 升级到实验指定版本以获得增强功能仓库代码目录已演进至mcp1.26.0以实际文件为准为 Agent Builder 与 Inspector 两条链路分别配置了专业调试工作流搭建起 MCP Inspector 交互式服务器测试环境掌握了面向 MCP 开发的 VS Code 调试配置launch.json的 compound 编排、tasks.json的 background 任务与依赖链。特性说明使用场景MCP Python SDK文档锁定 v1.9.3仓库代码目录现为mcp1.26.0uv.lock 解析 1.28.1现代服务器开发MCP Inspector文档锁定 0.14.0仓库代码目录现固定2.0.0交互式实时调试VS Code Debugging基于 debugpy attach 的集成调试专业调试工作流Agent Builder 集成直接经 Microsoft Foundry Toolkit 连接端到端 Agent 测试六、小结模块 3 的核心价值不是某个单一 API而是一套完整的「生成模板 → 固定依赖 → 双通道调试」的 MCP 开发范式python-weather模板给出 FastMCP 最小骨架pyproject.toml与inspector/package.json固定协议栈版本launch.json/tasks.json把端口3001 / 6274 / 6277 / 5678与启动顺序编排成一键 F5 流程。需要再次强调的是文档头部的版本注记本实验基于旧版/sse端点与锁定版本撰写若迁移到2026-07-28Streamable HTTP 规范需相应调整__init__.py的传输分支与launch.json中的serverUrl仓库 code 目录 中更新的依赖声明可以作为迁移起点参考。下一模块Lab 4将在本工作流基础上构建可自动执行 GitHub 仓库操作的实用 MCP 服务器对应文档见 10-StreamliningAIWorkflowsBuildingAnMCPServerWithAIToolkit/lab4/README.md。赞分享教程文档人工智能【免费下载链接】mcp-for-beginnersThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.项目地址https://gitcode.com/GitHub_Trending/mc/mcp-for-beginners点击查看免费下载相关推荐mcp-for-beginners 实战使用 Microsoft Foundry Toolkit 构建与调试 Weather MCP Servermcp for beginners 实战使用 Microsoft Foundry Toolkit 构建与调试 Weather MCP Server 本文是 m教程文档人工智能mcp-for-beginners 实战指南使用 Microsoft Foundry Toolkit 构建、调试与部署 MCP 服务器mcp for beginners 实战指南使用 Microsoft Foundry Toolkit 构建、调试与部署 MCP 服务器 本文以 mcp for教程文档人工智能mcp-for-beginners 模块二实战用 MCP 与 Microsoft Foundry Toolkit 构建 Playwright 浏览器自动化 Agentmcp for beginners 模块二实战用 MCP 与 Microsoft Foundry Toolkit 构建 Playwright 浏览器自动化 A教程文档人工智能上一篇3分钟免费解锁MobaXterm专业版终极许可证生成指南下一篇如何三步解锁Cursor Pro完整功能免费无限使用的完整实战指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考