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..d648c180 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2301,6 +2301,54 @@ 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) + 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 + 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)