> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.developerdoc.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速开始：将 MCP 服务器发布到 MCP 注册表

<Note>
  MCP 注册表目前处于预览阶段。正式可用前可能发生破坏性变更或数据重置。如果遇到问题，请在 [GitHub](https://github.com/modelcontextprotocol/registry/issues) 上反馈。
</Note>

本教程将演示如何使用官方 `mcp-publisher` CLI 工具，将用 TypeScript 编写的 MCP 服务器发布到 MCP 注册表。

## 前置条件

* **Node.js** — 本教程假定 MCP 服务器使用 TypeScript 编写。
* **npm 账号** — MCP 注册表只托管元数据，不托管构件。在发布到 MCP 注册表前，我们会先将 MCP 服务器包发布到 npm，因此你需要一个 [npm](https://www.npmjs.com) 账号。
* **GitHub 账号** — MCP 注册表支持[多种认证方式](./authentication)。为简化流程，本教程会使用基于 GitHub 的认证，因此你需要一个 [GitHub](https://github.com/) 账号。

如果你还没有用 TypeScript 编写的 MCP 服务器，可以从 [`modelcontextprotocol/quickstart-resources` 仓库](https://github.com/modelcontextprotocol/quickstart-resources)复制 `weather-server-typescript` 服务器，跟随本教程操作：

```bash theme={null}
git clone --depth 1 git@github.com:modelcontextprotocol/quickstart-resources.git
cp -r quickstart-resources/weather-server-typescript .
rm -rf quickstart-resources
cd weather-server-typescript
```

然后编辑 `package.json`，替换为你自己的信息：

```diff package.json theme={null}
 {
-  "name": "mcp-quickstart-ts",
-  "version": "1.0.0",
+  "name": "@my-username/mcp-weather-server",
+  "version": "1.0.1",
   "main": "index.js",
```

```diff package.json theme={null}
   "license": "ISC",
-  "description": "",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/my-username/mcp-weather-server.git"
+  },
+  "description": "An MCP server for weather information.",
   "devDependencies": {
```

## 第 1 步：向包中添加验证信息

MCP 注册表会验证服务器底层包是否与其元数据匹配。对于 npm 包，需要在 `package.json` 中添加 `mcpName` 属性：

```diff package.json theme={null}
 {
   "name": "@my-username/mcp-weather-server",
   "version": "1.0.1",
+  "mcpName": "io.github.my-username/weather",
   "main": "index.js",
```

`mcpName` 的值将作为该服务器在 MCP 注册表中的名称。

由于本教程会使用基于 GitHub 的认证，`mcpName` **必须**以 `io.github.my-username/` 开头。

## 第 2 步：发布包

MCP 注册表只托管元数据，不托管构件，因此必须先将包发布到 npm，再将服务器发布到 MCP 注册表。

确保已构建分发文件：

```bash theme={null}
# 进入项目目录
cd weather-server-typescript

# 安装依赖
npm install

# 构建分发文件
npm run build
```

然后按照 npm 的[发布指南](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages)操作。通常需要运行以下命令：

```bash theme={null}
# 如有需要，登录 npm
npm adduser

# 发布包
npm publish --access public
```

可以访问该包的 npm URL 来确认发布成功，例如 [https://www.npmjs.com/package/@my-username/mcp-weather-server。](https://www.npmjs.com/package/@my-username/mcp-weather-server。)

## 第 3 步：安装 `mcp-publisher`

使用预构建二进制文件或 [Homebrew](https://brew.sh) 安装 `mcp-publisher` CLI 工具：

<CodeGroup>
  ```bash macOS/Linux theme={null}
  curl -L "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/').tar.gz" | tar xz mcp-publisher && sudo mv mcp-publisher /usr/local/bin/
  ```

  ```powershell Windows theme={null}
  $arch = if ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -eq "Arm64") { "arm64" } else { "amd64" }; Invoke-WebRequest -Uri "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_windows_$arch.tar.gz" -OutFile "mcp-publisher.tar.gz"; tar xf mcp-publisher.tar.gz mcp-publisher.exe; rm mcp-publisher.tar.gz
  # 将 mcp-publisher.exe 移动到 PATH 中的某个目录
  ```

  ```bash theme={null}
  brew install mcp-publisher
  ```
</CodeGroup>

运行以下命令确认 `mcp-publisher` 已正确安装：

```bash theme={null}
mcp-publisher --help
```

你应该会看到类似输出：

```text Output theme={null}
MCP Registry Publisher Tool

Usage:
  mcp-publisher <command> [arguments]

Commands:
  init          Create a server.json file template
  login         Authenticate with the registry
  logout        Clear saved authentication
  publish       Publish server.json to the registry
```

## 第 4 步：创建 `server.json`

`mcp-publisher init` 命令可以生成一个 `server.json` 模板文件，其中包含从项目中推导出的一些信息。

在服务器项目目录中运行 `mcp-publisher init`：

```bash theme={null}
mcp-publisher init
```

打开生成的 `server.json` 文件，你应该会看到类似内容：

```json server.json theme={null}
{
  "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
  "name": "io.github.my-username/weather",
  "description": "An MCP server for weather information.",
  "repository": {
    "url": "https://github.com/my-username/mcp-weather-server",
    "source": "github"
  },
  "version": "1.0.0",
  "packages": [
    {
      "registryType": "npm",
      "identifier": "@my-username/mcp-weather-server",
      "version": "1.0.0",
      "transport": {
        "type": "stdio"
      },
      "environmentVariables": [
        {
          "description": "Your API key for the service",
          "isRequired": true,
          "format": "string",
          "isSecret": true,
          "name": "YOUR_API_KEY"
        }
      ]
    }
  ]
}
```

按需编辑其中内容：

```diff server.json theme={null}
 {
   "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
   "name": "io.github.my-username/weather",
   "description": "An MCP server for weather information.",
   "repository": {
     "url": "https://github.com/my-username/mcp-weather-server",
     "source": "github"
   },
-  "version": "1.0.0",
+  "version": "1.0.1",
   "packages": [
     {
       "registryType": "npm",
       "identifier": "@my-username/mcp-weather-server",
-      "version": "1.0.0",
+      "version": "1.0.1",
       "transport": {
         "type": "stdio"
-      },
-      "environmentVariables": [
-        {
-          "description": "Your API key for the service",
-          "isRequired": true,
-          "format": "string",
-          "isSecret": true,
-          "name": "YOUR_API_KEY"
-        }
-      ]
+      }
     }
   ]
 }
```

`server.json` 中的 `name` 属性**必须**与 `package.json` 中的 `mcpName` 属性一致。

## 第 5 步：向 MCP 注册表认证

本教程会使用基于 GitHub 的认证方式向 MCP 注册表认证。

运行 `mcp-publisher login` 命令发起认证：

```bash theme={null}
mcp-publisher login github
```

你应该会看到类似输出：

```text Output theme={null}
Logging in with github...

To authenticate, please:
1. Go to: https://github.com/login/device
2. Enter code: ABCD-1234
3. Authorize this application
Waiting for authorization...
```

访问该链接，按提示操作，并输入终端中打印的授权码（例如上方输出中的 `ABCD-1234`）。完成后回到终端，你应该会看到类似输出：

```text Output theme={null}
Successfully authenticated!
✓ Successfully logged in
```

## 第 6 步：发布到 MCP 注册表

最后，使用 `mcp-publisher publish` 命令将服务器发布到 MCP 注册表：

```bash theme={null}
mcp-publisher publish
```

你应该会看到类似输出：

```text Output theme={null}
Publishing to https://registry.modelcontextprotocol.io...
✓ Successfully published
✓ Server io.github.my-username/weather version 1.0.1
```

可以使用 MCP 注册表 API 搜索该服务器，确认它已发布：

```bash theme={null}
curl "https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.my-username/weather"
```

你应该会在搜索结果 JSON 中看到服务器元数据：

```text Output theme={null}
{"servers":[{ ... "name":"io.github.my-username/weather" ... }]}
```

## 排障

| 错误消息                                                | 处理方式                                                                      |
| --------------------------------------------------- | ------------------------------------------------------------------------- |
| "Registry validation failed for package"            | 确认你的包包含所需的验证信息（例如 `package.json` 中的 `mcpName` 属性）。                        |
| "Invalid or expired Registry JWT token"             | 运行 `mcp-publisher login github` 重新认证。                                     |
| "You do not have permission to publish this server" | 你的认证方式与服务器命名空间格式不匹配。使用 GitHub 认证时，服务器名称必须以 `io.github.your-username/` 开头。 |

## 后续步骤

* 了解[其他包类型的支持](./package-types)。
* 了解[远程服务器支持](./remote-servers)。
* 了解如何[使用其他认证方式](./authentication)，例如可为服务器名称前缀启用自定义域名的 [DNS 认证](./authentication#dns-authentication)。
* 了解如何[使用 GitHub Actions 自动发布](./github-actions)。
