server disconnected without sending a response
Description
running client but failed,my server is no problen,ican confirm in explore
----------client.py-------
[import asyncio
from fastmcp import Client from fastmcp.client.transports import SSETransport
sse_url = "http://localhost:8000/sse"
Option 1: Inferred transport
client_inferred = Client(sse_url)
Option 2: Explicit transport (e.g., to add custom headers)
headers = {"Authorization": "Bearer mytoken"} transport_explicit = SSETransport(url=sse_url, headers=headers) client_explicit = Client(transport_explicit)
async def use_sse_client(client): async with client: tools = await client.list_tools() print(f"Connected via SSE, found tools: {tools}")
asyncio.run(use_sse_client(client_inferred))
asyncio.run(use_sse_client(client_explicit))]
----------server.py------- [from fastmcp import FastMCP import random
创建MCP服务器实例,指定端口
mcp = FastMCP("Weather Service", port=8000)
模拟的天气数据
weather_data = { "New York": {"temp": range(10, 25), "conditions": ["sunny", "cloudy", "rainy"]}, "London": {"temp": range(5, 20), "conditions": ["cloudy", "rainy", "foggy"]}, "Tokyo": {"temp": range(15, 30), "conditions": ["sunny", "cloudy", "humid"]}, "Sydney": {"temp": range(20, 35), "conditions": ["sunny", "clear", "hot"]}, }
@mcp.tool() def get_weather(city: str) -> dict: """获取指定城市的当前天气""" if city not in weather_data: return {"error": f"无法找到城市 {city} 的天气数据"}
data = weather_data[city]
temp = random.choice(list(data["temp"]))
condition = random.choice(data["conditions"])
return {
"city": city,
"temperature": temp,
"condition": condition,
"unit": "celsius"
}
@mcp.resource("weather://cities") def get_available_cities() -> list: """获取所有可用的城市列表""" return list(weather_data.keys())
@mcp.resource("weather://forecast/{city}") def get_forecast(city: str) -> dict: """获取指定城市的天气预报资源""" if city not in weather_data: return {"error": f"无法找到城市 {city} 的天气预报"}
forecast = []
for i in range(5): # 5天预报
data = weather_data[city]
temp = random.choice(list(data["temp"]))
condition = random.choice(data["conditions"])
forecast.append({
"day": i + 1,
"temperature": temp,
"condition": condition
})
return {
"city": city,
"forecast": forecast,
"unit": "celsius"
}
if name == "main": # 使用SSE传输方式启动服务器 mcp.run(transport="sse")]
error info: [ D:\Users\10294692\software\python\python3.exe D:/Users/10294692/code/AI-knowledge-code/AI-knowledge-code/mcp-test/demo/client.py
- Exception Group Traceback (most recent call last):
| File "D:\Users\10294692\code\AI-knowledge-code\AI-knowledge-code\mcp-test\demo\client.py", line 22, in
| asyncio.run(use_sse_client(client_explicit)) | File "D:\Users\10294692\software\python\lib\asyncio\runners.py", line 44, in run | return loop.run_until_complete(main) | File "D:\Users\10294692\software\python\lib\asyncio\base_events.py", line 641, in run_until_complete | return future.result() | File "D:\Users\10294692\code\AI-knowledge-code\AI-knowledge-code\mcp-test\demo\client.py", line 17, in use_sse_client | async with client: | File "D:\Users\10294692\software\python\lib\site-packages\fastmcp\client\client.py", line 96, in aenter | self._session = await self._session_cm.aenter() | File "D:\Users\10294692\software\python\lib\contextlib.py", line 199, in aenter | return await anext(self.gen) | File "D:\Users\10294692\software\python\lib\site-packages\fastmcp\client\transports.py", line 116, in connect_session | async with sse_client(self.url, headers=self.headers) as transport: | File "D:\Users\10294692\software\python\lib\contextlib.py", line 199, in aenter | return await anext(self.gen) | File "D:\Users\10294692\software\python\lib\site-packages\mcp\client\sse.py", line 43, in sse_client | async with anyio.create_task_group() as tg: | File "D:\Users\10294692\software\python\lib\site-packages\anyio_backends_asyncio.py", line 772, in aexit | raise BaseExceptionGroup( | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | Traceback (most recent call last): | File "D:\Users\10294692\software\python\lib\site-packages\httpx_transports\default.py", line 101, in map_httpcore_exceptions | yield | File "D:\Users\10294692\software\python\lib\site-packages\httpx_transports\default.py", line 394, in handle_async_request | resp = await self._pool.handle_async_request(req) | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\connection_pool.py", line 216, in handle_async_request | raise exc from None | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\connection_pool.py", line 196, in handle_async_request | response = await connection.handle_async_request( | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\http_proxy.py", line 207, in handle_async_request | return await self._connection.handle_async_request(proxy_request) | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\connection.py", line 101, in handle_async_request | return await self._connection.handle_async_request(request) | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\http11.py", line 143, in handle_async_request | raise exc | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\http11.py", line 113, in handle_async_request | ) = await self._receive_response_headers(**kwargs) | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\http11.py", line 186, in _receive_response_headers | event = await self._receive_event(timeout=timeout) | File "D:\Users\10294692\software\python\lib\site-packages\httpcore_async\http11.py", line 238, in _receive_event | raise RemoteProtocolError(msg) | httpcore.RemoteProtocolError: Server disconnected without sending a response. | | The above exception was the direct cause of the following exception: | | Traceback (most recent call last): | File "D:\Users\10294692\software\python\lib\site-packages\mcp\client\sse.py", line 47, in sse_client | async with aconnect_sse( | File "D:\Users\10294692\software\python\lib\contextlib.py", line 199, in aenter | return await anext(self.gen) | File "D:\Users\10294692\software\python\lib\site-packages\httpx_sse_api.py", line 69, in aconnect_sse | async with client.stream(method, url, headers=headers, **kwargs) as response: | File "D:\Users\10294692\software\python\lib\contextlib.py", line 199, in aenter | return await anext(self.gen) | File "D:\Users\10294692\software\python\lib\site-packages\httpx_client.py", line 1583, in stream | response = await self.send( | File "D:\Users\10294692\software\python\lib\site-packages\httpx_client.py", line 1629, in send | response = await self._send_handling_auth( | File "D:\Users\10294692\software\python\lib\site-packages\httpx_client.py", line 1657, in _send_handling_auth | response = await self._send_handling_redirects( | File "D:\Users\10294692\software\python\lib\site-packages\httpx_client.py", line 1694, in _send_handling_redirects | response = await self._send_single_request(request) | File "D:\Users\10294692\software\python\lib\site-packages\httpx_client.py", line 1730, in _send_single_request | response = await transport.handle_async_request(request) | File "D:\Users\10294692\software\python\lib\site-packages\httpx_transports\default.py", line 393, in handle_async_request | with map_httpcore_exceptions(): | File "D:\Users\10294692\software\python\lib\contextlib.py", line 153, in exit | self.gen.throw(typ, value, traceback) | File "D:\Users\10294692\software\python\lib\site-packages\httpx_transports\default.py", line 118, in map_httpcore_exceptions | raise mapped_exc(message) from exc | httpx.RemoteProtocolError: Server disconnected without sending a response. +------------------------------------
Process finished with exit code 1
]
Example Code
Version Information
2.2.10
Additional Context
No response
Please fix your formatting to provide a copyable MRE or I can't debug this
把VPN(梯子)关掉,就可以了