如何使用 GitHub Actions 自动发布

如何使用 GitHub Actions 自动发布

MCP Registry 目前处于预览阶段。在正式发布之前,可能发生破坏性变更或数据重置。如果遇到任何问题,请在 GitHub 上报告。

第 1 步:创建工作流文件

在服务器项目目录中创建 .github/workflows/publish-mcp.yml 文件。以下示例适用于基于 npm 的本地服务器,但所有包类型的 MCP Registry 发布步骤都相同:

示例把 mcp-publisher 固定到特定 release,并在解压前验证其 Sigstore 捆绑包。升级时有意识地把 MCP_PUBLISHER_VERSION 更新为目标 release 标签;不要改回可变的 releases/latest URL。

name: Publish to MCP Registry

on:
  push:
    tags: ["v*"] # Triggers on version tags like v1.0.0

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      id-token: write # Required for OIDC authentication
      contents: read
    env:
      MCP_PUBLISHER_VERSION: v1.8.1

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      ### Publish underlying npm package:

      - name: Set up Node.js
        uses: actions/setup-node@v5
        with:
          node-version: "lts/*"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test --if-present

      - name: Build package
        run: npm run build --if-present

      - name: Publish package to npm
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      ### Publish MCP server:

      - name: Install cosign
        uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2

      - name: Download and verify mcp-publisher
        run: |
          set -euo pipefail
          os="$(uname -s | tr '[:upper:]' '[:lower:]')"
          arch="$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
          archive="mcp-publisher_${os}_${arch}.tar.gz"
          base_url="https://github.com/modelcontextprotocol/registry/releases/download/${MCP_PUBLISHER_VERSION}"

          curl --fail --location --silent --show-error --output "$archive" "$base_url/$archive"
          curl --fail --location --silent --show-error --output "$archive.sigstore.json" "$base_url/$archive.sigstore.json"
          cosign verify-blob \
            --bundle "$archive.sigstore.json" \
            --certificate-identity "https://github.com/modelcontextprotocol/registry/.github/workflows/release.yml@refs/tags/${MCP_PUBLISHER_VERSION}" \
            --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
            "$archive"
          tar xzf "$archive" mcp-publisher
          rm -f "$archive" "$archive.sigstore.json"

      - name: Authenticate to MCP Registry
        run: ./mcp-publisher login github-oidc

      # Optional:
      # - name: Set version in server.json
      #   run: |
      #     VERSION=${GITHUB_REF#refs/tags/v}
      #     jq --arg v "$VERSION" '.version = $v' server.json > server.tmp && mv server.tmp server.json

      - name: Publish server to MCP Registry
        run: ./mcp-publisher publish
name: Publish to MCP Registry

on:
  push:
    tags: ["v*"] # Triggers on version tags like v1.0.0

jobs:
  publish:
    runs-on: ubuntu-latest
    # Source MCP_GITHUB_TOKEN from a protected Environment, not a plain repo
    # secret, and restrict that environment to your default branch / release
    # tags. See "Securing your registry token in CI" below for why this matters.
    environment: mcp-registry-publish
    permissions:
      contents: read
    env:
      MCP_PUBLISHER_VERSION: v1.8.1

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      ### Publish underlying npm package:

      - name: Set up Node.js
        uses: actions/setup-node@v5
        with:
          node-version: "lts/*"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test --if-present

      - name: Build package
        run: npm run build --if-present

      - name: Publish package to npm
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      ### Publish MCP server:

      - name: Install cosign
        uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2

      - name: Download and verify mcp-publisher
        run: |
          set -euo pipefail
          os="$(uname -s | tr '[:upper:]' '[:lower:]')"
          arch="$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
          archive="mcp-publisher_${os}_${arch}.tar.gz"
          base_url="https://github.com/modelcontextprotocol/registry/releases/download/${MCP_PUBLISHER_VERSION}"

          curl --fail --location --silent --show-error --output "$archive" "$base_url/$archive"
          curl --fail --location --silent --show-error --output "$archive.sigstore.json" "$base_url/$archive.sigstore.json"
          cosign verify-blob \
            --bundle "$archive.sigstore.json" \
            --certificate-identity "https://github.com/modelcontextprotocol/registry/.github/workflows/release.yml@refs/tags/${MCP_PUBLISHER_VERSION}" \
            --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
            "$archive"
          tar xzf "$archive" mcp-publisher
          rm -f "$archive" "$archive.sigstore.json"

      - name: Authenticate to MCP Registry
        run: ./mcp-publisher login github --token ${{ secrets.MCP_GITHUB_TOKEN }}

      # Optional:
      # - name: Set version in server.json
      #   run: |
      #     VERSION=${GITHUB_REF#refs/tags/v}
      #     jq --arg v "$VERSION" '.version = $v' server.json > server.tmp && mv server.tmp server.json

      - name: Publish server to MCP Registry
        run: ./mcp-publisher publish
name: Publish to MCP Registry

