Token导航 LogoToken导航TokenDH.com
Python MCP Starter logo
AI代理stdio官方级别未说明来源级核验

Python MCP Starter

MCP Server

Opinionated starter kit for building Model-Context-Protocol (MCP) server in Python — clean project scaffold, typed code, VS Code debug config, Docker \u0026 GitHub Actions ready out-of-the-box

工具数

0

提示词数

0

GitHub Stars

3

资源数

0
服务器模板快速开发PythonVS Code模型集成VS Code

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

ltwlf

提供方

ltwlf

最后核验

2026/5/18 02:51

运行时

Python

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

uv run your-script-name

详细介绍

Python MCP服务器入门模板

特性

  • 使用以下工具实现MCP服务器 mcp Python SDK(FastMCP).
  • 暴露MCP 工具 (LLM可调用的函数)和 资源 (LLM可访问的数据)。
  • 干净、模块化的架构
  • 轻松注册工具和资源
  • 具有可定制选项的命令行界面
  • 已准备好进行生产部署
  • 使用debugpy与VS Code集成的内置调试功能
  • 配置开发工具(使用 uv 用于环境/包管理)
  • Docker支持容器化部署

入门指南

先决条件

  • Python 3.10或更高版本
  • 紫外线 (建议用于环境和包装管理)
  • MCP检查员 (用于测试/调试MCP服务器的推荐可视化工具)
  • \[可选\]Git
  • \[可选\]Docker用于容器化部署

安装

  1. 克隆或用作模板: 获取代码。
   git clone https://github.com/ltwlf/python-mcp-starter.git your-repo-name # Or use GitHub's "Use this template" button
   cd your-repo-name
  1. 重命名项目: 重命名核心组件:

- 重命名 hello_mcp_server 目录到您想要的Python包名称(例如。, my_awesome_mcp). - 搜索并替换 hello-mcp-server (in pyproject.toml, README.md, Dockerfile, docker-compose.yml)使用您的项目名称(例如。, my-awesome-mcp). - 搜索并替换 hello_mcp_server (在Python导入语句中,如 main.py, tests/test_server.py, pyproject.toml 脚本部分)使用您的新包名。 - 更新 APP_ID 在您的重命名中 server.py 文件。 - 更新 .vscode/launch.json 配置文件,以反映您的新项目和包名称。

  1. 创建虚拟环境:
   # Create the virtual environment
   uv venv
   # Activate the environment (Windows PowerShell)
   .venv\Scripts\Activate.ps1
   # Or for other shells:
   # source .venv/bin/activate  (Linux/macOS)
   # .venv\Scripts\activate.bat (Windows Command Prompt)
  1. 安装依赖关系:
   uv pip install -e ".[dev]"

运行MCP服务器进行开发

您可以直接使用以下命令运行服务器 uv runmcp SDK提供的CLI工具。这允许使用以下工具进行测试 MCP检查员.

在stdio模式下(用于MCP检查器)

方法1:使用 uv run

# Ensure your virtual environment is active first
# Replace 'hello-mcp-server' with the script name defined in your pyproject.toml
uv run your-script-name

方法2:使用 mcp CLI工具

# Ensure your virtual environment is active first
# Replace 'hello_mcp_server' with your renamed package directory
mcp dev ./your_package_name/server.py

在SSE模式下

# Ensure your virtual environment is active first
# Replace 'hello-mcp-server' with your script name
uv run your-script-name --sse

或者使用自定义主机和端口:

# Ensure your virtual environment is active first
# Replace 'hello-mcp-server' with your script name
uv run your-script-name --sse --host 127.0.0.1 --port 9000

Docker部署

更新 Dockerfiledocker-compose.yml 在构建之前反映重命名的项目/包。

# Build and run with Docker Compose
docker-compose up -d

# Or build and run directly with Docker
# Replace 'hello-mcp-server' with your image name
docker build -t your-image-name .
docker run -p 8000:8000 your-image-name

发展

调试

此项目包括使用debugpy的内置调试功能。调试MCP服务器有两种主要方法:

使用调试模块

该项目包括一个专用的调试模块,使调试变得容易:

# Using uv (recommended)
uv run mcp-debug

首先连接到MCP检查器,连接后使用VS Code调试配置 附属于MCP工人 附加调试器进行调试

使用VS代码

VS代码调试配置了多种启动配置:

  1. 附属于MCP工人 -连接到运行中的MCP服务器(将其连接到运行的MCP调试)
  2. 启动MCP服务器(SSE) -使用服务器发送的事件直接启动MCP服务器

为了获得最佳体验:

  1. 首先,跑 uv run mcp-debug 在终端中
  2. 然后,要设置断点,请使用具有“附加到MCP worker”配置的VS Code调试器
注: 确保您的虚拟环境已设置 uv venvuv pip install -e .[dev] 在调试之前。
  1. 检查Python解释器:

- 确保VS Code在虚拟环境中使用了正确的Python解释器 - 如果需要,使用Python扩展的“select interpreter”命令选择解释器

要使用这些配置,请执行以下操作:

  1. 在VS Code中打开运行和调试面板(Ctrl+Shift+D)
  2. 从下拉列表中选择所需的配置
  3. 单击播放按钮或按F5

安全考虑

  • 调试模式不应在生产环境中使用
  • 调试器暴露了一个网络端口,如果不安全,该端口可能会被利用
  • 始终在受信任的网络上使用调试或采取适当的安全措施

添加新工具

使用添加新工具 @mcp.tool() 室内装饰师 your_package_name/server.py:

# your_package_name/server.py
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("your-app-id") # Ensure APP_ID matches your project

# ... existing tools ...

# Add your new tool
@mcp.tool()
async def my_new_tool(param1: str, param2: int, ctx: Context) -> dict:
    """
    Description of what your tool does.
    Use the ctx object to report progress, log info, or read resources.
    """
    ctx.info(f"Running my_new_tool with {param1=}, {param2=}")
    # Tool implementation
    result = f"Processed {param1} {param2} times"
    await ctx.report_progress(1, 1) # Example progress
    return {"result": result}

添加新资源

使用添加新资源 @mcp.resource() 室内装饰师 your_package_name/server.py:

# your_package_name/server.py
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("your-app-id") # Ensure APP_ID matches your project

# ... existing resources ...

# Add resources
@mcp.resource("your-resource-uri")
def your_resource_func():
    """Returns application settings."""
    return {"theme": "dark", "language": "en"}

测试

确保中的导入语句 tests/test_server.py 指向您重命名的包。

pytest

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

致谢

目录标签

目录标签

服务器模板快速开发PythonVS Code模型集成developer-toolsMCP协议本地部署Python开发LLM集成

支持客户端

VS Code

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

运行时(runtime,运行环境)

Python

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP