{"record":{"id":"4e5c3a06b9adc1ac","repo":"mastra-ai/mastra","slug":"failed-to-load-resources","errorCode":null,"errorMessage":"Failed to load resources","messagePattern":"Failed to load resources","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mcp/src/server/server.ts","lineNumber":1308,"sourceCode":"          return { resources };\n        } catch (error) {\n          this.logger.error('Error fetching resources', { error });\n          throw error;\n        }\n      });\n    }\n\n    // Read resource handler\n    if (capturedResourceOptions.getResourceContent) {\n      serverInstance.setRequestHandler('resources/read', async (request, ctx) => {\n        const startTime = Date.now();\n        const uri = request.params.uri;\n        this.logger.debug('Handling ReadResource request', { uri });\n\n        // Resolve the resource list for the current caller's `extra` on every request\n        // rather than from a shared cache, so URI resolution respects per-caller auth.\n        const resources = await capturedResourceOptions.listResources?.({ extra: toMCPRequestHandlerExtra(ctx) });\n        if (!resources) throw new Error('Failed to load resources');\n        const resource = resources.find(r => r.uri === uri);\n\n        if (!resource) {\n          this.logger.warn('Unknown resource URI requested', { uri });\n          throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Resource not found: ${uri}`);\n        }\n\n        try {\n          const resourcesOrResourceContent = await capturedResourceOptions.getResourceContent({\n            uri,\n            extra: toMCPRequestHandlerExtra(ctx),\n          });\n          const resourcesContent = Array.isArray(resourcesOrResourceContent)\n            ? resourcesOrResourceContent\n            : [resourcesOrResourceContent];\n          // Preserve the resource's `_meta` on the read contents. MCP Apps hosts\n          // read the UI CSP (connectDomains) from `contents[]._meta.ui.csp`, so\n          // dropping it here silently ignores appResources CSP config.","sourceCodeStart":1290,"sourceCodeEnd":1326,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/mcp/src/server/server.ts#L1290-L1326","documentation":"On a `resources/read` request, the MCPServer calls the configured `listResources` callback (per-caller, respecting auth) to resolve the resource list. If that callback returns undefined/null, the server cannot resolve any resources and throws 'Failed to load resources'.","triggerScenarios":"`capturedResourceOptions.listResources?.({ extra })` returns undefined — i.e. your listResources implementation has no return statement, returns undefined on an error path, or no listResources was provided but a read was attempted on a handler path requiring it.","commonSituations":"Custom listResources implementations that swallow errors in a try/catch and fall through without returning, async callbacks whose result is discarded, or resources option omitted while clients still issue resources/read.","solutions":["Ensure your `listResources` callback always returns an array of resources (return `[]` when there are none).","Check server logs above this error for an exception inside listResources that was caught and turned into a bare return.","Verify the resources option is actually passed when constructing the MCPServer if clients will call resources/read."],"exampleFix":"// before\nlistResources: async ({ extra }) => { await db.connect(); }\n// after\nlistResources: async ({ extra }) => { const rows = await db.getResources(); return rows.map(toMcpResource); }","handlingStrategy":"validation","validationCode":"if (typeof listResources !== 'function') throw new Error('listResources must be provided'); const result = await listResources({ extra }); if (!Array.isArray(result)) throw new Error('listResources must return an array');","typeGuard":"function returnsResourceList(v: unknown): v is { resources: { uri: string }[] } { return !!v && Array.isArray((v as any).resources ?? v); }","tryCatchPattern":"try { const content = await client.readResource({ uri }); } catch (e) { if (String(e?.message) === 'Failed to load resources') { logger.error('server listResources returned undefined; check server callback'); } throw e; }","preventionTips":["Always `return []` from listResources instead of falling through after errors.","Never swallow exceptions inside listResources without returning.","Smoke-test resources/list before advertising resources/read to clients."],"tags":["mcp","resources","callback"],"backgroundTag":"callback-returned-undefined","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}