Skip to content

Conversation

floran-putter
Copy link
Contributor

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

@Robiemaan
Copy link

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

@erpic see @floran-putter 's message Re Authorization backwards compatibility. Can we merge?

@devilb2103
Copy link

On god please merge this 😭

Copy link

codecov bot commented Jul 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@shahar4499 shahar4499 left a comment

Choose a reason for hiding this comment

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

Great job! Thanks!!

@shahar4499 shahar4499 merged commit 1710b14 into tadata-org:main Jul 10, 2025
6 checks passed
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
5 participants