Skip to content

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

Rust

M2: MCP server (rmcp 1.7) + ai-memory serve over stdio and HTTP

AkitaOnRails1654684b800a
diff --git a/Cargo.lock b/Cargo.lock
index 749fa42a..1f84fe66 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -28,17 +28,21 @@ name = "ai-memory-cli"
version = "0.1.0"
dependencies = [
"ai-memory-core",
+ "ai-memory-mcp",
"ai-memory-store",
"ai-memory-wiki",
"anyhow",
+ "axum",
"clap",
"dirs",
"figment",
+ "rmcp",
"serde",
"serde_json",
"sysinfo",
"tempfile",
"tokio",
+ "tokio-util",
"tracing",
"tracing-appender",
"tracing-subscriber",
@@ -81,6 +85,17 @@ name = "ai-memory-mcp"
version = "0.1.0"
dependencies = [
"ai-memory-core",
+ "ai-memory-store",
+ "anyhow",
+ "axum",
+ "rmcp",
+ "schemars",
+ "serde",
+ "serde_json",
+ "tempfile",
+ "thiserror 2.0.18",
+ "tokio",
+ "tracing",
]
[[package]]
@@ -122,6 +137,15 @@ dependencies = [
"tracing",
]
+[[package]]
+name = "android_system_properties"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "anstream"
version = "1.0.0"
@@ -198,6 +222,76 @@ dependencies = [
"bytemuck",
]
+[[package]]
+name = "atomic-waker"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
+
+[[package]]
+name = "autocfg"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
+
+[[package]]
+name = "axum"
+version = "0.8.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90"
+dependencies = [
+ "axum-core",
+ "bytes",
+ "form_urlencoded",
+ "futures-util",
+ "http",
+ "http-body",
+ "http-body-util",
+ "hyper",
+ "hyper-util",
+ "itoa",
+ "matchit",
+ "memchr",
+ "mime",
+ "percent-encoding",
+ "pin-project-lite",
+ "serde_core",
+ "serde_json",
+ "serde_path_to_error",
+ "serde_urlencoded",
+ "sync_wrapper",
+ "tokio",
+ "tower",
+ "tower-layer",
+ "tower-service",
+ "tracing",
+]
+
+[[package]]
+name = "axum-core"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "http",
+ "http-body",
+ "http-body-util",
+ "mime",
+ "pin-project-lite",
+ "sync_wrapper",
+ "tower-layer",
+ "tower-service",
+ "tracing",
+]
+
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
[[package]]
name = "bitflags"
version = "2.11.1"
@@ -247,6 +341,29 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+[[package]]
+name = "chacha20"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601"
+dependencies = [
+ "cfg-if",
+ "cpufeatures 0.3.0",
+ "rand_core",
+]
+
+[[package]]
+name = "chrono"
+version = "0.4.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
+dependencies = [
+ "iana-time-zone",
+ "num-traits",
+ "serde",
+ "windows-link",
+]
+
[[package]]
name = "clap"
version = "4.6.1"
@@ -308,6 +425,15 @@ dependencies = [
"libc",
]
+[[package]]
+name = "cpufeatures"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "crossbeam-channel"
version = "0.5.15"
@@ -352,6 +478,40 @@ dependencies = [
"typenum",
]
+[[package]]
+name = "darling"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0"
+dependencies = [
+ "ident_case",
+ "proc-macro2",
+ "quote",
+ "strsim",
+ "syn",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d"
+dependencies = [
+ "darling_core",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "deranged"
version = "0.5.8"
@@ -403,6 +563,12 @@ dependencies = [
"syn",
]
+[[package]]
+name = "dyn-clone"
+version = "1.0.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555"
+
[[package]]
name = "either"
version = "1.16.0"
@@ -496,12 +662,71 @@ dependencies = [
"libc",
]
+[[package]]
+name = "futures"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-executor",
+ "futures-io",
+ "futures-sink",
+ "futures-task",
+ "futures-util",
+]
+
+[[package]]
+name = "futures-channel"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
+dependencies = [
+ "futures-core",
+ "futures-sink",
+]
+
[[package]]
name = "futures-core"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
+[[package]]
+name = "futures-executor"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d"
+dependencies = [
+ "futures-core",
+ "futures-task",
+ "futures-util",
+]
+
+[[package]]
+name = "futures-io"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718"
+
+[[package]]
+name = "futures-macro"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "futures-sink"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893"
+
[[package]]
name = "futures-task"
version = "0.3.32"
@@ -514,8 +739,13 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
+ "futures-channel",
"futures-core",
+ "futures-io",
+ "futures-macro",
+ "futures-sink",
"futures-task",
+ "memchr",
"pin-project-lite",
"slab",
]
@@ -550,6 +780,7 @@ dependencies = [
"cfg-if",
"libc",
"r-efi",
+ "rand_core",
"wasip2",
"wasip3",
]
@@ -593,6 +824,110 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+[[package]]
+name = "http"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a"
+dependencies = [
+ "bytes",
+ "itoa",
+]
+
+[[package]]
+name = "http-body"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
+dependencies = [
+ "bytes",
+ "http",
+]
+
+[[package]]
+name = "http-body-util"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "http",
+ "http-body",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "httparse"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
+
+[[package]]
+name = "httpdate"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
+
+[[package]]
+name = "hyper"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6299f016b246a94207e63da54dbe807655bf9e00044f73ded42c3ac5305fbcca"
+dependencies = [
+ "atomic-waker",
+ "bytes",
+ "futures-channel",
+ "futures-core",
+ "http",
+ "http-body",
+ "httparse",
+ "httpdate",
+ "itoa",
+ "pin-project-lite",
+ "smallvec",
+ "tokio",
+]
+
+[[package]]
+name = "hyper-util"
+version = "0.1.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0"
+dependencies = [
+ "bytes",
+ "http",
+ "http-body",
+ "hyper",
+ "pin-project-lite",
+ "tokio",
+ "tower-service",
+]
+
+[[package]]
+name = "iana-time-zone"
+version = "0.1.65"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
+dependencies = [
+ "android_system_properties",
+ "core-foundation-sys",
+ "iana-time-zone-haiku",
+ "js-sys",
+ "log",
+ "wasm-bindgen",
+ "windows-core",
+]
+
+[[package]]
+name = "iana-time-zone-haiku"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
+dependencies = [
+ "cc",
+]
+
[[package]]
name = "icu_collections"
version = "2.2.0"
@@ -681,6 +1016,12 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
+[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
[[package]]
name = "idna"
version = "1.1.0"
@@ -899,12 +1240,24 @@ dependencies = [
"regex-automata",
]
+[[package]]
+name = "matchit"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
+
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
+[[package]]
+name = "mime"
+version = "0.3.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
+
[[package]]
name = "mio"
version = "1.2.0"
@@ -981,6 +1334,15 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
+[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -1022,6 +1384,12 @@ dependencies = [
"windows-link",
]
+[[package]]
+name = "pastey"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"
+
[[package]]
name = "pear"
version = "0.2.9"
@@ -1140,6 +1508,23 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
+[[package]]
+name = "rand"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207"
+dependencies = [
+ "chacha20",
+ "getrandom 0.4.2",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
+
[[package]]
name = "rayon"
version = "1.12.0"
@@ -1180,6 +1565,26 @@ dependencies = [
"thiserror 1.0.69",
]
+[[package]]
+name = "ref-cast"
+version = "1.0.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f354300ae66f76f1c85c5f84693f0ce81d747e2c3f21a45fef496d89c960bf7d"
+dependencies = [
+ "ref-cast-impl",
+]
+
+[[package]]
+name = "ref-cast-impl"
+version = "1.0.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b7186006dcb21920990093f30e3dea63b7d6e977bf1256be20c3563a5db070da"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "refinery"
version = "0.8.16"
@@ -1253,6 +1658,50 @@ version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
+[[package]]
+name = "rmcp"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0810a9f717d9828f475fe1f629f4c305c8464b7f496c3a854b58d29e65f4058e"
+dependencies = [
+ "async-trait",
+ "base64",
+ "bytes",
+ "chrono",
+ "futures",
+ "http",
+ "http-body",
+ "http-body-util",
+ "pastey",
+ "pin-project-lite",
+ "rand",
+ "rmcp-macros",
+ "schemars",
+ "serde",
+ "serde_json",
+ "sse-stream",
+ "thiserror 2.0.18",
+ "tokio",
+ "tokio-stream",
+ "tokio-util",
+ "tower-service",
+ "tracing",
+ "uuid",
+]
+
+[[package]]
+name = "rmcp-macros"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6aefac48c364756e97f04c0401ba3231e8607882c7c1d92da0437dc16307904d"
+dependencies = [
+ "darling",
+ "proc-macro2",
+ "quote",
+ "serde_json",
+ "syn",
+]
+
[[package]]
name = "rusqlite"
version = "0.32.1"
@@ -1301,6 +1750,32 @@ dependencies = [
"winapi-util",
]
+[[package]]
+name = "schemars"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2b42f36aa1cd011945615b92222f6bf73c599a102a300334cd7f8dbeec726cc"
+dependencies = [
+ "chrono",
+ "dyn-clone",
+ "ref-cast",
+ "schemars_derive",
+ "serde",
+ "serde_json",
+]
+
+[[package]]
+name = "schemars_derive"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7d115b50f4aaeea07e79c1912f645c7513d81715d0420f8bc77a18c6260b307f"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "serde_derive_internals",
+ "syn",
+]
+
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -1343,6 +1818,17 @@ dependencies = [
"syn",
]
+[[package]]
+name = "serde_derive_internals"
+version = "0.29.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "serde_json"
version = "1.0.150"
@@ -1356,6 +1842,17 @@ dependencies = [
"zmij",
]
+[[package]]
+name = "serde_path_to_error"
+version = "0.1.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457"
+dependencies = [
+ "itoa",
+ "serde",
+ "serde_core",
+]
+
[[package]]
name = "serde_spanned"
version = "0.6.9"
@@ -1365,6 +1862,18 @@ dependencies = [
"serde",
]
+[[package]]
+name = "serde_urlencoded"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
+dependencies = [
+ "form_urlencoded",
+ "itoa",
+ "ryu",
+ "serde",
+]
+
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
@@ -1385,7 +1894,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
- "cpufeatures",
+ "cpufeatures 0.2.17",
"digest",
]
@@ -1442,6 +1951,19 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "sse-stream"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3962b63f038885f15bce2c6e02c0e7925c072f1ac86bb60fd44c5c6b762fb72"
+dependencies = [
+ "bytes",
+ "futures-util",
+ "http-body",
+ "http-body-util",
+ "pin-project-lite",
+]
+
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -1471,6 +1993,12 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "sync_wrapper"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
+
[[package]]
name = "synstructure"
version = "0.13.2"
@@ -1627,6 +2155,31 @@ dependencies = [
"syn",
]
+[[package]]
+name = "tokio-stream"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32da49809aab5c3bc678af03902d4ccddea2a87d028d86392a4b1560c6906c70"
+dependencies = [
+ "futures-core",
+ "pin-project-lite",
+ "tokio",
+]
+
+[[package]]
+name = "tokio-util"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "futures-sink",
+ "futures-util",
+ "pin-project-lite",
+ "tokio",
+]
+
[[package]]
name = "toml"
version = "0.8.23"
@@ -1668,12 +2221,41 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
+[[package]]
+name = "tower"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4"
+dependencies = [
+ "futures-core",
+ "futures-util",
+ "pin-project-lite",
+ "sync_wrapper",
+ "tokio",
+ "tower-layer",
+ "tower-service",
+ "tracing",
+]
+
+[[package]]
+name = "tower-layer"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
+
+[[package]]
+name = "tower-service"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
+
[[package]]
name = "tracing"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
+ "log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
diff --git a/Cargo.toml b/Cargo.toml
index e77ac497..c16d25d5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -40,6 +40,7 @@ serde_yaml = "0.9"
# Async runtime.
tokio = { version = "1", features = ["full"] }
+tokio-util = { version = "0.7", features = ["rt"] }
# Logging / tracing.
tracing = "0.1"
@@ -69,6 +70,11 @@ notify = "8"
notify-debouncer-full = "0.6"
sysinfo = "0.32"
+# MCP server SDK.
+rmcp = { version = "1.7", features = ["server", "macros", "transport-io", "transport-streamable-http-server", "schemars"] }
+schemars = "1"
+axum = "0.8"
+
# Testing.
tempfile = "3"
diff --git a/crates/ai-memory-cli/Cargo.toml b/crates/ai-memory-cli/Cargo.toml
index f561003a..20c61f21 100644
--- a/crates/ai-memory-cli/Cargo.toml
+++ b/crates/ai-memory-cli/Cargo.toml
@@ -14,16 +14,20 @@ path = "src/main.rs"
[dependencies]
ai-memory-core.workspace = true
+ai-memory-mcp.workspace = true
ai-memory-store.workspace = true
ai-memory-wiki.workspace = true
anyhow.workspace = true
+axum.workspace = true
clap.workspace = true
dirs.workspace = true
figment.workspace = true
+rmcp.workspace = true
serde.workspace = true
serde_json.workspace = true
sysinfo.workspace = true
tokio.workspace = true
+tokio-util.workspace = true
tracing.workspace = true
tracing-appender.workspace = true
tracing-subscriber.workspace = true
diff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rs
index c236971d..65205e53 100644
--- a/crates/ai-memory-cli/src/cli.rs
+++ b/crates/ai-memory-cli/src/cli.rs
@@ -37,6 +37,8 @@ pub enum Command {
WritePage(WritePageArgs),
/// Run the filesystem watcher (foreground; Ctrl-C to stop).
Watch(WatchArgs),
+ /// Run the MCP server (with watcher) over stdio or HTTP.
+ Serve(ServeArgs),
/// Wipe the data directory's wiki/, db/, raw/ contents.
Reset(ResetArgs),
}
@@ -89,6 +91,35 @@ pub struct ResetArgs {
pub confirm: bool,
}
+/// Transport for the MCP server.
+#[derive(Debug, Clone, Copy, clap::ValueEnum)]
+pub enum TransportKind {
+ /// Stdio — what `claude mcp add` uses.
+ Stdio,
+ /// Streamable HTTP — for HTTP clients and `mcp-inspector`.
+ Http,
+}
+
+/// Arguments for `serve`.
+#[derive(Debug, Args)]
+pub struct ServeArgs {
+ /// Transport to expose the MCP server on.
+ #[arg(long, value_enum, default_value_t = TransportKind::Stdio)]
+ pub transport: TransportKind,
+ /// Bind address for `--transport http` (default: from config).
+ #[arg(long)]
+ pub bind: Option<String>,
+ /// Skip the filesystem watcher; useful for transient debugging.
+ #[arg(long)]
+ pub no_watcher: bool,
+ /// Workspace name (auto-created).
+ #[arg(long, default_value = "default")]
+ pub workspace: String,
+ /// Project name within the workspace (auto-created).
+ #[arg(long, default_value = "scratch")]
+ pub project: String,
+}
+
/// Arguments for `write-page`.
#[derive(Debug, Args)]
pub struct WritePageArgs {
diff --git a/crates/ai-memory-cli/src/commands/mod.rs b/crates/ai-memory-cli/src/commands/mod.rs
index 9690d3f9..66e26656 100644
--- a/crates/ai-memory-cli/src/commands/mod.rs
+++ b/crates/ai-memory-cli/src/commands/mod.rs
@@ -3,6 +3,7 @@
pub mod init;
pub mod reset;
pub mod search;
+pub mod serve;
pub mod status;
pub mod watch;
pub mod write_page;
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
new file mode 100644
index 00000000..09f8f09b
--- /dev/null
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -0,0 +1,82 @@
+//! `ai-memory serve` — MCP server with optional filesystem watcher.
+
+use ai_memory_mcp::AiMemoryServer;
+use ai_memory_store::Store;
+use ai_memory_wiki::{WatcherHandle, Wiki};
+use anyhow::{Context, Result};
+use rmcp::ServiceExt;
+use rmcp::transport::stdio;
+use rmcp::transport::streamable_http_server::{
+ StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
+};
+use tokio_util::sync::CancellationToken;
+use tracing::info;
+
+use crate::cli::{ServeArgs, TransportKind};
+use crate::config::Config;
+
+/// Run the `serve` subcommand.
+///
+/// # Errors
+/// Returns an error if the store cannot be opened, the watcher cannot
+/// install, or the transport setup fails.
+pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
+ let store = Store::open(&config.data_dir)
+ .with_context(|| format!("opening store at {}", config.data_dir.display()))?;
+ let ws = store
+ .writer
+ .get_or_create_workspace(args.workspace.clone())
+ .await?;
+ let proj = store
+ .writer
+ .get_or_create_project(ws, args.project.clone(), None)
+ .await?;
+ let wiki = Wiki::new(&config.data_dir, store.writer.clone())?;
+
+ // Keep the guard alive for the lifetime of `serve`.
+ let _watcher = if args.no_watcher {
+ info!("watcher disabled by --no-watcher");
+ None
+ } else {
+ info!(
+ root = %wiki.root().display(),
+ workspace = %args.workspace,
+ project = %args.project,
+ "starting wiki watcher",
+ );
+ Some(WatcherHandle::start(wiki.clone(), ws, proj)?)
+ };
+
+ let server = AiMemoryServer::new(store.reader.clone());
+
+ match args.transport {
+ TransportKind::Stdio => {
+ info!("MCP server ready on stdio (Ctrl-C to stop)");
+ let service = server.serve(stdio()).await?;
+ service.waiting().await?;
+ }
+ TransportKind::Http => {
+ let bind = args.bind.unwrap_or_else(|| config.bind.clone());
+ let cancel = CancellationToken::new();
+ let server_clone = server.clone();
+ let service = StreamableHttpService::new(
+ move || Ok(server_clone.clone()),
+ LocalSessionManager::default().into(),
+ StreamableHttpServerConfig::default().with_cancellation_token(cancel.child_token()),
+ );
+ let router = axum::Router::new().nest_service("/mcp", service);
+ let listener = tokio::net::TcpListener::bind(&bind)
+ .await
+ .with_context(|| format!("binding {bind}"))?;
+ info!(%bind, "MCP HTTP server ready (POST /mcp, Ctrl-C to stop)");
+ axum::serve(listener, router)
+ .with_graceful_shutdown(async move {
+ let _ = tokio::signal::ctrl_c().await;
+ info!("ctrl-c received; shutting down");
+ cancel.cancel();
+ })
+ .await?;
+ }
+ }
+ Ok(())
+}
diff --git a/crates/ai-memory-cli/src/main.rs b/crates/ai-memory-cli/src/main.rs
index e25c2023..f2e952e8 100644
--- a/crates/ai-memory-cli/src/main.rs
+++ b/crates/ai-memory-cli/src/main.rs
@@ -41,6 +41,7 @@ async fn main() -> Result<()> {
Command::Search(args) => commands::search::run(&config, args).await,
Command::WritePage(args) => commands::write_page::run(&config, args).await,
Command::Watch(args) => commands::watch::run(&config, args).await,
+ Command::Serve(args) => commands::serve::run(&config, args).await,
Command::Reset(args) => commands::reset::run(&config, args),
}
}
diff --git a/crates/ai-memory-mcp/Cargo.toml b/crates/ai-memory-mcp/Cargo.toml
index 778e9c3d..b648942c 100644
--- a/crates/ai-memory-mcp/Cargo.toml
+++ b/crates/ai-memory-mcp/Cargo.toml
@@ -6,10 +6,23 @@ rust-version.workspace = true
license.workspace = true
repository.workspace = true
authors.workspace = true
-description = "MCP server transport and tool router."
+description = "MCP server transport and tool router for ai-memory."
[dependencies]
ai-memory-core.workspace = true
+ai-memory-store.workspace = true
+anyhow.workspace = true
+thiserror.workspace = true
+serde.workspace = true
+serde_json.workspace = true
+tokio.workspace = true
+tracing.workspace = true
+rmcp.workspace = true
+schemars.workspace = true
+axum.workspace = true
+
+[dev-dependencies]
+tempfile.workspace = true
[lints]
workspace = true
diff --git a/crates/ai-memory-mcp/src/lib.rs b/crates/ai-memory-mcp/src/lib.rs
index 6508be6f..4741b1b6 100644
--- a/crates/ai-memory-mcp/src/lib.rs
+++ b/crates/ai-memory-mcp/src/lib.rs
@@ -1,4 +1,13 @@
//! MCP server for ai-memory.
//!
-//! Wraps the official `rmcp` SDK with the workspace tool surface. Read-only
-//! tools land in milestone M2; write tools and hook ingress follow in M3.
+//! Hosts an [`AiMemoryServer`] that wraps the storage layer and exposes a
+//! deliberately narrow tool surface to coding agents. Read-only tools live
+//! here; write tools (M3+) and consolidation tools (M7+) follow.
+//!
+//! Pin the MCP protocol version explicitly so we never fall into the
+//! agentmemory #510 / #553 "negotiated-down to a version the client
+//! discards tools for" failure mode.
+
+mod server;
+
+pub use server::{AiMemoryServer, MEMORY_INSTRUCTIONS};
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rs
new file mode 100644
index 00000000..c29926ad
--- /dev/null
+++ b/crates/ai-memory-mcp/src/server.rs
@@ -0,0 +1,235 @@
+//! [`AiMemoryServer`] — the MCP server skeleton + tool router.
+
+use ai_memory_store::ReaderPool;
+use rmcp::handler::server::router::tool::ToolRouter;
+use rmcp::handler::server::wrapper::Parameters;
+use rmcp::model::{
+ CallToolResult, Content, Implementation, ProtocolVersion, ServerCapabilities, ServerInfo,
+};
+use rmcp::{ErrorData as McpError, ServerHandler, schemars, tool, tool_handler, tool_router};
+use serde::{Deserialize, Serialize};
+
+/// Instructions surfaced to clients via `ServerInfo`. Short and
+/// agent-readable — Claude Code / Codex will see this in their session
+/// preamble.
+pub const MEMORY_INSTRUCTIONS: &str = "Long-term memory for coding agents. Use \
+memory_query for free-text search, memory_recent to peek at recently-changed \
+pages, and memory_status for counts. All tools are read-only; writes happen \
+automatically via hooks and the watcher.";
+
+/// MCP server backed by the ai-memory store.
+#[derive(Clone)]
+pub struct AiMemoryServer {
+ reader: ReaderPool,
+ default_limit: usize,
+ // Read by the `#[tool_handler]` macro expansion; rustc's dead-code
+ // analysis can't see that, so the lint must be allowed explicitly.
+ #[allow(dead_code)]
+ tool_router: ToolRouter<Self>,
+}
+
+#[derive(Debug, Serialize, Deserialize, schemars::JsonSchema)]
+struct QueryArgs {
+ /// FTS5 query expression (e.g. `"karpathy wiki"` or `quick OR slow`).
+ #[serde(alias = "q", alias = "search")]
+ query: String,
+ /// Maximum number of hits to return (default 10, max 100).
+ #[serde(default, alias = "n", alias = "top_k")]
+ limit: Option<usize>,
+}
+
+#[derive(Debug, Serialize, Deserialize, schemars::JsonSchema)]
+struct RecentArgs {
+ /// Maximum number of recent pages to return (default 10, max 100).
+ #[serde(default, alias = "n")]
+ limit: Option<usize>,
+}
+
+#[derive(Debug, Serialize)]
+struct QueryResponse<T: Serialize> {
+ hits: Vec<T>,
+}
+
+#[derive(Debug, Serialize)]
+struct StatusResponse {
+ counts: ai_memory_store::StatusCounts,
+}
+
+#[tool_router]
+impl AiMemoryServer {
+ /// Construct a server backed by the given reader pool.
+ #[must_use]
+ pub fn new(reader: ReaderPool) -> Self {
+ Self {
+ reader,
+ default_limit: 10,
+ tool_router: Self::tool_router(),
+ }
+ }
+
+ /// Full-text search the wiki via FTS5. Returns up to `limit` hits with
+ /// HTML-marked snippets and a rank score.
+ #[tool(description = "Full-text search the long-term memory wiki via FTS5. \
+ Returns up to `limit` matching pages with HTML-marked snippets and a \
+ rank score (lower rank = better match). Only the latest version of \
+ each page is searched.")]
+ async fn memory_query(
+ &self,
+ Parameters(args): Parameters<QueryArgs>,
+ ) -> Result<CallToolResult, McpError> {
+ let limit = args.limit.unwrap_or(self.default_limit).clamp(1, 100);
+ let hits = self
+ .reader
+ .search_pages(args.query, limit)
+ .await
+ .map_err(|e| McpError::internal_error(e.to_string(), None))?;
+ let response = QueryResponse { hits };
+ ok_json(&response)
+ }
+
+ /// Return the N most-recently-updated pages.
+ #[tool(description = "Return the N most-recently-updated wiki pages \
+ (descending by updated_at). Useful for resuming a session: \
+ the agent can read the last few pages to see what was worked on.")]
+ async fn memory_recent(
+ &self,
+ Parameters(args): Parameters<RecentArgs>,
+ ) -> Result<CallToolResult, McpError> {
+ let limit = args.limit.unwrap_or(self.default_limit).clamp(1, 100);
+ let hits = self
+ .reader
+ .recent_pages(limit)
+ .await
+ .map_err(|e| McpError::internal_error(e.to_string(), None))?;
+ let response = QueryResponse { hits };
+ ok_json(&response)
+ }
+
+ /// Report aggregate counts (pages, sessions, observations).
+ #[tool(description = "Report aggregate memory counts and runtime status \
+ (pages latest, pages all versions, sessions, observations). \
+ Use this at session start to see how much context the agent has \
+ accumulated for this workspace.")]
+ async fn memory_status(&self) -> Result<CallToolResult, McpError> {
+ let counts = self
+ .reader
+ .status_counts()
+ .await
+ .map_err(|e| McpError::internal_error(e.to_string(), None))?;
+ let response = StatusResponse { counts };
+ ok_json(&response)
+ }
+}
+
+#[tool_handler]
+impl ServerHandler for AiMemoryServer {
+ fn get_info(&self) -> ServerInfo {
+ // `Implementation::from_build_env()` reads CARGO_PKG_NAME/VERSION
+ // from *rmcp's* compilation unit, not ours. Patch the fields
+ // post-construction so the wire protocol surfaces "ai-memory".
+ let mut implementation = Implementation::from_build_env();
+ implementation.name = "ai-memory".into();
+ implementation.version = env!("CARGO_PKG_VERSION").into();
+ ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
+ .with_server_info(implementation)
+ .with_protocol_version(ProtocolVersion::V_2024_11_05)
+ .with_instructions(MEMORY_INSTRUCTIONS.to_string())
+ }
+}
+
+fn ok_json<T: Serialize>(value: &T) -> Result<CallToolResult, McpError> {
+ let s = serde_json::to_string_pretty(value)
+ .map_err(|e| McpError::internal_error(e.to_string(), None))?;
+ Ok(CallToolResult::success(vec![Content::text(s)]))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use ai_memory_core::{NewPage, PagePath, Tier};
+ use ai_memory_store::Store;
+ use tempfile::TempDir;
+
+ async fn setup_server() -> (TempDir, Store, AiMemoryServer) {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .unwrap();
+ store
+ .writer
+ .upsert_page(NewPage {
+ workspace_id: ws,
+ project_id: proj,
+ path: PagePath::new("foo.md").unwrap(),
+ title: "Foo".into(),
+ body: "Karpathy says compile, not retrieve.".into(),
+ tier: Tier::Semantic,
+ frontmatter_json: serde_json::json!({}),
+ pinned: false,
+ })
+ .await
+ .unwrap();
+
+ let server = AiMemoryServer::new(store.reader.clone());
+ (tmp, store, server)
+ }
+
+ #[tokio::test]
+ async fn server_constructs_with_tool_router() {
+ let (_tmp, _store, _server) = setup_server().await;
+ }
+
+ #[tokio::test]
+ async fn memory_query_returns_hits_via_tool_method() {
+ let (_tmp, _store, server) = setup_server().await;
+ let result = server
+ .memory_query(Parameters(QueryArgs {
+ query: "karpathy".into(),
+ limit: Some(5),
+ }))
+ .await
+ .unwrap();
+ let text = match result.content.first().and_then(|c| c.as_text()) {
+ Some(t) => t.text.clone(),
+ None => panic!("expected text content"),
+ };
+ assert!(text.contains("foo.md"), "expected hit; got {text}");
+ }
+
+ #[tokio::test]
+ async fn memory_status_returns_counts() {
+ let (_tmp, _store, server) = setup_server().await;
+ let result = server.memory_status().await.unwrap();
+ let text = result
+ .content
+ .first()
+ .and_then(|c| c.as_text())
+ .map(|t| t.text.clone())
+ .unwrap();
+ assert!(text.contains("\"pages_latest\": 1"));
+ }
+
+ #[tokio::test]
+ async fn memory_recent_returns_one_hit() {
+ let (_tmp, _store, server) = setup_server().await;
+ let result = server
+ .memory_recent(Parameters(RecentArgs { limit: Some(5) }))
+ .await
+ .unwrap();
+ let text = result
+ .content
+ .first()
+ .and_then(|c| c.as_text())
+ .map(|t| t.text.clone())
+ .unwrap();
+ assert!(text.contains("foo.md"), "expected hit; got {text}");
+ }
+}
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index 3e75ef51..e300e39b 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -189,6 +189,48 @@ mod tests {
}
}
+ #[tokio::test]
+ async fn recent_pages_returns_latest_only_in_order() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .unwrap();
+ for i in 0..3u8 {
+ store
+ .writer
+ .upsert_page(sample_page(
+ ws,
+ proj,
+ &format!("p{i}.md"),
+ &format!("body-{i}"),
+ ))
+ .await
+ .unwrap();
+ }
+ // Bump the second page to force a later updated_at.
+ store
+ .writer
+ .upsert_page(sample_page(ws, proj, "p1.md", "body-1-rev"))
+ .await
+ .unwrap();
+
+ let hits = store.reader.recent_pages(10).await.unwrap();
+ assert_eq!(hits.len(), 3, "is_latest filter should give us 3 pages");
+ assert_eq!(
+ hits[0].path.as_str(),
+ "p1.md",
+ "most-recently-updated first"
+ );
+ }
+
#[tokio::test]
async fn status_counts_zero_on_fresh_db() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 2998d45f..abfa8651 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -141,6 +141,46 @@ impl ReaderPool {
.await
}
+ /// Return the N most-recently-updated `is_latest = 1` pages.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn recent_pages(&self, limit: usize) -> StoreResult<Vec<PageHit>> {
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare(
+ "SELECT id, path, title, \
+ substr(body, 1, 240) AS snip, \
+ CAST(updated_at AS REAL) AS rank \
+ FROM pages \
+ WHERE is_latest = 1 \
+ ORDER BY updated_at DESC \
+ LIMIT ?1",
+ )?;
+ #[allow(clippy::cast_possible_wrap)]
+ let rows = stmt.query_map(params![limit as i64], |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ let path: String = row.get(1)?;
+ let title: String = row.get(2)?;
+ let snippet: String = row.get(3)?;
+ let rank: f64 = row.get(4)?;
+ Ok((id_bytes, path, title, snippet, rank))
+ })?;
+ let mut hits = Vec::new();
+ for row in rows {
+ let (id_bytes, path, title, snippet, rank) = row?;
+ hits.push(PageHit {
+ id: PageId::from_slice(&id_bytes)?,
+ path: PagePath::new(path)?,
+ title,
+ snippet,
+ rank,
+ });
+ }
+ Ok(hits)
+ })
+ .await
+ }
+
/// Return aggregate counts for the `status` view.
///
/// # Errors