const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_client_client = require('./client.cjs');
let node_url = require("node:url");
let __modelcontextprotocol_sdk_client_sse_js = require("@modelcontextprotocol/sdk/client/sse.js");

//#region src/client/sse.ts
var SSEClient = class {
	client;
	transport = null;
	isCompleted = false;
	constructor() {
		this.client = require_client_client.loadClient();
	}
	async connectToServer() {
		const mcpServerURL = process.env.MCP_SERVER_URL ?? "https://mcp.intlayer.org";
		const url = new node_url.URL(mcpServerURL);
		try {
			console.info(`Connecting to server - ${mcpServerURL}`);
			this.transport = new __modelcontextprotocol_sdk_client_sse_js.SSEClientTransport(url);
			await this.client.connect(this.transport);
			console.info("Connected to MCP server");
			this.setUpTransport();
		} catch (e) {
			console.info("Failed to connect to MCP server: ", e);
			throw e;
		}
	}
	setUpTransport() {
		if (this.transport === null) return;
		this.transport.onclose = () => {
			console.info("SSE transport closed.");
			this.isCompleted = true;
		};
		this.transport.onerror = async (error) => {
			console.info("SSE transport error: ", error);
			await this.cleanup();
		};
		this.transport.onmessage = (message) => {
			console.info("message received: ", message);
		};
	}
	async waitForCompletion() {
		while (!this.isCompleted) await new Promise((resolve) => setTimeout(resolve, 100));
	}
	async cleanup() {
		await this.client.close();
	}
};
const main = async () => {
	const client = new SSEClient();
	try {
		await client.connectToServer();
		await client.waitForCompletion();
	} finally {
		await client.cleanup();
	}
};
main();

//#endregion
//# sourceMappingURL=sse.cjs.map