fastapi_mcp icon indicating copy to clipboard operation
fastapi_mcp copied to clipboard

How to customize the header

Open ihoment-lys opened this issue 4 months ago • 2 comments

Describe the bug Error calling batch_delete_batch_delete_post. Status code: 422. Response: {"detail":[{"type":"missing","loc":["header","x-token"],"msg":"Field required","input":null}]}

To Reproduce

Image

ihoment-lys avatar Aug 19 '25 08:08 ihoment-lys

Hey @ihoment-lys

I passed custom headers using the following implementation:

On the MCP server side:

#MCP server setup
mcp = FastApiMCP(
    app,
    name="Image generation mcp",
    include_tags=["image_generation", "utilities"],
    headers=["X-User-ID", "Authorization"],
)

On the client side using FastMCP client:

async def get_mcp_client(user_id: str) -> Optional[Client]:
    """
    Retrieves or creates a FastMCP client for a given user.
    Returns None if MCP_SERVER_URL is not configured.
    """
    if not server_url:
        print("Warning: MCP_SERVER_URL not set. Agent will run without tools.")
        return None
        
    if user_id not in mcp_clients:
        headers = {
            "X-User-ID": user_id
        }
        transport = SSETransport(server_url, headers=headers)
        mcp_clients[user_id] = Client(transport)
    return mcp_clients[user_id]

PratyushChauhan avatar Aug 23 '25 14:08 PratyushChauhan

From my undertanding, this library doesn't allow passing custom headers from the clientside to the MCP. Only the default headers are allowed and that is a problem.

Mod5ied avatar Sep 25 '25 09:09 Mod5ied