-
Notifications
You must be signed in to change notification settings - Fork 17
Classify MCP tool errors by operational category #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Classify public tool failures into bounded operational categories. | ||
|
|
||
| The public operator wraps several distinct failure modes before they reach the | ||
| telemetry and error-monitoring layers. Keep the classification logic here so | ||
| Prometheus and Sentry agree about which failures are expected and actionable. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from typing import Literal | ||
|
|
||
| from appwrite_console.exception import AppwriteException | ||
|
|
||
| ErrorCategory = Literal[ | ||
| "write_confirmation", | ||
| "appwrite_4xx", | ||
| "appwrite_5xx", | ||
| "sdk_validation", | ||
| "internal", | ||
| ] | ||
|
|
||
| ERROR_CATEGORIES: frozenset[str] = frozenset( | ||
| { | ||
| "write_confirmation", | ||
| "appwrite_4xx", | ||
| "appwrite_5xx", | ||
| "sdk_validation", | ||
| "internal", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class WriteConfirmationRequired(RuntimeError): | ||
| """A mutating hidden tool was called without explicit confirmation.""" | ||
|
|
||
|
|
||
| def classify_tool_error(exc: BaseException) -> ErrorCategory: | ||
| """Return the bounded operational category for an exception chain.""" | ||
| chain = tuple(_exception_chain(exc)) | ||
|
|
||
| if any(isinstance(item, WriteConfirmationRequired) for item in chain): | ||
| return "write_confirmation" | ||
|
|
||
| if any(_is_sdk_validation_error(item) for item in chain): | ||
| return "sdk_validation" | ||
|
|
||
| appwrite_error = next( | ||
| (item for item in chain if isinstance(item, AppwriteException)), None | ||
| ) | ||
| if appwrite_error is not None: | ||
| code = _appwrite_status_code(appwrite_error) | ||
| if code is not None and 400 <= code < 500: | ||
| return "appwrite_4xx" | ||
| if code is not None and 500 <= code < 600: | ||
| return "appwrite_5xx" | ||
|
|
||
| return "internal" | ||
|
|
||
|
|
||
| def _exception_chain(exc: BaseException) -> Iterator[BaseException]: | ||
| """Walk causes and contexts defensively, including malformed cycles.""" | ||
| pending: list[BaseException] = [exc] | ||
| seen: set[int] = set() | ||
| while pending: | ||
| current = pending.pop() | ||
| if id(current) in seen: | ||
| continue | ||
| seen.add(id(current)) | ||
| yield current | ||
|
|
||
| # Push context first so an explicit cause is inspected first. | ||
| if isinstance(current.__context__, BaseException): | ||
| pending.append(current.__context__) | ||
| if isinstance(current.__cause__, BaseException): | ||
| pending.append(current.__cause__) | ||
|
|
||
|
|
||
| def _is_sdk_validation_error(exc: BaseException) -> bool: | ||
| error_type = type(exc) | ||
| if error_type.__name__ == "ValidationError" and error_type.__module__.startswith( | ||
| "pydantic" | ||
| ): | ||
| return True | ||
|
|
||
| # The console SDK normally chains the Pydantic error, but retain a narrow | ||
| # fallback for SDK versions that only preserve their parse-error message. | ||
| return isinstance(exc, AppwriteException) and str(exc).startswith( | ||
| "Unable to parse response into " | ||
| ) | ||
|
|
||
|
|
||
| def _appwrite_status_code(exc: AppwriteException) -> int | None: | ||
| raw_code = getattr(exc, "code", None) | ||
| if raw_code is None: | ||
| return None | ||
| try: | ||
| return int(raw_code) | ||
| except (TypeError, ValueError): | ||
| return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import unittest | ||
|
|
||
| from appwrite_console.exception import AppwriteException | ||
| from pydantic import BaseModel, ValidationError | ||
|
|
||
| from mcp_server_appwrite.error_classification import ( | ||
| WriteConfirmationRequired, | ||
| classify_tool_error, | ||
| ) | ||
|
|
||
|
|
||
| class ErrorClassificationTests(unittest.TestCase): | ||
| def test_write_confirmation(self): | ||
| self.assertEqual( | ||
| classify_tool_error(WriteConfirmationRequired("confirm_write=true")), | ||
| "write_confirmation", | ||
| ) | ||
|
|
||
| def test_wrapped_appwrite_4xx(self): | ||
| for code in (400, 401, 404, 409, 429, 499): | ||
| with self.subTest(code=code): | ||
| appwrite_error = AppwriteException("client error", str(code), None) | ||
| wrapped = RuntimeError("wrapped") | ||
| wrapped.__cause__ = appwrite_error | ||
|
|
||
| self.assertEqual(classify_tool_error(wrapped), "appwrite_4xx") | ||
|
|
||
| def test_appwrite_5xx(self): | ||
| for code in (500, 503, 599): | ||
| with self.subTest(code=code): | ||
| self.assertEqual( | ||
| classify_tool_error( | ||
| AppwriteException("upstream failed", code, "server_error") | ||
| ), | ||
| "appwrite_5xx", | ||
| ) | ||
|
|
||
| def test_sdk_validation_takes_precedence_over_code(self): | ||
| class Provider(BaseModel): | ||
| options: dict | ||
|
|
||
| try: | ||
| Provider.model_validate({"options": []}) | ||
| except ValidationError as validation_error: | ||
| appwrite_error = AppwriteException( | ||
| "Unable to parse response into Provider", 0, None | ||
| ) | ||
| appwrite_error.__cause__ = validation_error | ||
| else: # pragma: no cover - defensive | ||
| self.fail("Expected Pydantic validation to fail") | ||
|
|
||
| self.assertEqual(classify_tool_error(appwrite_error), "sdk_validation") | ||
|
|
||
| def test_sdk_validation_message_fallback(self): | ||
| error = AppwriteException( | ||
| "Unable to parse response into ProviderList: invalid model", 0, None | ||
| ) | ||
| self.assertEqual(classify_tool_error(error), "sdk_validation") | ||
|
|
||
| def test_code_less_appwrite_and_unexpected_errors_are_internal(self): | ||
| self.assertEqual( | ||
| classify_tool_error(AppwriteException("network down", 0, None)), | ||
| "internal", | ||
| ) | ||
| self.assertEqual(classify_tool_error(TypeError("boom")), "internal") | ||
|
|
||
| def test_exception_cycle_is_safe(self): | ||
| first = RuntimeError("first") | ||
| second = AppwriteException("not found", 404, "not_found") | ||
| first.__cause__ = second | ||
| second.__context__ = first | ||
|
|
||
| self.assertEqual(classify_tool_error(first), "appwrite_4xx") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a public tool raises a Pydantic
ValidationErrordirectly, the broadValueErrorcheck suppresses it before the new classifier runs, causing ansdk_validationtelemetry failure to be silently omitted from Sentry.Prompt To Fix With AI