Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(mcp): drop the root-level anyOf that 400'd Anthropic-routed clients
AkitaOnRailsef492d0f8ea0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 54fef682..b9b80342 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -77,6 +77,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0||||||| a613bf6d### Fixed+- `memory_read_page` no longer ships a root-level `anyOf` in its tool+ schema (#577): the Anthropic Messages API rejects root combinators+ outright, so any provider-agnostic client routing tools through it+ (OpenCode with an Anthropic key, and every other Messages-API+ consumer) had its entire session 400 before a single tool ran. The+ exactly-one-of-`path`/`query` contract stays in the field+ descriptions and runtime validation; a new fence test asserts NO+ registered tool carries a root combinator so the class cannot return.- Docker-wrapper installs stage hook scripts back to the host-reachable`~/.local/share/ai-memory/hooks` instead of the container's `/data`volume (#581): the #554 change made staging follow the resolved datadiff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex db2028d7..6d772bea 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -1078,22 +1078,18 @@ struct ExploreArgs {workspace: Option<String>,}-// The `anyOf` encodes the "you MUST pass exactly one of path/query"-// contract in the machine-readable schema (issue #155): each branch-// demands the key's PRESENCE via `required` AND a non-null `type`, because-// clients that null-fill defaulted args (OpenCode) would satisfy a bare-// `required` with `path: null` and still hit the runtime error. Encoding-// it here lets schema-respecting clients refuse the invalid call before-// it ever reaches the server.-//-// Moonshot ("moonshot flavored json schema") rejects this root-level-// `anyOf`; Kimi Code sessions get a patched schema instead-// (`moonshot_safe_tool_list`). Every other client keeps this exact shape.+// The "you MUST pass exactly one of path/query" contract lives in the+// field descriptions and the runtime validation, NOT in a root-level+// `anyOf` (#577). The machine-readable encoding (#155) let+// schema-respecting clients refuse a null-filled call pre-flight — but+// the Anthropic Messages API rejects root-level `anyOf`/`oneOf`/`allOf`+// outright, so ANY provider-agnostic client routing tools through it+// (OpenCode with an Anthropic key, and every other Messages-API+// consumer) had its WHOLE session 400 before a single tool ran. One+// wasted round-trip for a null-filling client is a far smaller cost+// than dead sessions on the most common upstream; the per-flavor strip+// machinery (#412) stays for any future dialect that needs it.#[derive(Debug, Serialize, Deserialize, schemars::JsonSchema)]-#[schemars(extend("anyOf" = [- {"required": ["path"], "properties": {"path": {"type": "string"}}},- {"required": ["query"], "properties": {"query": {"type": "string"}}},-]))]struct ReadPageArgs {/// FTS5 query to find the page (searches and returns the top hit's full/// body). You MUST pass exactly one of `query` or `path` — never neither,@@ -7366,23 +7362,49 @@ mod tests {// non-null type — a bare `required` is satisfied by OpenCode-style// `path: null` filling. Pins against a schemars upgrade silently// dropping the `extend` attribute.+ /// #577's class fence: NO registered tool may carry a root-level+ /// combinator — one bad tool 400s the entire session for every+ /// Messages-API-routed client.+ #[tokio::test]+ async fn no_tool_schema_carries_root_combinators() {+ let (_tmp, _store, server, _ws, _pj) = setup_server().await;+ for tool in server.tool_router.list_all() {+ let schema = serde_json::to_value(&tool.input_schema).unwrap();+ for key in ["anyOf", "oneOf", "allOf"] {+ assert!(+ schema.get(key).is_none(),+ "tool `{}` carries root-level `{key}`; Anthropic-routed \+ clients reject the whole session over it",+ tool.name+ );+ }+ }+ }+#[test]- fn read_page_schema_encodes_one_of_path_or_query() {+ fn read_page_schema_carries_no_root_combinators() {+ // #577: the Anthropic Messages API rejects root-level+ // anyOf/oneOf/allOf on input_schema — a tool carrying one kills+ // the WHOLE session for every Messages-API-routed client before+ // any tool runs. The exactly-one contract lives in the field+ // descriptions and runtime validation instead. This pins every+ // tool schema, not just memory_read_page, so the class cannot+ // come back through another tool.let schema = serde_json::to_value(schemars::schema_for!(ReadPageArgs)).unwrap();- let any_of = schema- .get("anyOf")- .and_then(|v| v.as_array())- .unwrap_or_else(|| panic!("schema must carry the anyOf constraint: {schema}"));- for key in ["path", "query"] {- let branch = any_of- .iter()- .find(|b| b["required"] == serde_json::json!([key]))- .unwrap_or_else(|| panic!("missing anyOf branch requiring `{key}`: {schema}"));- assert_eq!(- branch["properties"][key]["type"],- serde_json::json!("string"),- "`{key}` branch must demand a non-null string so null-filling \- clients cannot satisfy it"+ for key in ["anyOf", "oneOf", "allOf"] {+ assert!(+ schema.get(key).is_none(),+ "root-level `{key}` breaks Anthropic-routed clients: {schema}"+ );+ }+ // The contract itself is still declared to the model.+ for field in ["path", "query"] {+ let desc = schema["properties"][field]["description"]+ .as_str()+ .unwrap_or_default();+ assert!(+ desc.contains("exactly one"),+ "`{field}` description must state the exactly-one contract");}}diff --git a/crates/ai-memory-mcp/tests/mcp_stateless_http.rs b/crates/ai-memory-mcp/tests/mcp_stateless_http.rsindex 9187e5de..3a8fb79e 100644--- a/crates/ai-memory-mcp/tests/mcp_stateless_http.rs+++ b/crates/ai-memory-mcp/tests/mcp_stateless_http.rs@@ -287,20 +287,27 @@ async fn stateless_config_strip_strips_root_any_of_without_flavor() {);}-/// The toggle is opt-in: with it off and no flavor marker, the upstream root-/// `anyOf` stays, so schema-respecting clients keep their early refusal-/// (issue #155).+/// #577 inverted the default: the source schema no longer carries a+/// root `anyOf` at all, so even with the strip toggle OFF and no flavor+/// marker, every Messages-API-routed client gets a session-safe schema.+/// (#155's early refusal moved to descriptions + runtime validation.)#[tokio::test]-async fn stateless_config_without_strip_keeps_root_any_of_without_flavor() {+async fn stateless_config_without_strip_is_already_root_combinator_free() {let tmp = TempDir::new().unwrap();let (router, _store) = make_router(&tmp, false).await;let resp = router.oneshot(post_to("/mcp", TOOLS_LIST)).await.unwrap();assert_eq!(resp.status(), StatusCode::OK);let schema = read_page_input_schema(&body_string(resp).await);+ for key in ["anyOf", "oneOf", "allOf"] {+ assert!(+ schema.get(key).is_none(),+ "the default schema must be root-combinator-free (#577): {schema}"+ );+ }assert!(- schema.get("anyOf").is_some(),- "default config must keep the upstream root anyOf: {schema}"+ schema.get("properties").is_some(),+ "the flat schema must keep describing the args: {schema}");}@@ -379,17 +386,20 @@ async fn stateless_moonshot_flavor_keeps_nullable_unions() {);}-/// Control: without the marker, tools/list keeps the upstream root `anyOf`.+/// Unflavored tools/list is root-combinator-free too (#577): the safe+/// shape is the default, not a per-flavor patch.#[tokio::test]-async fn stateless_tools_list_without_flavor_keeps_root_any_of() {+async fn stateless_tools_list_without_flavor_is_root_combinator_free() {let tmp = TempDir::new().unwrap();let (router, _store) = make_router(&tmp, false).await;let resp = router.oneshot(post(TOOLS_LIST)).await.unwrap();assert_eq!(resp.status(), StatusCode::OK);let schema = read_page_input_schema(&body_string(resp).await);- assert!(- schema.get("anyOf").is_some(),- "unflavored tools/list must keep the upstream root anyOf: {schema}"- );+ for key in ["anyOf", "oneOf", "allOf"] {+ assert!(+ schema.get(key).is_none(),+ "unflavored tools/list must be root-combinator-free (#577): {schema}"+ );+ }}