Skip to content

Add log_level parameter to FastMCP errors#4036

Open
daniel-tsiang wants to merge 2 commits intoPrefectHQ:mainfrom
daniel-tsiang:main
Open

Add log_level parameter to FastMCP errors#4036
daniel-tsiang wants to merge 2 commits intoPrefectHQ:mainfrom
daniel-tsiang:main

Conversation

@daniel-tsiang
Copy link
Copy Markdown

@daniel-tsiang daniel-tsiang commented Apr 24, 2026

Description

Closes #4035

Adds log_level parameter to FastMCPError and all subclasses (ToolError, ResourceError, PromptError, etc.) to control the server-side log level for expected control-flow errors.

When building multi-step LLM workflows, errors often represent normal control flow rather than bugs. For example, a tool might raise ToolError("Model not deployed, call deploy_model first") to guide the LLM's next action. These expected errors create noise in production error monitoring systems (Sentry, DataDog) that typically capture ERROR-level logs. Rather than suppressing logs entirely (losing all visibility), this lets server operators downgrade the log level so the error is still recorded but doesn't trigger alerts.

Usage

@mcp.tool
def check_model_status(model_id: str) -> dict:
  if not model_registry.is_deployed(model_id):
      raise ToolError(
          f"Model {model_id} not deployed. Call deploy_model first.",
          log_level=logging.WARNING  # Log at WARNING instead of ERROR
      )
  return model_registry.get_status(model_id)

The error still reaches the LLM client with the full message and is still logged server-side — just at the specified level instead of ERROR.

Implementation

  • Added log_level: int = logging.ERROR parameter to FastMCPError.__init__
  • Updated catch blocks in server.py to use logger.log(e.log_level, ..., exc_info=True) instead of logger.exception()
  • Updated sampling/run.py for sampling tool errors
  • Fixed pre-existing bug where function_prompt.py double-wrapped PromptError

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)

@marvin-context-protocol marvin-context-protocol Bot added enhancement Improvement to existing functionality. For issues and smaller PR improvements. server Related to FastMCP server implementation or server-side functionality. labels Apr 24, 2026
@daniel-tsiang daniel-tsiang marked this pull request as ready for review April 24, 2026 21:09
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.

@daniel-tsiang daniel-tsiang changed the title Add silent error variants to skip server-side exception logging Add suppress_log parameter to FastMCP errors Apr 24, 2026
@daniel-tsiang daniel-tsiang force-pushed the main branch 2 times, most recently from a8073fc to bed316d Compare April 24, 2026 22:24
Add suppress_log parameter to FastMCPError and subclasses to skip server-side logging for expected control-flow errors.
@jlowin
Copy link
Copy Markdown
Member

jlowin commented Apr 25, 2026

I don't think suppressing the log is the right remedy here? Perhaps allowing a custom log level override? Otherwise the server has no report that the error took place.

@daniel-tsiang daniel-tsiang changed the title Add suppress_log parameter to FastMCP errors Add log_level parameter to FastMCP errors Apr 28, 2026
@daniel-tsiang
Copy link
Copy Markdown
Author

I don't think suppressing the log is the right remedy here? Perhaps allowing a custom log level override? Otherwise the server has no report that the error took place.

@jlowin Thanks Jeremiah, great suggestion. I've amended the PR to allow a custom log level override instead. Please can you take another look?

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

Labels

enhancement Improvement to existing functionality. For issues and smaller PR improvements. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server-side exception logging for expected control-flow errors creates noise in error monitoring

2 participants