在 cursor 输入以下提示词,记得使用 agent 模式让 cursor 帮你写代码
我的需求:
- 使用python mcp sdk 1.9.3开发一个mcp server
- 支持stdio和Streamable HTTP两种传输模式
- uv管理python虚拟环境
- 调用高德天气接口作为tool
- 暴露出必要的配置,例如需要配置高德的api key之类的,具体参考文档来决定
高德天气接口文档地址:@<https://lbs.amap.com/api/webservice/guide/api/weatherinfo/>
参考文档内容:@<https://modelcontextprotocol.io/llms-full.txt>
@<https://github.com/modelcontextprotocol/python-sdk/blob/main/README.md>
经过实测,给网页 cursor 写的 python 代码有问题,可以直接复制 https://github.com/modelcontextprotocol/python-sdk/blob/main/README.md 文件的内容到 prompt 提示词里,这样会写的更准确。
生成的 uv 项目依赖,版本需要微调一下,大模型不太好把握依赖的版本号
[project]
name = "amap-weather-mcp"
version = "0.1.0"
description = "MCP server for weather information using Amap API"
requires-python = ">=3.8"
dependencies = [
"mcp[cli]==1.9.3",
"python-dotenv==1.0.0",
"httpx==0.28.1",
"pydantic==2.11.5",
"pydantic-settings==2.5.2",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.ruff]
line-length = 88
target-version = "py38"
创建虚拟环境安装依赖,运行 mcp,根据报错让 ai 帮你解决掉
uv venv
source .venv/bin/activate # Linux/macOS
uv pip install -e .
# 运行stdio模式
python -m weather_mcp.server stdio
# 运行HTTP模式
python -m weather_mcp.server http
调试方式:使用 mcp dev 调试
mcp dev src/weather_mcp/server.py
Starting MCP inspector...
⚙️ Proxy server listening on port 6277
🔍 MCP Inspector is up and running at <http://127.0.0.1:6274> 🚀
会启动 mcp inspector 工具,打开这个页面,可以链接 mcp 工具进行调试
这里在调试的时候要注意:
- stdio 模式,是使用命令启动 mcp server,需要将 mcp 项目的绝对地址加入到 PATH 里,然后使用虚拟环境的 python 执行启动命令
- **Command:**{to_mcp_server_path}/.venv/bin/python
- Arguments: -m weather_mcp.server stdio
- Streamable http 模式
- 在命令行 http 模式启动 mcp server
python -m weather_mcp.server http
- 在 inspector 页面里配置 URL http://localhost:8000/mcp
- 在命令行 http 模式启动 mcp server
调试没问题就可以啦
参考项目地址
GitHub - xiaowangzhixiao/amap-weather-mcp: amap weather mcp by python
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于