MCP Python教程
](https://smithery.ai/server/@jhj0517/mcp-python-tutorial) Python中MCP的教程应用程序,带有简单的本地DB和模拟数据
安装与运行
通过Smithery安装
通过以下方式自动安装Claude Desktop的Python MCP教程服务器 史密瑟里:
npx -y @smithery/cli install @jhj0517/mcp-python-tutorial --client claude手动安装
- 克隆此存储库
- 安装依赖项:
pip install -r requirements.txt- 以开发模式运行MCP服务器:
mcp dev localdb_app.py- MCP服务器的默认端口为
5173.访问http://localhost:5173.
MCP功能
本教程应用程序演示了MCP的核心概念。
您可以在中检查每个角色的注释 教程.app/mcpserver.py:
@mcp.resource
基本上,这个注释是关于代理“获取”资源的,就像 GET 在RESTAPI中。
users://all-获取所有用户users://{user_id}/profile-获取用户的个人资料posts://all-获取所有帖子posts://{post_id}-通过ID获取帖子
@mcp.tool
这是关于代理“生成”新资源的,就像 POST 在RESTAPI中。
create_user-创建新用户create_post-创建新帖子search_posts-按标题或内容搜索帖子
@mcp.prompt
这只是一个可重用的模板,可以方便地与LLM交互。
user_profile_analysis-生成用户个人资料分析post_feedback-帖子反馈互动提示
\[!注意\] 如需更多注释,请阅读:https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov文件#核心概念
连接到客户端
设置完MCP服务器后,您需要一个LLM客户端,它将使用您的MCP服务器来构建您的代理。 以下指南将帮助您连接 克劳德桌面 作为你的客户。
- Claude Desktop使用
uv安装MCP服务器依赖项。首先,安装uv:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"- 使用安装MCP服务器依赖项
uv:
# Create virtual environment and activate it
uv venv
.venv\Scripts\activate
uv pip install -r requirements.txt- 从以下网址下载Claude Desktop:
- https://claude.ai/download
- 查找或创建
claude_desktop_config.json文件。位置因操作系统而异:
- 窗户:
C:\Users\%USER%\AppData\Roaming\Claude\claude_desktop_config.json- MacOS/Linux:
~/Library/Application\ Support/Claude/claude_desktop_config.json- 添加
mcpServers归因于你claude_desktop_config.json:
{
"mcpServers": {
"local_db": {
"command": "uv",
"args": [
"--directory",
"/ABSOLUTE/PATH/TO/PARENT/FOLDER/weather",
"run",
"localdb_app.py"
]
}
}
}注意:您可以部署多个MCP服务器,每个服务器都有自己的专门关注点和专业知识。
这种关注点分离比在单个MCP服务器中实现所有内容要好。
- 重新启动克劳德桌面。
