Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(mcp): let the stored user key follow the bridge URL, keep root on loopback

AkitaOnRailsa4b86776fe58
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 89542dea..5f56e23a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -86,10 +86,12 @@ version in [`docs/changelog/`](docs/changelog/):
entries keep working. `uninstall --only hooks` keeps the stored key while
such a bridge entry remains. ([#1490](https://github.com/akitaonrails/ai-memory/issues/1490))
-- Scoped implicit bridge credentials to their configured endpoint, rejected a
- malformed stored user key instead of falling through to root, made failed
- installs roll back newly stored credentials, and included the absolute data
- dir in Claude Code's printed session-aware command as well as its JSON entry.
+- Kept the bridge from sending a discovered root key to a non-loopback
+ server, rejected a malformed stored user key instead of falling through to
+ root, made failed installs roll back newly stored credentials, and included
+ the absolute data dir in Claude Code's printed session-aware command as well
+ as its JSON entry.
+ ([#1490](https://github.com/akitaonrails/ai-memory/issues/1490))
## [3.1.2] - 2026-10-20
diff --git a/crates/ai-memory-cli/src/commands/install_mcp.rs b/crates/ai-memory-cli/src/commands/install_mcp.rs
index 2d51cc2f..dab4a105 100644
--- a/crates/ai-memory-cli/src/commands/install_mcp.rs
+++ b/crates/ai-memory-cli/src/commands/install_mcp.rs
@@ -568,7 +568,7 @@ pub(crate) fn mcp_server_url_from_base(server_url: &str) -> String {
}
}
-pub(crate) fn same_mcp_endpoint(left: &str, right: &str) -> bool {
+fn same_mcp_endpoint(left: &str, right: &str) -> bool {
fn normalized(url: &str) -> String {
mcp_server_url_from_base(url).split_once('?').map_or_else(
|| mcp_server_url_from_base(url),
diff --git a/crates/ai-memory-cli/src/commands/mcp_bridge.rs b/crates/ai-memory-cli/src/commands/mcp_bridge.rs
index 8107a30d..d533b2d6 100644
--- a/crates/ai-memory-cli/src/commands/mcp_bridge.rs
+++ b/crates/ai-memory-cli/src/commands/mcp_bridge.rs
@@ -51,7 +51,7 @@ use rmcp::transport::{StreamableHttpClientTransport, Transport, stdio};
use rmcp::{ErrorData as McpError, Peer, ServerHandler, ServiceExt};
use crate::cli::{BridgeAgent, McpBridgeArgs};
-use crate::commands::install_mcp::{mcp_server_url_from_base, same_mcp_endpoint};
+use crate::commands::install_mcp::mcp_server_url_from_base;
use crate::config::Config;
const ACTOR_SESSION_HEADER: HeaderName = HeaderName::from_static("x-memory-actor-session-id");
@@ -260,10 +260,11 @@ fn bridge_base_url<'a>(flag: Option<&'a str>, configured: &'a str) -> &'a str {
.unwrap_or(configured)
}
-/// The bearer the bridge sends. Configured credentials may intentionally
-/// follow an explicit remote URL. Implicit install-local credentials may not:
-/// both the stored hook key and a discovered root key are scoped to the
-/// configured endpoint that caused them to be found.
+/// The bearer the bridge sends: a configured one, else the user key stored
+/// for this install's hooks, else the root key `Config::load` discovered.
+/// Configured and stored keys follow the entry's `--server-url`: the stored
+/// key is the user's own, which the hooks already send to a remote server
+/// (#1490). The discovered root key never leaves loopback.
fn bridge_bearer(config: &Config, server_url: &str) -> Result<Option<String>> {
let usable = |token: &str| !token.trim().is_empty();
if let Some(token) = config
@@ -275,10 +276,6 @@ fn bridge_bearer(config: &Config, server_url: &str) -> Result<Option<String>> {
return Ok(Some(token));
}
- if !same_mcp_endpoint(&config.server_url, server_url) {
- return Ok(None);
- }
-
let stored_path = crate::config::hook_auth_token_path_in(&config.data_dir);
match std::fs::read_to_string(&stored_path) {
Ok(raw) => {
@@ -754,7 +751,7 @@ mod tests {
}
#[test]
- fn implicit_local_credentials_never_follow_a_server_url_override() {
+ fn discovered_root_never_leaves_loopback_but_the_stored_user_key_follows_the_entry() {
let tmp = tempfile::tempdir().unwrap();
let mut config = Config {
data_dir: tmp.path().to_path_buf(),
@@ -767,12 +764,30 @@ mod tests {
loaded: true,
discovered: true,
});
- crate::config::store_hook_auth_token(tmp.path(), "stored-user").unwrap();
+ // Adversarial: a remote --server-url never receives the root key.
assert_eq!(
bridge_bearer(&config, "https://remote.example/mcp").unwrap(),
None
);
+ // Control: a loopback server the config does not name (a packaged
+ // install whose config lives outside the data dir) still gets it.
+ assert_eq!(
+ bridge_bearer(&config, "http://127.0.0.1:41234/mcp")
+ .unwrap()
+ .as_deref(),
+ Some("discovered-root")
+ );
+
+ // The stored key is the user's own, the one the hooks send to that
+ // remote server (#1490), so it follows the entry's URL.
+ crate::config::store_hook_auth_token(tmp.path(), "stored-user").unwrap();
+ assert_eq!(
+ bridge_bearer(&config, "https://remote.example/mcp")
+ .unwrap()
+ .as_deref(),
+ Some("stored-user")
+ );
config.auth.token_file = None;
assert_eq!(
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 0b943138..ea4c6aaa 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -732,9 +732,8 @@ cwd parameters use their canonical names.
implementation. It serves the upstream tool catalogue over local stdio and
delegates tool calls to the configured HTTP server through rmcp's client
transport. It uses the bearer `Config::load` resolved, or the install's stored
-hook key before a loopback-only discovered root key. Implicit install-local
-keys are accepted only for that install's configured endpoint. The server
-therefore keeps the same auth, scope resolver, and tool handlers as direct
+hook key before a discovered root key, which it sends only to a loopback
+server. The server therefore keeps the same auth, scope resolver, and tool handlers as direct
HTTP clients. It has two modes, chosen by the entry that launches it:
- session-aware (Claude Code, `install-mcp --client claude-code
diff --git a/docs/design-marketplace-plugins.md b/docs/design-marketplace-plugins.md
index 30e639d8..40e68b21 100644
--- a/docs/design-marketplace-plugins.md
+++ b/docs/design-marketplace-plugins.md
@@ -326,7 +326,7 @@ install.
| Server URL | `userConfig.server_url` (string) | empty (shipped; proposed `http://127.0.0.1:49374`) | Read by the launcher from `CLAUDE_PLUGIN_OPTION_SERVER_URL`; MCP gets `${user_config.server_url}`. Empty means the `server_url` of the ai-memory config, for hooks and MCP alike |
| Binary path | `userConfig.ai_memory_bin` (string) | `ai-memory` | For installs off `PATH` |
| Data dir | `userConfig.data_dir` (string) | empty | Only for a non-default data dir |
-| Token | **none in the plugin** | — | Hooks read `<data_dir>/auth-token`, the OIDC `auth.json` or a server profile; `mcp-bridge` uses configured auth, then the same stored hook key, then loopback-only discovered root auth. Implicit install-local keys never follow an endpoint override. Same files `setup`, `install-hooks --auth-token` and `auth login` already write |
+| Token | **none in the plugin** | — | Hooks read `<data_dir>/auth-token`, the OIDC `auth.json` or a server profile; `mcp-bridge` uses configured auth, then the same stored hook key, then loopback-only discovered root auth. The discovered root key never leaves loopback. Same files `setup`, `install-hooks --auth-token` and `auth login` already write |
| Project override, capture mode, `ignore_paths`, briefing, server routing | `.ai-memory.toml` marker, persisted capture mode, `servers.toml` profiles | as today | The native hook already enforces all of these. Nothing is duplicated into plugin settings |
Keeping the token out of the plugin keeps one credential store for hooks, MCP