Skip to main content
Glama

MCP Python Tutorial

MCP Python 教程

使用 Python 编写的 MCP 教程应用程序,带有简单的本地数据库和模拟数据

安装和运行

通过 Smithery 安装

要通过Smithery自动为 Claude Desktop 安装 Python MCP 教程服务器:

npx -y @smithery/cli install @jhj0517/mcp-python-tutorial --client claude

手动安装

  1. 克隆此存储库
  2. 安装依赖项:
pip install -r requirements.txt
  1. 以开发模式运行 MCP 服务器:
mcp dev localdb_app.py
  1. 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作为客户端进行连接。

  1. Claude Desktop 使用uv安装 MCP 服务器依赖项。首先,安装uv
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
  1. 使用uv安装 MCP 服务器依赖项:
# Create virtual environment and activate it uv venv .venv\Scripts\activate uv pip install -r requirements.txt
  1. 下载 Claude Desktop 请访问:
  1. 找到或创建claude_desktop_config.json文件。具体位置因操作系统而异:
  • 视窗:
C:\Users\%USER%\AppData\Roaming\Claude\claude_desktop_config.json
  • MacOS/Linux:
~/Library/Application\ Support/Claude/claude_desktop_config.json
  1. mcpServers属性添加到您的claude_desktop_config.json中:
{ "mcpServers": { "local_db": { "command": "uv", "args": [ "--directory", "/ABSOLUTE/PATH/TO/PARENT/FOLDER/weather", "run", "localdb_app.py" ] } } }

注意:您可以部署多个 MCP 服务器,每个服务器都有各自的关注点和专长。这种关注点分离比将所有功能都集成到单个 MCP 服务器中效果更好。

  1. 重新启动 Claude Desktop。
-
security - not tested
F
license - not found
-
quality - not tested

local-only server

The server can only run on the client's local machine because it depends on local resources.

演示服务器展示了用 Python 实现的 MCP,包括资源处理、工具操作和可重复使用的提示,用于带有本地数据库的简单用户/帖子系统。

  1. 安装和运行
    1. 通过 Smithery 安装
    2. 手动安装
  2. MCP 功能
    1. @mcp.resource
    2. @mcp.tool
    3. @mcp.prompt
  3. 连接到客户端

    Related MCP Servers

    • A
      security
      A
      license
      A
      quality
      An MCP server to run commands.
      Last updated -
      2
      265
      150
      TypeScript
      MIT License
      • Apple
    • -
      security
      A
      license
      -
      quality
      A 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 -
      1
      Python
      MIT License
    • -
      security
      F
      license
      -
      quality
      This 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
    • A
      security
      A
      license
      A
      quality
      A server that provides a persistent Python REPL environment through the MCP protocol, allowing execution of Python code, variable management, and package installation.
      Last updated -
      3
      3
      Python
      MIT License

    View all related MCP servers

    MCP directory API

    We provide all the information about MCP servers via our MCP API.

    curl -X GET 'https://glama.ai/api/mcp/v1/servers/jhj0517/mcp-python-tutorial'

    If you have feedback or need assistance with the MCP directory API, please join our Discord server