Skip to content

chore(docs-mcp): Replace Mastra with the AI SDK - #2322

Merged
MathurAditya724 merged 2 commits into
mainfrom
chore/docs-mcp-drop-mastra
Nov 17, 2026
Merged

MathurAditya724 merged 2 commits into
mainfrom
chore/docs-mcp-drop-mastra

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Removes @mastra/core from the private apps/docs-mcp Worker. Mastra was only used there, as a wrapper around the AI SDK (ai 5), which the Worker already depends on. The three agents now call generateText directly. This drops 44 packages from the install and shrinks the Worker bundle from 14.8 MiB (2.74 MiB gzip) to 2.5 MiB (481 KiB gzip).

This removes @mastra/core, which #2318 bumped to clear the @modelcontextprotocol/server advisories. With Mastra gone, docs-mcp no longer pulls @modelcontextprotocol/server in at all. The only copy left is the patched 2.4.0 that @sentry/mcp-core and @sentry/mcp-cloudflare depend on directly.

The Worker's behavior stays the same as with Mastra 1.83.0 (what main runs now): same MCP tools, same /internal/docs API, same outputs, error messages, caching, span attributes, and metrics, and the same requests to OpenRouter. The exceptions are four deliberate differences, listed under Accepted differences.

What changed

  • model-fallback.ts: createNativeModelFallbacks() now returns one AI SDK model instead of a Mastra model list. On every model call it tries the primary, then FALLBACK_MODEL, then KIMI_FALLBACK_MODEL, with the same retries Mastra 1.83 did (see below). It sets response.modelId to the model that answered, so getUsedModelId and the agent.model span attribute keep working. When every model fails, it throws the last model's error. Like Mastra 1.83, it drops temperature for openai/gpt-5.4, which Mastra's provider registry lists as not supporting it.
  • query and tailor agents: generateText with stopWhen: stepCountIs(n) and the same model settings. The last step sends the tools with toolChoice: "none". The tailor synthesis pass sends the run's response messages plus the instruction, with the tools and toolChoice: "none", as Mastra 1.83 did.
  • features agent: Mastra 1.83's structured-output handling is reimplemented in parseFeaturesOutput. It asks for the JSON schema response format on every step and only reads steps that have text, no tool calls, and no error. It throws Structured output was truncated because the model finished with reason "length". for a step that was cut off. Otherwise it strips a <|message|> prefix or a ```json fence, pulls the last JSON object out of surrounding prose, escapes raw control characters, repairs partial JSON, and throws Structured output validation failed: - path: message on a schema mismatch.
  • tools.ts: the tools keep Mastra's wire format. minLength moves into the property description (constraints: minimum length 1), tool results are sent with sorted keys, and invalid tool input comes back to the model as Mastra's { error, message, validationErrors } result. The comment about Mastra vendoring AI SDK schema types is gone.
  • Removed the execa Worker stub and its wrangler alias, which only Mastra needed, and updated the README.

Retry and fallback semantics

Mastra 1.83.0 This PR
Fallback order Primary → FALLBACK_MODEL → Kimi, starting from the primary on every step Same
Retryable error (408, 409, 429, 5xx) on a model that isn't last Retry the same model, at most 2 retries per step across the chain Same
Retry delay 3 s, or the provider's Retry-After / Retry-After-Ms if longer, capped at 30 s Same
Connection reset (ECONNRESET, "socket hang up") 1 s, 2 s, … backoff, capped at 30 s Same
Non-retryable error (such as 400) Next model right away Same
Last model Never retried, and its error is thrown Same
AI SDK and per-model retries Off (maxRetries: 0) Off (maxRetries: 0)

Example: primary 503 three times, then the fallback 503 → primary, primary +3 s, primary +6 s, fallback, Kimi.

The old comment said Mastra retried each model 3 times through p-retry. In 1.83 the per-model maxRetries: 0 turns p-retry off, and the retries above come from Mastra's step-level retry instead. I replaced the comment.

Accepted differences

BYK reviewed these four differences from Mastra 1.83.0 and accepted them as deliberate:

  • Token and step counts after a retry. In Mastra 1.83, after a step retries, the run reports 0 input and output tokens, and the retry counts as an extra step. That's a bookkeeping bug. This PR reports the real usage and step count, so agent.tokens.*, agent.steps, and their metrics only differ in runs that retried.
  • Exception type. Structured-output errors are a plain Error instead of MastraError. The message is the same, but the Sentry issue type changes.
  • Logs. Mastra's own console logs ("Upstream LLM API error", "Error in agent stream") are gone. Instead, one [model-fallback] Error executing model … line is logged per failed attempt. The Worker doesn't send console logs to Sentry.
  • Not ported. Mastra heuristics that this Worker isn't expected to trigger: rewriting message history for provider compatibility, assistant prefill handling, and special handling for quota-error wording.

via @BYK.

--

View Session [Sentry]

Mastra was only used by the docs-mcp Worker, as a wrapper around the AI SDK
it already depends on. Call generateText directly instead, which drops
@mastra/core and its dependency tree from the Worker.

The port keeps what Mastra 1.83 sent to OpenRouter and returned:

- A local fallback model tries the primary model, the standard fallback,
  then Kimi on every step, and reports the model that answered. It retries
  retryable errors on every model but the last, at most twice per step,
  after 3 s or the provider's Retry-After (capped at 30 s), and backs off
  from 1 s on connection resets. It drops temperature for gpt-5.4.
- The last step and the tailor synthesis pass send the tools with
  toolChoice none, and the synthesis history is the run's messages plus
  the instruction.
- The features agent parses its JSON output with Mastra's rules.
- Tool schemas, tool results, and tool input errors keep Mastra's format.

Co-Authored-By: BYK <[email protected]>
@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
sentry-local Ready Ready Preview Nov 17, 2026 5:16pm UTC

Request Review

@BYK
BYK marked this pull request as ready for review November 17, 2026 16:35
@sentry-junior
sentry-junior Bot requested a review from BYK November 17, 2026 16:36
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 17, 2026
@BYK
BYK requested review from MathurAditya724 and removed request for BYK November 17, 2026 16:37
@MathurAditya724
MathurAditya724 merged commit 4555394 into main Nov 17, 2026
43 checks passed
@MathurAditya724
MathurAditya724 deleted the chore/docs-mcp-drop-mastra branch November 17, 2026 21:40
BYK added a commit that referenced this pull request Nov 17, 2026
…ry (#2352)

`pnpm-lock.yaml` on `main` points to
`@ai-sdk/[email protected]([email protected])`, but has no entry for it, so
pnpm rejects the lockfile as broken.

Two PRs merged one after the other, and each lockfile looked fine by
itself:

- #1754 bumped `@ai-sdk/anthropic` to `^3.0.137`. That
made `@ai-sdk/anthropic` and `@ai-sdk/gateway` depend on
`@ai-sdk/[email protected]`. That version was already in the
lockfile because `@mastra/core` pulled it in.
- #2322 removed `@mastra/core`. Its branch was cut
before #1754, so its lockfile dropped `[email protected]` and
`[email protected]`. Git merged the two lockfiles without a conflict, and
`main` was left pointing at entries that no longer exist.

This PR adds back the four missing entries: `packages` and `snapshots`
for `@ai-sdk/[email protected]` and `@ai-sdk/[email protected]`.
Nothing else in the lockfile changes, and Mastra stays out.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A720edeaf6006d91ce693aeda -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/local%3Aweb%3A720edeaf6006d91ce693aeda)
[[Sentry]](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A720edeaf6006d91ce693aeda/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 14ae1214 Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants