fastapi_mcp
fastapi_mcp copied to clipboard
How to customize the header
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
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]
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.