Skip to content

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

Rust

test(mcp,cli): pin initialized as a JSON-RPC notification

Vinícius DS Andradee51644d62362
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index 02150ba4..434a6502 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -11394,9 +11394,11 @@ model = "gpt-5"
assert!(extension.contains(
"async function mcpNotify(method: string, params?: unknown, ctx?: any, signal?: AbortSignal): Promise<void>"
));
- assert!(extension.contains(
- "body: JSON.stringify({ jsonrpc: \"2.0\", method, params: params ?? {} })"
- ));
+ assert!(
+ extension.contains(
+ "body: JSON.stringify({ jsonrpc: \"2.0\", method, params: params ?? {} })"
+ )
+ );
assert!(extension.contains("mcpRpc(\"tools/list\""));
assert!(extension.contains("pi.registerTool"));
assert!(extension.contains("label: tool.name"));
@@ -11432,6 +11434,50 @@ model = "gpt-5"
);
}
+ /// #1136: `notifications/initialized` is a JSON-RPC notification. The
+ /// generated `mcpNotify` helper must omit `id`, must not parse a 2xx
+ /// body (the server answers 202 empty), and must be the only path
+ /// `bootstrapMcpBridge` uses for that method.
+ #[test]
+ fn pi_mcp_bridge_sends_initialized_as_a_jsonrpc_notification() {
+ let extension =
+ build_pi_extension("http://127.0.0.1:49374/base", Some("tok"), None, "denylist");
+
+ let notify_start = extension
+ .find("async function mcpNotify(")
+ .expect("mcpNotify must be generated");
+ let notify_end = extension[notify_start..]
+ .find("function toolInputSchema")
+ .expect("toolInputSchema follows mcpNotify");
+ let notify = &extension[notify_start..notify_start + notify_end];
+ assert!(
+ notify.contains("JSON.stringify({ jsonrpc: \"2.0\", method, params: params ?? {} })"),
+ "notification JSON must omit id: {notify}"
+ );
+ assert!(
+ !notify.contains("\"id\""),
+ "mcpNotify must not mint an id member: {notify}"
+ );
+ assert!(
+ !notify.contains("response.json"),
+ "empty 202 must not be parsed as JSON: {notify}"
+ );
+ assert!(
+ notify.contains("if (!response.ok)"),
+ "non-2xx still fails the notification: {notify}"
+ );
+ assert!(
+ extension.contains(
+ "try { await mcpNotify(\"notifications/initialized\"); } catch (_e) {}"
+ ),
+ "bootstrap must call mcpNotify: {extension}"
+ );
+ assert!(
+ !extension.contains("mcpRpc(\"notifications/initialized\""),
+ "bootstrap must not send initialized through mcpRpc"
+ );
+ }
+
// Windows 11 + Git Bash support matters for regulated enterprise setups
// where Git Bash is the approved shell available from the corporate
// repository, so this installer contract should be exercised anywhere
diff --git a/crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs b/crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs
index 1dcdcad2..ff24341e 100644
--- a/crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs
+++ b/crates/ai-memory-mcp/tests/suite/mcp_stateless_http.rs
@@ -28,6 +28,12 @@ use tower::ServiceExt;
const INITIALIZE: &str = r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}"#;
const TOOLS_CALL_STATUS: &str = r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"memory_status","arguments":{}}}"#;
const TOOLS_LIST: &str = r#"{"jsonrpc":"2.0","id":3,"method":"tools/list","params":{}}"#;
+/// Exact JSON the generated Pi `mcpNotify` posts (#1136).
+const INITIALIZED_NOTIFICATION: &str =
+ r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#;
+/// The previous Pi `mcpRpc` shape: a notification sent as a request (#1136).
+const INITIALIZED_AS_REQUEST: &str =
+ r#"{"jsonrpc":"2.0","id":63,"method":"notifications/initialized","params":{}}"#;
/// Build a `/mcp` router exactly like `serve.rs` does, toggling stateful
/// mode. Returns the `Store` too so the writer actor stays alive for the
@@ -167,6 +173,76 @@ async fn stateless_initialize_returns_json_result() {
);
}
+/// #1136: JSON-RPC 2.0 notifications have no `id`. Streamable HTTP in the
+/// default stateless JSON mode must accept `notifications/initialized` with
+/// 202 and an empty body, which is what the generated Pi `mcpNotify` now
+/// posts and treats as success without calling `response.json()`.
+#[tokio::test]
+async fn stateless_initialized_notification_is_accepted_empty() {
+ let tmp = TempDir::new().unwrap();
+ let (router, _store) = make_router(&tmp, false).await;
+
+ let initialized = router.clone().oneshot(post(INITIALIZE)).await.unwrap();
+ assert_eq!(initialized.status(), StatusCode::OK);
+
+ let resp = router
+ .clone()
+ .oneshot(post(INITIALIZED_NOTIFICATION))
+ .await
+ .unwrap();
+ assert_eq!(
+ resp.status(),
+ StatusCode::ACCEPTED,
+ "a JSON-RPC notification must be 202 Accepted"
+ );
+ let body = body_string(resp).await;
+ assert!(
+ body.is_empty(),
+ "notification response must be empty so clients do not parse JSON, got: {body:?}"
+ );
+
+ let listed = router.oneshot(post(TOOLS_LIST)).await.unwrap();
+ assert_eq!(listed.status(), StatusCode::OK);
+ let listed_body = body_string(listed).await;
+ let json: serde_json::Value =
+ serde_json::from_str(&listed_body).expect("tools/list returns JSON");
+ assert!(
+ json.get("error").is_none(),
+ "tools/list after the notification must succeed: {listed_body}"
+ );
+ assert!(
+ listed_body.contains("memory_status"),
+ "tools/list should name a real tool: {listed_body}"
+ );
+}
+
+/// #1136 adversarial control: the previous Pi generator assigned an `id`,
+/// so rmcp treated `notifications/initialized` as a request method and
+/// returned JSON-RPC -32601. This is the warning operators saw on every
+/// Pi launch. Restoring that payload must still fail this way; a blanket
+/// accept of the method name would hide a client regression.
+#[tokio::test]
+async fn stateless_initialized_sent_as_a_request_is_method_not_found() {
+ let tmp = TempDir::new().unwrap();
+ let (router, _store) = make_router(&tmp, false).await;
+
+ let resp = router.oneshot(post(INITIALIZED_AS_REQUEST)).await.unwrap();
+ assert_eq!(
+ resp.status(),
+ StatusCode::OK,
+ "a JSON-RPC *request* still gets an HTTP 200 envelope, got {}",
+ resp.status()
+ );
+ let body = body_string(resp).await;
+ let json: serde_json::Value = serde_json::from_str(&body)
+ .unwrap_or_else(|e| panic!("expected JSON-RPC error body: {body}\n{e}"));
+ assert_eq!(
+ json["error"]["code"].as_i64(),
+ Some(-32601),
+ "request-shaped initialized must be method-not-found, got: {body}"
+ );
+}
+
async fn rpc(router: &Router, token: &str, body: serde_json::Value) -> serde_json::Value {
let mut request = post_to("/mcp", body.to_string());
request