mirror of
https://github.com/mastra-ai/mastra.git
synced 2026-11-17 08:44:46 +00:00
fix(server): limit built-in MCP memory tools to reads and add working-memory inspection (#30245)
## Summary The built-in MCP server (added in #29931, built-in MCP memory tools) gives connected MCP clients a set of memory tools. Two of them did more than an outside client should: `save_messages` wrote raw messages straight into a thread, and `search_memory` ran a broad semantic-recall search. There was also no way to read an agent's working memory through MCP. This PR narrows the built-in MCP memory toolset to inspection: - **Removed `save_messages` and `search_memory` from the built-in MCP toolset.** Their HTTP endpoints are unchanged; only MCP exposure is removed. - **Added read-only `get_working_memory`** for a thread or a resource. It never writes stored state and works in read-only mode. - With a thread (the request's reserved context thread takes precedence over a `threadId` argument), it uses the existing thread working-memory route and its ownership checks. - Without a thread, it uses a new `GET /memory/working-memory` route that reads resource-scoped working memory for the caller's effective resource. When fine-grained authorization (FGA) is configured, the route requires an authenticated user and checks `memory:read` on that resource; a grant on a single thread does not authorize the whole resource. - Regenerated client route types and CLI route metadata for the new route. ## Test plan - New regression tests with real `Memory` + `InMemoryStore`, `Agent` and `Mastra`, through the MCP catalogue and route handlers: tool list, thread and resource reads, FGA allow/deny, missing user, spoofed resource, thread grant vs resource grant, Studio/server FGA provider selection, reserved context thread precedence, and no storage mutation. - These tests fail on the code before the fix (8 failed / 25 passed) and pass after. - Focused MCP/memory/FGA-provider suites: 140/140. - Full server suite: 128 files, 3,552 passed, 3 skipped, 1 todo. - `pnpm build:server`, server TypeScript, lint, formatting, and `generate:permissions` / `check:permissions` pass. - Live loopback MCP SDK client check of `tools/list` and `tools/call`: removed tools are absent, caller identity is enforced, nothing is written. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## ELI5 This change makes the built-in MCP memory tools read-only. It removes the tools that save messages or search memory. It adds one tool that reads working memory for a thread or a resource. ## Summary This change limits the built-in MCP memory toolset to inspection. It removes the `save_messages` and `search_memory` tools from MCP. Their HTTP endpoints remain unchanged. It adds a read-only `get_working_memory` MCP tool. ### Behavior - **Thread reads:** The tool uses the reserved context thread ID when one exists. Otherwise, it uses the `threadId` argument. Thread reads use the existing thread working-memory route. The existing ownership checks apply. - **Resource reads:** When no thread ID is available, the tool calls the new route `GET /memory/working-memory`. The route reads working memory for the effective resource ID. When the tool call omits the resource ID, the tool uses the value from the request context. - **Fine-grained authorization (FGA):** When an FGA provider is configured, the resource route requires an authenticated user and `memory:read` permission on the effective resource. The FGA check passes request context. A grant on one thread does not authorize resource-wide access. - **Memory unavailable:** The resource route returns null working memory when memory is unavailable. - **Thread-scoped requests on the resource route:** The resource route rejects thread-scoped working memory. ### Changed files - `packages/server/src/server/handlers/memory.ts`: Adds MCP metadata to `GET_WORKING_MEMORY_ROUTE`. Adds `GET_RESOURCE_WORKING_MEMORY_ROUTE`. Removes MCP tool metadata from `SAVE_MESSAGES_ROUTE` and `SEARCH_MEMORY_ROUTE`. - `packages/server/src/server/mcp/toolsets/memory.ts`: Adds working-memory retrieval for threads and resources. Removes the search and message-saving operations. - `packages/server/src/server/server-adapter/routes/memory.ts`: Adds the new route to the exported `MEMORY_ROUTES`. - `packages/server/src/server/server-adapter/routes/fga-manifest.ts`: Registers `/memory/working-memory` as a resource route. The resource ID comes from the request context, or from the `resourceId` parameter. - `packages/server/src/server/server-adapter/index.test.ts`: Adds FGA tests for the resource working-memory endpoint. - `packages/server/src/server/mcp/toolsets.test.ts`: Adds tests for FGA provider selection, fail-closed behavior, resource and thread permissions, reserved-context precedence, and read-only inspection. Updates the expected tool list. - `server-adapters/server-adapters-test-suite/src/builtin-mcp-parity-test-suite.ts`: Removes `save_messages` and `search_memory` from the expected tool list. - `server-adapters/server-adapters-test-suite/src/builtin-mcp-toolsets-test-suite.ts`: Adds `get_working_memory` to the parity and read-only cases. Asserts that `save_messages` is not offered to a non-owner and that the HTTP write returns 403. - `server-adapters/server-adapters-test-suite/src/route-test-utils.ts`: Sets a default `resourceId` for `GET /memory/working-memory`. - `.changeset/red-pets-throw.md`: Declares patch releases for `@mastra/client-js`, `@mastra/server`, and `mastra`. The PR also regenerates client route types and CLI route metadata. The diff changes 12 files, with 558 lines added and 57 removed. ### Public API - New route: `GET_RESOURCE_WORKING_MEMORY_ROUTE` at `GET /memory/working-memory`. - Changed export: `MEMORY_ROUTES` includes the new route. - MCP tool surface: `save_messages` and `search_memory` are removed from MCP. `get_working_memory` is added. The HTTP endpoints for the removed tools are unchanged. ### Verification The author reports these results. This summary did not run them: - 140 of 140 focused MCP, memory, and FGA-provider tests pass. - 128 server test files pass, with 3,552 tests passed, 3 skipped, and 1 todo. - Server build, TypeScript, lint, formatting, and permission-generation checks pass. - A live loopback MCP SDK check of `tools/list` and `tools/call` passes. - The author reports that the new regression tests failed before the change (8 failed, 25 passed) and pass after it. CI status on the current head: not available. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: remy-builder[bot] <324360501+remy-builder[bot]@users.noreply.github.com>
This commit is contained in:
parent
174f81a1c9
commit
2087fdccb8
12 changed files with 558 additions and 57 deletions
Loading…
Add table
Add a link
Reference in a new issue