feat(mcp): add multi-user, multi-namespace, and session support to MCP tools#227
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors namespace initialization from a single global flag to per-namespace tracking with a new _resolve_namespace helper. MCP tool signatures for entity retrieval, trajectory saving, and mutation now accept optional Changes
Sequence Diagram(s)sequenceDiagram
actor Client
participant MCP as MCP Server
participant Resolver as Namespace Resolver
participant Store as Entity Storage Client
Client->>MCP: Call tool (namespace_id?, user_id?, session_id?)
MCP->>Resolver: _resolve_namespace(namespace_id)
alt namespace_id is None
Resolver->>Resolver: Select configured default namespace
else namespace_id provided
Resolver->>Resolver: Use provided namespace
end
Resolver->>Resolver: Check `_initialized_namespaces` cache
alt not initialized
Resolver->>Store: ensure_namespace(effective_namespace)
Store-->>Resolver: namespace ensured
Resolver->>Resolver: Add to `_initialized_namespaces`
end
Resolver-->>MCP: Return resolved namespace
MCP->>Store: Perform operation (resolved_namespace, entity data, metadata:user/session)
Store-->>MCP: Result
MCP-->>Client: Return response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…P tools Add optional user_id, namespace_id, and session_id parameters to all MCP tools. Namespace is resolved per-request with lazy initialization and caching. Trajectory and guideline writes inject user/session metadata. Retrieval stays broad (no user/session filtering) for backward compatibility.
fe357ee to
9454b92
Compare
|
@illeatmyhat @gaodan-fang can you check the issue #225. I am working towards creating that feature in this PR. Let me know if there are issues we need to address. |
- Avoid double ensure_namespace in _resolve_namespace - Redact raw user_id/session_id from INFO logs (moved to DEBUG) - Add owner check to delete_entity matching publish/unpublish pattern - Fix test mock: return_value must be iterable for save_trajectory loop Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@altk_evolve/frontend/mcp/mcp_server.py`:
- Around line 107-118: The cache _initialized_namespaces can become stale if a
namespace is deleted; update the code around where ensure_namespace() is called
(the block that checks default_ns in _initialized_namespaces and the analogous
block at lines 121-134) to handle missing namespaces: when a NamespaceNotFound
(or equivalent) error occurs on subsequent operations, remove/evict that
namespace from _initialized_namespaces and retry ensure_namespace() (or call
ensure_namespace() again on catching NamespaceNotFound) so the namespace is
recreated; ensure you reference and modify the logic that adds to
_initialized_namespaces so eviction and retry are applied symmetrically.
- Around line 329-333: The search_entities call that returns trajectories is
only filtering by metadata.task_id and can leak other users' data; update the
code that calls get_client().search_entities (the block returning
search_entities with namespace_id=resolved_ns, filters={"type": "trajectory",
"metadata.task_id": task_id, ...}) to include metadata.user_id and
metadata.session_id in the filters when those values are present (e.g., from the
current caller/context), constructing the filters dict to add
"metadata.user_id": user_id and "metadata.session_id": session_id conditionally
so the readback is scoped to the caller/session.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: edf4d024-d1fa-4815-aa6d-f9182f68b4a2
📒 Files selected for processing (2)
altk_evolve/frontend/mcp/mcp_server.pytests/unit/test_mcp_server.py
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/unit/test_mcp_server.py
Filter search_entities return by user_id and session_id when present so save_trajectory only returns the caller's own trajectories. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
illeatmyhat
left a comment
There was a problem hiding this comment.
hopefully clients can figure out which namespaces they're using
When a namespace is deleted externally, downstream operations now catch NamespaceNotFoundException, evict the namespace from the initialization cache, and either retry (read/write tools) or return an error (entity mutation tools). The next call re-runs ensure_namespace to recreate the namespace. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
added the isolation details in the issue #225 will also add a readme section on it. |
Document how namespace, user_id, and session_id map to physical storage across filesystem, PostgreSQL, and Milvus backends. Includes comparison tables and key implications for multi-tenant deployments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The "How Each Backend Stores These Layers" table already covers the same information more concisely. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
user_id,namespace_id, andsession_idparameters to all MCP tools (get_entities,get_guidelines,save_trajectory,create_entity,publish_entity,unpublish_entity,delete_entity)_resolve_namespace()— lazily initializes and caches non-default namespaces viaensure_namespace()_namespace_initializedboolean with_initialized_namespaces: set[str]to track multiple namespacesuser_idandsession_idinto trajectory and guideline entity metadata on writesTest plan
namespace_idisNoneensure_namespace)save_trajectoryinjectsuser_idandsession_idinto trajectory and guideline metadatasave_trajectoryuses customnamespace_idfor all storage callsaddresses issue #225
Summary by CodeRabbit