MCP客户端和MCP服务器快速入门

参考文献
- MCP服务器:https://modelcontextprotocol.io/quickstart/server
- MCP客户端:https://modelcontextprotocol.io/quickstart/client
结果
使用Claude Desktop作为MCP主机运行天气MCP服务器
- 注意:Claude Desktop是MCP主机的一个示例,可以根据配置文件自动创建MCP客户端。


使用CLI作为MCP主机和创建的MCP客户端运行天气MCP服务器


学习笔记
备注
- MCP客户端既可以在前端开发,也可以在后端开发。在这个例子中,MCP客户端是使用Python后端构建的。
- 了解MCP主机和MCP客户端之间的区别很重要
- MCP主机与MCP客户端的1对n关系 - MCP客户端与其各自的MCP服务器之间存在1:1的关系
提示和技巧
- 使用此选项列出mcp服务器
ps aux | grep "weather.py" | grep -v grep- 如何杀死服务器的示例
kill 79200 79199- 如何打开claude配置文件
code ~/Library/Application\ Support/Claude/claude_desktop_config.json- 如果您正在使用虚拟环境,请确保这样做
# Create virtual environment and activate it
uv venv
source .venv/bin/activate
# Install dependencies
uv add "mcp[cli]" httpx- 确保安装mcp服务器
uv run mcp install main.py
# change main.py to your entry filename- 如何解决Claude Desktop不断向我显示错误的问题
"MCP : spawn uv ENOENT"和"Could not connect MCP server ".

1. 确定紫外线位置
which uv
# /Users/shafie/.local/bin/uv1. 打开配置文件
code ~/Library/Application\ Support/Claude/claude_desktop_config.json1. 更新 "command": "uv" 向 "command": "/Users/shafie/.local/bin/uv"
例如,从这个
"weather": {
"command": "uv",
"args": [
"run",
"--with",
"mcp[cli]",
"mcp",
"run",
"/Users/shafie/Documents/Github/mcp-server-weather/weather.py"
]
}对于这个
"weather": {
"command": "/Users/shafie/.local/bin/uv",
"args": [
"run",
"--with",
"mcp[cli]",
"mcp",
"run",
"/Users/shafie/Documents/Github/mcp-server-weather/weather.py"
]
}1. 重新启动Claude Desktop,不再出现错误,服务器已添加

你会看到的

- CLI我用来在MCP客户端的仓库中运行MCP客户端和MCP服务器。
uv run client.py ../mcp-server-quickstart/weather.py