Skip to content

Forward headers #82

@jsulopzs

Description

@jsulopzs

I'm currently using mcp-proxy to call the mcp server running on fastapi_mcp.

The header with the bearer token is shown at first when connecting to the mcp endpoint. However, when the mcp host calls the tools, the header doesn't include the bearer token.

Checkout the logs:

INFO:     127.0.0.1:49358 - "POST /mcp/messages/?session_id=b88f9bf746c540b1ba08d8cfa7a9d123 HTTP/1.1" 202 Accepted
2025-04-19 16:29:18,535 - fastapi_mcp.transport.sse - DEBUG - Sending message to writer from background task: root=JSONRPCRequest(method='tools/list', params=None, jsonrpc='2.0', id=5)
2025-04-19 16:29:18,536 - mcp.server.lowlevel.server - INFO - Processing request of type ListToolsRequest
2025-04-19 16:29:53,000 - mcp-clickhouse-fastapi - DEBUG - Incoming request: POST http://localhost:8080/mcp/messages/?session_id=b88f9bf746c540b1ba08d8cfa7a9d123
2025-04-19 16:29:53,000 - mcp-clickhouse-fastapi - DEBUG - Headers: Headers({'host': 'localhost:8080', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.28.1', 'authorization': 'Bearer d315c4c3-321c-43c2-89de-da4cc264ce4c', 'content-length': '96', 'content-type': 'application/json'})
2025-04-19 16:29:53,000 - mcp-clickhouse-fastapi - DEBUG - Captured auth header: Bearer d315c4c3-321c-43c2-89de-da4cc264ce4c
2025-04-19 16:29:53,001 - fastapi_mcp.transport.sse - DEBUG - Handling POST message with FastAPI patterns
2025-04-19 16:29:53,001 - fastapi_mcp.transport.sse - DEBUG - Parsed session ID: b88f9bf7-46c5-40b1-ba08-d8cfa7a9d123
2025-04-19 16:29:53,002 - fastapi_mcp.transport.sse - DEBUG - Received JSON: {"method":"tools/call","params":{"name":"list_databases","arguments":{}},"jsonrpc":"2.0","id":6}
2025-04-19 16:29:53,002 - fastapi_mcp.transport.sse - DEBUG - Validated client message: root=JSONRPCRequest(method='tools/call', params={'name': 'list_databases', 'arguments': {}}, jsonrpc='2.0', id=6)
2025-04-19 16:29:53,002 - fastapi_mcp.transport.sse - DEBUG - Accepting message, will send in background
2025-04-19 16:29:53,002 - mcp-clickhouse-fastapi - DEBUG - Response status: 202
INFO:     127.0.0.1:49676 - "POST /mcp/messages/?session_id=b88f9bf746c540b1ba08d8cfa7a9d123 HTTP/1.1" 202 Accepted
2025-04-19 16:29:53,003 - fastapi_mcp.transport.sse - DEBUG - Sending message to writer from background task: root=JSONRPCRequest(method='tools/call', params={'name': 'list_databases', 'arguments': {}}, jsonrpc='2.0', id=6)
2025-04-19 16:29:53,003 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
2025-04-19 16:29:53,004 - fastapi_mcp.server - DEBUG - Making GET request to http://localhost:8080/databases
2025-04-19 16:29:53,009 - mcp-clickhouse-fastapi - DEBUG - Incoming request: GET http://localhost:8080/databases
2025-04-19 16:29:53,009 - mcp-clickhouse-fastapi - DEBUG - Headers: Headers({'host': 'localhost:8080', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br, zstd', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.28.1'})
2025-04-19 16:29:53,010 - mcp-clickhouse-fastapi - INFO - Request details: method=GET, url=http://localhost:8080/databases, headers=Headers({'host': 'localhost:8080', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br, zstd', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.28.1'}), body=b''
2025-04-19 16:29:53,010 - mcp-clickhouse-fastapi - INFO - Request headers: Headers({'host': 'localhost:8080', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br, zstd', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.28.1'})
2025-04-19 16:29:53,010 - mcp-clickhouse-fastapi - WARNING - Missing Authorization header
2025-04-19 16:29:53,010 - mcp-clickhouse-fastapi - DEBUG - Response status: 401
INFO:     127.0.0.1:49678 - "GET /databases HTTP/1.1" 401 Unauthorized
2025-04-19 16:29:53,011 - httpx - INFO - HTTP Request: GET http://localhost:8080/databases "HTTP/1.1 401 Unauthorized"
2025-04-19 16:29:53,012 - fastapi_mcp.server - ERROR - Error calling list_databases
Traceback (most recent call last):
  File "/Users/sota/miniforge3/envs/work/lib/python3.13/site-packages/fastapi_mcp/server.py", line 272, in _execute_api_tool
    raise Exception(
        f"Error calling {tool_name}. Status code: {response.status_code}. Response: {response.text}"
    )
Exception: Error calling list_databases. Status code: 401. Response: {"detail":"Unauthorized"}

How to make the headers defined in the mcp.json be forwarded on any request?

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