Skip to content

add 'http_headers' parameter to FastApiMCP constructor #187

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions fastapi_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def __init__(
"""
),
] = None,
http_headers: Annotated[
Optional[List[str]],
Doc(
"""
Optional list of headers to forward to the API calls. Headers are case-insensitive.
"""
),
] = None,
include_operations: Annotated[
Optional[List[str]],
Doc("List of operation IDs to include as MCP tools. Cannot be used with exclude_operations."),
Expand Down Expand Up @@ -147,6 +155,9 @@ def __init__(
timeout=10.0,
)

self._http_headers_lowercase_set = { h.lower() for h in http_headers or [] }
self._http_headers_lowercase_set.add("authorization")

self.setup_server()

def setup_server(self) -> None:
Expand Down Expand Up @@ -407,11 +418,11 @@ async def _execute_api_tool(
raise ValueError(f"Parameter name is None for parameter: {param}")
headers[param_name] = arguments.pop(param_name)

# Add headers that we want to forward.
if http_request_info and http_request_info.headers:
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"]
for k, v in http_request_info.headers.items():
if k.lower() in self._http_headers_lowercase_set:
headers[k] = v

body = arguments if arguments else None

Expand Down