mirror of
https://github.com/mastra-ai/mastra.git
synced 2026-11-17 08:44:46 +00:00
|
History
|
||
|---|---|---|
| .. | ||
| src | ||
| package.json | ||
| pnpm-lock.yaml | ||
| pnpm-workspace.yaml | ||
| README.md | ||
| tsconfig.json | ||
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 stdioMCPServerwith aticket.createdevent type. ItsopenTickettool emits an occurrence withserver.events.emit(), andmatchroutes each occurrence to subscribers whoseprojectargument matches.src/client.ts: AnMCPClientthat starts the server, lists its event types, and subscribes toticket.createdfor thewebproject. It opens three tickets through the server's tool, then starts a triage agent run for eachwebticket it receives. Theapiticket 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.