Throwing an error when the function takes too long, TypeError: 'NoneType' object is not callable
Throwing an error when the function takes too long
import asyncio
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Demo")
@mcp.tool()
async def add(a: int, b: int) -> int:
# await asyncio.sleep(5) # work well
await asyncio.sleep(60) # TypeError: 'NoneType' object is not callable
return a + b
mcp.run(transport="sse")
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 426, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
raise exc
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
raise exc
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 716, in __call__
await self.middleware_stack(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 736, in app
await route.handle(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 290, in handle
await self.app(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 78, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
raise exc
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 76, in app
await response(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not callable
Python 3.11.6 windows 11
mcp==1.6.0
starlette==0.46.1
uvicorn==0.34.0
Throwing an error when the function takes too long
import asyncio
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Demo")
@mcp.tool() async def add(a: int, b: int) -> int: # await asyncio.sleep(5) # work well await asyncio.sleep(60) # TypeError: 'NoneType' object is not callable return a + b
mcp.run(transport="sse") ERROR: Exception in ASGI application Traceback (most recent call last): File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 426, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in call return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\applications.py", line 112, in call await self.middleware_stack(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\errors.py", line 187, in call raise exc File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\errors.py", line 165, in call await self.app(scope, receive, _send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette_exception_handler.py", line 53, in wrapped_app raise exc File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette_exception_handler.py", line 42, in wrapped_app await app(scope, receive, sender) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 716, in call await self.middleware_stack(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 736, in app await route.handle(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 290, in handle await self.app(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 78, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette_exception_handler.py", line 53, in wrapped_app raise exc File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette_exception_handler.py", line 42, in wrapped_app await app(scope, receive, sender) File "D:\Users\Kan\miniconda3\envs\py311\Lib\site-packages\starlette\routing.py", line 76, in app await response(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'NoneType' object is not callable
Python 3.11.6 windows 11 mcp==1.6.0 starlette==0.46.1 uvicorn==0.34.0
Obviously, the timeout is not an error of SSE, it's an error reported by the client, that is, MCP Inspector. You can test it with MCP client, and SSE is OK. The method can return the correct data.
from mcp import ClientSession from mcp.client.sse import sse_client server_params = "http://localhost:8000/sse" async def run(): async with sse_client(server_params) as (read, write): async with ClientSession( read, write ) as session: await session.initialize() result = await session.call_tool("add", arguments={"a": "1","b": "1"}) print(result)
if name == "main": import asyncio asyncio.run(run())
If I change asynchronous to synchronous, there won't be any errors
# server.py
import asyncio
import time
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Demo")
@mcp.tool()
async def add1(a: int, b: int) -> int:
# await asyncio.sleep(5) # work well
await asyncio.sleep(60) # TypeError: 'NoneType' object is not callable
return a + b
@mcp.tool()
def add2(a: int, b: int) -> int:
# time.sleep(5) # MCP Inspector works fine
time.sleep(60) # MCP Inspector throws Request timed out, but MCP Server works fine.
return a + b
if __name__ == "__main__":
mcp.run(transport="sse")
use MCP Inspector like this http://localhost:5173/?timeout=60000
After my test, I found that it was still the problem of inspector, not sse. You can use the above py code to call add1 and add2, and the verification result is still ok.
I found the inspector problem. In the above picture, there is a timeout mechanism in both places 1 and 2. The default timeout in place 1 is 60 seconds, and the default timeout in place 2 is 10 seconds. http://localhost:5173/?timeout=60000 You solved the timeout mechanism in place 2 by accessing it this way, but place 1 still times out.
If you know how to use F12 to view the console, you can see a timeout error. In picture 2, modify the timeout variable to a larger number, and the test will be ok. Of course, you can submit a PR to the inspector, Remember to fully test before rp,Hhhhhhh
import asyncio
from mcp.server.fastmcp import FastMCP, Context
mcp = FastMCP("Demo")
@mcp.tool()
async def add(a: int, b: int, ctx: Context) -> int:
#await asyncio.sleep(60) # TypeError: 'NoneType' object is not callable
for i in range(1, 60):
asyncio.sleep(1)
await ctx.report_progress(0, 1, 'MCP Server正在处理中...') #向客户度发送信息或者progress 信息可以保证链接不会超时
#await ctx.info('处理中...')
return a + b
mcp.run(transport="sse")
Looks like this may have been an inspector issue, not on the SDK.