Feat: Add support for form-encoded and multipart request bodies#220
Open
ChingEnLin wants to merge 8 commits intotadata-org:mainfrom
Open
Feat: Add support for form-encoded and multipart request bodies#220ChingEnLin wants to merge 8 commits intotadata-org:mainfrom
ChingEnLin wants to merge 8 commits intotadata-org:mainfrom
Conversation
…ntent type and form fields
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces comprehensive support for form-encoded and multipart request bodies in OpenAPI specifications. The enhancement enables MCP servers to intelligently handle different content types beyond JSON, with automatic detection and proper HTTP client configuration.
- Implements content type detection with priority-based selection (form-encoded > multipart > JSON)
- Adds form field extraction from OpenAPI schemas for precise request body construction
- Refactors HTTP client handling to use appropriate encoding based on detected content types
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_openapi_conversion.py | Adds comprehensive tests for content type detection functionality and operation map structure verification |
| tests/test_mcp_execute_api_tool.py | Updates existing tests to accommodate new HTTP client behavior using generic request() method |
| tests/test_backward_compatibility.py | New extensive backward compatibility test suite ensuring JSON endpoints remain unchanged |
| fastapi_mcp/server.py | Implements form field filtering logic and new _request_with_body() method for content-type-specific HTTP requests |
| fastapi_mcp/openapi/utils.py | Adds utility functions for content type detection and form field extraction from schemas |
| fastapi_mcp/openapi/convert.py | Integrates content type detection into OpenAPI conversion process and stores metadata in operation map |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your changes
Add support for
application/x-www-form-urlencodedandmultipart/form-datarequest bodiesKey Changes and Features:
Included logic to automatically detect the appropriate content type for an operation's request body the OpenAPI conversion process (
fastapi_mcp/openapi/convert.py)application/x-www-form-urlencoded>multipart/form-data>application/json.detect_form_encoded_content_type,detect_multipart_content_type) tofastapi_mcp/openapi/utils.pyto facilitate detection.Added utility function
extract_form_field_namesinfastapi_mcp/openapi/utils.pyto extract the expected form field names directly from the OpenAPI schema's properties forapplication/x-www-form-urlencodedandmultipart/form-datarequest bodiesIncluded the detected
content_typeandform_fieldsfor each operation in theoperation_map_execute_api_toolinfastapi_mcp/server.py), the request body (arguments) is now intelligently filtered:form_fieldsare included in the request payload, ensuring thathttpxsends them correctly asdata(form-urlencoded) orfiles(multipart).application/jsonrequests (or fallback scenarios), all arguments are still sent as a JSON payload, maintaining existing behavior.Refactored the internal
_requestmethod infastapi_mcp/server.pyto use a new helper_request_with_body._request_with_bodydynamically chooses betweenjson=,data=, orfiles=parameters forhttpx.AsyncClient.requestbased on the detectedcontent_type, ensuring correct encoding for different request body types.Backward Compatibility:
tests/test_backward_compatibility.py) have been added to ensure that existing JSON-based API endpoints continue to function without any changes.This enhancement provides greater flexibility in integrating with diverse OpenAPI-defined APIs that utilize various request body formats.
Issue ticket number and link (if applicable)
#119
Screenshots of the feature / bugfix
Checklist before requesting a review