Initial Checks
Description
When trying to connect to an MCP server using streamablehttp_client and ClientSession, the client prints Unexpected content type error if the server responds with anything other than the expected MCP JSON stream (e.g. error page, misconfigured endpoint, etc.).
This makes it difficult to handle cases where the server is not reachable or not serving MCP correctly, since no exception is raised.
Example Code
importasynciofrommcp.client.streamable_httpimportstreamablehttp_clientfrommcpimportClientSessionasyncdefmain():
asyncwithstreamablehttp_client(
url="http://localhost:3001/mcp",
) as (read, write, _):
asyncwithClientSession(read, write) assession:
try:
awaitsession.initialize()
exceptExceptionase:
print(f"Failed to initialize MCP session: {e}")
returnprint("MCP session initialized")
if__name__=="__main__":
asyncio.run(main())Output
Unexpected content type: text/html; charset=utf-8
Python & MCP Python SDK
Initial Checks
Description
When trying to connect to an MCP server using
streamablehttp_clientandClientSession, the client printsUnexpected content typeerror if the server responds with anything other than the expected MCP JSON stream (e.g. error page, misconfigured endpoint, etc.).This makes it difficult to handle cases where the server is not reachable or not serving MCP correctly, since no exception is raised.
Example Code
Output
Python & MCP Python SDK