#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
mcp: validate request id type per JSON-RPC 2.0 §4
Rohit Ghumare461ef827fa56
diff --git a/src/mcp/transport.ts b/src/mcp/transport.tsindex 98856175..766e6472 100644--- a/src/mcp/transport.ts+++ b/src/mcp/transport.ts@@ -28,6 +28,19 @@ function isNotification(req: JsonRpcRequest): boolean {return req.id === undefined || req.id === null;}+// Per JSON-RPC 2.0 §4, a valid request id must be a String, Number, or Null+// (Null is technically only allowed in responses; in requests, omitting id+// is the convention for notifications, which we treat the same as null).+// Any other runtime type (object, array, boolean) is an Invalid Request.+function isValidId(id: unknown): id is string | number | null | undefined {+ return (+ id === undefined ||+ id === null ||+ typeof id === "string" ||+ typeof id === "number"+ );+}+// Exported for unit tests so the line-handling logic is exercised// independently of process.stdin / process.stdout.export async function processLine(@@ -52,21 +65,41 @@ export async function processLine(}const request = parsed as JsonRpcRequest;+ const rawId = (request as { id?: unknown } | null)?.id;++ // Invalid request shape (missing/wrong jsonrpc, non-string method).if (!request ||+ typeof request !== "object" ||request.jsonrpc !== "2.0" ||typeof request.method !== "string") {- if (request && (request as { id?: unknown }).id != null) {+ // Echo the id back only if it's a valid string/number. Notifications+ // (missing/null id) and malformed ids both drop silently — we don't+ // want to respond to something that could be a notification, and we+ // can't invent an id for a malformed one.+ if (typeof rawId === "string" || typeof rawId === "number") {writeOut({jsonrpc: "2.0",- id: (request as JsonRpcRequest).id as string | number,+ id: rawId,error: { code: -32600, message: "Invalid Request" },});}return;}+ // Request shape is valid but id may still be of the wrong type+ // (object, array, boolean). Per the spec, that's an Invalid Request.+ // Respond with id: null because we can't safely echo a non-JSON-RPC id.+ if (!isValidId(rawId)) {+ writeOut({+ jsonrpc: "2.0",+ id: null,+ error: { code: -32600, message: "Invalid Request: id must be string, number, or null" },+ });+ return;+ }+const notification = isNotification(request);try {diff --git a/test/mcp-transport.test.ts b/test/mcp-transport.test.tsindex 681d9c6b..bb8627dc 100644--- a/test/mcp-transport.test.ts+++ b/test/mcp-transport.test.ts@@ -149,4 +149,81 @@ describe("processLine — malformed input", () => {// No jsonrpc field, no id — drop without responding.expect(c.out).toHaveLength(0);});++ it("silently drops a malformed message with a non-primitive id (can't safely echo)", async () => {+ const c = collector();+ await processLine(+ JSON.stringify({ id: { nested: true }, method: "broken" }),+ okHandler,+ c.writeOut,+ c.writeErr,+ );+ // Malformed shape + non-primitive id — can't echo id back, drop silently.+ expect(c.out).toHaveLength(0);+ });+});++describe("processLine — id type validation (JSON-RPC §4)", () => {+ it("rejects a request whose id is an object with -32600 and id: null", async () => {+ const c = collector();+ const handlerCalled = vi.fn(okHandler);+ await processLine(+ JSON.stringify({+ jsonrpc: "2.0",+ id: { bogus: true },+ method: "tools/list",+ }),+ handlerCalled,+ c.writeOut,+ c.writeErr,+ );+ expect(handlerCalled).not.toHaveBeenCalled();+ expect(c.out).toHaveLength(1);+ expect(c.out[0].id).toBeNull();+ expect(c.out[0].error?.code).toBe(-32600);+ expect(c.out[0].error?.message).toContain("id must be");+ });++ it("rejects a request whose id is an array", async () => {+ const c = collector();+ const handlerCalled = vi.fn(okHandler);+ await processLine(+ JSON.stringify({ jsonrpc: "2.0", id: [1, 2], method: "tools/list" }),+ handlerCalled,+ c.writeOut,+ c.writeErr,+ );+ expect(handlerCalled).not.toHaveBeenCalled();+ expect(c.out).toHaveLength(1);+ expect(c.out[0].id).toBeNull();+ expect(c.out[0].error?.code).toBe(-32600);+ });++ it("rejects a request whose id is a boolean", async () => {+ const c = collector();+ const handlerCalled = vi.fn(okHandler);+ await processLine(+ JSON.stringify({ jsonrpc: "2.0", id: true, method: "tools/list" }),+ handlerCalled,+ c.writeOut,+ c.writeErr,+ );+ expect(handlerCalled).not.toHaveBeenCalled();+ expect(c.out).toHaveLength(1);+ expect(c.out[0].id).toBeNull();+ expect(c.out[0].error?.code).toBe(-32600);+ });++ it("accepts a request with string id", async () => {+ const c = collector();+ await processLine(+ JSON.stringify({ jsonrpc: "2.0", id: "abc-123", method: "ping" }),+ okHandler,+ c.writeOut,+ c.writeErr,+ );+ expect(c.out).toHaveLength(1);+ expect(c.out[0].id).toBe("abc-123");+ expect(c.out[0].result).toEqual({ method: "ping" });+ });});