[FEATURE] Add a SvelteKit server adapter (@mastra/sveltekit)
Problem Statement
Mastra has official server adapters for Next.js and TanStack Start, but none for SvelteKit. The current SvelteKit guide calls agents directly from custom +server.ts routes (for example /api/chat). That works for a single endpoint, but it doesn't expose Mastra's HTTP API (agents, tools, workflows, memory, MCP, A2A) from the same app. SvelteKit users who want that have to run a separate Mastra server or wire the routes by hand.
Proposed Solution
Add a @mastra/sveltekit package under server-adapters/, modeled on @mastra/tanstack-start. It mounts Mastra through a catch-all rest parameter route:
// src/routes/api/[...path]/+server.ts
import { createSvelteKitRouteHandler } from '@mastra/sveltekit';
import { mastra } from '$lib/server/mastra';
export const { GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD } = createSvelteKitRouteHandler({ mastra });- Options are
mastra,toolsandprefix(default/api), the same as the Next.js and TanStack Start adapters. event.requestis a standardRequest, so the handler forwards it to the Hono-basedMastraServerfrom@mastra/honoand returns theResponseas is. Streaming responses pass through unbuffered.- The handler context is the structural type
{ request: Request }, so the handlers are assignable to SvelteKit'sRequestHandlerwithout a dependency on@sveltejs/kit. - The Hono app is created lazily on the first request. A failed initialization isn't cached, so the next request retries.
I have a working implementation with tests and docs, and I'm happy to open the PR once this is approved.
Component
Server
Alternatives Considered
- Call agents directly from custom
+server.tsroutes, as the current SvelteKit guide does. This only exposes the endpoints you write yourself. - Run a separate Mastra server with
mastra devormastra build. This means a second deployment, and it doesn't share the SvelteKit app's process or configuration.
Example Use Case
As a developer with a SvelteKit app, I want to mount Mastra's HTTP API in the same app so that I can use agents, workflows and memory from the same deployment, and connect tools like @mastra/client-js or Studio (started with mastra studio) to it without running a second server.
Additional Context
Tested in a real SvelteKit 2 app with vite dev: listing agents, generate, streamed responses arriving chunk by chunk, and svelte-check on the exported handlers.
The adapter doesn't handle CORS, like the other Hono-based adapters. Calling the API from another origin needs CORS handling in the SvelteKit app, for example in hooks.server.ts. This will be noted in the reference page.
Verification
- I have searched the existing issues to make sure this is not a duplicate
- I have provided sufficient context for the team to understand the request
Source: mastra-ai/mastra