0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/examples/mcp-events
History 2026-10-21 09:25:38 +02:00
..
src MCP Events: client/server interop tests, example, and guide (#27331) 2026-10-20 20:47:42 -04:00
package.json MCP Events: client/server interop tests, example, and guide (#27331) 2026-10-20 20:47:42 -04:00
pnpm-lock.yaml chore(deps): lock file maintenance (#27013) 2026-10-21 09:25:38 +02:00
pnpm-workspace.yaml MCP Events: client/server interop tests, example, and guide (#27331) 2026-10-20 20:47:42 -04:00
README.md MCP Events: client/server interop tests, example, and guide (#27331) 2026-10-20 20:47:42 -04:00
tsconfig.json MCP Events: client/server interop tests, example, and guide (#27331) 2026-10-20 20:47:42 -04:00

MCP Events

Subscribe to events from a Mastra MCPServer and start an agent run for each occurrence.

MCP Events implement SEP-3415, a draft MCP extension. The API is experimental and may change in any release.

What it shows

  • src/server.ts: A stdio MCPServer with a ticket.created event type. Its openTicket tool emits an occurrence with server.events.emit(), and match routes each occurrence to subscribers whose project argument matches.
  • src/client.ts: An MCPClient that starts the server, lists its event types, and subscribes to ticket.created for the web project. It opens three tickets through the server's tool, then starts a triage agent run for each web ticket it receives. The api ticket never reaches it. The client logs the subscription cursor after each occurrence, which you'd persist to resume later.

Run it

Install the dependencies. The example links @mastra/core and @mastra/mcp from this repository, so build them first (pnpm turbo build --filter @mastra/mcp from the repository root):

cd examples/mcp-events
pnpm install --ignore-workspace

Set your OpenAI API key and start the client:

export OPENAI_API_KEY=sk-...
pnpm start

The output looks like this:

Server offers 'ticket.created' via push, poll
Subscribed via push
[T-1] High: checkout is broken for Safari users, which blocks purchases.
cursor: ...
[T-3] Low: a typo doesn't affect functionality.
cursor: ...

Read the MCP events guide for delivery modes, cursors, and limits. This example uses stdio, so it subscribes with push. To have a remote server POST occurrences to your app instead, see Webhook delivery.