工作室模型上下文协议(MCP)
文件官员:https://modelcontextprotocol.io/docs/getting-started/intro
SDK类型脚本:https://github.com/modelcontextprotocol/typescript-sdk
对于所有练习,必须对文件进行更改 src/mcp.ts 并且在文件中 src/main.js 将功能加载到MCP服务器。
练习1-代码审查
创建一个公开提示模板以进行代码审查的服务器。
提示符参数模式必须包含单个字段 code 解类型字符串。
prompt模板如下:
Fait une revue de code en te concentrant sur les bonnes pratiques et les bugs potentiels.
Format ta réponse avec :
✅ Points positifs
⚠️ Points à améliorer
💡 Suggestions :\n\n
${code}使用中定义的MCP客户端测试提示符 .vscode/mcp.json 然后在代理模式下在副驾驶中打开新聊天。
练习2-计算器
编写一个计算器作为MCP工具,公开基本操作:
- 加法
- 减法
- 乘法
- 分割
练习3-计算器(续)
用每个操作的提示模板补充计算器。
练习4-天气代理
编写一个MCP工具,使用OpenWeather API检索城市的当前天气(https://api.openweathermap.org/data/2.5/weather?lat={{lat}}&lon={{lon}}&units=metric&appid={{apikey}}).
API密钥通过环境变量提供给练习 OPENWEATHER_KEY.
该工具将设置城市的纬度和经度,并返回一条包含当前天气的文本消息,其中包含:
- 温度
- la描述(天气描述)
- 湿度
- 排气口
- 感觉到的温度
它可以在提示中以以下方式使用:
Donne moi la météo actuelle à Vannes.奖励练习-天气预报
完成天气工具,以便返回未来X天的天气预报。
这次使用API https://api.openweathermap.org/data/2.5/forecast?lat={{lat}}&lon={{lon}}&units=metric&appid={{apikey}}
