Python MCP Server Template

by nictuku
MIT License
4
  • Apple
  • Linux

py-mcp-服务器模板

此代码库是一个模板,可帮助您使用 Python 创建自己的 MCP(模型上下文协议)服务器。您可以 fork 此代码库来开始使用。

使用 uv 设置

本项目使用uv进行 Python 打包和管理虚拟环境。如果您尚未安装uv ,请参阅官方 uv 安装指南

  1. 克隆你的分支存储库:
    git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.git cd YOUR_REPOSITORY_NAME
  2. 创建并激活虚拟环境: uv通常会在项目根目录中创建一个.venv目录。
    uv venv source .venv/bin/activate # On macOS/Linux # .venv\Scripts\activate # On Windows
  3. **安装依赖项:**本项目使用pyproject.toml来管理依赖项。
    uv pip install .
    如果添加了新的依赖项,请在pyproject.toml文件中定义它们,然后再次运行此命令。如果出于某种原因使用了requirements.txt文件,可以使用uv pip install -r requirements.txt进行安装。

运行服务器

mcp_server.py脚本启动 MCP 服务器。

直接运行服务器:

uv run python mcp_server.py

与 Claude Desktop 或 Cursor 集成

要将此 MCP 服务器与 Claude Desktop 或 Cursor 等应用程序一起使用,您需要在应用程序的设置中进行配置。配置通常涉及指定运行服务器的命令。

以下是一个示例配置片段。您需要将/ABSOLUTE/PATH/TO/PARENT/FOLDER/YOUR_REPOSITORY_NAME替换为您系统上项目目录的实际绝对路径。

{ "mcpServers": { "my-custom-python-server": { "command": "uv", "args": [ "run", "--python", "/ABSOLUTE/PATH/TO/PARENT/FOLDER/YOUR_REPOSITORY_NAME/.venv/bin/python", "/ABSOLUTE/PATH/TO/PARENT/FOLDER/YOUR_REPOSITORY_NAME/mcp_server.py" ], "workingDirectory": "/ABSOLUTE/PATH/TO/PARENT/FOLDER/YOUR_REPOSITORY_NAME" } } }

配置说明:

  • "my-custom-python-server" :这是您为服务器配置指定的名称。
  • "command": "uv" :指定uv作为要执行的命令。
  • "args"uv命令的参数列表:
    • "run" :告诉uv在其管理环境中执行命令。
    • "--python" :指定要使用的 Python 解释器。务必将其指向uv虚拟环境 ( .venv/bin/python ) 中的 Python 解释器。
    • "/ABSOLUTE/PATH/TO/PARENT/FOLDER/YOUR_REPOSITORY_NAME/mcp_server.py" :服务器脚本的绝对路径。
  • "workingDirectory" :指定服务器进程的工作目录,该目录应该是项目的根目录。

重要的:

  • 确保argsworkingDirectory中的路径对于您的系统来说是正确的。
  • 如果应用程序找不到uv ,您可能需要在"command"字段中指定其完整路径。通常,您可以在 macOS 或 Linux 的终端中运行which uv来找到此路径,或者在 Windows 上where uv来找到此路径。
  • 按照mcp_server.py ( mcp.run(transport='stdio') ) 中的配置,服务器默认在stdio上监听,这通常是 Cursor 等应用程序所期望的。

配置完成后,应用程序应该能够与您的 Python MCP 服务器通信。

-
security - not tested
A
license - permissive license
-
quality - not tested

用于在 Python 中创建自定义模型上下文协议 (MCP) 服务器的模板存储库,可以与 Claude Desktop 或 Cursor 等应用程序集成。

  1. 使用 uv 设置
    1. 运行服务器
      1. 与 Claude Desktop 或 Cursor 集成

        Related MCP Servers

        • -
          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
          A Model Context Protocol server built with mcp-framework that allows users to create and manage custom tools for processing data, integrating with the Claude Desktop via CLI.
          Last updated -
          48
          4
          TypeScript
          • Apple
        • -
          security
          A
          license
          -
          quality
          A streamlined foundation for building Model Context Protocol servers in Python, designed to make AI-assisted development of MCP tools easier and more efficient.
          Last updated -
          13
          Python
          MIT License
        • -
          security
          F
          license
          -
          quality
          A starter template for building Model Context Protocol (MCP) servers, enabling developers to create and add custom tools that can be integrated with Claude Desktop.
          Last updated -
          TypeScript
          • Apple

        View all related MCP servers

        ID: 48pzj21eqm