@mastra/mcp-docs-server 1.1.5 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/docs/agents/adding-voice.md +349 -0
- package/.docs/docs/agents/agent-approval.md +558 -0
- package/.docs/docs/agents/agent-memory.md +209 -0
- package/.docs/docs/agents/guardrails.md +374 -0
- package/.docs/docs/agents/network-approval.md +275 -0
- package/.docs/docs/agents/networks.md +299 -0
- package/.docs/docs/agents/overview.md +304 -0
- package/.docs/docs/agents/processors.md +622 -0
- package/.docs/docs/agents/structured-output.md +273 -0
- package/.docs/docs/agents/supervisor-agents.md +304 -0
- package/.docs/docs/agents/using-tools.md +214 -0
- package/.docs/docs/build-with-ai/mcp-docs-server.md +238 -0
- package/.docs/docs/build-with-ai/skills.md +35 -0
- package/.docs/docs/community/contributing-templates.md +3 -0
- package/.docs/docs/community/discord.md +9 -0
- package/.docs/docs/community/licensing.md +66 -0
- package/.docs/docs/deployment/cloud-providers.md +15 -0
- package/.docs/docs/deployment/mastra-server.md +122 -0
- package/.docs/docs/deployment/monorepo.md +142 -0
- package/.docs/docs/deployment/overview.md +62 -0
- package/.docs/docs/deployment/studio.md +239 -0
- package/.docs/docs/deployment/web-framework.md +52 -0
- package/.docs/docs/deployment/workflow-runners.md +9 -0
- package/.docs/docs/evals/built-in-scorers.md +47 -0
- package/.docs/docs/evals/custom-scorers.md +519 -0
- package/.docs/docs/evals/overview.md +141 -0
- package/.docs/docs/evals/running-in-ci.md +124 -0
- package/.docs/docs/getting-started/build-with-ai.md +68 -0
- package/.docs/docs/getting-started/manual-install.md +226 -0
- package/.docs/docs/getting-started/project-structure.md +60 -0
- package/.docs/docs/getting-started/start.md +28 -0
- package/.docs/docs/getting-started/studio.md +101 -0
- package/.docs/docs/index.md +43 -0
- package/.docs/docs/mastra-cloud/deployment.md +77 -0
- package/.docs/docs/mastra-cloud/observability.md +38 -0
- package/.docs/docs/mastra-cloud/overview.md +23 -0
- package/.docs/docs/mastra-cloud/setup.md +42 -0
- package/.docs/docs/mastra-cloud/studio.md +24 -0
- package/.docs/docs/mastra-code/configuration.md +299 -0
- package/.docs/docs/mastra-code/customization.md +228 -0
- package/.docs/docs/mastra-code/modes.md +104 -0
- package/.docs/docs/mastra-code/overview.md +135 -0
- package/.docs/docs/mastra-code/tools.md +229 -0
- package/.docs/docs/mcp/overview.md +373 -0
- package/.docs/docs/mcp/publishing-mcp-server.md +95 -0
- package/.docs/docs/memory/memory-processors.md +314 -0
- package/.docs/docs/memory/message-history.md +260 -0
- package/.docs/docs/memory/observational-memory.md +248 -0
- package/.docs/docs/memory/overview.md +45 -0
- package/.docs/docs/memory/semantic-recall.md +272 -0
- package/.docs/docs/memory/storage.md +261 -0
- package/.docs/docs/memory/working-memory.md +400 -0
- package/.docs/docs/observability/datasets/overview.md +198 -0
- package/.docs/docs/observability/datasets/running-experiments.md +274 -0
- package/.docs/docs/observability/logging.md +99 -0
- package/.docs/docs/observability/overview.md +70 -0
- package/.docs/docs/observability/tracing/bridges/otel.md +209 -0
- package/.docs/docs/observability/tracing/exporters/arize.md +272 -0
- package/.docs/docs/observability/tracing/exporters/braintrust.md +111 -0
- package/.docs/docs/observability/tracing/exporters/cloud.md +127 -0
- package/.docs/docs/observability/tracing/exporters/datadog.md +187 -0
- package/.docs/docs/observability/tracing/exporters/default.md +209 -0
- package/.docs/docs/observability/tracing/exporters/laminar.md +100 -0
- package/.docs/docs/observability/tracing/exporters/langfuse.md +213 -0
- package/.docs/docs/observability/tracing/exporters/langsmith.md +198 -0
- package/.docs/docs/observability/tracing/exporters/otel.md +476 -0
- package/.docs/docs/observability/tracing/exporters/posthog.md +148 -0
- package/.docs/docs/observability/tracing/exporters/sentry.md +208 -0
- package/.docs/docs/observability/tracing/overview.md +1112 -0
- package/.docs/docs/observability/tracing/processors/sensitive-data-filter.md +300 -0
- package/.docs/docs/rag/chunking-and-embedding.md +183 -0
- package/.docs/docs/rag/graph-rag.md +215 -0
- package/.docs/docs/rag/overview.md +72 -0
- package/.docs/docs/rag/retrieval.md +515 -0
- package/.docs/docs/rag/vector-databases.md +645 -0
- package/.docs/docs/server/auth/auth0.md +220 -0
- package/.docs/docs/server/auth/better-auth.md +203 -0
- package/.docs/docs/server/auth/clerk.md +132 -0
- package/.docs/docs/server/auth/composite-auth.md +234 -0
- package/.docs/docs/server/auth/custom-auth-provider.md +513 -0
- package/.docs/docs/server/auth/firebase.md +272 -0
- package/.docs/docs/server/auth/jwt.md +110 -0
- package/.docs/docs/server/auth/simple-auth.md +180 -0
- package/.docs/docs/server/auth/supabase.md +117 -0
- package/.docs/docs/server/auth/workos.md +186 -0
- package/.docs/docs/server/auth.md +38 -0
- package/.docs/docs/server/custom-adapters.md +378 -0
- package/.docs/docs/server/custom-api-routes.md +267 -0
- package/.docs/docs/server/mastra-client.md +243 -0
- package/.docs/docs/server/mastra-server.md +71 -0
- package/.docs/docs/server/middleware.md +225 -0
- package/.docs/docs/server/request-context.md +471 -0
- package/.docs/docs/server/server-adapters.md +547 -0
- package/.docs/docs/streaming/events.md +237 -0
- package/.docs/docs/streaming/overview.md +175 -0
- package/.docs/docs/streaming/tool-streaming.md +175 -0
- package/.docs/docs/streaming/workflow-streaming.md +109 -0
- package/.docs/docs/voice/overview.md +959 -0
- package/.docs/docs/voice/speech-to-speech.md +102 -0
- package/.docs/docs/voice/speech-to-text.md +79 -0
- package/.docs/docs/voice/text-to-speech.md +83 -0
- package/.docs/docs/workflows/agents-and-tools.md +166 -0
- package/.docs/docs/workflows/control-flow.md +822 -0
- package/.docs/docs/workflows/error-handling.md +360 -0
- package/.docs/docs/workflows/human-in-the-loop.md +215 -0
- package/.docs/docs/workflows/overview.md +370 -0
- package/.docs/docs/workflows/snapshots.md +238 -0
- package/.docs/docs/workflows/suspend-and-resume.md +205 -0
- package/.docs/docs/workflows/time-travel.md +309 -0
- package/.docs/docs/workflows/workflow-state.md +181 -0
- package/.docs/docs/workspace/filesystem.md +164 -0
- package/.docs/docs/workspace/overview.md +239 -0
- package/.docs/docs/workspace/sandbox.md +63 -0
- package/.docs/docs/workspace/search.md +243 -0
- package/.docs/docs/workspace/skills.md +169 -0
- package/.docs/guides/agent-frameworks/ai-sdk.md +140 -0
- package/.docs/guides/build-your-ui/ai-sdk-ui.md +1499 -0
- package/.docs/guides/build-your-ui/assistant-ui.md +156 -0
- package/.docs/guides/build-your-ui/copilotkit.md +289 -0
- package/.docs/guides/deployment/amazon-ec2.md +130 -0
- package/.docs/guides/deployment/aws-lambda.md +248 -0
- package/.docs/guides/deployment/azure-app-services.md +114 -0
- package/.docs/guides/deployment/cloudflare.md +99 -0
- package/.docs/guides/deployment/digital-ocean.md +168 -0
- package/.docs/guides/deployment/inngest.md +682 -0
- package/.docs/guides/deployment/netlify.md +77 -0
- package/.docs/guides/deployment/vercel.md +101 -0
- package/.docs/guides/getting-started/astro.md +398 -0
- package/.docs/guides/getting-started/electron.md +504 -0
- package/.docs/guides/getting-started/express.md +251 -0
- package/.docs/guides/getting-started/hono.md +190 -0
- package/.docs/guides/getting-started/next-js.md +347 -0
- package/.docs/guides/getting-started/nuxt.md +497 -0
- package/.docs/guides/getting-started/quickstart.md +67 -0
- package/.docs/guides/getting-started/sveltekit.md +296 -0
- package/.docs/guides/getting-started/vite-react.md +425 -0
- package/.docs/guides/guide/ai-recruiter.md +226 -0
- package/.docs/guides/guide/chef-michel.md +211 -0
- package/.docs/guides/guide/code-review-bot.md +226 -0
- package/.docs/guides/guide/dev-assistant.md +307 -0
- package/.docs/guides/guide/docs-manager.md +238 -0
- package/.docs/guides/guide/github-actions-pr-description.md +236 -0
- package/.docs/guides/guide/notes-mcp-server.md +416 -0
- package/.docs/guides/guide/research-assistant.md +348 -0
- package/.docs/guides/guide/research-coordinator.md +416 -0
- package/.docs/guides/guide/stock-agent.md +132 -0
- package/.docs/guides/guide/web-search.md +320 -0
- package/.docs/guides/guide/whatsapp-chat-bot.md +405 -0
- package/.docs/guides/index.md +3 -0
- package/.docs/guides/migrations/agentnetwork.md +97 -0
- package/.docs/guides/migrations/ai-sdk-v4-to-v5.md +112 -0
- package/.docs/guides/migrations/network-to-supervisor.md +261 -0
- package/.docs/guides/migrations/upgrade-to-v1/agent.md +404 -0
- package/.docs/guides/migrations/upgrade-to-v1/cli.md +57 -0
- package/.docs/guides/migrations/upgrade-to-v1/client.md +337 -0
- package/.docs/guides/migrations/upgrade-to-v1/deployment.md +37 -0
- package/.docs/guides/migrations/upgrade-to-v1/evals.md +239 -0
- package/.docs/guides/migrations/upgrade-to-v1/mastra.md +143 -0
- package/.docs/guides/migrations/upgrade-to-v1/mcp.md +97 -0
- package/.docs/guides/migrations/upgrade-to-v1/memory.md +285 -0
- package/.docs/guides/migrations/upgrade-to-v1/overview.md +119 -0
- package/.docs/guides/migrations/upgrade-to-v1/processors.md +68 -0
- package/.docs/guides/migrations/upgrade-to-v1/rag.md +42 -0
- package/.docs/guides/migrations/upgrade-to-v1/storage.md +553 -0
- package/.docs/guides/migrations/upgrade-to-v1/tools.md +180 -0
- package/.docs/guides/migrations/upgrade-to-v1/tracing.md +412 -0
- package/.docs/guides/migrations/upgrade-to-v1/vectors.md +87 -0
- package/.docs/guides/migrations/upgrade-to-v1/voice.md +30 -0
- package/.docs/guides/migrations/upgrade-to-v1/workflows.md +341 -0
- package/.docs/guides/migrations/vnext-to-standard-apis.md +362 -0
- package/.docs/models/embeddings.md +161 -0
- package/.docs/models/gateways/azure-openai.md +128 -0
- package/.docs/models/gateways/custom-gateways.md +545 -0
- package/.docs/models/gateways/netlify.md +88 -0
- package/.docs/models/gateways/openrouter.md +219 -0
- package/.docs/models/gateways/vercel.md +225 -0
- package/.docs/models/gateways.md +14 -0
- package/.docs/models/index.md +286 -0
- package/.docs/models/providers/302ai.md +134 -0
- package/.docs/models/providers/abacus.md +125 -0
- package/.docs/models/providers/agentrouter.md +90 -0
- package/.docs/models/providers/aihubmix.md +107 -0
- package/.docs/models/providers/alibaba-cn.md +135 -0
- package/.docs/models/providers/alibaba.md +111 -0
- package/.docs/models/providers/amazon-bedrock.md +33 -0
- package/.docs/models/providers/anthropic.md +153 -0
- package/.docs/models/providers/azure.md +33 -0
- package/.docs/models/providers/bailing.md +72 -0
- package/.docs/models/providers/baseten.md +77 -0
- package/.docs/models/providers/berget.md +78 -0
- package/.docs/models/providers/cerebras.md +101 -0
- package/.docs/models/providers/chutes.md +136 -0
- package/.docs/models/providers/cloudflare-ai-gateway.md +33 -0
- package/.docs/models/providers/cloudflare-workers-ai.md +109 -0
- package/.docs/models/providers/cohere.md +33 -0
- package/.docs/models/providers/cortecs.md +91 -0
- package/.docs/models/providers/deepinfra.md +112 -0
- package/.docs/models/providers/deepseek.md +88 -0
- package/.docs/models/providers/fastrouter.md +84 -0
- package/.docs/models/providers/fireworks-ai.md +89 -0
- package/.docs/models/providers/firmware.md +85 -0
- package/.docs/models/providers/friendli.md +78 -0
- package/.docs/models/providers/github-models.md +125 -0
- package/.docs/models/providers/google-vertex.md +33 -0
- package/.docs/models/providers/google.md +159 -0
- package/.docs/models/providers/groq.md +107 -0
- package/.docs/models/providers/helicone.md +161 -0
- package/.docs/models/providers/huggingface.md +90 -0
- package/.docs/models/providers/iflowcn.md +84 -0
- package/.docs/models/providers/inception.md +72 -0
- package/.docs/models/providers/inference.md +79 -0
- package/.docs/models/providers/io-intelligence.md +87 -0
- package/.docs/models/providers/io-net.md +87 -0
- package/.docs/models/providers/jiekou.md +131 -0
- package/.docs/models/providers/kilo.md +333 -0
- package/.docs/models/providers/kimi-for-coding.md +100 -0
- package/.docs/models/providers/kuae-cloud-coding-plan.md +71 -0
- package/.docs/models/providers/llama.md +77 -0
- package/.docs/models/providers/lmstudio.md +73 -0
- package/.docs/models/providers/lucidquery.md +72 -0
- package/.docs/models/providers/minimax-cn-coding-plan.md +102 -0
- package/.docs/models/providers/minimax-cn.md +102 -0
- package/.docs/models/providers/minimax-coding-plan.md +102 -0
- package/.docs/models/providers/minimax.md +104 -0
- package/.docs/models/providers/mistral.md +124 -0
- package/.docs/models/providers/moark.md +72 -0
- package/.docs/models/providers/modelscope.md +77 -0
- package/.docs/models/providers/moonshotai-cn.md +76 -0
- package/.docs/models/providers/moonshotai.md +76 -0
- package/.docs/models/providers/morph.md +73 -0
- package/.docs/models/providers/nano-gpt.md +103 -0
- package/.docs/models/providers/nebius.md +116 -0
- package/.docs/models/providers/nova.md +72 -0
- package/.docs/models/providers/novita-ai.md +154 -0
- package/.docs/models/providers/nvidia.md +141 -0
- package/.docs/models/providers/ollama-cloud.md +103 -0
- package/.docs/models/providers/ollama.md +33 -0
- package/.docs/models/providers/openai.md +193 -0
- package/.docs/models/providers/opencode.md +100 -0
- package/.docs/models/providers/ovhcloud.md +83 -0
- package/.docs/models/providers/perplexity.md +100 -0
- package/.docs/models/providers/poe.md +183 -0
- package/.docs/models/providers/privatemode-ai.md +75 -0
- package/.docs/models/providers/requesty.md +90 -0
- package/.docs/models/providers/scaleway.md +84 -0
- package/.docs/models/providers/siliconflow-cn.md +138 -0
- package/.docs/models/providers/siliconflow.md +140 -0
- package/.docs/models/providers/stackit.md +78 -0
- package/.docs/models/providers/stepfun.md +73 -0
- package/.docs/models/providers/submodel.md +79 -0
- package/.docs/models/providers/synthetic.md +96 -0
- package/.docs/models/providers/togetherai.md +115 -0
- package/.docs/models/providers/upstage.md +73 -0
- package/.docs/models/providers/venice.md +95 -0
- package/.docs/models/providers/vivgrid.md +106 -0
- package/.docs/models/providers/vultr.md +75 -0
- package/.docs/models/providers/wandb.md +80 -0
- package/.docs/models/providers/xai.md +141 -0
- package/.docs/models/providers/xiaomi.md +71 -0
- package/.docs/models/providers/zai-coding-plan.md +80 -0
- package/.docs/models/providers/zai.md +79 -0
- package/.docs/models/providers/zenmux.md +161 -0
- package/.docs/models/providers/zhipuai-coding-plan.md +79 -0
- package/.docs/models/providers/zhipuai.md +79 -0
- package/.docs/models/providers.md +81 -0
- package/.docs/reference/agents/agent.md +141 -0
- package/.docs/reference/agents/generate.md +186 -0
- package/.docs/reference/agents/generateLegacy.md +173 -0
- package/.docs/reference/agents/getDefaultGenerateOptions.md +36 -0
- package/.docs/reference/agents/getDefaultOptions.md +34 -0
- package/.docs/reference/agents/getDefaultStreamOptions.md +36 -0
- package/.docs/reference/agents/getDescription.md +21 -0
- package/.docs/reference/agents/getInstructions.md +34 -0
- package/.docs/reference/agents/getLLM.md +37 -0
- package/.docs/reference/agents/getMemory.md +34 -0
- package/.docs/reference/agents/getModel.md +34 -0
- package/.docs/reference/agents/getTools.md +29 -0
- package/.docs/reference/agents/getVoice.md +34 -0
- package/.docs/reference/agents/listAgents.md +35 -0
- package/.docs/reference/agents/listScorers.md +34 -0
- package/.docs/reference/agents/listTools.md +34 -0
- package/.docs/reference/agents/listWorkflows.md +34 -0
- package/.docs/reference/agents/network.md +133 -0
- package/.docs/reference/ai-sdk/chat-route.md +82 -0
- package/.docs/reference/ai-sdk/handle-chat-stream.md +53 -0
- package/.docs/reference/ai-sdk/handle-network-stream.md +37 -0
- package/.docs/reference/ai-sdk/handle-workflow-stream.md +55 -0
- package/.docs/reference/ai-sdk/network-route.md +74 -0
- package/.docs/reference/ai-sdk/to-ai-sdk-stream.md +231 -0
- package/.docs/reference/ai-sdk/to-ai-sdk-v4-messages.md +79 -0
- package/.docs/reference/ai-sdk/to-ai-sdk-v5-messages.md +76 -0
- package/.docs/reference/ai-sdk/with-mastra.md +59 -0
- package/.docs/reference/ai-sdk/workflow-route.md +79 -0
- package/.docs/reference/auth/auth0.md +73 -0
- package/.docs/reference/auth/better-auth.md +71 -0
- package/.docs/reference/auth/clerk.md +36 -0
- package/.docs/reference/auth/firebase.md +80 -0
- package/.docs/reference/auth/jwt.md +26 -0
- package/.docs/reference/auth/supabase.md +33 -0
- package/.docs/reference/auth/workos.md +84 -0
- package/.docs/reference/cli/create-mastra.md +137 -0
- package/.docs/reference/cli/mastra.md +336 -0
- package/.docs/reference/client-js/agents.md +437 -0
- package/.docs/reference/client-js/error-handling.md +16 -0
- package/.docs/reference/client-js/logs.md +24 -0
- package/.docs/reference/client-js/mastra-client.md +63 -0
- package/.docs/reference/client-js/memory.md +221 -0
- package/.docs/reference/client-js/observability.md +72 -0
- package/.docs/reference/client-js/telemetry.md +20 -0
- package/.docs/reference/client-js/tools.md +44 -0
- package/.docs/reference/client-js/vectors.md +79 -0
- package/.docs/reference/client-js/workflows.md +199 -0
- package/.docs/reference/configuration.md +752 -0
- package/.docs/reference/core/addGateway.md +42 -0
- package/.docs/reference/core/getAgent.md +21 -0
- package/.docs/reference/core/getAgentById.md +21 -0
- package/.docs/reference/core/getDeployer.md +22 -0
- package/.docs/reference/core/getGateway.md +38 -0
- package/.docs/reference/core/getGatewayById.md +41 -0
- package/.docs/reference/core/getLogger.md +22 -0
- package/.docs/reference/core/getMCPServer.md +47 -0
- package/.docs/reference/core/getMCPServerById.md +55 -0
- package/.docs/reference/core/getMemory.md +50 -0
- package/.docs/reference/core/getScorer.md +54 -0
- package/.docs/reference/core/getScorerById.md +54 -0
- package/.docs/reference/core/getServer.md +22 -0
- package/.docs/reference/core/getStorage.md +22 -0
- package/.docs/reference/core/getStoredAgentById.md +89 -0
- package/.docs/reference/core/getTelemetry.md +22 -0
- package/.docs/reference/core/getVector.md +22 -0
- package/.docs/reference/core/getWorkflow.md +42 -0
- package/.docs/reference/core/listAgents.md +21 -0
- package/.docs/reference/core/listGateways.md +40 -0
- package/.docs/reference/core/listLogs.md +38 -0
- package/.docs/reference/core/listLogsByRunId.md +36 -0
- package/.docs/reference/core/listMCPServers.md +55 -0
- package/.docs/reference/core/listMemory.md +56 -0
- package/.docs/reference/core/listScorers.md +29 -0
- package/.docs/reference/core/listStoredAgents.md +93 -0
- package/.docs/reference/core/listVectors.md +22 -0
- package/.docs/reference/core/listWorkflows.md +21 -0
- package/.docs/reference/core/mastra-class.md +66 -0
- package/.docs/reference/core/mastra-model-gateway.md +153 -0
- package/.docs/reference/core/setLogger.md +26 -0
- package/.docs/reference/core/setStorage.md +27 -0
- package/.docs/reference/datasets/addItem.md +37 -0
- package/.docs/reference/datasets/addItems.md +35 -0
- package/.docs/reference/datasets/compareExperiments.md +52 -0
- package/.docs/reference/datasets/create.md +51 -0
- package/.docs/reference/datasets/dataset.md +82 -0
- package/.docs/reference/datasets/datasets-manager.md +94 -0
- package/.docs/reference/datasets/delete.md +25 -0
- package/.docs/reference/datasets/deleteExperiment.md +27 -0
- package/.docs/reference/datasets/deleteItem.md +27 -0
- package/.docs/reference/datasets/deleteItems.md +29 -0
- package/.docs/reference/datasets/get.md +31 -0
- package/.docs/reference/datasets/getDetails.md +47 -0
- package/.docs/reference/datasets/getExperiment.md +30 -0
- package/.docs/reference/datasets/getItem.md +33 -0
- package/.docs/reference/datasets/getItemHistory.md +31 -0
- package/.docs/reference/datasets/list.md +31 -0
- package/.docs/reference/datasets/listExperimentResults.md +39 -0
- package/.docs/reference/datasets/listExperiments.md +33 -0
- package/.docs/reference/datasets/listItems.md +46 -0
- package/.docs/reference/datasets/listVersions.md +33 -0
- package/.docs/reference/datasets/startExperiment.md +62 -0
- package/.docs/reference/datasets/startExperimentAsync.md +43 -0
- package/.docs/reference/datasets/update.md +48 -0
- package/.docs/reference/datasets/updateItem.md +38 -0
- package/.docs/reference/deployer/cloudflare.md +79 -0
- package/.docs/reference/deployer/netlify.md +80 -0
- package/.docs/reference/deployer/vercel.md +91 -0
- package/.docs/reference/deployer.md +100 -0
- package/.docs/reference/evals/answer-relevancy.md +105 -0
- package/.docs/reference/evals/answer-similarity.md +99 -0
- package/.docs/reference/evals/bias.md +120 -0
- package/.docs/reference/evals/completeness.md +136 -0
- package/.docs/reference/evals/content-similarity.md +101 -0
- package/.docs/reference/evals/context-precision.md +196 -0
- package/.docs/reference/evals/context-relevance.md +531 -0
- package/.docs/reference/evals/create-scorer.md +270 -0
- package/.docs/reference/evals/faithfulness.md +114 -0
- package/.docs/reference/evals/hallucination.md +213 -0
- package/.docs/reference/evals/keyword-coverage.md +128 -0
- package/.docs/reference/evals/mastra-scorer.md +123 -0
- package/.docs/reference/evals/noise-sensitivity.md +675 -0
- package/.docs/reference/evals/prompt-alignment.md +614 -0
- package/.docs/reference/evals/run-evals.md +179 -0
- package/.docs/reference/evals/scorer-utils.md +326 -0
- package/.docs/reference/evals/textual-difference.md +113 -0
- package/.docs/reference/evals/tone-consistency.md +119 -0
- package/.docs/reference/evals/tool-call-accuracy.md +533 -0
- package/.docs/reference/evals/toxicity.md +123 -0
- package/.docs/reference/harness/harness-class.md +708 -0
- package/.docs/reference/index.md +277 -0
- package/.docs/reference/logging/pino-logger.md +117 -0
- package/.docs/reference/mastra-code/createMastraCode.md +108 -0
- package/.docs/reference/memory/clone-utilities.md +199 -0
- package/.docs/reference/memory/cloneThread.md +130 -0
- package/.docs/reference/memory/createThread.md +68 -0
- package/.docs/reference/memory/deleteMessages.md +38 -0
- package/.docs/reference/memory/getThreadById.md +24 -0
- package/.docs/reference/memory/listThreads.md +145 -0
- package/.docs/reference/memory/memory-class.md +147 -0
- package/.docs/reference/memory/observational-memory.md +565 -0
- package/.docs/reference/memory/recall.md +91 -0
- package/.docs/reference/observability/tracing/bridges/otel.md +131 -0
- package/.docs/reference/observability/tracing/configuration.md +178 -0
- package/.docs/reference/observability/tracing/exporters/arize.md +141 -0
- package/.docs/reference/observability/tracing/exporters/braintrust.md +93 -0
- package/.docs/reference/observability/tracing/exporters/cloud-exporter.md +163 -0
- package/.docs/reference/observability/tracing/exporters/console-exporter.md +138 -0
- package/.docs/reference/observability/tracing/exporters/datadog.md +116 -0
- package/.docs/reference/observability/tracing/exporters/default-exporter.md +174 -0
- package/.docs/reference/observability/tracing/exporters/laminar.md +78 -0
- package/.docs/reference/observability/tracing/exporters/langfuse.md +134 -0
- package/.docs/reference/observability/tracing/exporters/langsmith.md +108 -0
- package/.docs/reference/observability/tracing/exporters/otel.md +199 -0
- package/.docs/reference/observability/tracing/exporters/posthog.md +92 -0
- package/.docs/reference/observability/tracing/exporters/sentry.md +184 -0
- package/.docs/reference/observability/tracing/instances.md +107 -0
- package/.docs/reference/observability/tracing/interfaces.md +743 -0
- package/.docs/reference/observability/tracing/processors/sensitive-data-filter.md +144 -0
- package/.docs/reference/observability/tracing/spans.md +224 -0
- package/.docs/reference/processors/batch-parts-processor.md +61 -0
- package/.docs/reference/processors/language-detector.md +82 -0
- package/.docs/reference/processors/message-history-processor.md +85 -0
- package/.docs/reference/processors/moderation-processor.md +104 -0
- package/.docs/reference/processors/pii-detector.md +108 -0
- package/.docs/reference/processors/processor-interface.md +521 -0
- package/.docs/reference/processors/prompt-injection-detector.md +72 -0
- package/.docs/reference/processors/semantic-recall-processor.md +117 -0
- package/.docs/reference/processors/system-prompt-scrubber.md +80 -0
- package/.docs/reference/processors/token-limiter-processor.md +115 -0
- package/.docs/reference/processors/tool-call-filter.md +85 -0
- package/.docs/reference/processors/tool-search-processor.md +111 -0
- package/.docs/reference/processors/unicode-normalizer.md +62 -0
- package/.docs/reference/processors/working-memory-processor.md +152 -0
- package/.docs/reference/rag/chunk.md +221 -0
- package/.docs/reference/rag/database-config.md +261 -0
- package/.docs/reference/rag/document.md +114 -0
- package/.docs/reference/rag/embeddings.md +92 -0
- package/.docs/reference/rag/extract-params.md +168 -0
- package/.docs/reference/rag/graph-rag.md +111 -0
- package/.docs/reference/rag/metadata-filters.md +216 -0
- package/.docs/reference/rag/rerank.md +75 -0
- package/.docs/reference/rag/rerankWithScorer.md +80 -0
- package/.docs/reference/server/create-route.md +262 -0
- package/.docs/reference/server/express-adapter.md +176 -0
- package/.docs/reference/server/fastify-adapter.md +90 -0
- package/.docs/reference/server/hono-adapter.md +162 -0
- package/.docs/reference/server/koa-adapter.md +127 -0
- package/.docs/reference/server/mastra-server.md +298 -0
- package/.docs/reference/server/register-api-route.md +249 -0
- package/.docs/reference/server/routes.md +306 -0
- package/.docs/reference/storage/cloudflare-d1.md +218 -0
- package/.docs/reference/storage/cloudflare.md +88 -0
- package/.docs/reference/storage/composite.md +235 -0
- package/.docs/reference/storage/convex.md +161 -0
- package/.docs/reference/storage/dynamodb.md +282 -0
- package/.docs/reference/storage/lance.md +131 -0
- package/.docs/reference/storage/libsql.md +135 -0
- package/.docs/reference/storage/mongodb.md +262 -0
- package/.docs/reference/storage/mssql.md +157 -0
- package/.docs/reference/storage/overview.md +121 -0
- package/.docs/reference/storage/postgresql.md +526 -0
- package/.docs/reference/storage/upstash.md +160 -0
- package/.docs/reference/streaming/ChunkType.md +292 -0
- package/.docs/reference/streaming/agents/MastraModelOutput.md +182 -0
- package/.docs/reference/streaming/agents/stream.md +221 -0
- package/.docs/reference/streaming/agents/streamLegacy.md +142 -0
- package/.docs/reference/streaming/workflows/observeStream.md +42 -0
- package/.docs/reference/streaming/workflows/resumeStream.md +61 -0
- package/.docs/reference/streaming/workflows/stream.md +88 -0
- package/.docs/reference/streaming/workflows/timeTravelStream.md +142 -0
- package/.docs/reference/templates/overview.md +194 -0
- package/.docs/reference/tools/create-tool.md +237 -0
- package/.docs/reference/tools/document-chunker-tool.md +89 -0
- package/.docs/reference/tools/graph-rag-tool.md +182 -0
- package/.docs/reference/tools/mcp-client.md +954 -0
- package/.docs/reference/tools/mcp-server.md +1271 -0
- package/.docs/reference/tools/vector-query-tool.md +459 -0
- package/.docs/reference/vectors/astra.md +121 -0
- package/.docs/reference/vectors/chroma.md +264 -0
- package/.docs/reference/vectors/convex.md +300 -0
- package/.docs/reference/vectors/couchbase.md +226 -0
- package/.docs/reference/vectors/duckdb.md +318 -0
- package/.docs/reference/vectors/elasticsearch.md +189 -0
- package/.docs/reference/vectors/lance.md +220 -0
- package/.docs/reference/vectors/libsql.md +305 -0
- package/.docs/reference/vectors/mongodb.md +295 -0
- package/.docs/reference/vectors/opensearch.md +99 -0
- package/.docs/reference/vectors/pg.md +408 -0
- package/.docs/reference/vectors/pinecone.md +168 -0
- package/.docs/reference/vectors/qdrant.md +222 -0
- package/.docs/reference/vectors/s3vectors.md +277 -0
- package/.docs/reference/vectors/turbopuffer.md +157 -0
- package/.docs/reference/vectors/upstash.md +294 -0
- package/.docs/reference/vectors/vectorize.md +147 -0
- package/.docs/reference/voice/azure.md +148 -0
- package/.docs/reference/voice/cloudflare.md +83 -0
- package/.docs/reference/voice/composite-voice.md +121 -0
- package/.docs/reference/voice/deepgram.md +79 -0
- package/.docs/reference/voice/elevenlabs.md +98 -0
- package/.docs/reference/voice/google-gemini-live.md +378 -0
- package/.docs/reference/voice/google.md +228 -0
- package/.docs/reference/voice/mastra-voice.md +311 -0
- package/.docs/reference/voice/murf.md +122 -0
- package/.docs/reference/voice/openai-realtime.md +203 -0
- package/.docs/reference/voice/openai.md +88 -0
- package/.docs/reference/voice/playai.md +80 -0
- package/.docs/reference/voice/sarvam.md +126 -0
- package/.docs/reference/voice/speechify.md +75 -0
- package/.docs/reference/voice/voice.addInstructions.md +55 -0
- package/.docs/reference/voice/voice.addTools.md +67 -0
- package/.docs/reference/voice/voice.answer.md +54 -0
- package/.docs/reference/voice/voice.close.md +51 -0
- package/.docs/reference/voice/voice.connect.md +94 -0
- package/.docs/reference/voice/voice.events.md +37 -0
- package/.docs/reference/voice/voice.getSpeakers.md +129 -0
- package/.docs/reference/voice/voice.listen.md +164 -0
- package/.docs/reference/voice/voice.off.md +54 -0
- package/.docs/reference/voice/voice.on.md +111 -0
- package/.docs/reference/voice/voice.send.md +65 -0
- package/.docs/reference/voice/voice.speak.md +157 -0
- package/.docs/reference/voice/voice.updateConfig.md +60 -0
- package/.docs/reference/workflows/run-methods/cancel.md +86 -0
- package/.docs/reference/workflows/run-methods/restart.md +33 -0
- package/.docs/reference/workflows/run-methods/resume.md +59 -0
- package/.docs/reference/workflows/run-methods/start.md +58 -0
- package/.docs/reference/workflows/run-methods/startAsync.md +67 -0
- package/.docs/reference/workflows/run-methods/timeTravel.md +142 -0
- package/.docs/reference/workflows/run.md +59 -0
- package/.docs/reference/workflows/step.md +119 -0
- package/.docs/reference/workflows/workflow-methods/branch.md +25 -0
- package/.docs/reference/workflows/workflow-methods/commit.md +17 -0
- package/.docs/reference/workflows/workflow-methods/create-run.md +63 -0
- package/.docs/reference/workflows/workflow-methods/dountil.md +25 -0
- package/.docs/reference/workflows/workflow-methods/dowhile.md +25 -0
- package/.docs/reference/workflows/workflow-methods/foreach.md +118 -0
- package/.docs/reference/workflows/workflow-methods/map.md +93 -0
- package/.docs/reference/workflows/workflow-methods/parallel.md +21 -0
- package/.docs/reference/workflows/workflow-methods/sleep.md +35 -0
- package/.docs/reference/workflows/workflow-methods/sleepUntil.md +35 -0
- package/.docs/reference/workflows/workflow-methods/then.md +21 -0
- package/.docs/reference/workflows/workflow.md +157 -0
- package/.docs/reference/workspace/e2b-sandbox.md +289 -0
- package/.docs/reference/workspace/filesystem.md +255 -0
- package/.docs/reference/workspace/gcs-filesystem.md +174 -0
- package/.docs/reference/workspace/local-filesystem.md +343 -0
- package/.docs/reference/workspace/local-sandbox.md +301 -0
- package/.docs/reference/workspace/s3-filesystem.md +175 -0
- package/.docs/reference/workspace/sandbox.md +87 -0
- package/.docs/reference/workspace/workspace-class.md +244 -0
- package/CHANGELOG.md +16 -0
- package/package.json +6 -6
|
@@ -0,0 +1,1499 @@
|
|
|
1
|
+
# Using AI SDK UI
|
|
2
|
+
|
|
3
|
+
[AI SDK UI](https://sdk.vercel.ai) is a library of React utilities and components for building AI-powered interfaces. In this guide, you'll learn how to use `@mastra/ai-sdk` to convert Mastra's output to AI SDK-compatible formats, enabling you to use its hooks and components in your frontend.
|
|
4
|
+
|
|
5
|
+
> **Note:** Migrating from AI SDK v4 to v5? See the [migration guide](https://mastra.ai/guides/migrations/ai-sdk-v4-to-v5).
|
|
6
|
+
|
|
7
|
+
> **Tip:** Want to see more examples? Visit Mastra's [**UI Dojo**](https://ui-dojo.mastra.ai/) or the [Next.js quickstart guide](https://mastra.ai/guides/getting-started/next-js).
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
Use Mastra and AI SDK UI together by installing the `@mastra/ai-sdk` package. `@mastra/ai-sdk` provides custom API routes and utilities for streaming Mastra agents in AI SDK-compatible formats. This includes chat, workflow, and network route handlers, along with utilities and exported types for UI integrations.
|
|
12
|
+
|
|
13
|
+
`@mastra/ai-sdk` integrates with AI SDK UI's three main hooks: [`useChat()`](https://ai-sdk.dev/docs/ai-sdk-ui/chatbot), [`useCompletion()`](https://ai-sdk.dev/docs/ai-sdk-ui/completion), and [`useObject()`](https://ai-sdk.dev/docs/ai-sdk-ui/object-generation).
|
|
14
|
+
|
|
15
|
+
Install the required packages to get started:
|
|
16
|
+
|
|
17
|
+
**npm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @mastra/ai-sdk@latest @ai-sdk/react ai
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**pnpm**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add @mastra/ai-sdk@latest @ai-sdk/react ai
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Yarn**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
yarn add @mastra/ai-sdk@latest @ai-sdk/react ai
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Bun**:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bun add @mastra/ai-sdk@latest @ai-sdk/react ai
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You're now ready to follow the integration guides and recipes below!
|
|
42
|
+
|
|
43
|
+
## Integration Guides
|
|
44
|
+
|
|
45
|
+
Typically, you'll set up API routes that stream Mastra content in AI SDK-compatible format, and then use those routes in AI SDK UI hooks like `useChat()`. Below you'll find two main approaches to achieve this:
|
|
46
|
+
|
|
47
|
+
- [Mastra's server](#mastras-server)
|
|
48
|
+
- [Framework-agnostic](#framework-agnostic)
|
|
49
|
+
|
|
50
|
+
Once you have your API routes set up, you can use them in the [`useChat()`](#usechat) hook.
|
|
51
|
+
|
|
52
|
+
### Mastra's server
|
|
53
|
+
|
|
54
|
+
Run Mastra as a standalone server and connect your frontend (e.g. using Vite + React) to its API endpoints. You'll be using Mastra's [custom API routes](https://mastra.ai/docs/server/custom-api-routes) feature for this.
|
|
55
|
+
|
|
56
|
+
> **Info:** Mastra's [**UI Dojo**](https://ui-dojo.mastra.ai/) is an example of this setup.
|
|
57
|
+
|
|
58
|
+
You can use [`chatRoute()`](https://mastra.ai/reference/ai-sdk/chat-route), [`workflowRoute()`](https://mastra.ai/reference/ai-sdk/workflow-route), and [`networkRoute()`](https://mastra.ai/reference/ai-sdk/network-route) to create API routes that stream Mastra content in AI SDK-compatible format. Once implemented, you can use these API routes in [`useChat()`](#usechat).
|
|
59
|
+
|
|
60
|
+
**chatRoute()**:
|
|
61
|
+
|
|
62
|
+
This example shows how to set up a chat route at the `/chat` endpoint that uses an agent with the ID `weatherAgent`.
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { Mastra } from '@mastra/core'
|
|
66
|
+
import { chatRoute } from '@mastra/ai-sdk'
|
|
67
|
+
|
|
68
|
+
export const mastra = new Mastra({
|
|
69
|
+
server: {
|
|
70
|
+
apiRoutes: [
|
|
71
|
+
chatRoute({
|
|
72
|
+
path: '/chat',
|
|
73
|
+
agent: 'weatherAgent',
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
You can also use dynamic agent routing, see the [`chatRoute()` reference documentation](https://mastra.ai/reference/ai-sdk/chat-route) for more details.
|
|
81
|
+
|
|
82
|
+
**workflowRoute()**:
|
|
83
|
+
|
|
84
|
+
This example shows how to set up a workflow route at the `/workflow` endpoint that uses a workflow with the ID `weatherWorkflow`.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { Mastra } from '@mastra/core'
|
|
88
|
+
import { workflowRoute } from '@mastra/ai-sdk'
|
|
89
|
+
|
|
90
|
+
export const mastra = new Mastra({
|
|
91
|
+
server: {
|
|
92
|
+
apiRoutes: [
|
|
93
|
+
workflowRoute({
|
|
94
|
+
path: '/workflow',
|
|
95
|
+
workflow: 'weatherWorkflow',
|
|
96
|
+
}),
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
You can also use dynamic workflow routing, see the [`workflowRoute()` reference documentation](https://mastra.ai/reference/ai-sdk/workflow-route) for more details.
|
|
103
|
+
|
|
104
|
+
> **Agent streaming in workflows:** When a workflow step pipes an agent's stream to the workflow writer (e.g., `await response.fullStream.pipeTo(writer)`), the agent's text chunks and tool calls are forwarded to the UI stream in real time, even when the agent runs inside workflow steps.
|
|
105
|
+
>
|
|
106
|
+
> See [Workflow Streaming](https://mastra.ai/docs/streaming/workflow-streaming) for more details.
|
|
107
|
+
|
|
108
|
+
**networkRoute()**:
|
|
109
|
+
|
|
110
|
+
This example shows how to set up a network route at the `/network` endpoint that uses an agent with the ID `weatherAgent`.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { Mastra } from '@mastra/core'
|
|
114
|
+
import { networkRoute } from '@mastra/ai-sdk'
|
|
115
|
+
|
|
116
|
+
export const mastra = new Mastra({
|
|
117
|
+
server: {
|
|
118
|
+
apiRoutes: [
|
|
119
|
+
networkRoute({
|
|
120
|
+
path: '/network',
|
|
121
|
+
agent: 'weatherAgent',
|
|
122
|
+
}),
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
You can also use dynamic network routing, see the [`networkRoute()` reference documentation](https://mastra.ai/reference/ai-sdk/network-route) for more details.
|
|
129
|
+
|
|
130
|
+
### Framework-agnostic
|
|
131
|
+
|
|
132
|
+
If you don't want to run Mastra's server and instead use frameworks like Next.js or Express, you can use the [`handleChatStream()`](https://mastra.ai/reference/ai-sdk/handle-chat-stream), [`handleWorkflowStream()`](https://mastra.ai/reference/ai-sdk/handle-workflow-stream), and [`handleNetworkStream()`](https://mastra.ai/reference/ai-sdk/handle-network-stream) functions in your own API route handlers.
|
|
133
|
+
|
|
134
|
+
They return a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
135
|
+
|
|
136
|
+
The examples below show you how to use them with Next.js App Router.
|
|
137
|
+
|
|
138
|
+
**handleChatStream()**:
|
|
139
|
+
|
|
140
|
+
This example shows how to set up a chat route at the `/chat` endpoint that uses an agent with the ID `weatherAgent`.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { handleChatStream } from '@mastra/ai-sdk'
|
|
144
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
145
|
+
import { mastra } from '@/src/mastra'
|
|
146
|
+
|
|
147
|
+
export async function POST(req: Request) {
|
|
148
|
+
const params = await req.json()
|
|
149
|
+
const stream = await handleChatStream({
|
|
150
|
+
mastra,
|
|
151
|
+
agentId: 'weatherAgent',
|
|
152
|
+
params,
|
|
153
|
+
})
|
|
154
|
+
return createUIMessageStreamResponse({ stream })
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**handleWorkflowStream()**:
|
|
159
|
+
|
|
160
|
+
This example shows how to set up a workflow route at the `/workflow` endpoint that uses a workflow with the ID `weatherWorkflow`.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { handleWorkflowStream } from '@mastra/ai-sdk'
|
|
164
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
165
|
+
import { mastra } from '@/src/mastra'
|
|
166
|
+
|
|
167
|
+
export async function POST(req: Request) {
|
|
168
|
+
const params = await req.json()
|
|
169
|
+
const stream = await handleWorkflowStream({
|
|
170
|
+
mastra,
|
|
171
|
+
workflowId: 'weatherWorkflow',
|
|
172
|
+
params,
|
|
173
|
+
})
|
|
174
|
+
return createUIMessageStreamResponse({ stream })
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**handleNetworkStream()**:
|
|
179
|
+
|
|
180
|
+
This example shows how to set up a network route at the `/network` endpoint that uses an agent with the ID `routingAgent`.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { handleNetworkStream } from '@mastra/ai-sdk'
|
|
184
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
185
|
+
import { mastra } from '@/src/mastra'
|
|
186
|
+
|
|
187
|
+
export async function POST(req: Request) {
|
|
188
|
+
const params = await req.json()
|
|
189
|
+
const stream = await handleNetworkStream({
|
|
190
|
+
mastra,
|
|
191
|
+
agentId: 'routingAgent',
|
|
192
|
+
params,
|
|
193
|
+
})
|
|
194
|
+
return createUIMessageStreamResponse({ stream })
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### `useChat()`
|
|
199
|
+
|
|
200
|
+
Whether you created API routes through [Mastra's server](#mastras-server) or used a [framework of your choice](#framework-agnostic), you can now use the API endpoints in the `useChat()` hook.
|
|
201
|
+
|
|
202
|
+
Assuming you set up a route at `/chat` that uses a weather agent, you can ask it questions as seen below. It's important that you set the correct `api` URL.
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { useChat } from '@ai-sdk/react'
|
|
206
|
+
import { useState } from 'react'
|
|
207
|
+
import { DefaultChatTransport } from 'ai'
|
|
208
|
+
|
|
209
|
+
export default function Chat() {
|
|
210
|
+
const [inputValue, setInputValue] = useState('')
|
|
211
|
+
const { messages, sendMessage } = useChat({
|
|
212
|
+
transport: new DefaultChatTransport({
|
|
213
|
+
api: 'http://localhost:4111/chat',
|
|
214
|
+
}),
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
const handleFormSubmit = (e: React.FormEvent) => {
|
|
218
|
+
e.preventDefault()
|
|
219
|
+
sendMessage({ text: inputValue })
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<div>
|
|
224
|
+
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
225
|
+
<form onSubmit={handleFormSubmit}>
|
|
226
|
+
<input
|
|
227
|
+
value={inputValue}
|
|
228
|
+
onChange={e => setInputValue(e.target.value)}
|
|
229
|
+
placeholder="Name of the city"
|
|
230
|
+
/>
|
|
231
|
+
</form>
|
|
232
|
+
</div>
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Use [`prepareSendMessagesRequest`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#transport.default-chat-transport.prepare-send-messages-request) to customize the request sent to the chat route, for example to pass additional configuration to the agent.
|
|
238
|
+
|
|
239
|
+
### `useCompletion()`
|
|
240
|
+
|
|
241
|
+
The `useCompletion()` hook handles single-turn completions between your frontend and a Mastra agent, allowing you to send a prompt and receive a streamed response over HTTP.
|
|
242
|
+
|
|
243
|
+
Your frontend could look like this:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { useCompletion } from '@ai-sdk/react'
|
|
247
|
+
|
|
248
|
+
export default function Page() {
|
|
249
|
+
const { completion, input, handleInputChange, handleSubmit } = useCompletion({
|
|
250
|
+
api: '/api/completion',
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
return (
|
|
254
|
+
<form onSubmit={handleSubmit}>
|
|
255
|
+
<input name="prompt" value={input} onChange={handleInputChange} id="input" />
|
|
256
|
+
<button type="submit">Submit</button>
|
|
257
|
+
<div>{completion}</div>
|
|
258
|
+
</form>
|
|
259
|
+
)
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Below are two approaches to implementing the backend:
|
|
264
|
+
|
|
265
|
+
**Mastra Server**:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
269
|
+
import { registerApiRoute } from '@mastra/core/server'
|
|
270
|
+
import { handleChatStream } from '@mastra/ai-sdk'
|
|
271
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
272
|
+
|
|
273
|
+
export const mastra = new Mastra({
|
|
274
|
+
server: {
|
|
275
|
+
apiRoutes: [
|
|
276
|
+
registerApiRoute('/completion', {
|
|
277
|
+
method: 'POST',
|
|
278
|
+
handler: async c => {
|
|
279
|
+
const { prompt } = await c.req.json()
|
|
280
|
+
const mastra = c.get('mastra')
|
|
281
|
+
const stream = await handleChatStream({
|
|
282
|
+
mastra,
|
|
283
|
+
agentId: 'weatherAgent',
|
|
284
|
+
params: {
|
|
285
|
+
messages: [
|
|
286
|
+
{
|
|
287
|
+
id: '1',
|
|
288
|
+
role: 'user',
|
|
289
|
+
parts: [
|
|
290
|
+
{
|
|
291
|
+
type: 'text',
|
|
292
|
+
text: prompt,
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
},
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
return createUIMessageStreamResponse({ stream })
|
|
301
|
+
},
|
|
302
|
+
}),
|
|
303
|
+
],
|
|
304
|
+
},
|
|
305
|
+
})
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
**Next.js**:
|
|
309
|
+
|
|
310
|
+
```ts
|
|
311
|
+
import { handleChatStream } from '@mastra/ai-sdk'
|
|
312
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
313
|
+
import { mastra } from '@/src/mastra'
|
|
314
|
+
|
|
315
|
+
// Allow streaming responses up to 30 seconds
|
|
316
|
+
export const maxDuration = 30
|
|
317
|
+
|
|
318
|
+
export async function POST(req: Request) {
|
|
319
|
+
const { prompt }: { prompt: string } = await req.json()
|
|
320
|
+
|
|
321
|
+
const stream = await handleChatStream({
|
|
322
|
+
mastra,
|
|
323
|
+
agentId: 'weatherAgent',
|
|
324
|
+
params: {
|
|
325
|
+
messages: [
|
|
326
|
+
{
|
|
327
|
+
id: '1',
|
|
328
|
+
role: 'user',
|
|
329
|
+
parts: [
|
|
330
|
+
{
|
|
331
|
+
type: 'text',
|
|
332
|
+
text: prompt,
|
|
333
|
+
},
|
|
334
|
+
],
|
|
335
|
+
},
|
|
336
|
+
],
|
|
337
|
+
},
|
|
338
|
+
})
|
|
339
|
+
return createUIMessageStreamResponse({ stream })
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Custom UI
|
|
344
|
+
|
|
345
|
+
Custom UI (also known as Generative UI) allows you to render custom React components based on data streamed from Mastra. Instead of displaying raw text or JSON, you can create visual components for tool outputs, workflow progress, agent network execution, and custom events.
|
|
346
|
+
|
|
347
|
+
Use Custom UI when you want to:
|
|
348
|
+
|
|
349
|
+
- Render tool outputs as visual components (e.g., a weather card instead of JSON)
|
|
350
|
+
- Display workflow step progress with status indicators
|
|
351
|
+
- Visualize agent network execution with step-by-step updates
|
|
352
|
+
- Show progress indicators or status updates during long-running operations
|
|
353
|
+
|
|
354
|
+
### Data part types
|
|
355
|
+
|
|
356
|
+
Mastra streams data to the frontend as "parts" within messages. Each part has a `type` that determines how to render it. The `@mastra/ai-sdk` package transforms Mastra streams into AI SDK-compatible [UI Message DataParts](https://ai-sdk.dev/docs/reference/ai-sdk-core/ui-message#datauipart).
|
|
357
|
+
|
|
358
|
+
| Data Part Type | Source | Description |
|
|
359
|
+
| -------------------- | ----------------------- | ---------------------------------------------------------------------------------- |
|
|
360
|
+
| `tool-{toolKey}` | AI SDK built-in | Tool invocation with states: `input-available`, `output-available`, `output-error` |
|
|
361
|
+
| `data-workflow` | `workflowRoute()` | Workflow execution with step inputs, outputs, and status |
|
|
362
|
+
| `data-network` | `networkRoute()` | Agent network execution with ordered steps and outputs |
|
|
363
|
+
| `data-tool-agent` | Nested agent in tool | Agent output streamed from within a tool's `execute()` |
|
|
364
|
+
| `data-tool-workflow` | Nested workflow in tool | Workflow output streamed from within a tool's `execute()` |
|
|
365
|
+
| `data-tool-network` | Nested network in tool | Network output streamed from within a tool's `execute()` |
|
|
366
|
+
| `data-{custom}` | `writer.custom()` | Custom events for progress indicators, status updates, etc. |
|
|
367
|
+
|
|
368
|
+
### Rendering tool outputs
|
|
369
|
+
|
|
370
|
+
AI SDK automatically creates `tool-{toolKey}` parts when an agent calls a tool. These parts include the tool's state and output, which you can use to render custom components.
|
|
371
|
+
|
|
372
|
+
The tool part cycles through states:
|
|
373
|
+
|
|
374
|
+
- `input-streaming`: Tool input is being streamed (when tool call streaming is enabled)
|
|
375
|
+
- `input-available`: Tool has been called with complete input, waiting for execution
|
|
376
|
+
- `output-available`: Tool execution completed with output
|
|
377
|
+
- `output-error`: Tool execution failed
|
|
378
|
+
|
|
379
|
+
Here's an example of rendering a weather tool's output as a custom `WeatherCard` component.
|
|
380
|
+
|
|
381
|
+
**Backend**:
|
|
382
|
+
|
|
383
|
+
Define a tool with an `outputSchema` so the frontend knows the shape of the data to render.
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
import { createTool } from '@mastra/core/tools'
|
|
387
|
+
import { z } from 'zod'
|
|
388
|
+
|
|
389
|
+
export const weatherTool = createTool({
|
|
390
|
+
id: 'get-weather',
|
|
391
|
+
description: 'Get current weather for a location',
|
|
392
|
+
inputSchema: z.object({
|
|
393
|
+
location: z.string().describe('The location to get the weather for'),
|
|
394
|
+
}),
|
|
395
|
+
outputSchema: z.object({
|
|
396
|
+
temperature: z.number(),
|
|
397
|
+
feelsLike: z.number(),
|
|
398
|
+
humidity: z.number(),
|
|
399
|
+
windSpeed: z.number(),
|
|
400
|
+
conditions: z.string(),
|
|
401
|
+
location: z.string(),
|
|
402
|
+
}),
|
|
403
|
+
execute: async inputData => {
|
|
404
|
+
const response = await fetch(
|
|
405
|
+
`https://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_API_KEY}&q=${inputData.location}`,
|
|
406
|
+
)
|
|
407
|
+
const data = await response.json()
|
|
408
|
+
return {
|
|
409
|
+
temperature: data.current.temp_c,
|
|
410
|
+
feelsLike: data.current.feelslike_c,
|
|
411
|
+
humidity: data.current.humidity,
|
|
412
|
+
windSpeed: data.current.wind_kph,
|
|
413
|
+
conditions: data.current.condition.text,
|
|
414
|
+
location: data.location.name,
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
})
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
**Frontend**:
|
|
421
|
+
|
|
422
|
+
Check for `tool-{toolKey}` parts in the message and render a custom component based on the tool's state and output.
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
import { useChat } from '@ai-sdk/react'
|
|
426
|
+
import { DefaultChatTransport } from 'ai'
|
|
427
|
+
import { WeatherCard } from './weather-card'
|
|
428
|
+
import { Loader } from './loader'
|
|
429
|
+
|
|
430
|
+
export function Chat() {
|
|
431
|
+
const { messages, sendMessage } = useChat({
|
|
432
|
+
transport: new DefaultChatTransport({
|
|
433
|
+
api: 'http://localhost:4111/chat/weatherAgent',
|
|
434
|
+
}),
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
return (
|
|
438
|
+
<div>
|
|
439
|
+
{messages.map(message => (
|
|
440
|
+
<div key={message.id}>
|
|
441
|
+
{message.parts.map((part, index) => {
|
|
442
|
+
// Handle user text messages
|
|
443
|
+
if (part.type === 'text' && message.role === 'user') {
|
|
444
|
+
return <p key={index}>{part.text}</p>
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Handle weather tool output
|
|
448
|
+
if (part.type === 'tool-weatherTool') {
|
|
449
|
+
switch (part.state) {
|
|
450
|
+
case 'input-available':
|
|
451
|
+
return <Loader key={index} />
|
|
452
|
+
case 'output-available':
|
|
453
|
+
return <WeatherCard key={index} {...part.output} />
|
|
454
|
+
case 'output-error':
|
|
455
|
+
return <div key={index}>Error: {part.errorText}</div>
|
|
456
|
+
default:
|
|
457
|
+
return null
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return null
|
|
462
|
+
})}
|
|
463
|
+
</div>
|
|
464
|
+
))}
|
|
465
|
+
</div>
|
|
466
|
+
)
|
|
467
|
+
}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
> **Tip:** The tool part type follows the pattern `tool-{toolKey}`, where `toolKey` is the key used when registering the tool with the agent. For example, if you register tools as `tools: { weatherTool }`, the part type will be `tool-weatherTool`.
|
|
471
|
+
|
|
472
|
+
### Rendering workflow data
|
|
473
|
+
|
|
474
|
+
When using `workflowRoute()` or `handleWorkflowStream()`, Mastra emits `data-workflow` parts that contain the workflow's execution state, including step statuses and outputs.
|
|
475
|
+
|
|
476
|
+
**Backend**:
|
|
477
|
+
|
|
478
|
+
Define a workflow with multiple steps that will emit `data-workflow` parts as it executes.
|
|
479
|
+
|
|
480
|
+
```typescript
|
|
481
|
+
import { createStep, createWorkflow } from '@mastra/core/workflows'
|
|
482
|
+
import { z } from 'zod'
|
|
483
|
+
|
|
484
|
+
const fetchWeather = createStep({
|
|
485
|
+
id: 'fetch-weather',
|
|
486
|
+
inputSchema: z.object({
|
|
487
|
+
location: z.string(),
|
|
488
|
+
}),
|
|
489
|
+
outputSchema: z.object({
|
|
490
|
+
temperature: z.number(),
|
|
491
|
+
conditions: z.string(),
|
|
492
|
+
}),
|
|
493
|
+
execute: async ({ inputData }) => {
|
|
494
|
+
// Fetch weather data...
|
|
495
|
+
return { temperature: 22, conditions: 'Sunny' }
|
|
496
|
+
},
|
|
497
|
+
})
|
|
498
|
+
|
|
499
|
+
const planActivities = createStep({
|
|
500
|
+
id: 'plan-activities',
|
|
501
|
+
inputSchema: z.object({
|
|
502
|
+
temperature: z.number(),
|
|
503
|
+
conditions: z.string(),
|
|
504
|
+
}),
|
|
505
|
+
outputSchema: z.object({
|
|
506
|
+
activities: z.string(),
|
|
507
|
+
}),
|
|
508
|
+
execute: async ({ inputData, mastra }) => {
|
|
509
|
+
const agent = mastra?.getAgent('activityAgent')
|
|
510
|
+
const response = await agent?.generate(
|
|
511
|
+
`Suggest activities for ${inputData.conditions} weather at ${inputData.temperature}°C`,
|
|
512
|
+
)
|
|
513
|
+
return { activities: response?.text || '' }
|
|
514
|
+
},
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
export const activitiesWorkflow = createWorkflow({
|
|
518
|
+
id: 'activities-workflow',
|
|
519
|
+
inputSchema: z.object({
|
|
520
|
+
location: z.string(),
|
|
521
|
+
}),
|
|
522
|
+
outputSchema: z.object({
|
|
523
|
+
activities: z.string(),
|
|
524
|
+
}),
|
|
525
|
+
})
|
|
526
|
+
.then(fetchWeather)
|
|
527
|
+
.then(planActivities)
|
|
528
|
+
|
|
529
|
+
activitiesWorkflow.commit()
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
Register the workflow with Mastra and expose it via `workflowRoute()` to stream workflow events to the frontend.
|
|
533
|
+
|
|
534
|
+
```typescript
|
|
535
|
+
import { Mastra } from '@mastra/core'
|
|
536
|
+
import { workflowRoute } from '@mastra/ai-sdk'
|
|
537
|
+
|
|
538
|
+
export const mastra = new Mastra({
|
|
539
|
+
workflows: { activitiesWorkflow },
|
|
540
|
+
server: {
|
|
541
|
+
apiRoutes: [
|
|
542
|
+
workflowRoute({
|
|
543
|
+
path: '/workflow/activitiesWorkflow',
|
|
544
|
+
workflow: 'activitiesWorkflow',
|
|
545
|
+
}),
|
|
546
|
+
],
|
|
547
|
+
},
|
|
548
|
+
})
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
**Frontend**:
|
|
552
|
+
|
|
553
|
+
Check for `data-workflow` parts and render each step's status and output using the `WorkflowDataPart` type for type safety.
|
|
554
|
+
|
|
555
|
+
```typescript
|
|
556
|
+
import { useChat } from '@ai-sdk/react'
|
|
557
|
+
import { DefaultChatTransport } from 'ai'
|
|
558
|
+
import type { WorkflowDataPart } from '@mastra/ai-sdk'
|
|
559
|
+
|
|
560
|
+
type WorkflowData = WorkflowDataPart['data']
|
|
561
|
+
type StepStatus = 'running' | 'success' | 'failed' | 'suspended' | 'waiting'
|
|
562
|
+
|
|
563
|
+
function StepIndicator({
|
|
564
|
+
name,
|
|
565
|
+
status,
|
|
566
|
+
output,
|
|
567
|
+
}: {
|
|
568
|
+
name: string
|
|
569
|
+
status: StepStatus
|
|
570
|
+
output: unknown
|
|
571
|
+
}) {
|
|
572
|
+
return (
|
|
573
|
+
<div className="step">
|
|
574
|
+
<div className="step-header">
|
|
575
|
+
<span>{name}</span>
|
|
576
|
+
<span className={`status status-${status}`}>{status}</span>
|
|
577
|
+
</div>
|
|
578
|
+
{status === 'success' && output && <pre>{JSON.stringify(output, null, 2)}</pre>}
|
|
579
|
+
</div>
|
|
580
|
+
)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
export function WorkflowChat() {
|
|
584
|
+
const { messages, sendMessage, status } = useChat({
|
|
585
|
+
transport: new DefaultChatTransport({
|
|
586
|
+
api: 'http://localhost:4111/workflow/activitiesWorkflow',
|
|
587
|
+
prepareSendMessagesRequest: ({ messages }) => ({
|
|
588
|
+
body: {
|
|
589
|
+
inputData: {
|
|
590
|
+
location: messages[messages.length - 1]?.parts[0]?.text,
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
}),
|
|
594
|
+
}),
|
|
595
|
+
})
|
|
596
|
+
|
|
597
|
+
return (
|
|
598
|
+
<div>
|
|
599
|
+
{messages.map(message => (
|
|
600
|
+
<div key={message.id}>
|
|
601
|
+
{message.parts.map((part, index) => {
|
|
602
|
+
if (part.type === 'data-workflow') {
|
|
603
|
+
const workflowData = part.data as WorkflowData
|
|
604
|
+
const steps = Object.values(workflowData.steps)
|
|
605
|
+
|
|
606
|
+
return (
|
|
607
|
+
<div key={index} className="workflow-progress">
|
|
608
|
+
<h3>Workflow: {workflowData.name}</h3>
|
|
609
|
+
<p>Status: {workflowData.status}</p>
|
|
610
|
+
{steps.map(step => (
|
|
611
|
+
<StepIndicator
|
|
612
|
+
key={step.name}
|
|
613
|
+
name={step.name}
|
|
614
|
+
status={step.status}
|
|
615
|
+
output={step.output}
|
|
616
|
+
/>
|
|
617
|
+
))}
|
|
618
|
+
</div>
|
|
619
|
+
)
|
|
620
|
+
}
|
|
621
|
+
return null
|
|
622
|
+
})}
|
|
623
|
+
</div>
|
|
624
|
+
))}
|
|
625
|
+
</div>
|
|
626
|
+
)
|
|
627
|
+
}
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
For more details on workflow streaming, see [Workflow Streaming](https://mastra.ai/docs/streaming/workflow-streaming).
|
|
631
|
+
|
|
632
|
+
### Rendering network data
|
|
633
|
+
|
|
634
|
+
When using `networkRoute()` or `handleNetworkStream()`, Mastra emits `data-network` parts that contain the agent network's execution state, including which agents were called and their outputs.
|
|
635
|
+
|
|
636
|
+
**Backend**:
|
|
637
|
+
|
|
638
|
+
Register agents with Mastra and expose the routing agent via `networkRoute()` to stream network execution events to the frontend.
|
|
639
|
+
|
|
640
|
+
```typescript
|
|
641
|
+
import { Mastra } from '@mastra/core'
|
|
642
|
+
import { networkRoute } from '@mastra/ai-sdk'
|
|
643
|
+
|
|
644
|
+
export const mastra = new Mastra({
|
|
645
|
+
agents: { routingAgent, researchAgent, weatherAgent },
|
|
646
|
+
server: {
|
|
647
|
+
apiRoutes: [
|
|
648
|
+
networkRoute({
|
|
649
|
+
path: '/network',
|
|
650
|
+
agent: 'routingAgent',
|
|
651
|
+
}),
|
|
652
|
+
],
|
|
653
|
+
},
|
|
654
|
+
})
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
**Frontend**:
|
|
658
|
+
|
|
659
|
+
Check for `data-network` parts and render each agent's execution step using the `NetworkDataPart` type for type safety.
|
|
660
|
+
|
|
661
|
+
```typescript
|
|
662
|
+
import { useChat } from '@ai-sdk/react'
|
|
663
|
+
import { DefaultChatTransport } from 'ai'
|
|
664
|
+
import type { NetworkDataPart } from '@mastra/ai-sdk'
|
|
665
|
+
|
|
666
|
+
type NetworkData = NetworkDataPart['data']
|
|
667
|
+
|
|
668
|
+
function AgentStep({ step }: { step: NetworkData['steps'][number] }) {
|
|
669
|
+
return (
|
|
670
|
+
<div className="agent-step">
|
|
671
|
+
<div className="step-header">
|
|
672
|
+
<span className="agent-name">{step.name}</span>
|
|
673
|
+
<span className={`status status-${step.status}`}>{step.status}</span>
|
|
674
|
+
</div>
|
|
675
|
+
{step.input && (
|
|
676
|
+
<div className="step-input">
|
|
677
|
+
<strong>Input:</strong>
|
|
678
|
+
<pre>{JSON.stringify(step.input, null, 2)}</pre>
|
|
679
|
+
</div>
|
|
680
|
+
)}
|
|
681
|
+
{step.output && (
|
|
682
|
+
<div className="step-output">
|
|
683
|
+
<strong>Output:</strong>
|
|
684
|
+
<pre>
|
|
685
|
+
{typeof step.output === 'string' ? step.output : JSON.stringify(step.output, null, 2)}
|
|
686
|
+
</pre>
|
|
687
|
+
</div>
|
|
688
|
+
)}
|
|
689
|
+
</div>
|
|
690
|
+
)
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
export function NetworkChat() {
|
|
694
|
+
const { messages, sendMessage, status } = useChat({
|
|
695
|
+
transport: new DefaultChatTransport({
|
|
696
|
+
api: 'http://localhost:4111/network',
|
|
697
|
+
}),
|
|
698
|
+
})
|
|
699
|
+
|
|
700
|
+
return (
|
|
701
|
+
<div>
|
|
702
|
+
{messages.map(message => (
|
|
703
|
+
<div key={message.id}>
|
|
704
|
+
{message.parts.map((part, index) => {
|
|
705
|
+
if (part.type === 'data-network') {
|
|
706
|
+
const networkData = part.data as NetworkData
|
|
707
|
+
|
|
708
|
+
return (
|
|
709
|
+
<div key={index} className="network-execution">
|
|
710
|
+
<div className="network-header">
|
|
711
|
+
<h3>Agent Network: {networkData.name}</h3>
|
|
712
|
+
<span className={`status status-${networkData.status}`}>
|
|
713
|
+
{networkData.status}
|
|
714
|
+
</span>
|
|
715
|
+
</div>
|
|
716
|
+
<div className="network-steps">
|
|
717
|
+
{networkData.steps.map((step, stepIndex) => (
|
|
718
|
+
<AgentStep key={stepIndex} step={step} />
|
|
719
|
+
))}
|
|
720
|
+
</div>
|
|
721
|
+
</div>
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
return null
|
|
725
|
+
})}
|
|
726
|
+
</div>
|
|
727
|
+
))}
|
|
728
|
+
</div>
|
|
729
|
+
)
|
|
730
|
+
}
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
For more details on agent networks, see [Agent Networks](https://mastra.ai/docs/agents/networks).
|
|
734
|
+
|
|
735
|
+
### Custom events
|
|
736
|
+
|
|
737
|
+
Use `writer.custom()` within a tool's `execute()` function to emit custom data parts. This is useful for progress indicators, status updates, or any custom UI updates during tool execution.
|
|
738
|
+
|
|
739
|
+
Custom event types must start with `data-` to be recognized as data parts.
|
|
740
|
+
|
|
741
|
+
> **Warning:** You must `await` the `writer.custom()` call, otherwise you may encounter a `WritableStream is locked` error.
|
|
742
|
+
|
|
743
|
+
**Backend**:
|
|
744
|
+
|
|
745
|
+
Use `writer.custom()` inside the tool's `execute()` function to emit custom `data-` prefixed events at different stages of execution.
|
|
746
|
+
|
|
747
|
+
```typescript
|
|
748
|
+
import { createTool } from '@mastra/core/tools'
|
|
749
|
+
import { z } from 'zod'
|
|
750
|
+
|
|
751
|
+
export const taskTool = createTool({
|
|
752
|
+
id: 'process-task',
|
|
753
|
+
description: 'Process a task with progress updates',
|
|
754
|
+
inputSchema: z.object({
|
|
755
|
+
task: z.string().describe('The task to process'),
|
|
756
|
+
}),
|
|
757
|
+
outputSchema: z.object({
|
|
758
|
+
result: z.string(),
|
|
759
|
+
status: z.string(),
|
|
760
|
+
}),
|
|
761
|
+
execute: async (inputData, context) => {
|
|
762
|
+
const { task } = inputData
|
|
763
|
+
|
|
764
|
+
// Emit "in progress" custom event
|
|
765
|
+
await context?.writer?.custom({
|
|
766
|
+
type: 'data-tool-progress',
|
|
767
|
+
data: {
|
|
768
|
+
status: 'in-progress',
|
|
769
|
+
message: 'Gathering information...',
|
|
770
|
+
},
|
|
771
|
+
})
|
|
772
|
+
|
|
773
|
+
// Simulate work
|
|
774
|
+
await new Promise(resolve => setTimeout(resolve, 3000))
|
|
775
|
+
|
|
776
|
+
// Emit "done" custom event
|
|
777
|
+
await context?.writer?.custom({
|
|
778
|
+
type: 'data-tool-progress',
|
|
779
|
+
data: {
|
|
780
|
+
status: 'done',
|
|
781
|
+
message: `Successfully processed "${task}"`,
|
|
782
|
+
},
|
|
783
|
+
})
|
|
784
|
+
|
|
785
|
+
return {
|
|
786
|
+
result: `Task "${task}" has been completed successfully!`,
|
|
787
|
+
status: 'completed',
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
})
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
**Frontend**:
|
|
794
|
+
|
|
795
|
+
Filter message parts for your custom event type and render a progress indicator that updates as new events arrive.
|
|
796
|
+
|
|
797
|
+
```typescript
|
|
798
|
+
import { useChat } from '@ai-sdk/react'
|
|
799
|
+
import { DefaultChatTransport } from 'ai'
|
|
800
|
+
import { useMemo } from 'react'
|
|
801
|
+
|
|
802
|
+
type ProgressData = {
|
|
803
|
+
status: 'in-progress' | 'done'
|
|
804
|
+
message: string
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function ProgressIndicator({ progress }: { progress: ProgressData }) {
|
|
808
|
+
return (
|
|
809
|
+
<div className="progress-indicator">
|
|
810
|
+
{progress.status === 'in-progress' ? (
|
|
811
|
+
<span className="spinner" />
|
|
812
|
+
) : (
|
|
813
|
+
<span className="check-icon" />
|
|
814
|
+
)}
|
|
815
|
+
<span className={`status-${progress.status}`}>{progress.message}</span>
|
|
816
|
+
</div>
|
|
817
|
+
)
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
export function TaskChat() {
|
|
821
|
+
const { messages, sendMessage } = useChat({
|
|
822
|
+
transport: new DefaultChatTransport({
|
|
823
|
+
api: 'http://localhost:4111/chat/taskAgent',
|
|
824
|
+
}),
|
|
825
|
+
})
|
|
826
|
+
|
|
827
|
+
// Extract the latest progress event from messages
|
|
828
|
+
const latestProgress = useMemo(() => {
|
|
829
|
+
const allProgressParts: ProgressData[] = []
|
|
830
|
+
messages.forEach(message => {
|
|
831
|
+
message.parts.forEach(part => {
|
|
832
|
+
if (part.type === 'data-tool-progress') {
|
|
833
|
+
allProgressParts.push(part.data as ProgressData)
|
|
834
|
+
}
|
|
835
|
+
})
|
|
836
|
+
})
|
|
837
|
+
return allProgressParts[allProgressParts.length - 1]
|
|
838
|
+
}, [messages])
|
|
839
|
+
|
|
840
|
+
return (
|
|
841
|
+
<div>
|
|
842
|
+
{latestProgress && <ProgressIndicator progress={latestProgress} />}
|
|
843
|
+
{messages.map(message => (
|
|
844
|
+
<div key={message.id}>
|
|
845
|
+
{message.parts.map((part, index) => {
|
|
846
|
+
if (part.type === 'text') {
|
|
847
|
+
return <p key={index}>{part.text}</p>
|
|
848
|
+
}
|
|
849
|
+
return null
|
|
850
|
+
})}
|
|
851
|
+
</div>
|
|
852
|
+
))}
|
|
853
|
+
</div>
|
|
854
|
+
)
|
|
855
|
+
}
|
|
856
|
+
```
|
|
857
|
+
|
|
858
|
+
### Tool streaming
|
|
859
|
+
|
|
860
|
+
Tools can also stream data using `context.writer.write()` for lower-level control, or pipe an agent's stream directly to the tool's writer. For more details, see [Tool Streaming](https://mastra.ai/docs/streaming/tool-streaming).
|
|
861
|
+
|
|
862
|
+
### Examples
|
|
863
|
+
|
|
864
|
+
For live examples of Custom UI patterns, visit [Mastra's UI Dojo](https://ui-dojo.mastra.ai/). The repository includes implementations for:
|
|
865
|
+
|
|
866
|
+
- [Generative UIs](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/generative-user-interfaces.tsx) - Custom components for tool outputs
|
|
867
|
+
- [Workflows](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/workflow.tsx) - Workflow step visualization
|
|
868
|
+
- [Agent Networks](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/network.tsx) - Network execution display
|
|
869
|
+
- [Custom Events](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/generative-user-interfaces-with-custom-events.tsx) - Progress indicators with custom events
|
|
870
|
+
|
|
871
|
+
## Recipes
|
|
872
|
+
|
|
873
|
+
### Stream transformations
|
|
874
|
+
|
|
875
|
+
To manually transform Mastra's streams to AI SDK-compatible format, use the [`toAISdkStream()`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-stream) utility. See the [examples](https://mastra.ai/reference/ai-sdk/to-ai-sdk-stream) for concrete usage patterns.
|
|
876
|
+
|
|
877
|
+
### Loading historical messages
|
|
878
|
+
|
|
879
|
+
When loading messages from Mastra's memory to display in a chat UI, use [`toAISdkV5Messages()`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-v5-messages) or [`toAISdkV4Messages()`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-v4-messages) to convert them to the appropriate AI SDK format for `useChat()`'s `initialMessages`.
|
|
880
|
+
|
|
881
|
+
### Passing additional data
|
|
882
|
+
|
|
883
|
+
[`sendMessage()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#send-message) allows you to pass additional data from the frontend to Mastra. This data can then be used on the server as [`RequestContext`](https://mastra.ai/docs/server/request-context).
|
|
884
|
+
|
|
885
|
+
Here's an example of the frontend code:
|
|
886
|
+
|
|
887
|
+
```typescript
|
|
888
|
+
import { useChat } from '@ai-sdk/react'
|
|
889
|
+
import { useState } from 'react'
|
|
890
|
+
import { DefaultChatTransport } from 'ai'
|
|
891
|
+
|
|
892
|
+
export function ChatAdditional() {
|
|
893
|
+
const [inputValue, setInputValue] = useState('')
|
|
894
|
+
const { messages, sendMessage } = useChat({
|
|
895
|
+
transport: new DefaultChatTransport({
|
|
896
|
+
api: 'http://localhost:4111/chat-extra',
|
|
897
|
+
}),
|
|
898
|
+
})
|
|
899
|
+
|
|
900
|
+
const handleFormSubmit = (e: React.FormEvent) => {
|
|
901
|
+
e.preventDefault()
|
|
902
|
+
sendMessage(
|
|
903
|
+
{ text: inputValue },
|
|
904
|
+
{
|
|
905
|
+
body: {
|
|
906
|
+
data: {
|
|
907
|
+
userId: 'user123',
|
|
908
|
+
preferences: {
|
|
909
|
+
language: 'en',
|
|
910
|
+
temperature: 'celsius',
|
|
911
|
+
},
|
|
912
|
+
},
|
|
913
|
+
},
|
|
914
|
+
},
|
|
915
|
+
)
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
return (
|
|
919
|
+
<div>
|
|
920
|
+
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
921
|
+
<form onSubmit={handleFormSubmit}>
|
|
922
|
+
<input
|
|
923
|
+
value={inputValue}
|
|
924
|
+
onChange={e => setInputValue(e.target.value)}
|
|
925
|
+
placeholder="Name of the city"
|
|
926
|
+
/>
|
|
927
|
+
</form>
|
|
928
|
+
</div>
|
|
929
|
+
)
|
|
930
|
+
}
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
Two examples on how to implement the backend portion of it.
|
|
934
|
+
|
|
935
|
+
**Mastra Server**:
|
|
936
|
+
|
|
937
|
+
Add a `chatRoute()` to your Mastra configuration like shown above. Then, add a server-level middleware:
|
|
938
|
+
|
|
939
|
+
```typescript
|
|
940
|
+
import { Mastra } from '@mastra/core'
|
|
941
|
+
|
|
942
|
+
export const mastra = new Mastra({
|
|
943
|
+
server: {
|
|
944
|
+
middleware: [
|
|
945
|
+
async (c, next) => {
|
|
946
|
+
const requestContext = c.get('requestContext')
|
|
947
|
+
|
|
948
|
+
if (c.req.method === 'POST') {
|
|
949
|
+
const clonedReq = c.req.raw.clone()
|
|
950
|
+
const body = await clonedReq.json()
|
|
951
|
+
|
|
952
|
+
if (body?.data) {
|
|
953
|
+
for (const [key, value] of Object.entries(body.data)) {
|
|
954
|
+
requestContext.set(key, value)
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
await next()
|
|
959
|
+
},
|
|
960
|
+
],
|
|
961
|
+
},
|
|
962
|
+
})
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
> **Info:** You can access this data in your tools via the `requestContext` parameter. See the [Request Context documentation](https://mastra.ai/docs/server/request-context) for more details.
|
|
966
|
+
|
|
967
|
+
**Next.js**:
|
|
968
|
+
|
|
969
|
+
```typescript
|
|
970
|
+
import { handleChatStream } from '@mastra/ai-sdk'
|
|
971
|
+
import { RequestContext } from '@mastra/core/request-context'
|
|
972
|
+
import { createUIMessageStreamResponse } from 'ai'
|
|
973
|
+
import { mastra } from '@/src/mastra'
|
|
974
|
+
|
|
975
|
+
export async function POST(req: Request) {
|
|
976
|
+
const { messages, data } = await req.json()
|
|
977
|
+
|
|
978
|
+
const requestContext = new RequestContext()
|
|
979
|
+
|
|
980
|
+
if (data) {
|
|
981
|
+
for (const [key, value] of Object.entries(data)) {
|
|
982
|
+
requestContext.set(key, value)
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
const stream = await handleChatStream({
|
|
987
|
+
mastra,
|
|
988
|
+
agentId: 'weatherAgent',
|
|
989
|
+
params: {
|
|
990
|
+
messages,
|
|
991
|
+
requestContext,
|
|
992
|
+
},
|
|
993
|
+
})
|
|
994
|
+
return createUIMessageStreamResponse({ stream })
|
|
995
|
+
}
|
|
996
|
+
```
|
|
997
|
+
|
|
998
|
+
### Workflow suspend/resume with user approval
|
|
999
|
+
|
|
1000
|
+
Workflows can suspend execution and wait for user input before continuing. This is useful for approval flows, confirmations, or any human-in-the-loop scenario.
|
|
1001
|
+
|
|
1002
|
+
The workflow uses:
|
|
1003
|
+
|
|
1004
|
+
- `suspendSchema` / `resumeSchema` - Define the data structure for suspend payload and resume input
|
|
1005
|
+
- `suspend()` - Pauses the workflow and sends the suspend payload to the UI
|
|
1006
|
+
- `resumeData` - Contains the user's response when the workflow resumes
|
|
1007
|
+
- `bail()` - Exits the workflow early (e.g., when user rejects)
|
|
1008
|
+
|
|
1009
|
+
**Backend**:
|
|
1010
|
+
|
|
1011
|
+
Create a workflow step that suspends for approval. The step checks `resumeData` to determine if it's resuming, and calls `suspend()` on first execution.
|
|
1012
|
+
|
|
1013
|
+
```typescript
|
|
1014
|
+
import { createStep, createWorkflow } from '@mastra/core/workflows'
|
|
1015
|
+
import { z } from 'zod'
|
|
1016
|
+
|
|
1017
|
+
const requestApproval = createStep({
|
|
1018
|
+
id: 'request-approval',
|
|
1019
|
+
inputSchema: z.object({ requestId: z.string(), summary: z.string() }),
|
|
1020
|
+
outputSchema: z.object({
|
|
1021
|
+
approved: z.boolean(),
|
|
1022
|
+
requestId: z.string(),
|
|
1023
|
+
approvedBy: z.string().optional(),
|
|
1024
|
+
}),
|
|
1025
|
+
resumeSchema: z.object({
|
|
1026
|
+
approved: z.boolean(),
|
|
1027
|
+
approverName: z.string().optional(),
|
|
1028
|
+
}),
|
|
1029
|
+
suspendSchema: z.object({
|
|
1030
|
+
message: z.string(),
|
|
1031
|
+
requestId: z.string(),
|
|
1032
|
+
}),
|
|
1033
|
+
execute: async ({ inputData, resumeData, suspend, bail }) => {
|
|
1034
|
+
// User rejected - bail out
|
|
1035
|
+
if (resumeData?.approved === false) {
|
|
1036
|
+
return bail({ message: 'Request rejected' })
|
|
1037
|
+
}
|
|
1038
|
+
// User approved - continue
|
|
1039
|
+
if (resumeData?.approved) {
|
|
1040
|
+
return {
|
|
1041
|
+
approved: true,
|
|
1042
|
+
requestId: inputData.requestId,
|
|
1043
|
+
approvedBy: resumeData.approverName || 'User',
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
// First execution - suspend and wait
|
|
1047
|
+
return await suspend({
|
|
1048
|
+
message: `Please approve: ${inputData.summary}`,
|
|
1049
|
+
requestId: inputData.requestId,
|
|
1050
|
+
})
|
|
1051
|
+
},
|
|
1052
|
+
})
|
|
1053
|
+
|
|
1054
|
+
export const approvalWorkflow = createWorkflow({
|
|
1055
|
+
id: 'approval-workflow',
|
|
1056
|
+
inputSchema: z.object({ requestId: z.string(), summary: z.string() }),
|
|
1057
|
+
outputSchema: z.object({
|
|
1058
|
+
approved: z.boolean(),
|
|
1059
|
+
requestId: z.string(),
|
|
1060
|
+
approvedBy: z.string().optional(),
|
|
1061
|
+
}),
|
|
1062
|
+
}).then(requestApproval)
|
|
1063
|
+
|
|
1064
|
+
approvalWorkflow.commit()
|
|
1065
|
+
```
|
|
1066
|
+
|
|
1067
|
+
Register the workflow. Storage is required for suspend/resume to persist state.
|
|
1068
|
+
|
|
1069
|
+
```typescript
|
|
1070
|
+
import { Mastra } from '@mastra/core'
|
|
1071
|
+
import { workflowRoute } from '@mastra/ai-sdk'
|
|
1072
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
1073
|
+
|
|
1074
|
+
export const mastra = new Mastra({
|
|
1075
|
+
workflows: { approvalWorkflow },
|
|
1076
|
+
storage: new LibSQLStore({
|
|
1077
|
+
url: 'file:../mastra.db',
|
|
1078
|
+
}),
|
|
1079
|
+
server: {
|
|
1080
|
+
apiRoutes: [
|
|
1081
|
+
workflowRoute({ path: '/workflow/approvalWorkflow', workflow: 'approvalWorkflow' }),
|
|
1082
|
+
],
|
|
1083
|
+
},
|
|
1084
|
+
})
|
|
1085
|
+
```
|
|
1086
|
+
|
|
1087
|
+
**Frontend**:
|
|
1088
|
+
|
|
1089
|
+
Detect when the workflow is suspended and send resume data with `runId`, `step`, and `resumeData`.
|
|
1090
|
+
|
|
1091
|
+
```typescript
|
|
1092
|
+
import { useChat } from '@ai-sdk/react'
|
|
1093
|
+
import { DefaultChatTransport } from 'ai'
|
|
1094
|
+
import { useMemo, useState } from 'react'
|
|
1095
|
+
import type { WorkflowDataPart } from '@mastra/ai-sdk'
|
|
1096
|
+
|
|
1097
|
+
type WorkflowData = WorkflowDataPart['data']
|
|
1098
|
+
|
|
1099
|
+
export function ApprovalWorkflow() {
|
|
1100
|
+
const [requestId, setRequestId] = useState('')
|
|
1101
|
+
const [summary, setSummary] = useState('')
|
|
1102
|
+
|
|
1103
|
+
const { messages, sendMessage, setMessages, status } = useChat({
|
|
1104
|
+
transport: new DefaultChatTransport({
|
|
1105
|
+
api: 'http://localhost:4111/workflow/approvalWorkflow',
|
|
1106
|
+
prepareSendMessagesRequest: ({ messages }) => {
|
|
1107
|
+
const lastMessage = messages[messages.length - 1]
|
|
1108
|
+
const text = lastMessage.parts.find(p => p.type === 'text')?.text
|
|
1109
|
+
const metadata = lastMessage.metadata as Record<string, string>
|
|
1110
|
+
|
|
1111
|
+
// Resuming: send runId, step, and resumeData
|
|
1112
|
+
if (text === 'Approve' || text === 'Reject') {
|
|
1113
|
+
return {
|
|
1114
|
+
body: {
|
|
1115
|
+
runId: metadata.runId,
|
|
1116
|
+
step: 'request-approval',
|
|
1117
|
+
resumeData: { approved: text === 'Approve' },
|
|
1118
|
+
},
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
// Starting: send inputData
|
|
1122
|
+
return {
|
|
1123
|
+
body: { inputData: { requestId: metadata.requestId, summary: metadata.summary } },
|
|
1124
|
+
}
|
|
1125
|
+
},
|
|
1126
|
+
}),
|
|
1127
|
+
})
|
|
1128
|
+
|
|
1129
|
+
// Find suspended workflow
|
|
1130
|
+
const suspended = useMemo(() => {
|
|
1131
|
+
for (const m of messages) {
|
|
1132
|
+
for (const p of m.parts) {
|
|
1133
|
+
if (p.type === 'data-workflow' && (p.data as WorkflowData).status === 'suspended') {
|
|
1134
|
+
return { data: p.data as WorkflowData, runId: p.id }
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return null
|
|
1139
|
+
}, [messages])
|
|
1140
|
+
|
|
1141
|
+
const handleApprove = () => {
|
|
1142
|
+
setMessages([])
|
|
1143
|
+
sendMessage({ text: 'Approve', metadata: { runId: suspended?.runId } })
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const handleReject = () => {
|
|
1147
|
+
setMessages([])
|
|
1148
|
+
sendMessage({ text: 'Reject', metadata: { runId: suspended?.runId } })
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
return (
|
|
1152
|
+
<div>
|
|
1153
|
+
{!suspended ? (
|
|
1154
|
+
<form
|
|
1155
|
+
onSubmit={e => {
|
|
1156
|
+
e.preventDefault()
|
|
1157
|
+
setMessages([])
|
|
1158
|
+
sendMessage({ text: 'Start', metadata: { requestId, summary } })
|
|
1159
|
+
}}
|
|
1160
|
+
>
|
|
1161
|
+
<input
|
|
1162
|
+
value={requestId}
|
|
1163
|
+
onChange={e => setRequestId(e.target.value)}
|
|
1164
|
+
placeholder="Request ID"
|
|
1165
|
+
/>
|
|
1166
|
+
<input value={summary} onChange={e => setSummary(e.target.value)} placeholder="Summary" />
|
|
1167
|
+
<button type="submit" disabled={status !== 'ready'}>
|
|
1168
|
+
Submit
|
|
1169
|
+
</button>
|
|
1170
|
+
</form>
|
|
1171
|
+
) : (
|
|
1172
|
+
<div>
|
|
1173
|
+
<p>
|
|
1174
|
+
{
|
|
1175
|
+
(suspended.data.steps['request-approval']?.suspendPayload as { message: string })
|
|
1176
|
+
?.message
|
|
1177
|
+
}
|
|
1178
|
+
</p>
|
|
1179
|
+
<button onClick={handleApprove}>Approve</button>
|
|
1180
|
+
<button onClick={handleReject}>Reject</button>
|
|
1181
|
+
</div>
|
|
1182
|
+
)}
|
|
1183
|
+
</div>
|
|
1184
|
+
)
|
|
1185
|
+
}
|
|
1186
|
+
```
|
|
1187
|
+
|
|
1188
|
+
Key points:
|
|
1189
|
+
|
|
1190
|
+
- The suspend payload is accessible via `step.suspendPayload`
|
|
1191
|
+
- To resume, send `runId`, `step` (the step ID), and `resumeData` in the request body
|
|
1192
|
+
- Storage must be configured for suspend/resume to persist workflow state
|
|
1193
|
+
|
|
1194
|
+
For a complete implementation, see the [workflow-suspend-resume example](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/workflow-suspend-resume.tsx) in UI Dojo.
|
|
1195
|
+
|
|
1196
|
+
### Nested agent streams in tools
|
|
1197
|
+
|
|
1198
|
+
Tools can call agents internally and stream the agent's output back to the UI. This creates `data-tool-agent` parts that can be rendered alongside the tool's final output.
|
|
1199
|
+
|
|
1200
|
+
The pattern uses:
|
|
1201
|
+
|
|
1202
|
+
- `context.mastra.getAgent()` - Get an agent instance from within a tool
|
|
1203
|
+
- `agent.stream()` - Stream the agent's response
|
|
1204
|
+
- `stream.fullStream.pipeTo(context.writer)` - Pipe the agent's stream to the tool's writer
|
|
1205
|
+
|
|
1206
|
+
**Backend**:
|
|
1207
|
+
|
|
1208
|
+
Create a tool that calls an agent and pipes its stream to the tool's writer.
|
|
1209
|
+
|
|
1210
|
+
```typescript
|
|
1211
|
+
import { createTool } from '@mastra/core/tools'
|
|
1212
|
+
import { z } from 'zod'
|
|
1213
|
+
|
|
1214
|
+
export const nestedAgentTool = createTool({
|
|
1215
|
+
id: 'nested-agent-stream',
|
|
1216
|
+
description: 'Analyze weather using a nested agent',
|
|
1217
|
+
inputSchema: z.object({
|
|
1218
|
+
city: z.string().describe('The city to analyze'),
|
|
1219
|
+
}),
|
|
1220
|
+
outputSchema: z.object({
|
|
1221
|
+
summary: z.string(),
|
|
1222
|
+
}),
|
|
1223
|
+
execute: async (inputData, context) => {
|
|
1224
|
+
const agent = context?.mastra?.getAgent('weatherAgent')
|
|
1225
|
+
if (!agent) {
|
|
1226
|
+
return { summary: 'Weather agent not available' }
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
const stream = await agent.stream(
|
|
1230
|
+
`Analyze the weather in ${inputData.city} and provide a summary.`,
|
|
1231
|
+
)
|
|
1232
|
+
|
|
1233
|
+
// Pipe the agent's stream to emit data-tool-agent parts
|
|
1234
|
+
await stream.fullStream.pipeTo(context!.writer!)
|
|
1235
|
+
|
|
1236
|
+
return { summary: (await stream.text) ?? 'No summary available' }
|
|
1237
|
+
},
|
|
1238
|
+
})
|
|
1239
|
+
```
|
|
1240
|
+
|
|
1241
|
+
Create an agent that uses this tool.
|
|
1242
|
+
|
|
1243
|
+
```typescript
|
|
1244
|
+
import { Agent } from '@mastra/core/agent'
|
|
1245
|
+
import { nestedAgentTool } from '../tools/nested-agent-tool'
|
|
1246
|
+
|
|
1247
|
+
export const forecastAgent = new Agent({
|
|
1248
|
+
id: 'forecast-agent',
|
|
1249
|
+
instructions: 'Use the nested-agent-stream tool when asked about weather.',
|
|
1250
|
+
model: 'openai/gpt-4o-mini',
|
|
1251
|
+
tools: { nestedAgentTool },
|
|
1252
|
+
})
|
|
1253
|
+
```
|
|
1254
|
+
|
|
1255
|
+
**Frontend**:
|
|
1256
|
+
|
|
1257
|
+
Handle `data-tool-agent` parts to display the nested agent's streamed output.
|
|
1258
|
+
|
|
1259
|
+
```typescript
|
|
1260
|
+
import { useChat } from '@ai-sdk/react'
|
|
1261
|
+
import { DefaultChatTransport } from 'ai'
|
|
1262
|
+
import { useState } from 'react'
|
|
1263
|
+
import type { AgentDataPart } from '@mastra/ai-sdk'
|
|
1264
|
+
|
|
1265
|
+
export function NestedAgentChat() {
|
|
1266
|
+
const [input, setInput] = useState('')
|
|
1267
|
+
const { messages, sendMessage, status } = useChat({
|
|
1268
|
+
transport: new DefaultChatTransport({
|
|
1269
|
+
api: 'http://localhost:4111/chat/forecastAgent',
|
|
1270
|
+
}),
|
|
1271
|
+
})
|
|
1272
|
+
|
|
1273
|
+
return (
|
|
1274
|
+
<div>
|
|
1275
|
+
<form
|
|
1276
|
+
onSubmit={e => {
|
|
1277
|
+
e.preventDefault()
|
|
1278
|
+
sendMessage({ text: input })
|
|
1279
|
+
setInput('')
|
|
1280
|
+
}}
|
|
1281
|
+
>
|
|
1282
|
+
<input value={input} onChange={e => setInput(e.target.value)} placeholder="Enter a city" />
|
|
1283
|
+
<button type="submit" disabled={status !== 'ready'}>
|
|
1284
|
+
Get Forecast
|
|
1285
|
+
</button>
|
|
1286
|
+
</form>
|
|
1287
|
+
|
|
1288
|
+
{messages.map(message => (
|
|
1289
|
+
<div key={message.id}>
|
|
1290
|
+
{message.parts.map((part, index) => {
|
|
1291
|
+
if (part.type === 'text') {
|
|
1292
|
+
return <p key={index}>{part.text}</p>
|
|
1293
|
+
}
|
|
1294
|
+
if (part.type === 'data-tool-agent') {
|
|
1295
|
+
const { id, data } = part as AgentDataPart
|
|
1296
|
+
return (
|
|
1297
|
+
<div key={index} className="nested-agent">
|
|
1298
|
+
<strong>Nested Agent: {id}</strong>
|
|
1299
|
+
{data.text && <p>{data.text}</p>}
|
|
1300
|
+
</div>
|
|
1301
|
+
)
|
|
1302
|
+
}
|
|
1303
|
+
return null
|
|
1304
|
+
})}
|
|
1305
|
+
</div>
|
|
1306
|
+
))}
|
|
1307
|
+
</div>
|
|
1308
|
+
)
|
|
1309
|
+
}
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
Key points:
|
|
1313
|
+
|
|
1314
|
+
- Piping `fullStream` to `context.writer` creates `data-tool-agent` parts
|
|
1315
|
+
- The `AgentDataPart` has `id` (on the part) and `data.text` (the agent's streamed text)
|
|
1316
|
+
- The tool still returns its own output after the stream completes
|
|
1317
|
+
|
|
1318
|
+
For a complete implementation, see the [tool-nested-streams example](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/tool-nested-streams.tsx) in UI Dojo.
|
|
1319
|
+
|
|
1320
|
+
### Streaming agent text from workflow steps
|
|
1321
|
+
|
|
1322
|
+
Workflow steps can stream an agent's text output in real-time by piping the agent's stream to the step's `writer`. This lets users see the agent "thinking" while the workflow executes, rather than waiting for the step to complete.
|
|
1323
|
+
|
|
1324
|
+
The pattern uses:
|
|
1325
|
+
|
|
1326
|
+
- `writer` in workflow step - Pipe the agent's `fullStream` to the step's writer
|
|
1327
|
+
- `text` and `data-workflow` parts - The frontend receives streaming text alongside step progress
|
|
1328
|
+
|
|
1329
|
+
**Backend**:
|
|
1330
|
+
|
|
1331
|
+
Create a workflow step that streams an agent's response by piping to the step's `writer`.
|
|
1332
|
+
|
|
1333
|
+
```typescript
|
|
1334
|
+
import { createStep, createWorkflow } from '@mastra/core/workflows'
|
|
1335
|
+
import { z } from 'zod'
|
|
1336
|
+
import { weatherAgent } from '../agents/weather-agent'
|
|
1337
|
+
|
|
1338
|
+
const analyzeWeather = createStep({
|
|
1339
|
+
id: 'analyze-weather',
|
|
1340
|
+
inputSchema: z.object({ location: z.string() }),
|
|
1341
|
+
outputSchema: z.object({ analysis: z.string(), location: z.string() }),
|
|
1342
|
+
execute: async ({ inputData, writer }) => {
|
|
1343
|
+
const response = await weatherAgent.stream(
|
|
1344
|
+
`Analyze the weather in ${inputData.location} and provide insights.`,
|
|
1345
|
+
)
|
|
1346
|
+
|
|
1347
|
+
// Pipe agent stream to step writer for real-time text streaming
|
|
1348
|
+
await response.fullStream.pipeTo(writer)
|
|
1349
|
+
|
|
1350
|
+
return {
|
|
1351
|
+
analysis: await response.text,
|
|
1352
|
+
location: inputData.location,
|
|
1353
|
+
}
|
|
1354
|
+
},
|
|
1355
|
+
})
|
|
1356
|
+
|
|
1357
|
+
const calculateScore = createStep({
|
|
1358
|
+
id: 'calculate-score',
|
|
1359
|
+
inputSchema: z.object({ analysis: z.string(), location: z.string() }),
|
|
1360
|
+
outputSchema: z.object({ score: z.number(), summary: z.string() }),
|
|
1361
|
+
execute: async ({ inputData }) => {
|
|
1362
|
+
const score = inputData.analysis.includes('sunny') ? 85 : 50
|
|
1363
|
+
return { score, summary: `Comfort score for ${inputData.location}: ${score}/100` }
|
|
1364
|
+
},
|
|
1365
|
+
})
|
|
1366
|
+
|
|
1367
|
+
export const weatherWorkflow = createWorkflow({
|
|
1368
|
+
id: 'weather-workflow',
|
|
1369
|
+
inputSchema: z.object({ location: z.string() }),
|
|
1370
|
+
outputSchema: z.object({ score: z.number(), summary: z.string() }),
|
|
1371
|
+
})
|
|
1372
|
+
.then(analyzeWeather)
|
|
1373
|
+
.then(calculateScore)
|
|
1374
|
+
|
|
1375
|
+
weatherWorkflow.commit()
|
|
1376
|
+
```
|
|
1377
|
+
|
|
1378
|
+
Register the workflow with a `workflowRoute()`. Text streaming is enabled by default.
|
|
1379
|
+
|
|
1380
|
+
```typescript
|
|
1381
|
+
import { Mastra } from '@mastra/core'
|
|
1382
|
+
import { workflowRoute } from '@mastra/ai-sdk'
|
|
1383
|
+
|
|
1384
|
+
export const mastra = new Mastra({
|
|
1385
|
+
agents: { weatherAgent },
|
|
1386
|
+
workflows: { weatherWorkflow },
|
|
1387
|
+
server: {
|
|
1388
|
+
apiRoutes: [workflowRoute({ path: '/workflow/weather', workflow: 'weatherWorkflow' })],
|
|
1389
|
+
},
|
|
1390
|
+
})
|
|
1391
|
+
```
|
|
1392
|
+
|
|
1393
|
+
**Frontend**:
|
|
1394
|
+
|
|
1395
|
+
Render both `text` parts (streaming agent output) and `data-workflow` parts (step progress).
|
|
1396
|
+
|
|
1397
|
+
```typescript
|
|
1398
|
+
import { useChat } from '@ai-sdk/react'
|
|
1399
|
+
import { DefaultChatTransport } from 'ai'
|
|
1400
|
+
import { useState } from 'react'
|
|
1401
|
+
import type { WorkflowDataPart } from '@mastra/ai-sdk'
|
|
1402
|
+
|
|
1403
|
+
type WorkflowData = WorkflowDataPart['data']
|
|
1404
|
+
|
|
1405
|
+
export function WeatherWorkflow() {
|
|
1406
|
+
const [location, setLocation] = useState('')
|
|
1407
|
+
const { messages, sendMessage, status } = useChat({
|
|
1408
|
+
transport: new DefaultChatTransport({
|
|
1409
|
+
api: 'http://localhost:4111/workflow/weather',
|
|
1410
|
+
prepareSendMessagesRequest: ({ messages }) => ({
|
|
1411
|
+
body: {
|
|
1412
|
+
inputData: {
|
|
1413
|
+
location: messages[messages.length - 1].parts.find(p => p.type === 'text')?.text,
|
|
1414
|
+
},
|
|
1415
|
+
},
|
|
1416
|
+
}),
|
|
1417
|
+
}),
|
|
1418
|
+
})
|
|
1419
|
+
|
|
1420
|
+
return (
|
|
1421
|
+
<div>
|
|
1422
|
+
<form
|
|
1423
|
+
onSubmit={e => {
|
|
1424
|
+
e.preventDefault()
|
|
1425
|
+
sendMessage({ text: location })
|
|
1426
|
+
setLocation('')
|
|
1427
|
+
}}
|
|
1428
|
+
>
|
|
1429
|
+
<input
|
|
1430
|
+
value={location}
|
|
1431
|
+
onChange={e => setLocation(e.target.value)}
|
|
1432
|
+
placeholder="Enter city"
|
|
1433
|
+
/>
|
|
1434
|
+
<button type="submit" disabled={status !== 'ready'}>
|
|
1435
|
+
Analyze
|
|
1436
|
+
</button>
|
|
1437
|
+
</form>
|
|
1438
|
+
|
|
1439
|
+
{messages.map(message => (
|
|
1440
|
+
<div key={message.id}>
|
|
1441
|
+
{message.parts.map((part, index) => {
|
|
1442
|
+
// Streaming agent text
|
|
1443
|
+
if (part.type === 'text' && message.role === 'assistant') {
|
|
1444
|
+
return (
|
|
1445
|
+
<div key={index}>
|
|
1446
|
+
{status === 'streaming' && (
|
|
1447
|
+
<p>
|
|
1448
|
+
<em>Agent analyzing...</em>
|
|
1449
|
+
</p>
|
|
1450
|
+
)}
|
|
1451
|
+
<p>{part.text}</p>
|
|
1452
|
+
</div>
|
|
1453
|
+
)
|
|
1454
|
+
}
|
|
1455
|
+
// Workflow step progress
|
|
1456
|
+
if (part.type === 'data-workflow') {
|
|
1457
|
+
const workflow = part.data as WorkflowData
|
|
1458
|
+
return (
|
|
1459
|
+
<div key={index}>
|
|
1460
|
+
{Object.entries(workflow.steps).map(([stepId, step]) => (
|
|
1461
|
+
<div key={stepId}>
|
|
1462
|
+
<strong>{stepId}</strong>: {step.status}
|
|
1463
|
+
</div>
|
|
1464
|
+
))}
|
|
1465
|
+
</div>
|
|
1466
|
+
)
|
|
1467
|
+
}
|
|
1468
|
+
return null
|
|
1469
|
+
})}
|
|
1470
|
+
</div>
|
|
1471
|
+
))}
|
|
1472
|
+
</div>
|
|
1473
|
+
)
|
|
1474
|
+
}
|
|
1475
|
+
```
|
|
1476
|
+
|
|
1477
|
+
Key points:
|
|
1478
|
+
|
|
1479
|
+
- The step's `writer` is available in the `execute` function (not via `context`)
|
|
1480
|
+
- `includeTextStreamParts` defaults to `true` on `workflowRoute()`, so text streams by default
|
|
1481
|
+
- Text parts stream in real-time while `data-workflow` parts update with step status
|
|
1482
|
+
|
|
1483
|
+
For a complete implementation, see the [workflow-agent-text-stream example](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/workflow-agent-text-stream.tsx) in UI Dojo.
|
|
1484
|
+
|
|
1485
|
+
### Multi-stage progress with branching workflows
|
|
1486
|
+
|
|
1487
|
+
For workflows with conditional branching (e.g., express vs standard shipping), you can track progress across different branches by including a identifier in your custom events.
|
|
1488
|
+
|
|
1489
|
+
The UI Dojo example uses a `stage` field in the event data to identify which branch is executing (e.g., `"validation"`, `"standard-processing"`, `"express-processing"`). The frontend groups events by this field to show a pipeline-style progress UI.
|
|
1490
|
+
|
|
1491
|
+
See the [branching-workflow.ts](https://github.com/mastra-ai/ui-dojo/blob/main/src/mastra/workflows/branching-workflow.ts) (backend) and [workflow-custom-events.tsx](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/workflow-custom-events.tsx) (frontend) in UI Dojo.
|
|
1492
|
+
|
|
1493
|
+
### Progress indicators in agent networks
|
|
1494
|
+
|
|
1495
|
+
When using agent networks, you can emit custom progress events from tools used by subagents to show which agent is currently active.
|
|
1496
|
+
|
|
1497
|
+
The UI Dojo example includes a `stage` field in the event data to identify which subagent is running (e.g., `"report-generation"`, `"report-review"`). The frontend groups events by this field and displays the latest status for each.
|
|
1498
|
+
|
|
1499
|
+
See the [report-generation-tool.ts](https://github.com/mastra-ai/ui-dojo/blob/main/src/mastra/tools/report-generation-tool.ts) (backend) and [agent-network-custom-events.tsx](https://github.com/mastra-ai/ui-dojo/blob/main/src/pages/ai-sdk/agent-network-custom-events.tsx) (frontend) in UI Dojo.
|