|
| 1 | +package agents_engine.mcp |
| 2 | + |
| 3 | +import agents_engine.core.agent |
| 4 | +import java.net.URI |
| 5 | +import java.net.http.HttpClient |
| 6 | +import java.net.http.HttpRequest |
| 7 | +import java.net.http.HttpResponse |
| 8 | +import kotlin.test.AfterTest |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertEquals |
| 11 | +import kotlin.test.assertTrue |
| 12 | + |
| 13 | +// Tests for #889 (catch-all) — McpServer error-path coverage. |
| 14 | +// |
| 15 | +// PIT NO_COVERAGE clusters in McpServer.handle / handleToolCall: |
| 16 | +// - L86: malformed JSON → 400 |
| 17 | +// - L87: missing "method" → 400 |
| 18 | +// - L107: internal exception → 500 (in the outer catch) |
| 19 | +// - L133: missing tool name in tools/call → -32602 |
| 20 | +// - L135: unknown tool name → -32601 |
| 21 | +// - L148: skill execution throws → isError:true response |
| 22 | +class McpServerErrorPathsTest { |
| 23 | + |
| 24 | + private val toStop = mutableListOf<() -> Unit>() |
| 25 | + |
| 26 | + @AfterTest fun cleanup() { toStop.forEach { runCatching { it() } } } |
| 27 | + |
| 28 | + private fun trivialAgent() = agent<String, String>("greeter") { |
| 29 | + skills { skill<String, String>("greet", "Greets") { implementedBy { "hi $it" } } } |
| 30 | + } |
| 31 | + |
| 32 | + private fun explodingAgent() = agent<String, String>("boomer") { |
| 33 | + skills { |
| 34 | + skill<String, String>("boom", "Always throws") { |
| 35 | + implementedBy { _ -> throw RuntimeException("kaboom") } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private fun startServer(agent: agents_engine.core.Agent<*, *>, exposed: List<String>): McpServer { |
| 41 | + val server = McpServer.from(agent) { exposed.forEach { expose(it) }; port = 0 }.start() |
| 42 | + toStop.add { server.stop() } |
| 43 | + return server |
| 44 | + } |
| 45 | + |
| 46 | + private fun postRaw(url: String, body: String): HttpResponse<String> { |
| 47 | + val req = HttpRequest.newBuilder() |
| 48 | + .uri(URI.create(url)) |
| 49 | + .header("Content-Type", "application/json") |
| 50 | + .POST(HttpRequest.BodyPublishers.ofString(body)) |
| 51 | + .build() |
| 52 | + return HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString()) |
| 53 | + } |
| 54 | + |
| 55 | + // L86 — malformed JSON body |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `malformed JSON body returns 400`() { |
| 59 | + val server = startServer(trivialAgent(), listOf("greet")) |
| 60 | + val r = postRaw(server.url, "not json at all") |
| 61 | + assertEquals(400, r.statusCode()) |
| 62 | + } |
| 63 | + |
| 64 | + // L87 — JSON without "method" field |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `JSON without method field returns 400`() { |
| 68 | + val server = startServer(trivialAgent(), listOf("greet")) |
| 69 | + val r = postRaw(server.url, """{"jsonrpc":"2.0","id":1}""") |
| 70 | + assertEquals(400, r.statusCode()) |
| 71 | + } |
| 72 | + |
| 73 | + // L107 — internal exception path. Hard to trigger directly (the outer |
| 74 | + // catch wraps anything that escapes the dispatcher). Sending a method |
| 75 | + // that the dispatcher can handle but with malformed `params` won't |
| 76 | + // exercise it because handlers tolerate empty params. Using a |
| 77 | + // `notifications/`-prefixed method exits early. The cleanest reachable |
| 78 | + // case: a method that LooksLikeJsonButIsn't valid for the parser |
| 79 | + // mid-deserialization. Skipping this branch — it's defensively wrapping |
| 80 | + // the entire dispatcher and only fires on truly unexpected runtime |
| 81 | + // errors. Documented but not exercised. |
| 82 | + |
| 83 | + // L133 — tools/call without "name" parameter |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `tools-call without name returns JSON-RPC error code -32602`() { |
| 87 | + val server = startServer(trivialAgent(), listOf("greet")) |
| 88 | + val r = postRaw( |
| 89 | + server.url, |
| 90 | + """{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{}}""", |
| 91 | + ) |
| 92 | + assertEquals(200, r.statusCode()) |
| 93 | + assertTrue(r.body().contains("\"code\":-32602"), "body: ${r.body()}") |
| 94 | + assertTrue( |
| 95 | + r.body().contains("Missing tool name", ignoreCase = true), |
| 96 | + "body: ${r.body()}", |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + // L135 — tools/call with unknown tool name |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `tools-call with unknown tool name returns JSON-RPC error code -32601`() { |
| 104 | + val server = startServer(trivialAgent(), listOf("greet")) |
| 105 | + val r = postRaw( |
| 106 | + server.url, |
| 107 | + """{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"nonexistent"}}""", |
| 108 | + ) |
| 109 | + assertEquals(200, r.statusCode()) |
| 110 | + assertTrue(r.body().contains("\"code\":-32601"), "body: ${r.body()}") |
| 111 | + assertTrue(r.body().contains("nonexistent"), "body: ${r.body()}") |
| 112 | + } |
| 113 | + |
| 114 | + // L148 — skill execution throws → isError:true response |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `tools-call where skill throws returns isError true with the exception message`() { |
| 118 | + val server = startServer(explodingAgent(), listOf("boom")) |
| 119 | + val r = postRaw( |
| 120 | + server.url, |
| 121 | + """{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"boom","arguments":{"input":"hi"}}}""", |
| 122 | + ) |
| 123 | + assertEquals(200, r.statusCode(), "body: ${r.body()}") |
| 124 | + assertTrue(r.body().contains("\"isError\":true"), "must mark isError true; body: ${r.body()}") |
| 125 | + assertTrue(r.body().contains("kaboom"), "must include exception message; body: ${r.body()}") |
| 126 | + } |
| 127 | + |
| 128 | + // Bonus — sanity that ordinary methods still work alongside these error tests. |
| 129 | + |
| 130 | + @Test |
| 131 | + fun `unknown top-level method returns -32601 method not found`() { |
| 132 | + val server = startServer(trivialAgent(), listOf("greet")) |
| 133 | + val r = postRaw( |
| 134 | + server.url, |
| 135 | + """{"jsonrpc":"2.0","id":1,"method":"completely/unknown"}""", |
| 136 | + ) |
| 137 | + assertEquals(200, r.statusCode()) |
| 138 | + assertTrue(r.body().contains("\"code\":-32601"), "body: ${r.body()}") |
| 139 | + assertTrue(r.body().contains("Method not found"), "body: ${r.body()}") |
| 140 | + } |
| 141 | +} |
0 commit comments