发布 MCP 服务器
🌐 Publishing an MCP Server
本示例将指导你使用 stdio 传输设置一个基本的 Mastra MCPServer,进行构建,并准备发布到 NPM。
🌐 This example guides you through setting up a basic Mastra MCPServer using the stdio transport, building it, and preparing it for publishing to NPM.
安装依赖Direct link to 安装依赖
🌐 Install dependencies
安装必要的软件包:
🌐 Install the necessary packages:
pnpm add @mastra/mcp @mastra/core tsup
搭建 MCP 服务器Direct link to 搭建 MCP 服务器
🌐 Setting up an MCP Server
为你的 stdio 服务器创建一个文件,例如
/src/mastra/stdio.ts。🌐 Create a file for your stdio server, for example,
/src/mastra/stdio.ts.将以下代码添加到文件中。记得导入你实际的 Mastra 工具,并为服务器命名。
🌐 Add the following code to the file. Remember to import your actual Mastra tools and name the server appropriately.
src/mastra/stdio.ts#!/usr/bin/env node
import { MCPServer } from "@mastra/mcp";
import { weatherTool } from "./tools";
const server = new MCPServer({
name: "my-mcp-server",
version: "1.0.0",
tools: { weatherTool },
});
server.startStdio().catch((error) => {
console.error("Error running MCP server:", error);
process.exit(1);
});更新你的
package.json,添加指向已构建服务器文件的bin条目,以及一个用于同时生成 ESM 和 CJS 输出的服务器构建脚本。🌐 Update your
package.jsonto include thebinentry pointing to your built server file and a script to build the server with both ESM and CJS outputs.package.json{
"bin": "dist/stdio.mjs",
"scripts": {
"build:mcp": "tsup src/mastra/stdio.ts --format esm,cjs --no-splitting --dts && echo '#!/usr/bin/env node' | cat - dist/stdio.mjs > temp && mv temp dist/stdio.mjs && chmod +x dist/stdio.mjs"
}
}构建命令会生成 ESM(
.mjs)和 CJS(.cjs)输出,以实现最大的兼容性。shebang(#!/usr/bin/env node)会被添加到 ESM 构建产物的开头,使其可以直接执行,而bin入口则指向该文件。🌐 The build command generates both ESM (
.mjs) and CJS (.cjs) outputs for maximum compatibility. The shebang (#!/usr/bin/env node) is prepended to the ESM artifact to make it directly executable, and thebinentry points to this file.运行构建命令:
🌐 Run the build command:
pnpm run build:mcp这将把你的服务器代码编译为 ESM 和 CJS 两种格式,并使 ESM 输出文件可执行。在类 Unix 系统上,
chmod +x步骤可以使文件直接可执行。Windows 用户可能需要使用 WSL 或直接通过 Node.js 处理执行。🌐 This will compile your server code into both ESM and CJS formats and make the ESM output file executable. On Unix-like systems, the
chmod +xstep makes the file directly executable. Windows users may need to use WSL or handle execution through Node.js directly.
发布到 NPMDirect link to 发布到 NPM
🌐 Publishing to NPM
要使你的 MCP 服务器可供他人(或自己)通过 npx 使用或作为依赖使用,你可以将其发布到 NPM。
🌐 To make your MCP server available for others (or yourself) to use via npx or as a dependency, you can publish it to NPM.
确保你有一个 NPM 账户并且已登录(
npm login)。🌐 Ensure you have an NPM account and are logged in (
npm login).确保你在
package.json中的包名称是唯一且可用的。🌐 Make sure your package name in
package.jsonis unique and available.在构建完成后,从项目根目录运行发布命令:
🌐 Run the publish command from your project root after building:
npm publish --access public有关发布软件包的更多详情,请参阅NPM 文档。
🌐 For more details on publishing packages, refer to the NPM documentation.
使用已发布的 MCP 服务器Direct link to 使用已发布的 MCP 服务器
🌐 Using a published MCP Server
一旦发布,你的 MCP 服务器可以被 MCPClient 使用,只需指定运行你的软件包的命令。你也可以使用其他任何 MCP 客户端,如 Claude 桌面版、Cursor 或 Windsurf。
🌐 Once published, your MCP server can be used by an MCPClient by specifying the command to run your package. You can also use any other MCP client like Claude desktop, Cursor, or Windsurf.
import { MCPClient } from "@mastra/mcp";
const mcp = new MCPClient({
servers: {
// Give this MCP server instance a name
yourServerName: {
command: "npx",
args: ["-y", "@your-org-name/your-package-name@latest"], // Replace with your package name
},
},
});
// You can then get tools or toolsets from this configuration to use in your agent
const tools = await mcp.listTools();
const toolsets = await mcp.listToolsets();
注意:如果你在没有组织范围的情况下发布,args 可能只是 ["-y", "your-package-name@latest"]。
🌐 Note: If you published without an organization scope, the args might just be ["-y", "your-package-name@latest"].