Skip to main content
Glama
truongvanhaoem-0111

MCP Server Template (Python)

MCP Server Template (Python)

一个极简的 Model Context Protocol 服务器模板,适用于 Render。Fork 它,添加自己的工具并部署。

包含内容

  • 一个使用 MCP Python SDK 的可运行 MCP 服务器,支持 Streamable HTTP 传输

  • 通过 MCP_API_TOKEN 进行 Bearer token 身份验证(部署时自动生成)

  • 一个用于演示模式的示例工具(hello

  • 一个用于 Render 健康检查的 /health 端点

  • 一个用于一键部署的 render.yaml Blueprint

  • 一个 AGENTS.md 文件,让 AI 编码助手可以为你搭建新工具

注意: 此模板默认在免费计划上部署。免费服务在闲置 15 分钟后会进入休眠状态,导致下一次请求时出现 30-60 秒的冷启动。MCP 客户端可能在此期间超时。为了可靠使用,请在 Render Dashboard 中升级到付费计划 — Starter 计划可让你的服务持续运行。

Related MCP server: MCP Server Template (Python)

本地入门

git clone https://github.com/render-examples/mcp-server-python.git
cd mcp-server-python
pip install -r requirements.txt
python server.py

服务器启动于 http://localhost:10000。MCP 端点为 /mcp

运行测试

pip install -r requirements.txt
pytest

身份验证

服务器使用 bearer token 对请求进行身份验证。Render 的 Blueprint 会在首次部署时自动生成一个随机的 MCP_API_TOKEN

部署后要查找你的 token,请前往 Render Dashboard > 你的服务 > Environment,然后复制 MCP_API_TOKEN 的值。

客户端必须在 Authorization 头中包含该 token:

Authorization: Bearer YOUR_TOKEN

当未设置 MCP_API_TOKEN 时(例如在本地开发期间),身份验证被禁用,所有请求都会被放行。

管理你的 token

初始部署后,token 便由你自行管理:

  • 轮换它:在 Render Dashboard 的 Environment 下更新 MCP_API_TOKEN。服务将自动使用新值重启。

  • 生成新 token,可以使用以下任一方法:

    • openssl rand -base64 32

    • python3 -c "import secrets; print(secrets.token_urlsafe(32))"

    • 密码管理器的生成器(1Password、Bitwarden 等)

  • 不要将 token 提交到版本控制。请使用环境变量或 .env 文件(这些文件已在 .gitignore 中)。

  • 对于多用户或生产环境,请考虑升级到 OAuth 2.1

连接到你的 MCP 服务器

部署到 Render 后,你的 MCP 端点可通过以下地址访问:

https://your-service-name.onrender.com/mcp

Cursor

将以下内容添加到项目的 .cursor/mcp.json 中:

{
  "mcpServers": {
    "my-mcp-server": {
      "url": "https://your-service-name.onrender.com/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_TOKEN"
      }
    }
  }
}

Claude Desktop

将以下内容添加到你的 Claude Desktop 配置中:

{
  "mcpServers": {
    "my-mcp-server": {
      "type": "streamable-http",
      "url": "https://your-service-name.onrender.com/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_TOKEN"
      }
    }
  }
}

Codex

codex mcp add --transport streamable-http \
  --url https://your-service-name.onrender.com/mcp \
  --header "Authorization: Bearer YOUR_TOKEN" \
  my-mcp-server

或者添加到 .codex/config.toml

[mcp_servers.my-mcp-server]
url = "https://your-service-name.onrender.com/mcp"
http_headers = { Authorization = "Bearer YOUR_TOKEN" }

添加工具

通过使用 @mcp.tool() 装饰函数,将工具添加到 server.py

@mcp.tool()
def fetch_weather(city: str, units: str = "celsius") -> str:
    """Get the current weather for a city."""
    # your implementation here
    return f"Weather for {city}"

docstring 会成为工具的描述,MCP 客户端会将其展示给 LLM。请务必编写清晰的描述。

此仓库包含一个 AGENTS.md 文件。如果你使用 AI 编码助手(Cursor、Copilot、Codex、Windsurf 等),你可以要求它“添加一个新工具”,它会自动遵循 AGENTS.md 中的约定。

项目结构

server.py              MCP server with example tool and health check
requirements.txt       Python dependencies
render.yaml            Render Blueprint for deployment
.env.example           Environment variable reference
tests/test_server.py   Test suite (auth, health, tool calls)
pyproject.toml         pytest configuration
AGENTS.md              Instructions for AI coding assistants
CLAUDE.md              Pointer to AGENTS.md for Claude Code

了解更多

F
license - not found
-
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    -
    quality
    C
    maintenance
    A minimal MCP server template for Python with Streamable HTTP transport, bearer token authentication, and a hello tool, designed for deployment on Render.
  • F
    license
    -
    quality
    C
    maintenance
    A minimal MCP server template for Render with Streamable HTTP transport, bearer token authentication, and an example 'hello' tool. Enables developers to quickly scaffold and deploy their own MCP servers.
  • F
    license
    -
    quality
    C
    maintenance
    A minimal MCP server template for Render with Streamable HTTP transport, bearer token authentication, and an example 'hello' tool.
  • F
    license
    -
    quality
    C
    maintenance
    A minimal MCP server template for Render with Streamable HTTP transport, bearer token authentication, and an example 'hello' tool.

View all related MCP servers

Related MCP Connectors

  • Primarily to be used as a template repository for developing MCP servers with FastMCP in Python, P…

  • An MCP server for Arcjet - the runtime security platform that ships with your AI code.

  • Remote ChromaDB vector database MCP server with streamable HTTP transport

View all MCP Connectors

Latest Blog Posts

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/truongvanhaoem-0111/mcp-server-python-msx1l7h1'

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