Mastra 推出 MCP Events

从 MCP 服务器向智能体、工作流和应用发送实时事件。

Paul Scanlon·

2026年10月23日

·

阅读时长 4 分钟

你的 MCP 服务器现在可以使用推送、轮询或 webhook 投递方式,向已连接的客户端发送 事件 。

智能体和应用常常需要对对话之外发生的变化做出响应,例如部署失败、拉取请求被合并、新用户注册或订单状态变更。MCP 事件为管道的两端提供了一种标准方式,用于 .emit() 和 .subscribe() 事件,而无需额外的发布/订阅服务。

在 MCP 事件出现之前,MCP 服务器只能响应请求。如果已连接的客户端需要更新,就必须轮询服务器,这通常需要额外的工具并增加复杂性。现在,基于 Mastra 对 MCP 2.0 的支持,可以使用事件让双向通信更易于管理。

配置 可观测性 后,MCP 事件 trace 会被自动采集。

开始使用

在此示例中,每当仓库活动发生时,GitHub 都会向 webhook URL 发送请求。该 webhook URL 是一个 自定义 Mastra API 端点, 用于从 Mastra MCPServer发出事件。一个桌面 Electron 应用使用 Mastra MCPClient.

Terminal
npm install @mastra/mcp@latest @mastra/core@latest
订阅相同的事件 注意需要 @mastra/mcp@2.5.0 和 @mastra/core@1.79.0 或更高版本,已在 PR #27324 中添加.

Define events on the MCP server using defineEvent():

src/mastra/mcp/server.ts
export const github = new MCPServer({
  // ...
  events: {
    "repo.starred": defineEvent({
      description: "Someone starred the repository",
      inputSchema: z.object({ repo: z.string() }),
      payloadSchema: z.object({
        repo: z.string(),
        user: z.string(),
        stars: z.number()
      }),
      match: (args, data) => {
        return data.repo === args.repo;
      }
    })
  }
});

Publish events using .emit(), passing in the data from the event payload:

src/mastra/index.ts
export const mastra = new Mastra({
  mcpServers: { github },
  server: {
    apiRoutes: [
      registerApiRoute("/github/webhook", {
        method: "POST",
        handler: async (context) => {
          const event = context.req.header("X-GitHub-Event");
          const body = await context.req.json();
 
          if (event === "star" && body.action === "created") {
            await github.events.emit("repo.starred", {
              repo: body.repository.full_name,
              user: body.sender.login,
              stars: body.repository.stargazers_count
            });
          }
 
          return context.json({ ok: true });
        }
      })
    ]
  }
});

Subscribe to events using .subscribe() to handle each event as it arrives. The .subscribe() method takes up to four arguments:

  • "github": The name of the MCP server in the client's servers config.
  • "repo.starred": The name of the event to subscribe to.
  • { repo }: The arguments defined by the event's inputSchema.
  • { delivery }: Optional. How events are delivered to the subscription.
src/octocat-app/main.js
const mcp = new MCPClient({
  servers: {
    github: {
      url: new URL("http://<mcp-server-url>/api/mcp/github/mcp")
    }
  }
});
 
const starSubscription = await mcp.events.subscribe("github", "repo.starred", { repo: "PaulieScanlon/mastra-mcp-events" }, { delivery: "push" });
 
for await (const event of starSubscription) {
  const { user, stars } = event.data;
  // trigger star animation with user and star count
}

Delivery methods

Set delivery on .subscribe() to define the subscription behavior. The default is push:

  • push: The subscription holds one events/stream request open listening for server events.
  • poll: The subscription repeatedly asks the server for new events, at most once a second by default.
  • webhook: The server sends each event to a URL on the client as a signed POST request. Requires webhook settings on the MCPClient.

Events and agents

Agents can react to MCP events using MCPEventsSignalProvider. Each event is delivered to the agent's thread as a notification:

const mcp = new MCPClient({
  // ...
});
 
const starSignal = new MCPEventsSignalProvider({
  client: mcp,
  subscribeOptions: { delivery: "push" }
});
 
export const repoAgent = new Agent({
  // ...
  signals: [starSignal]
});
 
await starSignal.subscribeThread(
  { resourceId: "repo-agent", threadId: "thread-1" },
  {
    server: "github",
    event: "repo.starred",
    args: { repo: "PaulieScanlon/mastra-mcp-events" }
  }
);

Events and workflows

Workflows can react to MCP events using a subscription. Each event can be used to start a workflow run:

import { repoWorkflow } from "./src/mastra/workflows/repo-workflow.ts";
 
const mcp = new MCPClient({
  // ...
});
 
const starSubscription = await mcp.events.subscribe("github", "repo.starred", { repo: "PaulieScanlon/mastra-mcp-events" }, { delivery: "push" });
 
for await (const event of starSubscription) {
  const run = await repoWorkflow.createRun();
  await run.start({ inputData: event.data });
}

For more information and full configuration options, see:

MCP events are still experimental — the API may change between releases.

Share on X or LinkedInPaul ScanlonPaul ScanlonTechnical Product Marketing Manager

Paul Scanlon sits between Developer Education and Product Marketing at Mastra. Previously, he was a Technical Product Marketing Manager at Neon and worked in Developer Relations at Gatsby, where he created educational content and developer experiences.

All articles by Paul Scanlon →