Two gates to prevent reintroduction of the malformed-JSON bug class that shipped in #417 (Outreach error helpers used a """{...}""" template with raw exception text, producing unparseable bodies). (A) Harness-level JSON parse assertion. Introduces shared `McpToolTestHarness` (in `integrations/src/test/.../testing/`) that wraps the duplicated mockk(Server) + addTool capture pattern previously copy-pasted across `OutreachMcpProviderTest`, `UsageMcpProviderTest`, `NotificationMcpToolExecutionTest`, and `NotificationMcpProviderToolsTest`. Every `callTool(...)` invocation runs the result body through `Json.parseToJsonElement`; parse failures throw with the offending tool name and body content. Per-call `expectJsonBody = false` opt-out for tools whose contract is plain text (currently only one test uses it, referencing #427 which tracks the underlying core MCP DSL gap the harness surfaced). Regression test `McpToolTestHarnessTest` constructs fake tools that return non-JSON bodies and asserts the harness throws with the tool name + body in the message, plus a passing-case to lock down the expectJsonBody opt-out. (B) CI grep check. New `scripts/check-no-manual-json.sh` greps `*/src/main/*.kt` for `"""\{` and fails the build on hits unless the line carries a `// allow-manual-json: <reason>` marker, or matches the explicit `AuditDetailScreen.kt` allowlist (Compose preview fixtures). Wired into `.github/workflows/android-ci.yml` next to the existing `check-zombie-screens.sh` step. Verified manually that the script fires on a synthetic `"""{"x":"$bar"}"""` and clears once the marker is added. Documents the convention in new `docs/agent-quickstart.md`. Co-authored-by: Claude Opus 4 (1M context) <noreply@anthropic.com>
3 KiB
Agent Quickstart
A short reference for agents (and humans) writing code in this repo. For the
full architecture and module layout, see docs/workflow.md and the
project-root CLAUDE.md.
JSON construction
Tools that return structured data MUST use @Serializable data classes plus
Json.encodeToString (or Json.encodeToJsonElement if a tree is preferable).
Manual template literals — """{"field":"$value"}""" — are NOT permitted in
production code.
Why
Issue #417 shipped
malformed JSON to user testing because a template-literal error helper
interpolated a raw exception message containing quotes, producing output the
connector proxy refused to parse. The @Serializable + encodeToString path
escapes embedded quotes/backslashes/control characters automatically; the
manual path does not.
Enforcement
Two gates run in CI:
- Test harness (
McpToolTestHarnessinintegrations/src/test/kotlin/com/rousecontext/integrations/testing/) — parse-asserts everyCallToolResultbody returned through the harness. Any tool whose result body failsJson.parseToJsonElementcauses the test to fail with the offending tool name and body content. - Static check (
scripts/check-no-manual-json.sh) — runs in theAndroid CIworkflow. Greps for"""{in*/src/main/*.ktand fails the build if any match isn't allowlisted.
The // allow-manual-json marker
For the rare case where a template literal is genuinely safe (e.g. a
@Preview-only Compose fixture with hard-coded literals that never see
runtime input), append a marker comment to the line:
val sample = """{"steps":7000}""" // allow-manual-json: @Preview fixture, no interpolation
The reason text after the colon should describe why this isn't a runtime risk — typically because no user-controlled value flows into the template. A reviewer should treat the absence of a clear reason as a blocker.
AuditDetailScreen.kt is implicitly allowlisted in the script because its
preview fixtures exist purely for IDE rendering and never reach runtime; new
files should use the inline marker rather than expanding the file allowlist.
Test harness
When writing or refactoring an *McpProvider*Test, use
McpToolTestHarness rather than hand-rolling the mockk capture pattern:
private val harness = McpToolTestHarness()
private val fakeConnection: ClientConnection = mockk(relaxed = true)
@Before fun setUp() {
val server = harness.createMockServer()
MyMcpProvider(...).register(server)
}
@Test fun `tool does the thing`() = runBlocking {
val result = harness.callTool(
name = "my_tool",
arguments = buildJsonObject { put("x", JsonPrimitive(1)) },
connection = fakeConnection
)
// Body is guaranteed JSON-parseable; assert further as needed.
}
Pass expectJsonBody = false only for tools whose contract specifies a
plain-text body (rare). Document the reason inline if you do.