Skip to content

Conversation

ChingEnLin
Copy link

@ChingEnLin ChingEnLin commented Aug 18, 2025

Describe your changes

Add support for application/x-www-form-urlencoded and multipart/form-data request bodies

Key Changes and Features:

  1. Included logic to automatically detect the appropriate content type for an operation's request body the OpenAPI conversion process (fastapi_mcp/openapi/convert.py)

    • Established priority order: application/x-www-form-urlencoded > multipart/form-data > application/json.
    • Added new utility functions (detect_form_encoded_content_type, detect_multipart_content_type) to fastapi_mcp/openapi/utils.py to facilitate detection.
  2. Added utility function extract_form_field_names in fastapi_mcp/openapi/utils.py to extract the expected form field names directly from the OpenAPI schema's properties for application/x-www-form-urlencoded and multipart/form-data request bodies

  3. Included the detected content_type and form_fields for each operation in the operation_map

    • When an API tool is executed (_execute_api_tool in fastapi_mcp/server.py), the request body (arguments) is now intelligently filtered:
      • For form-based requests, only the arguments corresponding to the extracted form_fields are included in the request payload, ensuring that httpx sends them correctly as data (form-urlencoded) or files (multipart).
      • For application/json requests (or fallback scenarios), all arguments are still sent as a JSON payload, maintaining existing behavior.
  4. Refactored the internal _request method in fastapi_mcp/server.py to use a new helper _request_with_body.

    • _request_with_body dynamically chooses between json=, data=, or files= parameters for httpx.AsyncClient.request based on the detected content_type, ensuring correct encoding for different request body types.

Backward Compatibility:

  • Extensive backward compatibility tests (tests/test_backward_compatibility.py) have been added to ensure that existing JSON-based API endpoints continue to function without any changes.
  • GET, PUT, DELETE, and other HTTP methods, along with error handling and header forwarding, have been verified to behave identically.
  • The system gracefully falls back to JSON behavior if no specific form-based content type is detected or if schema extraction for form fields fails.

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

Profile Update (Form-encoded)
Tool: update_profile_users_profile_post
   Arguments: {
      "name": "John Doe",
      "email": "john.doe@example.com",
      "bio": "Software developer passionate about AI and automation",
      "newsletter": true
}
   Result: {
  "success": true,
  "message": "Profile updated successfully",
  "user_id": "user_1",
  "profile": {
    "name": "John Doe",
    "email": "john.doe@example.com",
    "bio": "Software developer passionate about AI and automation",
    "newsletter": true
  }
}
  HTTP Request Type: application/x-www-form-urlencoded

Checklist before requesting a review

  • Added relevant tests
  • Run ruff & mypy
  • All tests pass

@Copilot Copilot AI review requested due to automatic review settings August 18, 2025 10:04
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant