Skip to content

Feature: Forward custom x- prefixed headers in FastApiMCP server #113

Open
@gustusmorten

Description

@gustusmorten

When using the FastApiMCP server, custom headers (particularly those starting with 'x-') are not automatically forwarded from the original HTTP request to the API calls. This can be problematic when working with APIs that rely on custom headers for functionality or authentication.

I would like to modify the header handling logic in the _execute_api_tool method to explicitly forward all custom headers that start with 'x-' from the original HTTP request. This would involve adding a new section in the header processing block that copies all x- prefixed headers from the original request.

  1. Forwarding all headers from the original request

The change would be made in the _execute_api_tool method around line 411, where headers are currently being processed. The modification would look something like this:

if http_request_info and http_request_info.headers:
    # Forward Authorization header
    if "Authorization" in http_request_info.headers:
        headers["Authorization"] = http_request_info.headers["Authorization"]
    elif "authorization" in http_request_info.headers:
        headers["Authorization"] = http_request_info.headers["authorization"]
    
    # Forward all x- prefixed headers
    for header_name, header_value in http_request_info.headers.items():
        if header_name.lower().startswith('x-'):
            headers[header_name] = header_value

This change would maintain backward compatibility while adding support for custom headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions