Describe the bug
When using the sse_client function to connect to a remote MCP server, if the MCP server sets an incorrect base URL (like http://localhost:8080), the sse_client will raise an exception in the following code:
| raiseValueError(error_msg) |
The read_stream_writer then sends this exception to the read_stream:
| awaitread_stream_writer.send(exc) |
However, since the read_stream has not been yielded yet, the exception will never be received.
As a result, the sse_client will be blocked indefinitely.
To Reproduce
- Set up an MCP server with an incorrect base URL on a remote server, for example:
importexpress,{Request,Response}from'express';import{McpServer}from'@modelcontextprotocol/sdk/server/mcp.js';import{SSEServerTransport}from'@modelcontextprotocol/sdk/server/sse.js';import{z}from'zod';constserver=newMcpServer({name: 'example-server',version: '1.0.0',});server.tool("calculate-bmi",{weightKg: z.number(),heightM: z.number()},async({ weightKg, heightM })=>({content: [{type: "text",text: String(weightKg/(heightM*heightM))}]}));constapp=express();consttransports: {[sessionId: string]: SSEServerTransport}={};app.get('/sse',async(_: Request,res: Response)=>{// set baseUrl for the transportconstbaseUrl='http://localhost:8080';consttransport=newSSEServerTransport(`${baseUrl}/messages`,res);transports[transport.sessionId]=transport;res.on('close',()=>{deletetransports[transport.sessionId];});awaitserver.connect(transport);});app.post('/messages',async(req: Request,res: Response)=>{constsessionId=req.query.sessionIdasstring;consttransport=transports[sessionId];if(transport){awaittransport.handlePostMessage(req,res);}else{res.status(400).send('No transport found for sessionId');}});app.listen(8080);- Try to connect to the remote MCP server, for example:
importasynciofrommcp.client.sseimportsse_clientasyncdefrun():
try:
asyncwithsse_client("http://{your_remote_server}:8080/sse"):
print("Connected to SSE endpoint successfully.")
exceptExceptionase:
print(f"Error: {e}")
if__name__=="__main__":
asyncio.run(run())Expected behavior
The sse_client should raise an exception and exit gracefully.
Logs
None
Additional context
None
Describe the bug
When using the
sse_clientfunction to connect to a remote MCP server, if the MCP server sets an incorrect base URL (like http://localhost:8080), thesse_clientwill raise an exception in the following code:python-sdk/src/mcp/client/sse.py
Line 81 in c2ca8e0
The
read_stream_writerthen sends this exception to theread_stream:python-sdk/src/mcp/client/sse.py
Line 107 in c2ca8e0
However, since the
read_streamhas not been yielded yet, the exception will never be received.As a result, the
sse_clientwill be blocked indefinitely.To Reproduce
Expected behavior
The sse_client should raise an exception and exit gracefully.
Logs
None
Additional context
None