MCP服务器Python模板

一个简单的Python模板,用于使用可流式HTTP传输构建MCP服务器,适用于与AI助手和其他MCP客户端通信的开发场景。

  • by alpic-ai
  • Python
  • 本地部署
  • 7 星标
  • 更新于
在 GitHub 查看 alpic-ai/mcp-server-template-python

使用说明

mcp服务器模板python

一个非常简单的Python模板,用于使用Streamable HTTP传输构建MCP服务器。

概述

此模板为创建可以与AI助手和其他MCP客户端通信的MCP服务器提供了基础。它包括一个简单的HTTP服务器实现,其中包含示例工具、资源和提示,以帮助您开始构建自己的MCP集成。

部署

使用以下按钮克隆存储库并将服务器直接部署到Alpic

先决条件

  • 安装uv(https://docs.astral.sh/uv/getting-started/installation/)

安装

  1. 克隆存储库:
git clone git@github.com:alpic-ai/mcp-server-template-python.git
cd mcp-server-template-python
  1. 安装python版本和依赖项:
uv python install
uv sync --locked

用法

在端口3000上启动服务器:

uv run main.py

运行检查器

需求

  • Node.js:^22.7.5

快速启动(UI模式)

要立即启动并运行UI,只需执行以下操作:

npx @modelcontextprotocol/inspector

检查器服务器将启动,用户界面将可在以下位置访问http://localhost:6274.

您可以通过选择以下选项在本地测试服务器:

  • 传输类型:流式HTTP
  • 网址:http://127.0.0.1:3000/mcp

发展

添加新工具

要添加新工具,请修改 main.py:

@mcp.tool(
    title="Your Tool Name",
    description="Tool Description for the LLM",
)
async def new_tool(
    tool_param1: str = Field(description="The description of the param1 for the LLM"), 
    tool_param2: float = Field(description="The description of the param2 for the LLM") 
)-> str:
    """The new tool underlying method"""
    result = await some_api_call(tool_param1, tool_param2)
    return result

添加新资源

要添加新资源,请修改 main.py:

@mcp.resource(
    uri="your-scheme://{param1}/{param2}",
    description="Description of what this resource provides",
    name="Your Resource Name",
)
def your_resource(param1: str, param2: str) -> str:
    """The resource template implementation"""
    # Your resource logic here
    return f"Resource content for {param1} and {param2}"

URI模板使用 {param_name} 语法定义将从资源URI中提取并传递给函数的参数。

添加新提示

要添加新提示,请修改 main.py:

@mcp.prompt("")
async def your_prompt(
    prompt_param: str = Field(description="The description of the param for the user")
) -> str:
    """Generate a helpful prompt"""

    return f"You are a friendly assistant, help the user and don't forget to {prompt_param}."