Feat: Add support for form-encoded and multipart request bodies #220
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-urlencoded
andmultipart/form-data
request 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.py
to facilitate detection.Added utility function
extract_form_field_names
infastapi_mcp/openapi/utils.py
to extract the expected form field names directly from the OpenAPI schema's properties forapplication/x-www-form-urlencoded
andmultipart/form-data
request bodiesIncluded the detected
content_type
andform_fields
for each operation in theoperation_map
_execute_api_tool
infastapi_mcp/server.py
), the request body (arguments
) is now intelligently filtered:form_fields
are included in the request payload, ensuring thathttpx
sends them correctly asdata
(form-urlencoded) orfiles
(multipart).application/json
requests (or fallback scenarios), all arguments are still sent as a JSON payload, maintaining existing behavior.Refactored the internal
_request
method infastapi_mcp/server.py
to use a new helper_request_with_body
._request_with_body
dynamically chooses betweenjson=
,data=
, orfiles=
parameters forhttpx.AsyncClient.request
based 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