on:
  push:
    tags: ["v*"] # Triggers on version tags like v1.0.0

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    env:
      MCP_PUBLISHER_VERSION: v1.8.1

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      ### Publish underlying npm package:

      - name: Set up Node.js
        uses: actions/setup-node@v5
        with:
          node-version: "lts/*"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test --if-present

      - name: Build package
        run: npm run build --if-present

      - name: Publish package to npm
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      ### Publish MCP server:

      - name: Install cosign
        uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2

      - name: Download and verify mcp-publisher
        run: |
          set -euo pipefail
          os="$(uname -s | tr '[:upper:]' '[:lower:]')"
          arch="$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
          archive="mcp-publisher_${os}_${arch}.tar.gz"
          base_url="https://github.com/modelcontextprotocol/registry/releases/download/${MCP_PUBLISHER_VERSION}"

          curl --fail --location --silent --show-error --output "$archive" "$base_url/$archive"
          curl --fail --location --silent --show-error --output "$archive.sigstore.json" "$base_url/$archive.sigstore.json"
          cosign verify-blob \
            --bundle "$archive.sigstore.json" \
            --certificate-identity "https://github.com/modelcontextprotocol/registry/.github/workflows/release.yml@refs/tags/${MCP_PUBLISHER_VERSION}" \
            --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
            "$archive"
          tar xzf "$archive" mcp-publisher
          rm -f "$archive" "$archive.sigstore.json"

      # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      # TODO: Replace `example.com` with your domain name
      # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      - name: Authenticate to MCP Registry
        run: ./mcp-publisher login dns --domain example.com --private-key ${{ secrets.MCP_PRIVATE_KEY }}

      # Optional:
      # - name: Set version in server.json
      #   run: |
      #     VERSION=${GITHUB_REF#refs/tags/v}
      #     jq --arg v "$VERSION" '.version = $v' server.json > server.tmp && mv server.tmp server.json

      - name: Publish server to MCP Registry
        run: ./mcp-publisher publish

第 2 步:添加 Secret

根据所选的身份验证方法,你可能需要向仓库添加 secret:

  • GitHub OIDC 身份验证:不需要专用 secret。

  • GitHub PAT 身份验证:添加名为 MCP_GITHUB_TOKEN 的 secret,其值为 GitHub Personal Access Token。Registry 只需要读取你的组织角色来确认你是 Owner、然后才授予命名空间,因此按此最小化授权且不要给任何仓库 scope(registry 绝不读写你的代码):

    • 经典 PAT:read:org scope(不需要 read:user;裸 token 也能完成身份验证并发布到个人命名空间)。
    • 细粒度 PAT:Organization permissions → Members → Read-only 权限(即 read:org 的等价物)。缺少它时你只能拿到个人命名空间。细粒度 PAT 绑定单一资源所有者,请为你要发布的组织创建它。

    组织 token 请作为 mcp-registry-publish environment 上的 Environment secret 存储,而不是普通仓库 secret——原因和 environment 的配置方法见下文“在 CI 中保护 registry 令牌”。

  • DNS 身份验证:添加名为 MCP_PRIVATE_KEY 的 secret,其值为你的 Ed25519 私钥。

你可能还需要为包注册表添加 secret。例如,上面的工作流需要名为 NPM_TOKEN 的 secret,其值为 npm token。

有关如何向仓库添加 secret 的信息,请参阅 在 GitHub Actions 中使用 secret。

在 CI 中保护 registry 令牌

无论用哪种 token 做身份验证,都把它当作高价值凭据。当你向组织命名空间做身份验证时,拿到的 registry token 可以发布——并覆盖——io.github.<org>/* 下的任何服务器,而不只是本仓库里的这一个。因此威胁模型里的关键问题是:谁能让代码在这个 secret 暴露的 job 里运行? 默认答案是所有拥有仓库写权限的人(通过分支或标签推送),而不只是组织 Owner——普通仓库 secret 会把发布权限悄悄扩大到每一个有写权限的人。

GitHub 提供了收窄这个缺口的工具。按强度递增,我们建议:

  1. 把 token 存为 Environment secret,而不是仓库或组织 secret。 job 只有声明了 environment:(如上面 PAT 示例)才能读取 environment secret。注意 environment: 键本身不提供任何保护:environment 尚不存在时,GitHub 首次运行会自动创建一个没有任何保护规则的 environment;声明了 environment: 的 job 也仍然能拿到普通仓库和组织 secret。保护来自把 token 存为该 environment 上的 secret 并配置该 environment(下面第 2–3 步),而不是 environment: 键本身。
  2. 用部署分支规则把该 environment 限制到默认分支和/或发布标签,并用必需的 PR 审查保护该分支。这样只有经过审查、维护者批准的代码才能碰到这个 token。
  3. (最强)为 environment 添加必需审查者,让每次发布都暂停等待一次明确的人工批准。

无论上面怎么做,都要避开两个陷阱:

  • 绝不在 checkout 了 PR head 代码的 pull_request_target 工作流里发布。 该触发器带着你基础仓库上下文中的 secret 运行,不可信的 fork PR 可以借此窃取 token。environment 的分支规则在这里保护不了你(ref 仍是你的基础分支)——只有必需审查者可以。
  • 公开仓库的发布 job 避免使用自托管 runner,fork PR 可能有机会把 job 调度到它们上面。

默认情况下,来自 fork 的拉取请求拿不到任何 secret,也不能推送分支,因此外部贡献者碰不到 token——除非踩中上面某个错误配置。

第 3 步:创建标签并发布

创建并推送版本标签以触发工作流:

git tag v1.0.0
git push origin v1.0.0

工作流将运行测试、构建包、把包发布到 npm,并把服务器发布到 MCP Registry。

故障排除

错误消息操作
“Authentication failed”确保为 OIDC 设置了 id-token: write 权限,或检查 secret。
“invalid audience”你的 mcp-publisher 二进制对当前 registry 部署来说太旧了。把 MCP_PUBLISHER_VERSION 更新到兼容的 release 后重新运行工作流。
“Package validation failed”确认包已成功发布到包注册表(例如 npm、PyPI),并且包中包含必要的验证信息。