Skip to content
This repository was archived by the owner on Nov 27, 2026. It is now read-only.

Raise ServiceError and return it with add_error_contract (SDK 0.7) - #20

Merged
mgoldsborough merged 1 commit into
mainfrom
sdk-0-7
Nov 15, 2026
Merged

mgoldsborough merged 1 commit into
mainfrom
sdk-0-7

Conversation

@mgoldsborough

Copy link
Copy Markdown
Contributor

What

Moves the template onto the idiom nimblebrain-server-sdk 0.7 provides, so a server started from it begins there.

  • Dependency: nimblebrain-server-sdk>=0.7,<0.8. The SDK is pre-stable and a minor release may change the Python API, so its compatibility page recommends pinning a minor.
  • Errors: add_error_contract(mcp) is called once where the server is built, and tools raise ServiceError(...) / ServiceError.from_upstream(...). No tool wraps its body in try / return to_tool_error(...).
  • Tests: the parametrized protocol-era parameter is annotated with nimblebrain_server_sdk.testing.Mode.
  • Docs: README, CLAUDE.md and CONTRIBUTING.md say "raise ServiceError; add_error_contract returns it", and keep the warning never to return an error as a plain dict.

Why

Returning to_tool_error(...) at every failure site means each tool repeats the same except ServiceError clause, and a helper that raises (get_client, read_item) needs a catch in every tool that calls it. With the contract installed once, a tool reads as its happy path and a raised ServiceError from any depth reaches the caller in the same shape.

Caller-visible behaviour: unchanged

  • Every error-envelope test passes with the same expectations; no assertion changed. The only test edits are the Mode annotations.
  • Beyond the suite, the full result of 58 failing calls and 4 successful ones (is_error, structuredContent, content), the tool listing, and the resource-read errors were recorded on main and on this branch, on both protocol eras, and are byte-identical. The calls cover bad input, a missing API key, upstream statuses 400, 401, 402, 403, 404, 408, 409, 429, 500, 503 and 504 for both tools, an argument of the wrong type, and an unclassified exception.
  • A resource read keeps its behaviour: the contract covers tool calls, so item_resource still turns a ServiceError into a ResourceError carrying the message safe to show.
  • Each surfaced error is still recorded once as a service_error log line.

One difference, in the server's own log only

FastMCP logs Error calling tool '<name>' with a traceback to stderr for any exception a tool raises, before the middleware turns it into the result. A raised ServiceError therefore adds a traceback to the server's stderr beside the service_error line, including for bad_input. Nothing in it reaches the caller.

Testing

  • make check (ruff format, ruff check, ty, pytest): 49 passed on main, 49 passed here. Python 3.13.
  • The three Python snippets in the README were extracted and run: the tool snippet (called on both eras, result compared with the README's stated structured content), the skill snippet, and the test snippet (4 passed under this repo's pytest.ini, and it type-checks with ty).
  • pytest.ini's error::mcp.MCPDeprecationWarning filter, the ruff rules, the scan workflow and the licence are untouched.

Pin nimblebrain-server-sdk to the 0.7 minor, install add_error_contract where
the server is built, and have tools raise ServiceError instead of returning
to_tool_error at each site. Tests annotate their protocol-era parameter with
nimblebrain_server_sdk.testing.Mode. The result a caller receives is the same.
@mgoldsborough

Copy link
Copy Markdown
Contributor Author

QA Review: sdk-0-7

Scope: 6 files, +59/−49 · prepared worktree · reviewed: 20e97b2 · CI: green
Risk: LOW
Why this exists: Start generated servers on SDK 0.7’s centralized error contract, removing repeated catches from tools.

Critical (must fix)

None.

Fix in-PR

None.

Suggestions

None.

What Looks Good

  • Middleware delegates raised ServiceError instances to the same error serializer; resource errors retain their separate handling.
  • Tool schemas and existing error assertions remain unchanged. The dependency is bounded to SDK 0.7.
  • Local verification: PYTHONDONTWRITEBYTECODE=1 .venv/bin/python -m pytest tests/ -p no:cacheprovider — 49 passed. Prefetched bundle, lint, scan and test checks passed.
  • Worktree remains clean. Unscoped pytest collection requires integration credentials; the documented unit suite passes without them.

Body claims

  • ✅ Test edits only add Mode typing; existing expectations are unchanged.
  • ✅ Dependency, centralized error registration and documentation changes match the description.
  • ✅ Deprecation filter, lint rules, scan workflow and licence are untouched.
  • The reported 62-call baseline comparison and README execution receipts were not supplied or independently reproduced; the passing unit suite and installed SDK implementation support the described behavior.
machine record
{
  "kind": "review",
  "pr": 20,
  "round": 1,
  "reviewed": "20e97b2066dae3c1cdbb483237178f302686c705",
  "delta_base": null,
  "ci": "green",
  "risk": "LOW",
  "stop_gate": "n/a",
  "verdict": "Approve; no actionable findings.",
  "label": "eligible-on-green",
  "findings": []
}

@mgoldsborough mgoldsborough added the qa-reviewed QA review completed with no critical issues label Nov 15, 2026
@mgoldsborough
mgoldsborough merged commit b741cba into main Nov 15, 2026
4 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

qa-reviewed QA review completed with no critical issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant