MCP Python 教程
使用 Python 编写的 MCP 教程应用程序,带有简单的本地数据库和模拟数据
安装和运行
通过 Smithery 安装
要通过Smithery自动为 Claude Desktop 安装 Python MCP 教程服务器:
手动安装
- 克隆此存储库
- 安装依赖项:
- 以开发模式运行 MCP 服务器:
- MCP 服务器默认端口为
5173
。访问http://localhost:5173
。
MCP 功能
本教程演示了 MCP 的核心概念。你可以在tutorial_app/mcp_server.py中查看每个角色的注解:
@mcp.resource
基本上,这个注释是关于代理“获取”资源,就像 RESTAPI 中的GET
一样。
users://all
- 获取所有用户users://{user_id}/profile
- 获取用户的个人资料posts://all
- 获取所有帖子posts://{post_id}
- 通过 ID 获取帖子
@mcp.tool
这是关于代理“生成”新资源,就像 RESTAPI 中的POST
一样。
create_user
创建新用户create_post
创建新帖子search_posts
- 按标题或内容搜索帖子
@mcp.prompt
这只是一个可重复使用的模板,以便于与 LLM 方便地交互。
user_profile_analysis
- 生成用户个人资料的分析post_feedback
- 帖子反馈的交互式提示
[!NOTE] 有关更多注释,请阅读: https://github.com/modelcontextprotocol/python-sdk? tab=readme-ov-file#core-concepts
连接到客户端
设置好 MCP 服务器后,您需要一个 LLM 客户端,该客户端将使用 MCP 服务器来构建代理。以下指南将帮助您使用Claude Desktop作为客户端进行连接。
- Claude Desktop 使用
uv
安装 MCP 服务器依赖项。首先,安装uv
:
- 使用
uv
安装 MCP 服务器依赖项:
- 下载 Claude Desktop 请访问:
- 找到或创建
claude_desktop_config.json
文件。具体位置因操作系统而异:
- 视窗:
- MacOS/Linux:
- 将
mcpServers
属性添加到您的claude_desktop_config.json
中:
注意:您可以部署多个 MCP 服务器,每个服务器都有各自的关注点和专长。这种关注点分离比将所有功能都集成到单个 MCP 服务器中效果更好。
- 重新启动 Claude Desktop。
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
演示服务器展示了用 Python 实现的 MCP,包括资源处理、工具操作和可重复使用的提示,用于带有本地数据库的简单用户/帖子系统。
Related MCP Servers
- AsecurityAlicenseAqualityAn MCP server to run commands.Last updated -2265150TypeScriptMIT License
- -securityAlicense-qualityA Python-based server that implements the Model Context Protocol to interface with Claude Desktop as an MCP client, supporting interaction through efficient memory management.Last updated -1PythonMIT License
- -securityFlicense-qualityThis is an MCP server that facilitates building tools for interacting with various APIs and workflows, supporting Python-based development with potential for customizable prompts and user configurations.Last updated -Python
- AsecurityAlicenseAqualityA server that provides a persistent Python REPL environment through the MCP protocol, allowing execution of Python code, variable management, and package installation.Last updated -33PythonMIT License