Skip to content

Add configurable HTTP header forwarding #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

floran-putter
Copy link

Describe your changes

  • Add headers argument to FastApiMCP to specify an allow-list for HTTP header forwarding.
  • In _execute_api_tool, match incoming headers case-insensitive against the allow-list and forward them using their original names.
  • Remove previous logic that renamed "authorization" to "Authorization" and update the existing header-passthrough test accordingly.
  • Add pytest fixture fastapi_mcp_with_custom_header and test_custom_header_passthrough_to_tool_handler to test custom header passthrough.

Issue ticket number and link (if applicable)

Closes #113

Screenshots of the feature / bugfix

Checklist before requesting a review

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

@erpic
Copy link

erpic commented Jun 29, 2025

self._forward_headers = {h.lower() for h in (headers or ["Authorization"])}

I think adding "Authorization" as default value here has no effect. This set is tested against lower cased strings so that would never match. Also, "Authorization" is added separately

Suggest changing to this?

self._forward_headers = {h.lower() for h in (headers or [])}

Or to this:

self._forward_headers = {h.lower() for h in (headers or ["authorization"])}
and then remove the logic on testing for "Authroization" or "authroization

@floran-putter
Copy link
Author

Just to clarify, "Authorization" does get lowercased by the set comprehension, so the default of self._forward_headers becomes {"authorization"} as intended.

The special-case check for testing "Authorization" or "authorization" was already removed in this PR, and keeping ["Authorization"] as the default is necessary for backwards compatibility, so this setup makes sense as-is.

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.

Feature: Forward custom x- prefixed headers in FastApiMCP server
2 participants