MCP Transport Auto-Detection

How the MCP class picks stdio, SSE, Streamable HTTP, or WebSocket from your URL

The MCP class reads your connection string and picks the right transport automatically — you never set a transport flag.

python
from praisonaiagents import Agent, MCP

agent = Agent(
    name="Assistant",
    instructions="You help users with external tools.",
    tools=MCP("https://api.example.com/mcp"),  # transport auto-detected
)

agent.start("Use the available tools to help me.")

The user passes one URL; the agent connects over Streamable HTTP because the URL is http(s):// with a path.

Diagram

ws:// or wss://

http s :// ending in /sse

http s :// with path

anything else

🔗 Connection String

Match pattern?

🔌 WebSocket

📡 SSE Legacy

🌐 Streamable HTTP

📟 stdio subprocess

Quick Start

  1. Streamable HTTP (path required)
    python
    from praisonaiagents import Agent, MCP
    
    agent = Agent(
        tools=MCP("https://api.example.com/mcp")
    )
  2. stdio, SSE, or WebSocket
    python
    from praisonaiagents import Agent, MCP
    
    # stdio subprocess
    agent = Agent(tools=MCP("npx @modelcontextprotocol/server-memory"))
    
    # Legacy SSE (URL ends in /sse)
    agent = Agent(tools=MCP("http://localhost:8080/sse"))
    
    # WebSocket
    agent = Agent(tools=MCP("wss://api.example.com/mcp"))

Detection Rules

The MCP class routes URL classification through a single helper, get_transport_type, so every transport follows the same rules.

URL PatternTransportExample
ws:// or wss://WebSocketws://localhost:8080/mcp
http(s)://... ending in /sseSSE (Legacy)http://localhost:8080/sse
http(s)://<host><path> (path required)Streamable HTTPhttps://api.example.com/mcp
Command stringstdionpx @mcp/server-memory

How It Works

The helper checks the URL scheme and suffix in order, then defaults to stdio for anything that is not a URL.

Diagram
get_transport_typeMCP ClassUserget_transport_typeMCP ClassUserMCP(“https://api.example.com/mcp”)classify URL“http_stream”Streamable HTTP client ready
CheckResult
Matches wss?://websocket
Matches https?:// and ends in /ssesse
Matches https?:// with any other pathhttp_stream
Anything elsestdio

Common Patterns

Override auto-detection only when you must — pass a URL that already fits the transport you want.

python
from praisonaiagents import Agent, MCP

# Force legacy SSE by using a /sse URL
agent = Agent(tools=MCP("http://localhost:8080/sse"))

# Force Streamable HTTP by including a path
agent = Agent(tools=MCP("https://api.example.com/mcp"))

Best Practices

Always include a path for HTTP servers

A bare https://host POSTs to / and fails with Session terminated. Pass the full endpoint, commonly /mcp.

Use /sse only for legacy servers

URLs ending in /sse select the deprecated SSE transport. Prefer Streamable HTTP for new servers.

Use ws:// or wss:// for real-time

WebSocket is auto-detected from the scheme and suits long-lived, bidirectional connections.

Command strings default to stdio

Anything that is not a URL runs as a local subprocess over stdio.


Related