Skip to content

Fix unhandled exceptions in OpenAPI POST tool calls#3133

Merged
jlowin merged 4 commits intomainfrom
fix/openapi-post-error-handling
Feb 10, 2026
Merged

Fix unhandled exceptions in OpenAPI POST tool calls#3133
jlowin merged 4 commits intomainfrom
fix/openapi-post-error-handling

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Feb 10, 2026

OpenAPITool.run() only catches httpx.* exceptions, so any failure during request building (parameter routing, body construction) escapes unhandled and surfaces to clients as a generic "Internal Server Error" with no diagnostic info. This particularly affects POST requests where the body construction code exercises more fragile paths.

Two changes: harden _unflatten_arguments() to guard against empty content_schema dicts and non-dict body schemas, and add a catch-all in run() that wraps unexpected errors with the tool's method, path, and original error — so users see something like "Error building request for POST /items: KeyError: missing_param" instead of a silent 500.

mcp = FastMCP.from_openapi(openapi_spec=spec, client=client)
# POST tool calls that previously failed with "Internal Server Error"
# now produce actionable error messages

Closes #3113

Harden body construction in _unflatten_arguments() to guard against
empty content_schema dicts, and add a catch-all in OpenAPITool.run()
so unexpected errors during request building produce actionable messages
instead of generic "Internal Server Error".

Closes #3113
@jlowin jlowin added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. openapi Related to OpenAPI integration, parsing, or code generation features. labels Feb 10, 2026
@marvin-context-protocol marvin-context-protocol Bot added the http Related to HTTP transport, networking, or web server functionality. label Feb 10, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 10, 2026

Walkthrough

Documentation anchors were updated to reflect new source line numbers. In src/fastmcp/server/providers/openapi/components.py, OpenAPITool.run now wraps request construction in a try/except and raises a ValueError containing HTTP method, path, exception type, and message if construction fails; the subsequent request send/response handling remains separate. In src/fastmcp/utilities/openapi/director.py, request-body construction now checks that route.request_body.content_schema exists and is non-empty and that the resolved body schema is a dict before accessing its "type" property.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing unhandled exceptions in OpenAPI POST tool calls, which matches the core objective of improving error handling.
Description check ✅ Passed The description comprehensively covers the changes, explains the problem (unhandled exceptions in request building), describes both code changes with examples, and explicitly links to the related issue #3113.
Linked Issues check ✅ Passed The PR addresses issue #3113 by adding try/except exception handling in OpenAPITool.run() and defensive checks in _unflatten_arguments() to catch errors during request building and provide actionable error messages instead of generic 500 responses.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing exception handling in OpenAPI request building: exception handling in components.py, defensive schema checks in director.py, and documentation anchor updates. No unrelated changes detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/openapi-post-error-handling

No actionable comments were generated in the recent review. 🎉

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jlowin
Copy link
Copy Markdown
Member Author

jlowin commented Feb 10, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 10, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bd3c3ccf4e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +221 to +224
except Exception as e:
raise ValueError(
f"Error building request for {self._route.method.upper()} "
f"{self._route.path}: {type(e).__name__}: {e}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Limit catch-all to request-building failures

The new except Exception in OpenAPITool.run catches non-HTTPX errors from the entire method, not just request construction, so failures that happen after send() (for example response parsing or ToolResult validation) are now rethrown as Error building request ..., which is misleading and makes production debugging harder. Please scope this handler to the build step or use a message that matches all covered phases.

Useful? React with 👍 / 👎.



### `OpenAPIResource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/openapi/components.py#L222" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
### `OpenAPIResource` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/providers/openapi/components.py#L228" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove edits under auto-generated python-sdk docs

This change modifies docs/python-sdk/**, but the repository guideline in /workspace/fastmcp/AGENTS.md states "Never modify docs/python-sdk/** (auto-generated)." Committing these generated line-reference updates creates avoidable churn and risks stale docs diffs being overwritten by generation tooling.

Useful? React with 👍 / 👎.

jlowin and others added 2 commits February 10, 2026 13:07
The previous except Exception covered the entire try block including
the HTTP send and response handling, producing misleading "Error
building request" messages for post-send failures. Now request
building has its own try/except and HTTP failures fall through to the
specific httpx exception handlers.
@jlowin jlowin merged commit a715176 into main Feb 10, 2026
14 checks passed
@jlowin jlowin deleted the fix/openapi-post-error-handling branch February 10, 2026 18:13
@sandeepkunusoth
Copy link
Copy Markdown

hi @jlowin how can i test this change with 2.14. version can this be released in 2.14.6?

@jlowin
Copy link
Copy Markdown
Member Author

jlowin commented Feb 14, 2026

This went out in 3.0 rc1

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. http Related to HTTP transport, networking, or web server functionality. openapi Related to OpenAPI integration, parsing, or code generation features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastMCP.from_openapi() tool call fails on some models like claude when tool call with POST parameters in prompt

2 participants