fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

When I use fastmcp Client, how should I set the timeout?

Open Cherwayway opened this issue 7 months ago • 2 comments

Description

I searched through the client settings but couldn't find the timeout setting. I have upgraded to the latest version 2.3.3 and use the streaming-http protocol. My usage scenario is that I have an mcp tool that takes a very long time, maybe 10-20 minutes. Whether it is sse or streaming-http, there is a high probability of a timeout error.

Error reading SSE stream:                                                                                                                                                                                                                                                                                                                                                                      
Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                             
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpx/_transports/default.py", line 101, in map_httpcore_exceptions                                                                                                                                                                                                                                                      
    yield                                                                                                                                                                                                                                                                                                                                                                                      
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpx/_transports/default.py", line 271, in __aiter__                                                                                                                                                                                                                                                                    
    async for part in self._httpcore_stream:                                                                                                                                                                                                                                                                                                                                                   
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 407, in __aiter__                                                                                                                                                                                                                                                              
    raise exc from None                                                                                                                                                                                                                                                                                                                                                                        
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 403, in __aiter__                                                                                                                                                                                                                                                              
    async for part in self._stream:                                                                                                                                                                                                                                                                                                                                                            
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/http11.py", line 342, in __aiter__                                                                                                                                                                                                                                                                       
    raise exc                                                                                                                                                                                                                                                                                                                                                                                  
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/http11.py", line 334, in __aiter__                                                                                                                                                                                                                                                                       
    async for chunk in self._connection._receive_response_body(**kwargs):                                                                                                                                                                                                                                                                                                                      
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/http11.py", line 203, in _receive_response_body                                                                                                                                                                                                                                                          
    event = await self._receive_event(timeout=timeout)                                                                                                                                                                                                                                                                                                                                         
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_async/http11.py", line 217, in _receive_event                                                                                                                                                                                                                                                                  
    data = await self._network_stream.read(                                                                                                                                                                                                                                                                                                                                                    
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_backends/anyio.py", line 32, in read                                                                                                                                                                                                                                                                           
    with map_exceptions(exc_map):                                                                                                                                                                                                                                                                                                                                                              
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/contextlib.py", line 153, in __exit__                                                                                                                                                                                                                                                                                                  
    self.gen.throw(typ, value, traceback)                                                                                                                                                                                                                                                                                                                                                      
  File "/home/ouyang/miniconda3/envs/mcp/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions                                                                                                                                                                                                                                                                     
    raise to_exc(exc) from exc                                                                                                                                                                                                                                                                                                                                                                 
httpcore.ReadTimeout

Example Code

async with Client(
    SSE_ENDPOINT,
    read_timeout_seconds=datetime.timedelta(hours=1)
) as client:
    result = await client.call_tool(tool_name, tool_args)

Version Information

Latest version: 2.3.3

Additional Context

No response

Cherwayway avatar May 14 '25 06:05 Cherwayway

I fixed it by changing the httpcore package source file timeout here: File: /httpcore/_async/http11.py - Line 203 event = await self._receive_event(timeout=timeout)

Change it to any float second you want.

But if possible, we need to set the environment variable for the timeout in the package also.

constanreedjohn avatar May 14 '25 08:05 constanreedjohn

I fixed it by changing the httpcore package source file timeout here: File: /httpcore/_async/http11.py - Line 203 event = await self._receive_event(timeout=timeout)

Change it to any float second you want.

But if possible, we need to set the environment variable for the timeout in the package also.

cool, but I need to deploy my service, and it's a bit too hard code to modify the httpcore code directly...

Cherwayway avatar May 14 '25 08:05 Cherwayway

+1, would be great if you could expose the args for httpx client via some **kwargs.

kocsszi avatar May 14 '25 16:05 kocsszi

PR incoming

jlowin avatar May 14 '25 21:05 jlowin

Please see #455

jlowin avatar May 15 '25 00:05 jlowin