Skip to content

feat(mcp): #33 add is_rejected_with_code and because_message_equals#35

Merged
othercodes merged 1 commit into
masterfrom
feat/33-mcp-custom-error-matchers
May 18, 2026
Merged

feat(mcp): #33 add is_rejected_with_code and because_message_equals#35
othercodes merged 1 commit into
masterfrom
feat/33-mcp-custom-error-matchers

Conversation

@othercodes
Copy link
Copy Markdown
Owner

Closes #33.

Summary

Two thin additions to AssertableMCP that close gaps in the fluent error vocabulary:

  • is_rejected_with_code(code: int) — assert arbitrary JSON-RPC error codes (custom application-level codes outside the spec set, e.g. -32001 for auth failures).
  • because_message_equals(expected: str) — exact match sibling of the existing because_message_contains(substr).

Both compose inside assert_mcp(lambda m: ...), eliminating the need to drop into assert_json("error", ...) for custom error codes or strict message matching.

Before / after

# Before — bypass MCP module for custom codes
response.assert_unauthorized().assert_json(
    "error",
    lambda e: e.where("code", -32001).where("message", AUTH_ERROR_MESSAGE),
)

# After — stays in MCP fluent style
response.assert_unauthorized().assert_mcp(lambda m:
    m.is_rejected_with_code(-32001).because_message_equals(AUTH_ERROR_MESSAGE)
)

Implementation

  • Widened _expect_error_code signature from ErrorCode to int | ErrorCode (no behavior change — internal already used int(expected)).
  • is_rejected_with_code is a 2-line public wrapper over the internal helper.
  • because_message_equals mirrors because_message_contains with == and consistent error-message wording ("Error message does not match X: Y").

Test plan

  • is_rejected_with_code happy + sad path + "no error envelope" branch.
  • because_message_equals happy + sad path + "no error envelope" branch.
  • mypy strict clean.
  • Coverage 100% maintained.
  • ruff lint + format clean.
  • README MCP method catalog updated.

@sonarqubecloud
Copy link
Copy Markdown

@othercodes othercodes merged commit ab0c90f into master May 18, 2026
33 checks passed
@othercodes othercodes deleted the feat/33-mcp-custom-error-matchers branch May 18, 2026 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose public matchers for custom error codes and exact message matching on AssertableMCP

1 participant