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-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