Skip to content

fix: log expected tool failures as WARNING, drop tracebacks#4029

Open
sergeykad wants to merge 1 commit intoPrefectHQ:mainfrom
sergeykad:fix/validation-log-as-warning
Open

fix: log expected tool failures as WARNING, drop tracebacks#4029
sergeykad wants to merge 1 commit intoPrefectHQ:mainfrom
sergeykad:fix/validation-log-as-warning

Conversation

@sergeykad
Copy link
Copy Markdown

Description

Tool dispatch in src/fastmcp/server/server.py logs every failure via logger.exception, printing a full traceback to stderr. Three cases land in those branches, but only one is an actual bug:

  • except FastMCPError: a tool deliberately raised ToolError (or similar) to return a structured error to the client. The exception message already carries that payload; the stack through fastmcp's dispatcher adds nothing.
  • except (ValidationError, PydanticValidationError): the caller passed bad arguments. err.errors() already describes the failing field, expected type, and offending value. The stack walks Pydantic internals.
  • except Exception: an unexpected error in tool code. This one benefits from a stack.

On stdio deployments the tracebacks from the first two cases are written to the parent process's stderr on every malformed caller request or tool-raised error. For a long-running server with a misbehaving agent this floods the console.

This PR logs the first two categories as WARNING with the relevant payload and drops the traceback. The except Exception branch is untouched. Pydantic URLs are stripped via include_url=False.

Also drops fastmcp.exceptions.ValidationError from the second except tuple. It is a FastMCPError subclass and is already caught by the branch above, so that entry was unreachable. Removing it lets the branch type-check as a single PydanticValidationError.

Closes #

Contribution type

  • Bug fix (simple, well-scoped fix for a clearly broken behavior)
  • Documentation improvement
  • Enhancement (maintainers typically implement enhancements — see CONTRIBUTING.md)

Checklist

  • This PR addresses an existing issue (or fixes a self-evident bug)
  • I have read CONTRIBUTING.md
  • I have added tests that cover my changes
  • I have run uv run prek run --all-files and all checks pass
  • I have self-reviewed my changes
  • If I used an LLM, it followed the repo's contributing conventions (not generic output)

FastMCPError and Pydantic ValidationError are expected control flow,
not server bugs. Log them as WARNING with the relevant payload and
no traceback. Bare `except Exception` keeps logger.exception so real
bugs still get a stack.

Drop fastmcp.ValidationError from the except tuple: already caught
by the FastMCPError branch above.
@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. server Related to FastMCP server implementation or server-side functionality. labels Apr 24, 2026
@sergeykad sergeykad marked this pull request as ready for review April 24, 2026 13:03
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copy link
Copy Markdown
Contributor

@ZLeventer ZLeventer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense — expected tool failures (like validation errors or user-facing error responses) shouldn't produce full tracebacks in the logs. WARNING level is appropriate since the tool did fail, but it's an expected failure path rather than a server error.

One thought: it might be worth distinguishing between "tool returned an error response" (WARNING, no traceback) vs "tool raised an unexpected exception" (ERROR, with traceback). If this PR already makes that distinction, looks good. If it suppresses tracebacks for all tool failures uniformly, unexpected bugs in tool implementations could become harder to debug.

@jlowin
Copy link
Copy Markdown
Member

jlowin commented Apr 25, 2026

What issue is this closing? It looks like #4035, but that was opened after this PR and is addressed in #4036

@sergeykad
Copy link
Copy Markdown
Author

I didn't open an issue for this.

Copy link
Copy Markdown
Member

@jlowin jlowin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, for the future we typically expect every PR to have an associated issue to describe the work. #4036 is merged and includes a per-error log_level so users can downgrade to WARNING themselves. What's still useful is dropping exc_info to hide tracebacks and the unreachable ValidationError cleanup - would you want to rescope to that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants