Skip to content

feat: Allow setting Authorization header from tool arguments #144

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
Open
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
9 changes: 9 additions & 0 deletions fastapi_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ async def _execute_api_tool(
Returns:
The result as MCP content types
"""
logger.debug(f"FastAPI-MCP: Received call_tool request. Tool: {tool_name}, Arguments: {json.dumps(arguments)}")

if tool_name not in operation_map:
raise Exception(f"Unknown tool: {tool_name}")

Expand Down Expand Up @@ -413,6 +415,13 @@ async def _execute_api_tool(
elif "authorization" in http_request_info.headers:
headers["Authorization"] = http_request_info.headers["authorization"]

# NEW: Check arguments for a user_access_token if Authorization header is still missing
if "Authorization" not in headers and arguments and "user_access_token" in arguments:
token = arguments.pop("user_access_token", None) # Remove it from arguments so it doesn't become body
if token:
headers["Authorization"] = f"Bearer {token}"
logger.debug("Set Authorization header from 'user_access_token' in tool arguments.")

body = arguments if arguments else None

try:
Expand Down