Skip to content

Keep the code and message of MCP JSON-RPC errors - #1

Open
Mr-Noor-aldeen wants to merge 18 commits into
mainfrom
mcp-jsonrpc-errors
Open

Mr-Noor-aldeen wants to merge 18 commits into
mainfrom
mcp-jsonrpc-errors

Conversation

@Mr-Noor-aldeen

Copy link
Copy Markdown

Fixes prooflinedev#161.

_decode raised a fixed "Invalid or refused MCP response." for every JSON-RPC error, so the server's code and message never reached the caller.

A well-formed JSON-RPC error now raises a ProoflineError with the type unchanged (urn:proofline:sdk:mcp), the title set to JSON-RPC error <code>, and the server message as the detail, with the API token redacted through the same redacted helper problem documents use. Anything malformed still raises the fixed refusal: the error must be an object with an integer code (a boolean is rejected) and a string message, and the response cannot carry both error and result. Tool calls still never retry. BearerAuth gains a read-only token property so the MCP layer can redact the credential from error text.

Tests: test_mcp_jsonrpc_error_keeps_code_and_redacted_message and test_mcp_jsonrpc_error_malformed_still_refused (both run sync and async) fail on main and pass with the change. Full suite (238 tests), ruff, mypy, the generate check and the contract check all pass. README updated.

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 2 new findings to review.

View full review in Proofline

Comment thread proofline/_mcp.py
Comment on lines +99 to +100
if "error" in value:
raise _jsonrpc_error(value, token)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Boolean response IDs surface server errors instead of a refusal

In proofline/_mcp.py, _decode matches JSON true to initialization request ID 1 and passes the malformed response to _jsonrpc_error.

Both synchronous and asynchronous callers receive server error text instead of the fixed invalid-response message.

Require an integer response ID, excluding booleans, before comparing it with the request ID.

Details

Before this change, _decode in proofline/_mcp.py rejected every error envelope, even when its ID passed the equality check.

View finding in Proofline

Also at:

