@@ -22,6 +22,18 @@ def fastapi_mcp(simple_fastapi_app: FastAPI) -> FastApiMCP:
22
22
return mcp
23
23
24
24
25
+ @pytest .fixture
26
+ def fastapi_mcp_with_custom_header (simple_fastapi_app : FastAPI ) -> FastApiMCP :
27
+ mcp = FastApiMCP (
28
+ simple_fastapi_app ,
29
+ name = "Test MCP Server with custom header" ,
30
+ description = "Test description" ,
31
+ headers = ["X-Custom-Header" ],
32
+ )
33
+ mcp .mount ()
34
+ return mcp
35
+
36
+
25
37
@pytest .fixture
26
38
def lowlevel_server_simple_app (fastapi_mcp : FastApiMCP ) -> Server :
27
39
return fastapi_mcp .server
@@ -311,5 +323,48 @@ async def test_headers_passthrough_to_tool_handler(fastapi_mcp: FastApiMCP):
311
323
312
324
if mock_request .called :
313
325
headers_arg = mock_request .call_args [0 ][4 ] # headers are the 5th argument
314
- assert "Authorization" in headers_arg
315
- assert headers_arg ["Authorization" ] == "Bearer token456"
326
+ assert "authorization" in headers_arg
327
+ assert headers_arg ["authorization" ] == "Bearer token456"
328
+
329
+
330
+ @pytest .mark .asyncio
331
+ async def test_custom_header_passthrough_to_tool_handler (fastapi_mcp_with_custom_header : FastApiMCP ):
332
+ from unittest .mock import patch , MagicMock
333
+ from fastapi_mcp .types import HTTPRequestInfo
334
+
335
+ # Test with custom header "X-Custom-Header"
336
+ with patch .object (fastapi_mcp_with_custom_header , "_request" ) as mock_request :
337
+ mock_response = MagicMock ()
338
+ mock_response .status_code = 200
339
+ mock_response .text = '{"result": "success"}'
340
+ mock_response .json .return_value = {"result" : "success" }
341
+ mock_request .return_value = mock_response
342
+
343
+ http_request_info = HTTPRequestInfo (
344
+ method = "POST" ,
345
+ path = "/test" ,
346
+ headers = {"X-Custom-Header" : "MyValue123" },
347
+ cookies = {},
348
+ query_params = {},
349
+ body = None ,
350
+ )
351
+
352
+ try :
353
+ # Call the _execute_api_tool method directly
354
+ # We don't care if it succeeds, just that _request gets the right headers
355
+ await fastapi_mcp_with_custom_header ._execute_api_tool (
356
+ client = fastapi_mcp_with_custom_header ._http_client ,
357
+ tool_name = "get_item" ,
358
+ arguments = {"item_id" : 1 },
359
+ operation_map = fastapi_mcp_with_custom_header .operation_map ,
360
+ http_request_info = http_request_info ,
361
+ )
362
+ except Exception :
363
+ pass
364
+
365
+ assert mock_request .called , "The _request method was not called"
366
+
367
+ if mock_request .called :
368
+ headers_arg = mock_request .call_args [0 ][4 ] # headers are the 5th argument
369
+ assert "X-Custom-Header" in headers_arg
370
+ assert headers_arg ["X-Custom-Header" ] == "MyValue123"
0 commit comments