-
Notifications
You must be signed in to change notification settings - Fork 326
Forward headers #82
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
Comments
Thank you for this great library. I am also experiencing the same problem. I am making a request with a header from client sied, but it seems that the header value is not being referenced on the server side. |
In case it might be of help, I got it to work with the following code, which creates an # Create FastAPI app
app = FastAPI(
title="ClickHouse MCP Server",
description="A Model Context Protocol server for ClickHouse databases",
version="1.0.0",
debug=True, # Enable FastAPI debug mode
)
# Create a custom HTTP client with auth header
class AuthClient(httpx.AsyncClient):
def __init__(self, auth_token: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.headers.update({"Authorization": f"Bearer {auth_token}"})
# Create FastAPI-MCP instance
mcp = FastApiMCP(
app,
name="mcp-clickhouse-fastapi",
description="ClickHouse database access via MCP",
base_url="http://localhost/mcp",
describe_all_responses=True,
describe_full_response_schema=True,
)
# Dependency to get auth client for each request
async def get_auth_client(request: Request) -> AuthClient:
auth_header = request.headers.get("authorization")
if not auth_header:
raise HTTPException(status_code=401, detail="Missing Authorization header")
try:
token = auth_header.split(" ")[1]
return AuthClient(auth_token=token)
except IndexError:
raise HTTPException(status_code=401, detail="Invalid Authorization header format")
# Add middleware to log requests
@app.middleware("http")
async def log_requests(request, call_next):
logger.debug(f"Incoming request: {request.method} {request.url}")
logger.debug(f"Headers: {request.headers}")
# Create a new auth client for this request
request.state.auth_client = await get_auth_client(request)
# Set the client in MCP for this request
mcp._http_client = request.state.auth_client
response = await call_next(request)
logger.debug(f"Response status: {response.status_code}")
return response It's a tricky solution, although it'd be great to be handled by |
@jsulopzs Thank you very much! |
I agree. This would be a good one to have a built-in way to handle this. Seems related to #10? |
Hi all! |
Awesome, great work! |
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:
How to make the headers defined in the mcp.json be forwarded on any request?
The text was updated successfully, but these errors were encountered: