Not all MCP servers run locally. For cloud-hosted servers, you use the SSE (Server-Sent Events) Client Transport.
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
const transport = new SSEClientTransport(
new URL("https://mcp.example.com/sse")
);
const client = new Client({ name: "my-app", version: "1.0.0" });
await client.connect(transport);
// Now use the client exactly like stdio — the API is identical
const { tools } = await client.listTools();
const result = await client.callTool("search_knowledge_base", {
query: "refund policy"
});
// For OAuth-secured remote servers:
const transport = new SSEClientTransport(
new URL("https://mcp.example.com/sse"),
{
requestInit: {
headers: {
"Authorization": "Bearer eyJhbG..."
}
}
}
);
| Transport | Setup | Security | Latency | Best For |
|---|---|---|---|---|
| Stdio | Launch child process | OS-level (local only) | ~1ms | Local tools, dev environments |
| SSE | HTTP URL + auth | OAuth 2.1 / Bearer | ~50-200ms | Cloud servers, shared services |
// Handle connection errors gracefully
client.onclose = () => {
console.error("Connection lost. Attempting reconnect...");
setTimeout(async () => {
try {
await client.connect(transport);
console.log("Reconnected successfully");
} catch (e) {
console.error("Reconnection failed:", e);
}
}, 5000);
};
// Handle transport errors
transport.onerror = (error) => {
console.error("Transport error:", error);
};