refactor(#2796): shared JsonRpc helper for MCP — wire constants, erro…#82
Merged
Conversation
…r codes, McpException hierarchy
New `agents_engine.mcp.JsonRpc` consolidates the JSON-RPC 2.0 envelope
shape that was hand-rolled separately on both sides:
JsonRpc helper
- `encodeRequest(id, method, params)` — builds request/notification
envelopes; passing null for id signals a notification.
- `encodeResult(id, result)` / `encodeError(id, code, message)` —
response envelope builders the server now uses.
- `parseEnvelope(payload)` — single Map lookup point.
- `isNotification(envelope)` — the duplicated
`!containsKey("id") || method.startsWith("notifications/")`
predicate, in one place.
Wire constants + error codes
- `JsonRpcWire` centralises the literal `"2.0"`, the key names
(`jsonrpc/method/params/id/result/error/code/message`), and the
notification prefix.
- `JsonRpcErrorCode` names the standard JSON-RPC error codes
(`-32700/-32600/-32601/-32602/-32603`) as `PARSE_ERROR` /
`INVALID_REQUEST` / `METHOD_NOT_FOUND` / `INVALID_PARAMS` /
`INTERNAL_ERROR`. Every bare literal in McpServer.kt replaced.
McpException hierarchy
- `sealed class McpException : IllegalStateException` (extends ISE so
existing `catch (e: IllegalStateException)` test sites keep
working — no breaking change for downstream consumers).
- Three subclasses: `Transport` (network / stdio EOF), `Protocol`
(JSON-RPC error response or malformed envelope), `ToolFailure`
(tool ran but signalled error). `McpClient.post` now throws
`McpException.Protocol` instead of bare `error()`, and the
helper's `parseResponse` does the same.
Side effect (small)
- McpClient's `handshake()` `notifications/initialized` raw-string
emission now flows through `JsonRpc.encodeRequest` so the wire is
identical to every other notification.
Full ./gradlew test green; 254+ TEST-*.xml files, zero failures/errors.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…r codes, McpException hierarchy
New
agents_engine.mcp.JsonRpcconsolidates the JSON-RPC 2.0 envelope shape that was hand-rolled separately on both sides:JsonRpc helper
encodeRequest(id, method, params)— builds request/notification envelopes; passing null for id signals a notification.encodeResult(id, result)/encodeError(id, code, message)— response envelope builders the server now uses.parseEnvelope(payload)— single Map lookup point.isNotification(envelope)— the duplicated!containsKey("id") || method.startsWith("notifications/")predicate, in one place.Wire constants + error codes
JsonRpcWirecentralises the literal"2.0", the key names (jsonrpc/method/params/id/result/error/code/message), and the notification prefix.JsonRpcErrorCodenames the standard JSON-RPC error codes (-32700/-32600/-32601/-32602/-32603) asPARSE_ERROR/INVALID_REQUEST/METHOD_NOT_FOUND/INVALID_PARAMS/INTERNAL_ERROR. Every bare literal in McpServer.kt replaced.McpException hierarchy
sealed class McpException : IllegalStateException(extends ISE so existingcatch (e: IllegalStateException)test sites keep working — no breaking change for downstream consumers).Transport(network / stdio EOF),Protocol(JSON-RPC error response or malformed envelope),ToolFailure(tool ran but signalled error).McpClient.postnow throwsMcpException.Protocolinstead of bareerror(), and the helper'sparseResponsedoes the same.Side effect (small)
handshake()notifications/initializedraw-string emission now flows throughJsonRpc.encodeRequestso the wire is identical to every other notification.Full ./gradlew test green; 254+ TEST-*.xml files, zero failures/errors.