mcpField
M
MCP Server Template (Python)

MCP Server Template (Python)

本地服务
github.com/Nisarg38开发工具284 周前更新

关于 MCP Server Template (Python)

MCP Server Template (Python) 是供开发者构建 MCP 服务的 Python 起始模板。它提供暴露工具和提示词的示例,支持 HTTP 与 stdio 传输,并附有部署指引。

主要功能

  1. 01注册工具和提示词
  2. 02通过 HTTP 或 stdio 运行
  3. 03配置端口和调试日志
  4. 04使用 Docker 或云容器部署
  5. 05附带测试和格式化指引

适用场景

  1. 01启动 Python MCP 服务项目
  2. 02向模型提供自定义工具和提示词
  3. 03通过 HTTP 或 stdio 在本地运行服务
  4. 04将服务打包为容器部署

MCP 相关

该模板帮助开发者构建向 MCP 客户端提供工具和提示词的 MCP 服务,并支持 HTTP 与 stdio 传输。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

MCP Server Template(Python)

Python 3.10+ License: MIT

用于在 Python 中构建 Model Context Protocol(MCP)服务的即用型模板。借助此模板,你可以快速创建能够注册并公开供 AI 模型使用的工具和提示词的服务。

📚 目录

🚀 快速开始

前置条件
  • Python 3.10 或更高版本
轻松三步完成设置
1️⃣ 安装软件包
# Clone the repository
git clone https://github.com/nisarg38/mcp-server-template-python.git my-mcp-server
cd my-mcp-server

# Install in development mode
pip install -e ".[dev]"
2️⃣ 运行服务
# Run with Python
python -m src.main

# Or use the convenient CLI
mcp-server-template
3️⃣ 服务现已上线!

访问你的 MCP 服务:

你将看到日志输出,确认服务已成功运行。

🎮 命令行选项

使用以下命令行选项自定义服务行为:

# Change port (default: 8080)
mcp-server-template --port 9000

# Enable debug mode for more detailed logs
mcp-server-template --debug

# Use stdio transport instead of HTTP
mcp-server-template --transport stdio

# Set logging level (options: debug, info, warning, error)
mcp-server-template --log-level debug

🛠️ 创建自己的工具和提示词

添加工具

工具是 AI 模型可以调用的函数。要添加新工具:

  1. 编辑 src/main.py
  2. 使用 @mcp.tool() 装饰器添加一个新函数:
@mcp.tool()
def your_tool_name(param1: str, param2: int) -> Dict[str, Any]:
    """
    Your tool description - this will be shown to the AI.
    
    Args:
        param1: Description of first parameter
        param2: Description of second parameter
        
    Returns:
        Dictionary with your results
    """
    # Your tool logic here
    return {"result": "your result"}
添加提示词

提示词是 AI 模型可以访问的模板:

@mcp.prompt()
def your_prompt_name(param: str) -> str:
    """Your prompt description."""
    return f"""
    Your formatted prompt with {param} inserted.
    Use this for structured prompt templates.
    """

📁 项目结构

src/                      # Source code directory
├── main.py               # Server entry point with tools & prompts
├── config.py             # Configuration settings
├── utils/                # Utility functions
├── tools/                # Tools implementation
└── resources/            # Resource definitions
test/                     # Tests directory
pyproject.toml            # Package configuration
Dockerfile                # Docker support

🚢 部署选项

Docker 部署
# Build the Docker image
docker build -t my-mcp-server .

# Run the container
docker run -p 8080:8080 my-mcp-server
云端部署

此模板旨在与各种云平台良好配合:

  • 在 AWS、GCP 或 Azure 上以容器形式部署
  • 在支持容器化应用的无服务器平台上运行
  • 使用 Kubernetes 进行编排

🧪 开发指南

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black src test
isort src test

# Run linting
flake8 src test

❓ 需要帮助?


为 AI 开发者社区倾心打造 ❤️

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。