Commit 2471290
test(tool-service): cover _looks_like_mcp_envelope layer-2 branches and BaseModel fallback
Five new tests in ``TestCoerceToToolResult`` close the coverage gaps
flagged by the tool_service.py coverage report (86.2% → higher on the
helper):
* ``test_accepts_dict_with_structured_content_marker_only`` — exercises
the ``structuredContent`` positive-marker branch of
``_looks_like_mcp_envelope`` (was uncovered).
* ``test_accepts_dict_with_snake_case_structured_content_marker`` — the
snake-case sibling; forwarded-worker responses use this form.
* ``test_accepts_dict_with_only_typed_content_items`` — the weakest
positive marker (content items carrying a string ``type`` field);
without this a future tightening could drop the branch silently.
* ``test_rejects_dict_with_only_meta_marker`` — the final ``return
False`` branch when all keys are envelope-shaped but no positive
marker fires. ``_meta`` alone is not sufficient.
* ``test_basemodel_with_content_that_fails_validation_falls_back`` —
pins the ``except ValidationError`` branch inside
``_coerce_to_tool_result``'s BaseModel dispatch; asserts WARNING-level
log with ``exc_info=True``.
While writing the BaseModel fallback test it surfaced a real bug: a
Pydantic ``BaseModel`` that failed ``ToolResult.model_validate`` and
fell through to the opaque-JSON fallback caused ``orjson.dumps`` to
raise ``TypeError`` — breaking the helper's "always returns a valid
ToolResult" invariant. Fixed by dumping BaseModel payloads via
``model_dump(mode="json", by_alias=True)`` before serialising, and
adding a last-resort ``TypeError`` catch that falls back to
``str(payload)`` with a WARNING log. ``test_non_json_serialisable_payload_falls_back_to_str``
pins the new branch.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Springer <jps@s390x.com>1 parent 8788c10 commit 2471290
File tree
3 files changed
+186
-14
lines changed- mcpgateway/services
- tests/unit/mcpgateway/services
3 files changed
+186
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
8666 | 8666 | | |
8667 | 8667 | | |
8668 | 8668 | | |
8669 | | - | |
| 8669 | + | |
8670 | 8670 | | |
8671 | 8671 | | |
8672 | 8672 | | |
8673 | 8673 | | |
8674 | 8674 | | |
8675 | 8675 | | |
8676 | 8676 | | |
8677 | | - | |
| 8677 | + | |
8678 | 8678 | | |
8679 | 8679 | | |
8680 | 8680 | | |
8681 | 8681 | | |
8682 | 8682 | | |
8683 | 8683 | | |
8684 | 8684 | | |
8685 | | - | |
| 8685 | + | |
8686 | 8686 | | |
8687 | 8687 | | |
8688 | 8688 | | |
8689 | 8689 | | |
8690 | 8690 | | |
8691 | 8691 | | |
8692 | 8692 | | |
8693 | | - | |
| 8693 | + | |
8694 | 8694 | | |
8695 | 8695 | | |
8696 | 8696 | | |
8697 | 8697 | | |
8698 | 8698 | | |
8699 | 8699 | | |
8700 | 8700 | | |
8701 | | - | |
| 8701 | + | |
8702 | 8702 | | |
8703 | 8703 | | |
8704 | 8704 | | |
8705 | 8705 | | |
8706 | 8706 | | |
8707 | 8707 | | |
8708 | 8708 | | |
8709 | | - | |
| 8709 | + | |
8710 | 8710 | | |
8711 | 8711 | | |
8712 | 8712 | | |
8713 | 8713 | | |
8714 | 8714 | | |
8715 | 8715 | | |
8716 | 8716 | | |
8717 | | - | |
| 8717 | + | |
8718 | 8718 | | |
8719 | 8719 | | |
8720 | 8720 | | |
8721 | 8721 | | |
8722 | 8722 | | |
8723 | 8723 | | |
8724 | 8724 | | |
8725 | | - | |
| 8725 | + | |
8726 | 8726 | | |
8727 | 8727 | | |
8728 | 8728 | | |
8729 | 8729 | | |
8730 | 8730 | | |
8731 | 8731 | | |
8732 | 8732 | | |
8733 | | - | |
| 8733 | + | |
8734 | 8734 | | |
8735 | 8735 | | |
8736 | 8736 | | |
8737 | 8737 | | |
8738 | 8738 | | |
8739 | 8739 | | |
8740 | 8740 | | |
8741 | | - | |
| 8741 | + | |
8742 | 8742 | | |
8743 | 8743 | | |
8744 | 8744 | | |
8745 | 8745 | | |
8746 | 8746 | | |
8747 | 8747 | | |
8748 | 8748 | | |
8749 | | - | |
| 8749 | + | |
8750 | 8750 | | |
8751 | 8751 | | |
8752 | 8752 | | |
8753 | 8753 | | |
8754 | 8754 | | |
8755 | 8755 | | |
8756 | 8756 | | |
8757 | | - | |
| 8757 | + | |
8758 | 8758 | | |
8759 | 8759 | | |
8760 | 8760 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1678 | 1678 | | |
1679 | 1679 | | |
1680 | 1680 | | |
1681 | | - | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
1682 | 1698 | | |
1683 | 1699 | | |
1684 | 1700 | | |
| |||
Lines changed: 156 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2247 | 2247 | | |
2248 | 2248 | | |
2249 | 2249 | | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
| 2260 | + | |
| 2261 | + | |
| 2262 | + | |
| 2263 | + | |
| 2264 | + | |
| 2265 | + | |
| 2266 | + | |
| 2267 | + | |
| 2268 | + | |
| 2269 | + | |
| 2270 | + | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
| 2274 | + | |
| 2275 | + | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
| 2290 | + | |
| 2291 | + | |
| 2292 | + | |
| 2293 | + | |
| 2294 | + | |
| 2295 | + | |
| 2296 | + | |
| 2297 | + | |
| 2298 | + | |
| 2299 | + | |
| 2300 | + | |
| 2301 | + | |
| 2302 | + | |
| 2303 | + | |
| 2304 | + | |
| 2305 | + | |
| 2306 | + | |
| 2307 | + | |
| 2308 | + | |
| 2309 | + | |
| 2310 | + | |
| 2311 | + | |
| 2312 | + | |
| 2313 | + | |
| 2314 | + | |
| 2315 | + | |
| 2316 | + | |
| 2317 | + | |
| 2318 | + | |
| 2319 | + | |
| 2320 | + | |
| 2321 | + | |
| 2322 | + | |
| 2323 | + | |
| 2324 | + | |
| 2325 | + | |
| 2326 | + | |
| 2327 | + | |
| 2328 | + | |
| 2329 | + | |
| 2330 | + | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
| 2343 | + | |
| 2344 | + | |
| 2345 | + | |
| 2346 | + | |
| 2347 | + | |
| 2348 | + | |
| 2349 | + | |
| 2350 | + | |
| 2351 | + | |
| 2352 | + | |
| 2353 | + | |
| 2354 | + | |
| 2355 | + | |
| 2356 | + | |
| 2357 | + | |
| 2358 | + | |
| 2359 | + | |
| 2360 | + | |
| 2361 | + | |
| 2362 | + | |
| 2363 | + | |
| 2364 | + | |
| 2365 | + | |
| 2366 | + | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
| 2370 | + | |
| 2371 | + | |
| 2372 | + | |
| 2373 | + | |
| 2374 | + | |
| 2375 | + | |
| 2376 | + | |
| 2377 | + | |
| 2378 | + | |
| 2379 | + | |
| 2380 | + | |
| 2381 | + | |
| 2382 | + | |
| 2383 | + | |
| 2384 | + | |
| 2385 | + | |
| 2386 | + | |
| 2387 | + | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
2250 | 2406 | | |
2251 | 2407 | | |
2252 | 2408 | | |
| |||
0 commit comments