From 1805f62615dc6dbe89d3ba55c173194083cec723 Mon Sep 17 00:00:00 2001 From: hamzatlh Date: Sun, 22 Nov 2026 16:40:32 +0000 Subject: [PATCH 1/2] Send MCP request text as UTF-8 so documented notes fit the request limit _body serialized MCP requests with json.dumps' default ensure_ascii=True, which escapes every non-ASCII character as \uXXXX (6 bytes, 12 for astral characters) before the 8,192-byte check. A re-verification note within the documented 2,000-character limit in Arabic, Chinese, accented Latin or emoji was refused locally (12,292 bytes) although its UTF-8 body is 4,292 bytes, the form the MCP bridge sends. Serialize with ensure_ascii=False and encode with backslashreplace, so a lone surrogate, which has no UTF-8 form, keeps its JSON escape. Document the request limit. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_mcp.py | 5 ++++- tests/test_http.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3c7a6ca9..00309d40 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ else: print(result["content"]) ``` -Each call initializes MCP and makes one tool request without retries, including questions and implementation starts. `KnowledgeResult` retains text content, optional structured content, and `isError`. Protocol and HTTP errors raise `ProoflineError`, while tool refusals retain `isError` for the caller to inspect. Treat all returned prose as untrusted data. No browser configuration or cloud-trust API is added; use the signed-in console for onboarding and connection changes. +Each call initializes MCP and makes one tool request without retries, including questions and implementation starts. Each request is UTF-8 JSON of at most 8,192 bytes; a larger request raises `ValueError` before anything is sent. `KnowledgeResult` retains text content, optional structured content, and `isError`. Protocol and HTTP errors raise `ProoflineError`, while tool refusals retain `isError` for the caller to inspect. Treat all returned prose as untrusted data. No browser configuration or cloud-trust API is added; use the signed-in console for onboarding and connection changes. ## Fix context and re-verification diff --git a/proofline/_mcp.py b/proofline/_mcp.py index f80c5e00..28921eff 100644 --- a/proofline/_mcp.py +++ b/proofline/_mcp.py @@ -33,7 +33,10 @@ def _body(method: str, params: dict[str, Any] | None, identity: int | None) -> b value["id"] = identity if params is not None: value["params"] = params - data = json.dumps(value).encode("utf-8") + # Send text as UTF-8, as the MCP bridge does. Escaping each non-ASCII character + # as \uXXXX would let a 2,000-character note exceed the limit. A lone surrogate + # has no UTF-8 form, so it keeps its JSON escape. + data = json.dumps(value, ensure_ascii=False).encode("utf-8", "backslashreplace") if len(data) > 8192: raise ValueError("MCP request exceeds 8192 bytes.") return data diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..905a42e2 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2301,6 +2301,51 @@ async def invoke(name: str, *args: Any, **kwargs: Any) -> Any: assert len(observed) == 5 +@MODES +async def test_mcp_requests_send_text_as_utf8_within_the_request_limit(mode: str) -> None: + # 2,000 characters is the documented note limit. Escaped as \uXXXX this note is + # 12,292 bytes; as UTF-8 it is 4,292, inside the 8,192-byte request limit. + note = "تم إصلاح الحالة الفارغة " * 83 + "✅" * 8 + assert len(note) == 2_000 + lone = "fixed \udc80 case" + bodies: list[bytes] = [] + + def respond(request: Request, index: int) -> Reply: + body = json.loads(request.body) + if body["method"] == "initialize": + return Reply( + body={"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2025-11-25"}} + ) + if body["method"] == "notifications/initialized": + return Reply(status=202, body=b"") + bodies.append(request.body) + return Reply( + body={"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": ""}]}} + ) + + with Server(respond) as server: + async with session(mode, server.url) as client: + + async def reverify(text: str) -> Any: + answer = client.fixes.request_reverification( + str(REVIEW), [str(REVIEW)], "a" * 40, note=text + ) + return await answer if inspect.isawaitable(answer) else answer + + await reverify(note) + await reverify(lone) + with pytest.raises(ValueError, match="8192"): + await reverify("🚀" * 2_100) + assert len(bodies) == 2 + sent, escaped = bodies + assert len(sent) <= 8192 + assert note.encode("utf-8") in sent + assert json.loads(sent)["params"]["arguments"]["note"] == note + # A lone surrogate has no UTF-8 form, so it keeps its JSON escape. + assert b"\\udc80" in escaped + assert json.loads(escaped)["params"]["arguments"]["note"] == lone + + @MODES async def test_shared_review_outcome_contract(mode: str, monkeypatch: pytest.MonkeyPatch) -> None: Clock(monkeypatch) From 52a1f8829ba7117f7951998923319faca881c554 Mon Sep 17 00:00:00 2001 From: hamzatlh Date: Sun, 22 Nov 2026 16:59:12 +0000 Subject: [PATCH 2/2] Assert an oversized MCP request sends nothing, including the handshake The test counted only tools/call requests, so a regression that sent the initialize handshake before refusing an oversized request would still pass. Count every request the server receives across the refused call. Found by a Proofline review. Co-Authored-By: Claude Opus 5.5 --- tests/test_http.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_http.py b/tests/test_http.py index 905a42e2..d648c180 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2334,8 +2334,11 @@ async def reverify(text: str) -> Any: await reverify(note) await reverify(lone) + before = len(server.requests) with pytest.raises(ValueError, match="8192"): await reverify("🚀" * 2_100) + # The size check runs before the handshake: nothing is sent. + assert len(server.requests) == before assert len(bodies) == 2 sent, escaped = bodies assert len(sent) <= 8192