@mastra/mcp-docs-server 1.0.0-beta.1 → 1.0.0-beta.11
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/organized/changelogs/%40internal%2Fchangeset-cli.md +1 -15
- package/.docs/organized/changelogs/%40internal%2Fexternal-types.md +1 -7
- package/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +201 -1
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +1 -55
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +276 -76
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fauth.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +200 -0
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +26 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +89 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +392 -192
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +200 -0
- package/.docs/organized/changelogs/%40mastra%2Fduckdb.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +61 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Flance.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +263 -63
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Frag.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Freact.md +154 -1
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +274 -74
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +92 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +67 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +201 -1
- package/.docs/organized/changelogs/create-mastra.md +201 -1
- package/.docs/organized/changelogs/mastra.md +201 -1
- package/.docs/organized/code-examples/agui.md +1 -0
- package/.docs/organized/code-examples/ai-elements.md +2 -2
- package/.docs/organized/code-examples/ai-sdk-useChat.md +2 -2
- package/.docs/organized/code-examples/ai-sdk-v5.md +3 -2
- package/.docs/organized/code-examples/assistant-ui.md +2 -2
- package/.docs/organized/code-examples/bird-checker-with-express.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +2 -2
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +2 -2
- package/.docs/organized/code-examples/client-side-tools.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +2 -2
- package/.docs/organized/code-examples/fireworks-r1.md +1 -1
- package/.docs/organized/code-examples/heads-up-game.md +1 -1
- package/.docs/organized/code-examples/mcp-configuration.md +0 -3
- package/.docs/organized/code-examples/mcp-server-adapters.md +720 -0
- package/.docs/organized/code-examples/memory-per-resource-example.md +1 -1
- package/.docs/organized/code-examples/memory-with-processors.md +1 -1
- package/.docs/organized/code-examples/openapi-spec-writer.md +1 -1
- package/.docs/organized/code-examples/processors-with-ai-sdk.md +14 -0
- package/.docs/organized/code-examples/quick-start.md +2 -2
- package/.docs/organized/code-examples/server-app-access.md +342 -0
- package/.docs/organized/code-examples/server-express-adapter.md +87 -0
- package/.docs/organized/code-examples/server-hono-adapter.md +85 -0
- package/.docs/organized/code-examples/stock-price-tool.md +1 -21
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +1 -1
- package/.docs/organized/code-examples/workflow-with-inline-steps.md +1 -1
- package/.docs/organized/code-examples/workflow-with-memory.md +1 -1
- package/.docs/organized/code-examples/workflow-with-separate-steps.md +1 -1
- package/.docs/organized/code-examples/workflow-with-suspend-resume.md +1 -1
- package/.docs/raw/agents/adding-voice.mdx +56 -10
- package/.docs/raw/agents/agent-approval.mdx +189 -0
- package/.docs/raw/agents/agent-memory.mdx +1 -1
- package/.docs/raw/agents/guardrails.mdx +70 -30
- package/.docs/raw/agents/networks.mdx +3 -3
- package/.docs/raw/agents/overview.mdx +18 -167
- package/.docs/raw/agents/processors.mdx +430 -0
- package/.docs/raw/agents/structured-output.mdx +224 -0
- package/.docs/raw/agents/using-tools.mdx +14 -14
- package/.docs/raw/auth/auth0.mdx +1 -1
- package/.docs/raw/auth/clerk.mdx +1 -1
- package/.docs/raw/auth/firebase.mdx +1 -1
- package/.docs/raw/auth/index.mdx +1 -1
- package/.docs/raw/auth/jwt.mdx +1 -1
- package/.docs/raw/auth/supabase.mdx +1 -1
- package/.docs/raw/auth/workos.mdx +1 -1
- package/.docs/raw/community/contributing-templates.mdx +1 -1
- package/.docs/raw/community/discord.mdx +2 -2
- package/.docs/raw/community/licensing.mdx +1 -1
- package/.docs/raw/course/01-first-agent/05-running-playground.md +5 -5
- package/.docs/raw/course/01-first-agent/09-testing-your-agent.md +3 -3
- package/.docs/raw/course/01-first-agent/13-testing-your-tool.md +3 -3
- package/.docs/raw/course/01-first-agent/17-testing-memory.md +2 -2
- package/.docs/raw/course/04-workflows/07-using-playground.md +1 -1
- package/.docs/raw/deployment/{server-deployment.mdx → building-mastra.mdx} +4 -4
- package/.docs/raw/deployment/cloud-providers/amazon-ec2.mdx +2 -2
- package/.docs/raw/deployment/cloud-providers/aws-lambda.mdx +3 -3
- package/.docs/raw/deployment/cloud-providers/azure-app-services.mdx +2 -2
- package/.docs/raw/deployment/{serverless-platforms → cloud-providers}/cloudflare-deployer.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/digital-ocean.mdx +2 -2
- package/.docs/raw/deployment/cloud-providers/index.mdx +26 -29
- package/.docs/raw/deployment/{serverless-platforms → cloud-providers}/netlify-deployer.mdx +45 -14
- package/.docs/raw/deployment/{serverless-platforms → cloud-providers}/vercel-deployer.mdx +1 -1
- package/.docs/raw/{mastra-cloud → deployment/mastra-cloud}/dashboard.mdx +3 -3
- package/.docs/raw/deployment/mastra-cloud/observability.mdx +53 -0
- package/.docs/raw/{mastra-cloud → deployment/mastra-cloud}/overview.mdx +3 -3
- package/.docs/raw/{mastra-cloud → deployment/mastra-cloud}/setting-up.mdx +4 -4
- package/.docs/raw/deployment/monorepo.mdx +3 -3
- package/.docs/raw/deployment/overview.mdx +7 -7
- package/.docs/raw/deployment/web-framework.mdx +6 -6
- package/.docs/raw/evals/{off-the-shelf-scorers.mdx → built-in-scorers.mdx} +17 -15
- package/.docs/raw/evals/custom-scorers.mdx +4 -6
- package/.docs/raw/evals/overview.mdx +5 -6
- package/.docs/raw/evals/running-in-ci.mdx +2 -4
- package/.docs/raw/getting-started/{installation.mdx → manual-install.mdx} +6 -80
- package/.docs/raw/getting-started/mcp-docs-server.mdx +63 -6
- package/.docs/raw/getting-started/project-structure.mdx +2 -2
- package/.docs/raw/getting-started/start.mdx +72 -0
- package/.docs/raw/getting-started/studio.mdx +32 -7
- package/.docs/raw/guides/agent-frameworks/ai-sdk.mdx +147 -0
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +627 -0
- package/.docs/raw/{frameworks/agentic-uis → guides/build-your-ui}/assistant-ui.mdx +71 -19
- package/.docs/raw/guides/build-your-ui/copilotkit.mdx +206 -0
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/astro.mdx +24 -26
- package/.docs/raw/{frameworks/servers → guides/getting-started}/express.mdx +4 -5
- package/.docs/raw/guides/{quickstarts/nextjs.mdx → getting-started/next-js.mdx} +26 -25
- package/.docs/raw/guides/getting-started/quickstart.mdx +97 -0
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/sveltekit.mdx +24 -26
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/vite-react.mdx +7 -7
- package/.docs/raw/guides/guide/ai-recruiter.mdx +2 -3
- package/.docs/raw/guides/guide/chef-michel.mdx +2 -3
- package/.docs/raw/guides/guide/notes-mcp-server.mdx +2 -2
- package/.docs/raw/guides/guide/research-assistant.mdx +7 -8
- package/.docs/raw/guides/guide/stock-agent.mdx +4 -6
- package/.docs/raw/guides/guide/web-search.mdx +12 -10
- package/.docs/raw/guides/guide/whatsapp-chat-bot.mdx +421 -0
- package/.docs/raw/guides/index.mdx +3 -35
- package/.docs/raw/guides/migrations/agentnetwork.mdx +4 -4
- package/.docs/raw/guides/migrations/ai-sdk-v4-to-v5.mdx +119 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/agent.mdx +121 -1
- package/.docs/raw/guides/migrations/upgrade-to-v1/client.mdx +42 -2
- package/.docs/raw/guides/migrations/upgrade-to-v1/evals.mdx +31 -1
- package/.docs/raw/guides/migrations/upgrade-to-v1/mastra.mdx +27 -3
- package/.docs/raw/guides/migrations/upgrade-to-v1/mcp.mdx +1 -1
- package/.docs/raw/guides/migrations/upgrade-to-v1/memory.mdx +28 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/overview.mdx +25 -3
- package/.docs/raw/guides/migrations/upgrade-to-v1/storage.mdx +80 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/tools.mdx +68 -3
- package/.docs/raw/guides/migrations/upgrade-to-v1/vectors.mdx +25 -1
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +174 -1
- package/.docs/raw/guides/migrations/vnext-to-standard-apis.mdx +2 -2
- package/.docs/raw/index.mdx +3 -3
- package/.docs/raw/mcp/overview.mdx +358 -0
- package/.docs/raw/mcp/publishing-mcp-server.mdx +139 -0
- package/.docs/raw/memory/conversation-history.mdx +1 -1
- package/.docs/raw/memory/memory-processors.mdx +265 -80
- package/.docs/raw/memory/overview.mdx +2 -2
- package/.docs/raw/memory/semantic-recall.mdx +9 -9
- package/.docs/raw/memory/storage/memory-with-libsql.mdx +3 -8
- package/.docs/raw/memory/storage/memory-with-mongodb.mdx +150 -0
- package/.docs/raw/memory/storage/memory-with-pg.mdx +3 -8
- package/.docs/raw/memory/storage/memory-with-upstash.mdx +3 -8
- package/.docs/raw/memory/threads-and-resources.mdx +4 -4
- package/.docs/raw/memory/working-memory.mdx +16 -8
- package/.docs/raw/{logging.mdx → observability/logging.mdx} +7 -7
- package/.docs/raw/observability/overview.mdx +3 -4
- package/.docs/raw/observability/tracing/bridges/otel.mdx +200 -0
- package/.docs/raw/observability/tracing/exporters/arize.mdx +37 -1
- package/.docs/raw/observability/tracing/exporters/braintrust.mdx +20 -1
- package/.docs/raw/observability/tracing/exporters/cloud.mdx +2 -2
- package/.docs/raw/observability/tracing/exporters/default.mdx +1 -1
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +84 -1
- package/.docs/raw/observability/tracing/exporters/langsmith.mdx +13 -1
- package/.docs/raw/observability/tracing/exporters/otel.mdx +123 -25
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +127 -0
- package/.docs/raw/observability/tracing/overview.mdx +80 -9
- package/.docs/raw/observability/tracing/processors/sensitive-data-filter.mdx +1 -2
- package/.docs/raw/rag/chunking-and-embedding.mdx +16 -17
- package/.docs/raw/rag/overview.mdx +4 -3
- package/.docs/raw/rag/retrieval.mdx +44 -39
- package/.docs/raw/rag/vector-databases.mdx +94 -3
- package/.docs/raw/reference/agents/agent.mdx +19 -16
- package/.docs/raw/reference/agents/generate.mdx +67 -99
- package/.docs/raw/reference/agents/generateLegacy.mdx +3 -3
- package/.docs/raw/reference/agents/getDefaultGenerateOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultStreamOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDescription.mdx +1 -1
- package/.docs/raw/reference/agents/getInstructions.mdx +1 -1
- package/.docs/raw/reference/agents/getLLM.mdx +2 -2
- package/.docs/raw/reference/agents/getMemory.mdx +1 -1
- package/.docs/raw/reference/agents/getModel.mdx +1 -1
- package/.docs/raw/reference/agents/getTools.mdx +69 -0
- package/.docs/raw/reference/agents/getVoice.mdx +1 -1
- package/.docs/raw/reference/agents/listAgents.mdx +1 -1
- package/.docs/raw/reference/agents/listScorers.mdx +1 -1
- package/.docs/raw/reference/agents/listTools.mdx +2 -2
- package/.docs/raw/reference/agents/listWorkflows.mdx +1 -1
- package/.docs/raw/reference/agents/network.mdx +33 -72
- package/.docs/raw/reference/ai-sdk/chat-route.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +117 -0
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +64 -0
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +116 -0
- package/.docs/raw/reference/ai-sdk/network-route.mdx +99 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-stream.mdx +289 -0
- package/.docs/raw/reference/ai-sdk/with-mastra.mdx +119 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +110 -0
- package/.docs/raw/reference/auth/auth0.mdx +1 -1
- package/.docs/raw/reference/auth/clerk.mdx +1 -1
- package/.docs/raw/reference/auth/firebase.mdx +1 -1
- package/.docs/raw/reference/auth/jwt.mdx +1 -1
- package/.docs/raw/reference/auth/supabase.mdx +1 -1
- package/.docs/raw/reference/auth/workos.mdx +1 -1
- package/.docs/raw/reference/cli/create-mastra.mdx +1 -1
- package/.docs/raw/reference/cli/mastra.mdx +19 -6
- package/.docs/raw/reference/client-js/agents.mdx +252 -68
- package/.docs/raw/reference/client-js/error-handling.mdx +1 -1
- package/.docs/raw/reference/client-js/logs.mdx +1 -1
- package/.docs/raw/reference/client-js/mastra-client.mdx +4 -4
- package/.docs/raw/reference/client-js/memory.mdx +48 -2
- package/.docs/raw/reference/client-js/observability.mdx +1 -1
- package/.docs/raw/reference/client-js/telemetry.mdx +1 -1
- package/.docs/raw/reference/client-js/tools.mdx +1 -1
- package/.docs/raw/reference/client-js/vectors.mdx +1 -1
- package/.docs/raw/reference/client-js/workflows.mdx +93 -64
- package/.docs/raw/reference/core/addGateway.mdx +59 -0
- package/.docs/raw/reference/core/getAgent.mdx +1 -1
- package/.docs/raw/reference/core/getAgentById.mdx +1 -1
- package/.docs/raw/reference/core/getDeployer.mdx +1 -1
- package/.docs/raw/reference/core/getGateway.mdx +59 -0
- package/.docs/raw/reference/core/getGatewayById.mdx +62 -0
- package/.docs/raw/reference/core/getLogger.mdx +2 -2
- package/.docs/raw/reference/core/getMCPServer.mdx +2 -2
- package/.docs/raw/reference/core/getMCPServerById.mdx +2 -2
- package/.docs/raw/reference/core/getMemory.mdx +73 -0
- package/.docs/raw/reference/core/getScorer.mdx +1 -1
- package/.docs/raw/reference/core/getScorerById.mdx +1 -1
- package/.docs/raw/reference/core/getServer.mdx +3 -3
- package/.docs/raw/reference/core/getStorage.mdx +1 -1
- package/.docs/raw/reference/core/getStoredAgentById.mdx +183 -0
- package/.docs/raw/reference/core/getTelemetry.mdx +1 -1
- package/.docs/raw/reference/core/getVector.mdx +1 -1
- package/.docs/raw/reference/core/getWorkflow.mdx +1 -1
- package/.docs/raw/reference/core/listAgents.mdx +1 -1
- package/.docs/raw/reference/core/listGateways.mdx +53 -0
- package/.docs/raw/reference/core/listLogs.mdx +2 -2
- package/.docs/raw/reference/core/listLogsByRunId.mdx +2 -2
- package/.docs/raw/reference/core/listMCPServers.mdx +2 -2
- package/.docs/raw/reference/core/listMemory.mdx +70 -0
- package/.docs/raw/reference/core/listScorers.mdx +1 -1
- package/.docs/raw/reference/core/listStoredAgents.mdx +151 -0
- package/.docs/raw/reference/core/listVectors.mdx +1 -1
- package/.docs/raw/reference/core/listWorkflows.mdx +1 -1
- package/.docs/raw/reference/core/mastra-class.mdx +17 -1
- package/.docs/raw/reference/core/mastra-model-gateway.mdx +227 -0
- package/.docs/raw/reference/core/setLogger.mdx +2 -2
- package/.docs/raw/reference/core/setStorage.mdx +1 -1
- package/.docs/raw/reference/core/setTelemetry.mdx +2 -2
- package/.docs/raw/reference/deployer/cloudflare.mdx +1 -1
- package/.docs/raw/reference/deployer/deployer.mdx +1 -1
- package/.docs/raw/reference/deployer/netlify.mdx +2 -3
- package/.docs/raw/reference/deployer/vercel.mdx +1 -1
- package/.docs/raw/reference/evals/answer-relevancy.mdx +29 -99
- package/.docs/raw/reference/evals/answer-similarity.mdx +13 -259
- package/.docs/raw/reference/evals/bias.mdx +30 -88
- package/.docs/raw/reference/evals/completeness.mdx +32 -91
- package/.docs/raw/reference/evals/content-similarity.mdx +29 -89
- package/.docs/raw/reference/evals/context-precision.mdx +29 -131
- package/.docs/raw/reference/evals/context-relevance.mdx +12 -12
- package/.docs/raw/reference/evals/create-scorer.mdx +1 -1
- package/.docs/raw/reference/evals/faithfulness.mdx +29 -102
- package/.docs/raw/reference/evals/hallucination.mdx +29 -104
- package/.docs/raw/reference/evals/keyword-coverage.mdx +29 -108
- package/.docs/raw/reference/evals/mastra-scorer.mdx +1 -1
- package/.docs/raw/reference/evals/noise-sensitivity.mdx +12 -12
- package/.docs/raw/reference/evals/prompt-alignment.mdx +16 -16
- package/.docs/raw/reference/evals/run-evals.mdx +1 -1
- package/.docs/raw/reference/evals/scorer-utils.mdx +362 -0
- package/.docs/raw/reference/evals/textual-difference.mdx +28 -101
- package/.docs/raw/reference/evals/tone-consistency.mdx +26 -99
- package/.docs/raw/reference/evals/tool-call-accuracy.mdx +8 -8
- package/.docs/raw/reference/evals/toxicity.mdx +30 -93
- package/.docs/raw/reference/index.mdx +1 -0
- package/.docs/raw/reference/logging/pino-logger.mdx +2 -2
- package/.docs/raw/reference/memory/createThread.mdx +1 -1
- package/.docs/raw/reference/memory/deleteMessages.mdx +1 -1
- package/.docs/raw/reference/memory/getThreadById.mdx +1 -1
- package/.docs/raw/reference/memory/listThreadsByResourceId.mdx +1 -1
- package/.docs/raw/reference/memory/memory-class.mdx +6 -8
- package/.docs/raw/reference/memory/query.mdx +1 -1
- package/.docs/raw/reference/memory/recall.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +177 -0
- package/.docs/raw/reference/observability/tracing/configuration.mdx +1 -5
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +30 -1
- package/.docs/raw/reference/observability/tracing/exporters/braintrust.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/exporters/cloud-exporter.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/exporters/console-exporter.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/exporters/default-exporter.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/exporters/langfuse.mdx +44 -1
- package/.docs/raw/reference/observability/tracing/exporters/langsmith.mdx +18 -2
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +34 -44
- package/.docs/raw/reference/observability/tracing/exporters/posthog.mdx +132 -0
- package/.docs/raw/reference/observability/tracing/instances.mdx +1 -5
- package/.docs/raw/reference/observability/tracing/interfaces.mdx +30 -5
- package/.docs/raw/reference/observability/tracing/processors/sensitive-data-filter.mdx +1 -1
- package/.docs/raw/reference/observability/tracing/spans.mdx +1 -5
- package/.docs/raw/reference/processors/batch-parts-processor.mdx +2 -2
- package/.docs/raw/reference/processors/language-detector.mdx +11 -4
- package/.docs/raw/reference/processors/message-history-processor.mdx +131 -0
- package/.docs/raw/reference/processors/moderation-processor.mdx +13 -6
- package/.docs/raw/reference/processors/pii-detector.mdx +13 -6
- package/.docs/raw/reference/processors/processor-interface.mdx +803 -0
- package/.docs/raw/reference/processors/prompt-injection-detector.mdx +11 -4
- package/.docs/raw/reference/processors/semantic-recall-processor.mdx +197 -0
- package/.docs/raw/reference/processors/system-prompt-scrubber.mdx +4 -5
- package/.docs/raw/reference/processors/token-limiter-processor.mdx +3 -3
- package/.docs/raw/reference/processors/tool-call-filter.mdx +125 -0
- package/.docs/raw/reference/processors/unicode-normalizer.mdx +2 -2
- package/.docs/raw/reference/processors/working-memory-processor.mdx +221 -0
- package/.docs/raw/reference/rag/chunk.mdx +1 -1
- package/.docs/raw/reference/rag/database-config.mdx +1 -1
- package/.docs/raw/reference/rag/document.mdx +1 -1
- package/.docs/raw/reference/rag/embeddings.mdx +6 -6
- package/.docs/raw/reference/rag/extract-params.mdx +1 -1
- package/.docs/raw/reference/rag/graph-rag.mdx +1 -1
- package/.docs/raw/reference/rag/metadata-filters.mdx +1 -1
- package/.docs/raw/reference/rag/rerank.mdx +2 -3
- package/.docs/raw/reference/rag/rerankWithScorer.mdx +1 -2
- package/.docs/raw/reference/server/create-route.mdx +328 -0
- package/.docs/raw/reference/server/express-adapter.mdx +260 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +242 -0
- package/.docs/raw/reference/server/mastra-server.mdx +345 -0
- package/.docs/raw/reference/server/routes.mdx +250 -0
- package/.docs/raw/reference/storage/cloudflare-d1.mdx +38 -1
- package/.docs/raw/reference/storage/cloudflare.mdx +1 -1
- package/.docs/raw/reference/storage/convex.mdx +164 -0
- package/.docs/raw/reference/storage/dynamodb.mdx +1 -1
- package/.docs/raw/reference/storage/lance.mdx +34 -1
- package/.docs/raw/reference/storage/libsql.mdx +38 -1
- package/.docs/raw/reference/storage/mongodb.mdx +40 -1
- package/.docs/raw/reference/storage/mssql.mdx +38 -1
- package/.docs/raw/reference/storage/postgresql.mdx +38 -1
- package/.docs/raw/reference/storage/upstash.mdx +1 -1
- package/.docs/raw/reference/streaming/ChunkType.mdx +25 -4
- package/.docs/raw/reference/streaming/agents/MastraModelOutput.mdx +1 -1
- package/.docs/raw/reference/streaming/agents/stream.mdx +84 -124
- package/.docs/raw/reference/streaming/agents/streamLegacy.mdx +1 -1
- package/.docs/raw/reference/streaming/workflows/observeStream.mdx +8 -10
- package/.docs/raw/reference/streaming/workflows/{resumeStreamVNext.mdx → resumeStream.mdx} +51 -11
- package/.docs/raw/reference/streaming/workflows/stream.mdx +84 -25
- package/.docs/raw/reference/streaming/workflows/timeTravelStream.mdx +170 -0
- package/.docs/raw/reference/templates/overview.mdx +2 -5
- package/.docs/raw/reference/tools/client.mdx +2 -3
- package/.docs/raw/reference/tools/create-tool.mdx +135 -4
- package/.docs/raw/reference/tools/document-chunker-tool.mdx +1 -1
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +7 -7
- package/.docs/raw/reference/tools/mcp-client.mdx +131 -23
- package/.docs/raw/reference/tools/mcp-server.mdx +56 -8
- package/.docs/raw/reference/tools/vector-query-tool.mdx +16 -17
- package/.docs/raw/reference/vectors/astra.mdx +1 -1
- package/.docs/raw/reference/vectors/chroma.mdx +82 -2
- package/.docs/raw/reference/vectors/convex.mdx +429 -0
- package/.docs/raw/reference/vectors/couchbase.mdx +25 -18
- package/.docs/raw/reference/vectors/duckdb.mdx +462 -0
- package/.docs/raw/reference/vectors/elasticsearch.mdx +310 -0
- package/.docs/raw/reference/vectors/lance.mdx +39 -23
- package/.docs/raw/reference/vectors/libsql.mdx +36 -3
- package/.docs/raw/reference/vectors/mongodb.mdx +36 -3
- package/.docs/raw/reference/vectors/opensearch.mdx +38 -17
- package/.docs/raw/reference/vectors/pg.mdx +44 -37
- package/.docs/raw/reference/vectors/pinecone.mdx +70 -3
- package/.docs/raw/reference/vectors/qdrant.mdx +37 -2
- package/.docs/raw/reference/vectors/s3vectors.mdx +1 -1
- package/.docs/raw/reference/vectors/turbopuffer.mdx +75 -1
- package/.docs/raw/reference/vectors/upstash.mdx +1 -1
- package/.docs/raw/reference/vectors/vectorize.mdx +1 -1
- package/.docs/raw/reference/voice/azure.mdx +96 -81
- package/.docs/raw/reference/voice/cloudflare.mdx +1 -1
- package/.docs/raw/reference/voice/composite-voice.mdx +72 -29
- package/.docs/raw/reference/voice/deepgram.mdx +1 -1
- package/.docs/raw/reference/voice/elevenlabs.mdx +1 -1
- package/.docs/raw/reference/voice/google-gemini-live.mdx +1 -1
- package/.docs/raw/reference/voice/google.mdx +160 -21
- package/.docs/raw/reference/voice/mastra-voice.mdx +1 -1
- package/.docs/raw/reference/voice/murf.mdx +1 -1
- package/.docs/raw/reference/voice/openai-realtime.mdx +3 -3
- package/.docs/raw/reference/voice/openai.mdx +1 -1
- package/.docs/raw/reference/voice/playai.mdx +1 -1
- package/.docs/raw/reference/voice/sarvam.mdx +1 -1
- package/.docs/raw/reference/voice/speechify.mdx +1 -1
- package/.docs/raw/reference/voice/voice.addInstructions.mdx +3 -4
- package/.docs/raw/reference/voice/voice.addTools.mdx +2 -2
- package/.docs/raw/reference/voice/voice.answer.mdx +2 -2
- package/.docs/raw/reference/voice/voice.close.mdx +2 -2
- package/.docs/raw/reference/voice/voice.connect.mdx +2 -2
- package/.docs/raw/reference/voice/voice.events.mdx +1 -1
- package/.docs/raw/reference/voice/voice.getSpeakers.mdx +1 -1
- package/.docs/raw/reference/voice/voice.listen.mdx +87 -53
- package/.docs/raw/reference/voice/voice.off.mdx +2 -2
- package/.docs/raw/reference/voice/voice.on.mdx +2 -2
- package/.docs/raw/reference/voice/voice.send.mdx +2 -2
- package/.docs/raw/reference/voice/voice.speak.mdx +76 -41
- package/.docs/raw/reference/voice/voice.updateConfig.mdx +2 -2
- package/.docs/raw/reference/workflows/run-methods/cancel.mdx +1 -1
- package/.docs/raw/reference/workflows/run-methods/restart.mdx +142 -0
- package/.docs/raw/reference/workflows/run-methods/resume.mdx +45 -1
- package/.docs/raw/reference/workflows/run-methods/start.mdx +48 -4
- package/.docs/raw/reference/workflows/run-methods/timeTravel.mdx +310 -0
- package/.docs/raw/reference/workflows/run.mdx +28 -6
- package/.docs/raw/reference/workflows/step.mdx +65 -1
- package/.docs/raw/reference/workflows/workflow-methods/branch.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/commit.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/create-run.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/dountil.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/dowhile.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/foreach.mdx +69 -4
- package/.docs/raw/reference/workflows/workflow-methods/map.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/parallel.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sendEvent.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sleep.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/sleepUntil.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/then.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow-methods/waitForEvent.mdx +1 -1
- package/.docs/raw/reference/workflows/workflow.mdx +44 -2
- package/.docs/raw/server-db/custom-adapters.mdx +386 -0
- package/.docs/raw/server-db/custom-api-routes.mdx +6 -6
- package/.docs/raw/server-db/mastra-client.mdx +3 -4
- package/.docs/raw/server-db/{production-server.mdx → mastra-server.mdx} +22 -12
- package/.docs/raw/server-db/middleware.mdx +31 -31
- package/.docs/raw/server-db/request-context.mdx +3 -4
- package/.docs/raw/server-db/server-adapters.mdx +312 -0
- package/.docs/raw/server-db/storage.mdx +12 -1
- package/.docs/raw/streaming/events.mdx +1 -1
- package/.docs/raw/streaming/overview.mdx +27 -16
- package/.docs/raw/streaming/tool-streaming.mdx +58 -19
- package/.docs/raw/streaming/workflow-streaming.mdx +6 -12
- package/.docs/raw/tools-mcp/advanced-usage.mdx +2 -3
- package/.docs/raw/tools-mcp/mcp-overview.mdx +4 -6
- package/.docs/raw/tools-mcp/overview.mdx +1 -1
- package/.docs/raw/voice/overview.mdx +88 -41
- package/.docs/raw/voice/speech-to-speech.mdx +5 -5
- package/.docs/raw/voice/speech-to-text.mdx +2 -3
- package/.docs/raw/voice/text-to-speech.mdx +2 -3
- package/.docs/raw/workflows/agents-and-tools.mdx +43 -5
- package/.docs/raw/workflows/control-flow.mdx +534 -3
- package/.docs/raw/workflows/error-handling.mdx +5 -4
- package/.docs/raw/workflows/human-in-the-loop.mdx +160 -213
- package/.docs/raw/workflows/inngest-workflow.mdx +1 -1
- package/.docs/raw/workflows/input-data-mapping.mdx +1 -1
- package/.docs/raw/workflows/overview.mdx +67 -47
- package/.docs/raw/workflows/snapshots.mdx +5 -3
- package/.docs/raw/workflows/suspend-and-resume.mdx +121 -204
- package/.docs/raw/workflows/time-travel.mdx +313 -0
- package/.docs/raw/workflows/workflow-state.mdx +190 -0
- package/CHANGELOG.md +88 -0
- package/dist/{chunk-5NJC7NRO.js → chunk-4CM2BQNP.js} +24 -4
- package/dist/prepare-docs/package-changes.d.ts.map +1 -1
- package/dist/prepare-docs/prepare.js +1 -1
- package/dist/stdio.js +17 -19
- package/package.json +10 -14
- package/.docs/raw/deployment/serverless-platforms/index.mdx +0 -58
- package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +0 -688
- package/.docs/raw/frameworks/agentic-uis/cedar-os.mdx +0 -123
- package/.docs/raw/frameworks/agentic-uis/copilotkit.mdx +0 -382
- package/.docs/raw/frameworks/agentic-uis/openrouter.mdx +0 -179
- package/.docs/raw/frameworks/web-frameworks/next-js.mdx +0 -379
- package/.docs/raw/getting-started/templates.mdx +0 -73
- package/.docs/raw/mastra-cloud/observability.mdx +0 -51
- package/.docs/raw/reference/streaming/workflows/observeStreamVNext.mdx +0 -47
- package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +0 -153
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Reference: Workflow Class | Workflows
|
|
2
|
+
title: "Reference: Workflow Class | Workflows"
|
|
3
3
|
description: Documentation for the `Workflow` class in Mastra, which enables you to create state machines for complex sequences of operations with conditional branching and data validation.
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -75,7 +75,7 @@ export const workflow = createWorkflow({
|
|
|
75
75
|
description:
|
|
76
76
|
"Optional flag to determine whether to validate the workflow inputs. This also applies default values from zodSchemas on the workflow/step input/resume data. If input/resume data validation fails on start/resume, the workflow will not start/resume, it throws an error instead. If input data validation fails on a step execution, the step fails, causing the workflow to fail and the error is returned.",
|
|
77
77
|
isOptional: true,
|
|
78
|
-
defaultValue: "
|
|
78
|
+
defaultValue: "true",
|
|
79
79
|
},
|
|
80
80
|
{
|
|
81
81
|
name: "shouldPersistSnapshot",
|
|
@@ -88,6 +88,24 @@ export const workflow = createWorkflow({
|
|
|
88
88
|
]}
|
|
89
89
|
/>
|
|
90
90
|
|
|
91
|
+
## Running with initial state
|
|
92
|
+
|
|
93
|
+
When starting a workflow run, you can pass `initialState` to set the starting values for the workflow's state:
|
|
94
|
+
|
|
95
|
+
```typescript showLineNumbers copy
|
|
96
|
+
const run = await workflow.createRun();
|
|
97
|
+
|
|
98
|
+
const result = await run.start({
|
|
99
|
+
inputData: { value: "hello" },
|
|
100
|
+
initialState: {
|
|
101
|
+
counter: 0,
|
|
102
|
+
items: [],
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The `initialState` object should match the structure defined in the workflow's `stateSchema`. See [Workflow State](/docs/v1/workflows/workflow-state) for more details.
|
|
108
|
+
|
|
91
109
|
## Workflow status
|
|
92
110
|
|
|
93
111
|
A workflow's `status` indicates its current execution state. The possible values are:
|
|
@@ -112,10 +130,34 @@ A workflow's `status` indicates its current execution state. The possible values
|
|
|
112
130
|
description:
|
|
113
131
|
"Workflow execution is paused waiting for resume, with suspended step information",
|
|
114
132
|
},
|
|
133
|
+
{
|
|
134
|
+
name: "tripwire",
|
|
135
|
+
type: "string",
|
|
136
|
+
description:
|
|
137
|
+
"Workflow was terminated by a processor tripwire. This occurs when an agent step within the workflow triggers a tripwire (e.g., content was blocked by a guardrail). The tripwire information is available on the result.",
|
|
138
|
+
},
|
|
115
139
|
]}
|
|
116
140
|
/>
|
|
117
141
|
|
|
142
|
+
### Handling tripwire status
|
|
143
|
+
|
|
144
|
+
When a workflow contains an agent step that triggers a tripwire, the workflow returns with `status: 'tripwire'` and includes tripwire details:
|
|
145
|
+
|
|
146
|
+
```typescript showLineNumbers copy
|
|
147
|
+
const run = await workflow.createRun();
|
|
148
|
+
const result = await run.start({ inputData: { message: "Hello" } });
|
|
149
|
+
|
|
150
|
+
if (result.status === "tripwire") {
|
|
151
|
+
console.log("Workflow terminated by tripwire:", result.tripwire?.reason);
|
|
152
|
+
console.log("Processor ID:", result.tripwire?.processorId);
|
|
153
|
+
console.log("Retry requested:", result.tripwire?.retry);
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
This is distinct from `status: 'failed'` which indicates an unexpected error. A tripwire status means a processor intentionally stopped execution (e.g., for content moderation).
|
|
158
|
+
|
|
118
159
|
## Related
|
|
119
160
|
|
|
120
161
|
- [Step Class](./step)
|
|
162
|
+
- [Workflow State](/docs/v1/workflows/workflow-state)
|
|
121
163
|
- [Control flow](/docs/v1/workflows/control-flow)
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Custom Adapters | Server & DB"
|
|
3
|
+
description: "Create a custom server adapter for frameworks other than Hono or Express."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# Custom Adapters
|
|
9
|
+
|
|
10
|
+
Create a custom adapter when you need to run Mastra with a framework other than Hono or Express. This might be necessary if you have specific request/response handling requirements that `@mastra/hono` and `@mastra/express` don't support.
|
|
11
|
+
|
|
12
|
+
A custom adapter translates between Mastra's route definitions and your framework's routing system. You'll implement methods that register middleware, handle requests, and send responses using your framework's APIs.
|
|
13
|
+
|
|
14
|
+
:::info
|
|
15
|
+
|
|
16
|
+
For Hono or Express, use the provided adapters instead:
|
|
17
|
+
- [@mastra/hono](/reference/v1/server/hono-adapter)
|
|
18
|
+
- [@mastra/express](/reference/v1/server/express-adapter)
|
|
19
|
+
|
|
20
|
+
:::
|
|
21
|
+
|
|
22
|
+
## Abstract class
|
|
23
|
+
|
|
24
|
+
The `MastraServer` abstract class from `@mastra/server/server-adapter` provides the foundation for all adapters. It handles route registration logic, parameter validation, and other shared functionality. Your custom adapter extends this class and implements the framework-specific parts.
|
|
25
|
+
|
|
26
|
+
The class takes three type parameters that represent your framework's types:
|
|
27
|
+
|
|
28
|
+
```typescript title="my-framework-adapter.ts" copy
|
|
29
|
+
import { MastraServer } from '@mastra/server/server-adapter';
|
|
30
|
+
|
|
31
|
+
export class MyFrameworkServer extends MastraServer<
|
|
32
|
+
// Your framework's app type (e.g., FastifyInstance)
|
|
33
|
+
MyApp,
|
|
34
|
+
// Your framework's request type (e.g., FastifyRequest)
|
|
35
|
+
MyRequest,
|
|
36
|
+
// Your framework's response type (e.g., FastifyReply)
|
|
37
|
+
MyResponse
|
|
38
|
+
> {
|
|
39
|
+
// Implement abstract methods
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
These type parameters ensure type safety throughout your adapter implementation and enable proper typing when accessing framework-specific APIs.
|
|
44
|
+
|
|
45
|
+
## Required methods
|
|
46
|
+
|
|
47
|
+
You must implement these six abstract methods. Each handles a specific part of the request lifecycle, from attaching context to sending responses.
|
|
48
|
+
|
|
49
|
+
### registerContextMiddleware()
|
|
50
|
+
|
|
51
|
+
This method runs first and attaches Mastra context to every incoming request. Route handlers need access to the Mastra instance, tools, and other context to function. How you attach this context depends on your framework — Express uses `res.locals`, Hono uses `c.set()`, and other frameworks have their own patterns.
|
|
52
|
+
|
|
53
|
+
```typescript copy
|
|
54
|
+
registerContextMiddleware(): void {
|
|
55
|
+
this.app.use('*', (req, res, next) => {
|
|
56
|
+
// Attach context to your framework's request/response
|
|
57
|
+
res.locals.mastra = this.mastra;
|
|
58
|
+
res.locals.requestContext = new RequestContext();
|
|
59
|
+
res.locals.tools = this.tools;
|
|
60
|
+
res.locals.abortSignal = createAbortSignal(req);
|
|
61
|
+
next();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Context to attach:
|
|
67
|
+
|
|
68
|
+
| Key | Type | Description |
|
|
69
|
+
|-----|------|-------------|
|
|
70
|
+
| `mastra` | `Mastra` | The Mastra instance |
|
|
71
|
+
| `requestContext` | `RequestContext` | Request-scoped context map |
|
|
72
|
+
| `tools` | `Record<string, Tool>` | Available tools |
|
|
73
|
+
| `abortSignal` | `AbortSignal` | Request cancellation signal |
|
|
74
|
+
| `taskStore` | `InMemoryTaskStore` | A2A task storage (if configured) |
|
|
75
|
+
|
|
76
|
+
### registerAuthMiddleware()
|
|
77
|
+
|
|
78
|
+
Register authentication and authorization middleware. This method should check if authentication is configured on the Mastra instance and skip registration entirely if not. When auth is configured, you'll typically register two middleware functions: one for authentication (validating tokens and setting the user) and one for authorization (checking if the user can access the requested resource).
|
|
79
|
+
|
|
80
|
+
```typescript copy
|
|
81
|
+
registerAuthMiddleware(): void {
|
|
82
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
83
|
+
if (!authConfig) return;
|
|
84
|
+
|
|
85
|
+
// Register authentication (validate token, set user)
|
|
86
|
+
this.app.use('*', async (req, res, next) => {
|
|
87
|
+
const token = extractToken(req);
|
|
88
|
+
const user = await authConfig.authenticateToken?.(token, req);
|
|
89
|
+
if (!user) {
|
|
90
|
+
return res.status(401).json({ error: 'Unauthorized' });
|
|
91
|
+
}
|
|
92
|
+
res.locals.user = user;
|
|
93
|
+
next();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Register authorization (check permissions)
|
|
97
|
+
this.app.use('*', async (req, res, next) => {
|
|
98
|
+
const allowed = await authConfig.authorize?.(
|
|
99
|
+
req.path,
|
|
100
|
+
req.method,
|
|
101
|
+
res.locals.user,
|
|
102
|
+
res
|
|
103
|
+
);
|
|
104
|
+
if (!allowed) {
|
|
105
|
+
return res.status(403).json({ error: 'Forbidden' });
|
|
106
|
+
}
|
|
107
|
+
next();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### registerRoute()
|
|
113
|
+
|
|
114
|
+
Register a single route with your framework. This method is called once for each Mastra route during initialization. It receives a `ServerRoute` object containing the path, HTTP method, handler function, and Zod schemas for validation. Your implementation should wire this up to your framework's routing system.
|
|
115
|
+
|
|
116
|
+
```typescript copy
|
|
117
|
+
async registerRoute(
|
|
118
|
+
app: MyApp,
|
|
119
|
+
route: ServerRoute,
|
|
120
|
+
{ prefix }: { prefix?: string }
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
const path = `${prefix || ''}${route.path}`;
|
|
123
|
+
const method = route.method.toLowerCase();
|
|
124
|
+
|
|
125
|
+
app[method](path, async (req, res) => {
|
|
126
|
+
try {
|
|
127
|
+
// 1. Extract parameters
|
|
128
|
+
const params = await this.getParams(route, req);
|
|
129
|
+
|
|
130
|
+
// 2. Validate with Zod schemas
|
|
131
|
+
const queryParams = await this.parseQueryParams(route, params.queryParams);
|
|
132
|
+
const body = await this.parseBody(route, params.body);
|
|
133
|
+
|
|
134
|
+
// 3. Build handler params
|
|
135
|
+
const handlerParams = {
|
|
136
|
+
...params.urlParams,
|
|
137
|
+
...queryParams,
|
|
138
|
+
...(typeof body === 'object' ? body : {}),
|
|
139
|
+
mastra: this.mastra,
|
|
140
|
+
requestContext: res.locals.requestContext,
|
|
141
|
+
tools: res.locals.tools,
|
|
142
|
+
abortSignal: res.locals.abortSignal,
|
|
143
|
+
taskStore: this.taskStore,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// 4. Call handler
|
|
147
|
+
const result = await route.handler(handlerParams);
|
|
148
|
+
|
|
149
|
+
// 5. Send response
|
|
150
|
+
return this.sendResponse(route, res, result);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
const status = error.status ?? error.details?.status ?? 500;
|
|
153
|
+
return res.status(status).json({ error: error.message });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### getParams()
|
|
160
|
+
|
|
161
|
+
Extract URL parameters, query parameters, and request body from the incoming request. Different frameworks expose these values in different ways—Express uses `req.params`, `req.query`, and `req.body`, while other frameworks may use different property names or require method calls. This method normalizes the extraction for your framework.
|
|
162
|
+
|
|
163
|
+
```typescript copy
|
|
164
|
+
async getParams(
|
|
165
|
+
route: ServerRoute,
|
|
166
|
+
request: MyRequest
|
|
167
|
+
): Promise<{
|
|
168
|
+
urlParams: Record<string, string>;
|
|
169
|
+
queryParams: Record<string, string>;
|
|
170
|
+
body: unknown;
|
|
171
|
+
}> {
|
|
172
|
+
return {
|
|
173
|
+
// From route path (e.g., :agentId)
|
|
174
|
+
urlParams: request.params,
|
|
175
|
+
// From URL query string
|
|
176
|
+
queryParams: request.query,
|
|
177
|
+
// From request body
|
|
178
|
+
body: request.body,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### sendResponse()
|
|
184
|
+
|
|
185
|
+
Send the response back to the client based on the route's response type. Mastra routes can return different response types: JSON for most API responses, streams for agent generation, and special types for MCP transports. Your implementation should handle each type appropriately for your framework.
|
|
186
|
+
|
|
187
|
+
```typescript copy
|
|
188
|
+
async sendResponse(
|
|
189
|
+
route: ServerRoute,
|
|
190
|
+
response: MyResponse,
|
|
191
|
+
result: unknown
|
|
192
|
+
): Promise<unknown> {
|
|
193
|
+
switch (route.responseType) {
|
|
194
|
+
case 'json':
|
|
195
|
+
return response.json(result);
|
|
196
|
+
|
|
197
|
+
case 'stream':
|
|
198
|
+
return this.stream(route, response, result);
|
|
199
|
+
|
|
200
|
+
case 'datastream-response':
|
|
201
|
+
// Return AI SDK Response directly
|
|
202
|
+
return result;
|
|
203
|
+
|
|
204
|
+
case 'mcp-http':
|
|
205
|
+
// Handle MCP HTTP transport
|
|
206
|
+
return this.handleMcpHttp(response, result);
|
|
207
|
+
|
|
208
|
+
case 'mcp-sse':
|
|
209
|
+
// Handle MCP SSE transport
|
|
210
|
+
return this.handleMcpSse(response, result);
|
|
211
|
+
|
|
212
|
+
default:
|
|
213
|
+
return response.json(result);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### stream()
|
|
219
|
+
|
|
220
|
+
Handle streaming responses for agent generation. When an agent generates a response, it produces a stream of chunks that should be sent to the client as they become available. This method reads from the stream, optionally applies redaction to hide sensitive data, and writes chunks to the response in the appropriate format (SSE or newline-delimited JSON).
|
|
221
|
+
|
|
222
|
+
```typescript copy
|
|
223
|
+
async stream(
|
|
224
|
+
route: ServerRoute,
|
|
225
|
+
response: MyResponse,
|
|
226
|
+
result: unknown
|
|
227
|
+
): Promise<unknown> {
|
|
228
|
+
const isSSE = route.streamFormat === 'sse';
|
|
229
|
+
|
|
230
|
+
// Set streaming headers based on format
|
|
231
|
+
response.setHeader('Content-Type', isSSE ? 'text/event-stream' : 'text/plain');
|
|
232
|
+
response.setHeader('Transfer-Encoding', 'chunked');
|
|
233
|
+
|
|
234
|
+
const reader = result.fullStream.getReader();
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
while (true) {
|
|
238
|
+
const { done, value } = await reader.read();
|
|
239
|
+
if (done) break;
|
|
240
|
+
|
|
241
|
+
// Apply redaction if enabled
|
|
242
|
+
const chunk = this.streamOptions.redact
|
|
243
|
+
? redactChunk(value)
|
|
244
|
+
: value;
|
|
245
|
+
|
|
246
|
+
// Format based on stream format
|
|
247
|
+
if (isSSE) {
|
|
248
|
+
response.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
|
249
|
+
} else {
|
|
250
|
+
response.write(JSON.stringify(chunk) + '\x1E');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Send completion marker (SSE uses data: [DONE], other formats use record separator)
|
|
255
|
+
if (isSSE) {
|
|
256
|
+
response.write('data: [DONE]\n\n');
|
|
257
|
+
}
|
|
258
|
+
response.end();
|
|
259
|
+
} catch (error) {
|
|
260
|
+
reader.cancel();
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Helper methods
|
|
267
|
+
|
|
268
|
+
The base class provides helper methods you can use in your implementation. These handle common tasks like parameter validation and route registration, so you don't need to reimplement them:
|
|
269
|
+
|
|
270
|
+
| Method | Description |
|
|
271
|
+
|--------|-------------|
|
|
272
|
+
| `parsePathParams(route, params)` | Validate path params with Zod schema |
|
|
273
|
+
| `parseQueryParams(route, params)` | Validate query params with Zod schema |
|
|
274
|
+
| `parseBody(route, body)` | Validate body with Zod schema |
|
|
275
|
+
| `mergeRequestContext({ paramsRequestContext, bodyRequestContext })` | Merge request context from multiple sources |
|
|
276
|
+
| `registerRoutes()` | Register all Mastra routes (calls `registerRoute` for each) |
|
|
277
|
+
| `registerOpenAPIRoute(app, config, { prefix })` | Register OpenAPI spec endpoint |
|
|
278
|
+
|
|
279
|
+
The `parse*` methods use Zod schemas defined on each route to validate input and return typed results. If validation fails, they throw an error with details about what went wrong.
|
|
280
|
+
|
|
281
|
+
## Constructor
|
|
282
|
+
|
|
283
|
+
Your adapter's constructor should accept the same options as the base class and pass them to `super()`. You can add additional framework-specific options if needed:
|
|
284
|
+
|
|
285
|
+
```typescript copy
|
|
286
|
+
constructor(options: {
|
|
287
|
+
app: MyApp;
|
|
288
|
+
mastra: Mastra;
|
|
289
|
+
prefix?: string;
|
|
290
|
+
openapiPath?: string;
|
|
291
|
+
bodyLimitOptions?: BodyLimitOptions;
|
|
292
|
+
streamOptions?: StreamOptions;
|
|
293
|
+
customRouteAuthConfig?: Map<string, boolean>;
|
|
294
|
+
}) {
|
|
295
|
+
super(options);
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
See [Server Adapters](/docs/v1/server-db/server-adapters#constructor-options) for full documentation on each option.
|
|
300
|
+
|
|
301
|
+
## Full example
|
|
302
|
+
|
|
303
|
+
Here's a skeleton implementation showing all the required methods. This uses pseudocode for framework-specific parts—replace with your framework's actual APIs:
|
|
304
|
+
|
|
305
|
+
```typescript title="my-framework-adapter.ts" copy
|
|
306
|
+
import { MastraServer, ServerRoute } from '@mastra/server/server-adapter';
|
|
307
|
+
import type { Mastra } from '@mastra/core';
|
|
308
|
+
|
|
309
|
+
export class MyFrameworkServer extends MastraServer<MyApp, MyRequest, MyResponse> {
|
|
310
|
+
constructor(options: { app: MyApp; mastra: Mastra; prefix?: string }) {
|
|
311
|
+
super(options);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
registerContextMiddleware(): void {
|
|
315
|
+
this.app.use('*', (req, res, next) => {
|
|
316
|
+
res.locals.mastra = this.mastra;
|
|
317
|
+
res.locals.requestContext = this.mergeRequestContext({
|
|
318
|
+
paramsRequestContext: req.query.requestContext,
|
|
319
|
+
bodyRequestContext: req.body?.requestContext,
|
|
320
|
+
});
|
|
321
|
+
res.locals.tools = this.tools ?? {};
|
|
322
|
+
res.locals.abortSignal = createAbortSignal(req);
|
|
323
|
+
next();
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
registerAuthMiddleware(): void {
|
|
328
|
+
const authConfig = this.mastra.getServer()?.auth;
|
|
329
|
+
if (!authConfig) return;
|
|
330
|
+
// ... implement auth middleware
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async registerRoute(app: MyApp, route: ServerRoute, { prefix }: { prefix?: string }): Promise<void> {
|
|
334
|
+
// ... implement route registration
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async getParams(route: ServerRoute, request: MyRequest) {
|
|
338
|
+
return {
|
|
339
|
+
urlParams: request.params,
|
|
340
|
+
queryParams: request.query,
|
|
341
|
+
body: request.body,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async sendResponse(route: ServerRoute, response: MyResponse, result: unknown) {
|
|
346
|
+
if (route.responseType === 'stream') {
|
|
347
|
+
return this.stream(route, response, result);
|
|
348
|
+
}
|
|
349
|
+
return response.json(result);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async stream(route: ServerRoute, response: MyResponse, result: unknown) {
|
|
353
|
+
// ... implement streaming
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Usage
|
|
359
|
+
|
|
360
|
+
Once your adapter is implemented, use it the same way as the provided adapters:
|
|
361
|
+
|
|
362
|
+
```typescript title="server.ts" copy
|
|
363
|
+
import { MyFrameworkServer } from './my-framework-adapter';
|
|
364
|
+
import { mastra } from './mastra';
|
|
365
|
+
|
|
366
|
+
const app = createMyFrameworkApp();
|
|
367
|
+
const server = new MyFrameworkServer({ app, mastra });
|
|
368
|
+
|
|
369
|
+
await server.init();
|
|
370
|
+
|
|
371
|
+
app.listen(4111);
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
:::tip
|
|
375
|
+
|
|
376
|
+
The existing [@mastra/hono](https://github.com/mastra-ai/mastra/blob/main/server-adapters/hono/src/index.ts) and [@mastra/express](https://github.com/mastra-ai/mastra/blob/main/server-adapters/express/src/index.ts) implementations are good references when building your custom adapter. They show how to handle framework-specific patterns for context storage, middleware registration, and response handling.
|
|
377
|
+
|
|
378
|
+
:::
|
|
379
|
+
|
|
380
|
+
## Related
|
|
381
|
+
|
|
382
|
+
- [Server Adapters](/docs/v1/server-db/server-adapters) - Overview and shared concepts
|
|
383
|
+
- [Hono Adapter](/reference/v1/server/hono-adapter) - Reference implementation
|
|
384
|
+
- [Express Adapter](/reference/v1/server/express-adapter) - Reference implementation
|
|
385
|
+
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
386
|
+
- [createRoute() Reference](/reference/v1/server/create-route) - Creating type-safe custom routes
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Custom API Routes | Server & DB
|
|
2
|
+
title: "Custom API Routes | Server & DB"
|
|
3
3
|
description: "Expose additional HTTP endpoints from your Mastra server."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Custom API Routes
|
|
7
7
|
|
|
8
|
-
By default Mastra automatically exposes registered agents and workflows via
|
|
8
|
+
By default, Mastra automatically exposes registered agents and workflows via its server. For additional behavior you can define your own HTTP routes.
|
|
9
9
|
|
|
10
|
-
Routes are provided with a helper `registerApiRoute` from `@mastra/core/server`. Routes can live in the same file as the `Mastra` instance but separating them helps keep configuration concise.
|
|
10
|
+
Routes are provided with a helper `registerApiRoute()` from `@mastra/core/server`. Routes can live in the same file as the `Mastra` instance but separating them helps keep configuration concise.
|
|
11
11
|
|
|
12
|
-
```typescript title="src/mastra/index.ts" copy
|
|
12
|
+
```typescript title="src/mastra/index.ts" copy
|
|
13
13
|
import { Mastra } from "@mastra/core";
|
|
14
14
|
import { registerApiRoute } from "@mastra/core/server";
|
|
15
15
|
|
|
@@ -39,9 +39,9 @@ curl http://localhost:4111/my-custom-route
|
|
|
39
39
|
|
|
40
40
|
Each route's handler receives the Hono `Context`. Within the handler you can access the `Mastra` instance to fetch or call agents and workflows.
|
|
41
41
|
|
|
42
|
-
To add route-specific middleware pass a `middleware` array when calling `registerApiRoute`.
|
|
42
|
+
To add route-specific middleware pass a `middleware` array when calling `registerApiRoute()`.
|
|
43
43
|
|
|
44
|
-
```typescript title="src/mastra/index.ts" copy
|
|
44
|
+
```typescript title="src/mastra/index.ts" copy
|
|
45
45
|
import { Mastra } from "@mastra/core";
|
|
46
46
|
import { registerApiRoute } from "@mastra/core/server";
|
|
47
47
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Mastra Client SDK | Server & DB
|
|
2
|
+
title: "Mastra Client SDK | Server & DB"
|
|
3
3
|
description: "Learn how to set up and use the Mastra Client SDK"
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ import TabItem from "@theme/TabItem";
|
|
|
8
8
|
|
|
9
9
|
# Mastra Client SDK
|
|
10
10
|
|
|
11
|
-
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](/docs/v1/server-db/
|
|
11
|
+
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](/docs/v1/server-db/mastra-server) from your client environment.
|
|
12
12
|
|
|
13
13
|
## Prerequisites
|
|
14
14
|
|
|
@@ -240,7 +240,6 @@ const handleClientTool = async () => {
|
|
|
240
240
|
This is a standard Mastra [agent](../agents/overview#setting-up-agents) configured to return hex color codes, intended to work with the browser-based client tool defined above.
|
|
241
241
|
|
|
242
242
|
```typescript title="src/mastra/agents/color-agent" showLineNumbers copy
|
|
243
|
-
import { openai } from "@ai-sdk/openai";
|
|
244
243
|
import { Agent } from "@mastra/core/agent";
|
|
245
244
|
|
|
246
245
|
export const colorAgent = new Agent({
|
|
@@ -249,7 +248,7 @@ export const colorAgent = new Agent({
|
|
|
249
248
|
instructions: `You are a helpful CSS assistant.
|
|
250
249
|
You can change the background color of web pages.
|
|
251
250
|
Respond with a hex reference for the color requested by the user`,
|
|
252
|
-
model: openai
|
|
251
|
+
model: "openai/gpt-5.1",
|
|
253
252
|
});
|
|
254
253
|
```
|
|
255
254
|
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "
|
|
3
|
-
description: "
|
|
2
|
+
title: "Server Configuration | Server & DB"
|
|
3
|
+
description: "Configure the Mastra server with custom settings for port, timeout, CORS, and more."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# Server Configuration
|
|
7
7
|
|
|
8
8
|
When deploying your Mastra application to production, it runs as an HTTP server that exposes your agents, workflows, and other functionality as API endpoints. This page covers how to configure and customize the server for a production environment.
|
|
9
9
|
|
|
10
|
+
:::info
|
|
11
|
+
|
|
12
|
+
This page covers the `server` configuration options passed to the `Mastra` constructor. For running Mastra with your own HTTP server (Hono, Express, etc.), see [Server Adapters](/docs/v1/server-db/server-adapters).
|
|
13
|
+
|
|
14
|
+
:::
|
|
15
|
+
|
|
10
16
|
## Server architecture
|
|
11
17
|
|
|
12
18
|
Mastra uses [Hono](https://hono.dev) as its underlying HTTP server framework. When you build a Mastra application using `mastra build`, it generates a Hono-based HTTP server in the `.mastra` directory.
|
|
@@ -27,23 +33,22 @@ adding additional server behaviour.
|
|
|
27
33
|
|
|
28
34
|
## Server configuration
|
|
29
35
|
|
|
30
|
-
You can configure server `port` and `timeout` in the Mastra instance.
|
|
36
|
+
You can configure server `port`, `host`, `studioBase`, and `timeout` in the Mastra instance.
|
|
31
37
|
|
|
32
|
-
```typescript title="src/mastra/index.ts" copy
|
|
38
|
+
```typescript title="src/mastra/index.ts" copy
|
|
33
39
|
import { Mastra } from "@mastra/core";
|
|
34
40
|
|
|
35
41
|
export const mastra = new Mastra({
|
|
36
|
-
// ...
|
|
37
42
|
server: {
|
|
38
43
|
port: 3000, // Defaults to 4111
|
|
39
|
-
|
|
44
|
+
host: "0.0.0.0", // Defaults to 'localhost'
|
|
45
|
+
studioBase: "/my-mastra-studio", // Sub-path for the studio (optional)
|
|
46
|
+
timeout: 10000, // Defaults to 3 * 60 * 1000 (3 minutes)
|
|
40
47
|
},
|
|
41
48
|
});
|
|
42
49
|
```
|
|
43
50
|
|
|
44
|
-
The `
|
|
45
|
-
`"DELETE"` or `"ALL"`. Using `"ALL"` will cause the handler to be
|
|
46
|
-
invoked for any HTTP method that matches the path.
|
|
51
|
+
The `studioBase` option allows you to host Mastra Studio on a sub-path of your existing application. For example, with `studioBase: "/my-mastra-studio"`, the studio will be available at `http://your-host:port/my-mastra-studio` instead of the root.
|
|
47
52
|
|
|
48
53
|
## TypeScript configuration
|
|
49
54
|
|
|
@@ -72,11 +77,10 @@ Mastra requires `module` and `moduleResolution` values that support modern Node.
|
|
|
72
77
|
|
|
73
78
|
Mastra allows you to configure CORS (Cross-Origin Resource Sharing) settings for your server.
|
|
74
79
|
|
|
75
|
-
```typescript title="src/mastra/index.ts" copy
|
|
80
|
+
```typescript title="src/mastra/index.ts" copy
|
|
76
81
|
import { Mastra } from "@mastra/core";
|
|
77
82
|
|
|
78
83
|
export const mastra = new Mastra({
|
|
79
|
-
// ...
|
|
80
84
|
server: {
|
|
81
85
|
cors: {
|
|
82
86
|
origin: ["https://example.com"], // Allow specific origins or '*' for all
|
|
@@ -87,3 +91,9 @@ export const mastra = new Mastra({
|
|
|
87
91
|
},
|
|
88
92
|
});
|
|
89
93
|
```
|
|
94
|
+
|
|
95
|
+
## Stream data redaction
|
|
96
|
+
|
|
97
|
+
When streaming agent responses, the HTTP streaming layer redacts system prompts, tool definitions, API keys, and similar data from each chunk before sending it to clients. This is enabled by default.
|
|
98
|
+
|
|
99
|
+
If you're using server adapters directly, you can configure redaction behavior. See [Stream data redaction](/docs/v1/server-db/server-adapters#stream-data-redaction) in the Server Adapters docs.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Middleware | Server & DB
|
|
2
|
+
title: "Middleware | Server & DB"
|
|
3
3
|
description: "Apply custom middleware functions to intercept requests."
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -64,6 +64,35 @@ registerApiRoute("/my-custom-route", {
|
|
|
64
64
|
|
|
65
65
|
## Common examples
|
|
66
66
|
|
|
67
|
+
### Using `RequestContext`
|
|
68
|
+
|
|
69
|
+
You can populate `RequestContext` dynamically in server middleware by extracting information from the request. In this example, the `temperature-unit` is set based on the Cloudflare `CF-IPCountry` header to ensure responses match the user's locale.
|
|
70
|
+
|
|
71
|
+
```typescript title="src/mastra/index.ts" showLineNumbers copy
|
|
72
|
+
import { Mastra } from "@mastra/core";
|
|
73
|
+
import { RequestContext } from "@mastra/core/request-context";
|
|
74
|
+
import { testWeatherAgent } from "./agents/test-weather-agent";
|
|
75
|
+
|
|
76
|
+
export const mastra = new Mastra({
|
|
77
|
+
agents: { testWeatherAgent },
|
|
78
|
+
server: {
|
|
79
|
+
middleware: [
|
|
80
|
+
async (context, next) => {
|
|
81
|
+
const country = context.req.header("CF-IPCountry");
|
|
82
|
+
const requestContext = context.get("requestContext");
|
|
83
|
+
|
|
84
|
+
requestContext.set(
|
|
85
|
+
"temperature-unit",
|
|
86
|
+
country === "US" ? "fahrenheit" : "celsius",
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
await next();
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
67
96
|
### Authentication
|
|
68
97
|
|
|
69
98
|
```typescript copy
|
|
@@ -143,35 +172,6 @@ be inspected by middleware to tailor behavior:
|
|
|
143
172
|
- `x-mastra-client-type`: identifies the client SDK, e.g. `js` or `python`
|
|
144
173
|
- `x-studio`: request triggered from Studio
|
|
145
174
|
|
|
146
|
-
### Setting `requestContext`
|
|
147
|
-
|
|
148
|
-
You can populate `requestContext` dynamically in server middleware by extracting information from the request. In this example, the `temperature-unit` is set based on the Cloudflare `CF-IPCountry` header to ensure responses match the user's locale.
|
|
149
|
-
|
|
150
|
-
```typescript title="src/mastra/index.ts" showLineNumbers copy
|
|
151
|
-
import { Mastra } from "@mastra/core";
|
|
152
|
-
import { RequestContext } from "@mastra/core/request-context";
|
|
153
|
-
import { testWeatherAgent } from "./agents/test-weather-agent";
|
|
154
|
-
|
|
155
|
-
export const mastra = new Mastra({
|
|
156
|
-
agents: { testWeatherAgent },
|
|
157
|
-
server: {
|
|
158
|
-
middleware: [
|
|
159
|
-
async (context, next) => {
|
|
160
|
-
const country = context.req.header("CF-IPCountry");
|
|
161
|
-
const requestContext = context.get("requestContext");
|
|
162
|
-
|
|
163
|
-
requestContext.set(
|
|
164
|
-
"temperature-unit",
|
|
165
|
-
country === "US" ? "fahrenheit" : "celsius",
|
|
166
|
-
);
|
|
167
|
-
|
|
168
|
-
await next();
|
|
169
|
-
},
|
|
170
|
-
],
|
|
171
|
-
},
|
|
172
|
-
});
|
|
173
|
-
```
|
|
174
|
-
|
|
175
175
|
# Related
|
|
176
176
|
|
|
177
|
-
- [Request Context](
|
|
177
|
+
- [Request Context](/docs/v1/server-db/request-context)
|