Fix Mastra auth flow and Better Auth base URL#10
Merged
teallarson merged 2 commits intomainfrom Mar 4, 2026
Merged
Conversation
- Replace Mastra MCPClient-based OAuth initiation in the connect route with the MCP SDK's auth() function (initiateOAuth), matching the ai-sdk template. Mastra's transport tried both HTTP and SSE before calling redirectToAuthorization, unreliably saving client registration and PKCE verifier, causing tokens to never be stored after callback. - Add initiateOAuth() export to arcade.ts (Mastra template) - Improve plan route error handling: detect 401/auth errors and show a user-friendly message instead of the raw MCPClientError - Set baseURL on betterAuth() using NEXT_PUBLIC_APP_URL with localhost fallback, fixing "Network error" on sign-in when running without ngrok Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mcpClient.listTools()(Mastra's MCPClient) to trigger OAuth, but Mastra tries both Streamable HTTP and SSE transports before callingredirectToAuthorization— unreliably saving client registration and PKCE verifier. The callback'sauth()call then had no state to work with, so tokens were never stored. Plan route got 401 "Missing Authorization header".initiateOAuth()to Mastra'sarcade.ts(mirrors ai-sdk template) and rewrotedoConnect()/verifyExistingConnection()in the connect route to use itbetterAuth()had nobaseURL, so it couldn't determine the base URL for callbacks/redirects, causing "Network error" on sign-inbaseURLfromNEXT_PUBLIC_APP_URLwithhttp://localhost:<PORT>fallbackMCPClientErrormessage was shown to users on auth failureTest plan
🤖 Generated with Claude Code