M
Mastraโ€ข5d ago
Lukas

Can't get mastra + copilotkit memory to work

No matter how I try, I cannot get messages to be saved across sessions. If I refresh the page, it starts a new chat with new memory every single time. I've tried to fix this for hours, reading all docs. Here's some of the code used:
apiRoutes: [
registerCopilotKit<RuntimeContext>({
path: "/copilotkit",
resourceId: "submissionAgent", // Agent name
setContext: (c, runtimeContext) => {
try {
const bearer = c.req.header("Authorization");
let userId = "anonymous";

if (bearer?.startsWith("Bearer ")) {
const token = bearer.slice(7);
try {
const payload = jwt.verify(token, process.env.AUTH_SECRET ?? "changeme") as jwt.JwtPayload;
if (typeof payload.sub === "string") userId = payload.sub;
} catch (err) {
console.warn("[Mastra] JWT verification failed:", err);
}
}

runtimeContext.set("user-id", userId);

// Extract submissionId from X-Submission-ID header for memory isolation
// This header is set by CopilotKit in the frontend
// The resourceId is used by Mastra's memory system to scope conversations
const submissionId = c.req.header("X-Submission-ID");
if (submissionId) {
// Use submissionId as resourceId for memory to isolate conversations per submission
runtimeContext.set("resource-id", `submission-${submissionId}`);
}
} catch (err) {
console.error("[Mastra] Error in setContext:", err);
throw err;
}
}
}),
// Minimal /api/health route for health check
{
path: "/api/health",
method: "GET",
handler: (c) => {
// Respond with 200 OK to indicate server is up
return c.json({ status: "ok" }, 200);
}
}
],
apiRoutes: [
registerCopilotKit<RuntimeContext>({
path: "/copilotkit",
resourceId: "submissionAgent", // Agent name
setContext: (c, runtimeContext) => {
try {
const bearer = c.req.header("Authorization");
let userId = "anonymous";

if (bearer?.startsWith("Bearer ")) {
const token = bearer.slice(7);
try {
const payload = jwt.verify(token, process.env.AUTH_SECRET ?? "changeme") as jwt.JwtPayload;
if (typeof payload.sub === "string") userId = payload.sub;
} catch (err) {
console.warn("[Mastra] JWT verification failed:", err);
}
}

runtimeContext.set("user-id", userId);

// Extract submissionId from X-Submission-ID header for memory isolation
// This header is set by CopilotKit in the frontend
// The resourceId is used by Mastra's memory system to scope conversations
const submissionId = c.req.header("X-Submission-ID");
if (submissionId) {
// Use submissionId as resourceId for memory to isolate conversations per submission
runtimeContext.set("resource-id", `submission-${submissionId}`);
}
} catch (err) {
console.error("[Mastra] Error in setContext:", err);
throw err;
}
}
}),
// Minimal /api/health route for health check
{
path: "/api/health",
method: "GET",
handler: (c) => {
// Respond with 200 OK to indicate server is up
return c.json({ status: "ok" }, 200);
}
}
],
7 Replies
Mastra Triager
Mastra Triagerโ€ข4d ago
๐Ÿ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10816 ๐Ÿ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. ๐Ÿ™ Thank you for helping us improve Mastra!
Abhi Aiyer
Abhi Aiyerโ€ข4d ago
what versions of things are you running
Lukas
LukasOPโ€ข4d ago
"devDependencies": {
"@types/jsonwebtoken": "^9.0.9",
"@types/node": "^24.0.1",
"mastra": "^0.17.7",
"typescript": "^5.8.3"
},
"dependencies": {
"@ag-ui/mastra": "^0.2.0",
"@copilotkit/runtime": "^1.9.0",
"@mastra/agui": "^1.0.8",
"@mastra/core": "^0.23.3",
"@mastra/libsql": "^0.16.1",
"@mastra/loggers": "^0.10.18",
"@mastra/memory": "^0.15.10",
"@openrouter/ai-sdk-provider": "^1.2.0",
"jsonwebtoken": "^9.0.2",
"node-fetch": "^3.3.2",
"reflect-metadata": "^0.2.2"
}
"devDependencies": {
"@types/jsonwebtoken": "^9.0.9",
"@types/node": "^24.0.1",
"mastra": "^0.17.7",
"typescript": "^5.8.3"
},
"dependencies": {
"@ag-ui/mastra": "^0.2.0",
"@copilotkit/runtime": "^1.9.0",
"@mastra/agui": "^1.0.8",
"@mastra/core": "^0.23.3",
"@mastra/libsql": "^0.16.1",
"@mastra/loggers": "^0.10.18",
"@mastra/memory": "^0.15.10",
"@openrouter/ai-sdk-provider": "^1.2.0",
"jsonwebtoken": "^9.0.2",
"node-fetch": "^3.3.2",
"reflect-metadata": "^0.2.2"
}
Abhi Aiyer
Abhi Aiyerโ€ข4d ago
https://github.com/ag-ui-protocol/ag-ui/blob/main/integrations/mastra/typescript/src/copilotkit.ts Resource ID is not being propagated down to the agent on the ag-ui side
GitHub
ag-ui/integrations/mastra/typescript/src/copilotkit.ts at main ยท a...
AG-UI: the Agent-User Interaction Protocol. Bring Agents into Frontend Applications. - ag-ui-protocol/ag-ui
Abhi Aiyer
Abhi Aiyerโ€ข4d ago
I think we should open this issue in #mastra-copilotkit or @Tyler can maybe help
Lukas
LukasOPโ€ข4d ago
alright, but it should be possible to do, its just that I have just made a mistake? Or is this a bug
Abhi Aiyer
Abhi Aiyerโ€ข4d ago
Yes ofcourse if resourceId made it to the agent properly all would work! But setting runtime context like you did does not magically set the resourceId down to the agent In Mastra v1.0.0 there are reserved keys you can use in context mastra__resourceId which would allow this to work Lukas I believe this version of ag-ui is compatible with v1.0.0-beta https://www.npmjs.com/package/@ag-ui/mastra/v/0.2.1-beta.1

Did you find this page helpful?