Comment thread tests/test_http.py
Comment on lines +2698 to +2701
def respond(request: Request, index: int) -> Reply:
body = json.loads(request.body)
if body["method"] == "initialize":
return Reply(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · The JSON-RPC error test would miss a duplicate tool request

The responder in tests/test_http.py returns the same valid JSON-RPC error on every tool attempt, and the test only checks the final exception.

Either client could retry after the valid error and still pass the test.

Assert that the server received exactly one tools/call request in each mode.

Details

The new valid-error path in _decode (proofline/_mcp.py) is distinct from the malformed responses counted by test_mcp_jsonrpc_error_malformed_still_refused (tests/test_http.py). A retry limited to well-formed errors would not affect that count.

View finding in Proofline

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 1 new finding to review.

View full review in Proofline

This change got a quick review of its diff. Review this change in more depth

Comment thread tests/test_http.py
async def test_mcp_jsonrpc_error_malformed_still_refused(mode: str) -> None:
payloads = [
# The response id must be the request's integer id; a bool is not one.
{"jsonrpc": "2.0", "id": True, "error": {"code": -32602, "message": "refused"}},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Boolean response ID test passes without the boolean guard

The malformed-response test in tests/test_http.py sends True for request ID 2, so _decode in proofline/_mcp.py refuses it on ID mismatch even without the boolean guard.

Deleting the guard leaves the tests passing while allowing True to match initialization ID 1.

Return True with an otherwise valid initialization result and assert refusal before any tool call.

Details

The affected test is test_mcp_jsonrpc_error_malformed_still_refused in tests/test_http.py. Both sync and async callers use integer ID 1 for initialization and 2 for the tool call; the fixture sends the boolean only for the latter.

View finding in Proofline

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 5 new findings to review.

View full review in Proofline

Comment thread proofline/_mcp.py Outdated
Comment on lines +58 to +60
def _token(http: httpx2.Client | httpx2.AsyncClient) -> SecretStr | None:
auth = http.auth
return auth.token if isinstance(auth, BearerAuth) else None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serious · MCP error details can leak bearer tokens set in headers

_token in proofline/_mcp.py checks only BearerAuth from proofline/_transport.py, so an echoed token supplied through an Authorization header bypasses redaction.

The raw credential reaches callers through ProoflineError.detail in proofline/_transport.py.

Redact against the bearer credential in the actual request associated with the response.

Details

Both KnowledgeClient and AsyncKnowledgeClient in proofline/knowledge.py accept caller-provided HTTP clients through their inherited constructors. Before this change, proofline/_mcp.py rejected JSON-RPC errors without preserving their message.

View finding in Proofline

Also at:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in this version: Proofline found the issue again. Proofline's finding in this version: MCP errors can expose bearer tokens set by custom auth. View review

Comment thread proofline/_mcp.py
Comment on lines 93 to +96
if (
not isinstance(value, dict)
or value.get("jsonrpc") != "2.0"
or not isinstance(value.get("id"), int)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · MCP tests would miss acceptance of a float response ID

Removing the integer check in _decode (proofline/_mcp.py) would leave the ID assertions in tests/test_http.py unchanged: boolean IDs are still rejected, and every other fixture ID is an integer.

Both clients could accept 2.0 as matching request ID 2 without a test catching it.

Add an otherwise valid JSON-RPC error with ID 2.0 to the malformed-response matrix and require the fixed refusal.

Details

The test_mcp_jsonrpc_error_malformed_still_refused matrix in tests/test_http.py uses True for request 2, which also fails equality. The test_mcp_jsonrpc_bool_id_rejected_before_tool_call case matches True to request 1 but still exercises only the separate boolean guard. This assessment isolates removal of the integer check once the file is collectable.

View finding in Proofline

Also at:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in this version: Proofline found the issue again. Proofline's finding in this version: MCP tests miss a floating-point response ID that matches the request. View review

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in this version: Proofline found the issue again. Proofline's finding in this version: Tests miss a float response ID matching the tool request ID. View review

Comment thread README.md Outdated
```

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. The SDK does not include a browser configuration or cloud-trust API; 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. `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. A well-formed JSON-RPC error raises `ProoflineError` with type `urn:proofline:sdk:mcp`, a `title` of `JSON-RPC error <code>`, and the server message as `detail` with the credential redacted; a malformed error raises the fixed `Invalid or refused MCP response.` refusal instead. Treat all returned prose as untrusted data. The SDK does not include a browser configuration or cloud-trust API; use the signed-in console for onboarding and connection changes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Mixed-case JSON media types hide JSON-RPC error details

README.md promises to preserve well-formed JSON-RPC errors, but _decode in proofline/_mcp.py rejects Application/JSON before parsing the response.

Callers get a generic refusal instead of the error code in title and the redacted message in detail.

Lowercase the media type after stripping parameters and whitespace, before comparing it with application/json.

Details

The case-sensitive check predates this change, but the promise in README.md is new. Both synchronous and asynchronous tool calls use the same decoder in proofline/_mcp.py.

View finding in Proofline

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in this version: Proofline found the issue again. Proofline's finding in this version: Document the HTTP 200 requirement for JSON-RPC error details. View review

Comment thread tests/test_http.py Resolved
Comment thread tests/test_http.py Resolved

@prooflinedev prooflinedev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proofline has 4 new findings to review. 7 open findings need attention in this pull request.

View full review in Proofline

More findings

Warning · HTTP retry and redirect regressions can now pass the suite

Deleting tests/test_http.py removes assertions for bounded read retries, single-attempt REST writes and MCP tool calls on retryable HTTP errors, and cross-origin redirect refusal.

The remaining HTTP smoke test can pass even if the client repeats a mutation, exceeds the read retry limit, or contacts a redirect target.

Restore the request-count and redirect-destination assertions for both sync and async clients.

Details

test_new_public_operation in tests/test_generated_operation.py counts requests, but its peer only returns success and HTTP 403. tests/test_exports.py calls decode_export from proofline/exports.py directly rather than exercising HTTP policy. The removed MCP attempt assertion specifically covered HTTP 503.

View finding in Proofline

Location: tests/test_generated_operation.py lines 98 to 134

Comment thread proofline/_mcp.py Outdated
Comment on lines +58 to +61
def _token(http: httpx2.Client | httpx2.AsyncClient) -> SecretStr | None:
auth = http.auth
if isinstance(auth, BearerAuth):
return auth.token

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serious · Custom bearer credentials can leak into MCP error details

_token in proofline/_mcp.py reads client settings, so it misses bearer credentials that custom auth adds only to the outgoing request.

An echoed credential reaches caller-visible ProoflineError.detail in proofline/_transport.py without redaction.

In both request paths, redact using the bearer value from the response’s associated request Authorization header.

Details

KnowledgeClient in proofline/knowledge.py inherits the constructor that accepts an injected HTTP client. BearerAuth.auth_flow in proofline/_transport.py shows that auth can set the header directly on the request. Before this change, proofline/_mcp.py discarded JSON-RPC error messages.

View finding in Proofline

Comment thread proofline/_mcp.py
Comment on lines +38 to +41
error = value.get("error")
if (
"result" in value
or not isinstance(error, dict)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · The suite misses regressions in MCP error handling

The remaining tests cover REST and CSV, not the new JSON-RPC error handling in proofline/_mcp.py.

The suite would pass if valid errors lost their codes, either client stopped redacting echoed tokens, or malformed errors were accepted as well formed.

Add sync and async tool-call tests that assert code preservation, token redaction, and generic refusal for malformed envelopes.

Details

The authorization assertion in tests/test_generated_operation.py checks outgoing REST headers, not whether McpTools._call and AsyncMcpTools._call in proofline/_mcp.py pass the token to _decode. The CSV assertions in tests/test_exports.py never exercise _jsonrpc_error in proofline/_mcp.py. Before this change, the decoder refused every envelope containing error; it did not have the new behavior to protect.

View finding in Proofline

Comment thread proofline/_mcp.py
Comment on lines +103 to +104
or not isinstance(value.get("id"), int)
or isinstance(value.get("id"), bool)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning · Tests miss non-integer JSON-RPC initialization IDs

No test exercises the new ID type checks in _decode in proofline/_mcp.py. The removed fixture in tests/test_http.py used a mismatched integer, so restoring it unchanged would not catch this regression.

Either guard could be removed and tests would still pass while malformed initialization replies allow tool requests.

Test replies with IDs true and 1.0; assert refusal and that only initialization was sent.

Details

Python compares both values equal to integer 1. The base had only the equality check, not the type guards whose regression this change leaves untested.

View finding in Proofline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant