From ed6dd06a04326b5c283ddeb968a2acf7b0ccb9e1 Mon Sep 17 00:00:00 2001 From: ngoiyaeric <1.15367894e+08+ngoiyaeric@users.noreply.github.com> Date: Tue, 1 Sep 2026 06:54:32 +0000 Subject: [PATCH] fix: initialize SkyFi MCP before connection status check --- lib/actions/skyfi.ts | 53 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/actions/skyfi.ts b/lib/actions/skyfi.ts index 1f72406c..41bc0dc9 100644 --- a/lib/actions/skyfi.ts +++ b/lib/actions/skyfi.ts @@ -9,6 +9,8 @@ import { SkyfiOAuthProvider } from '@/lib/skyfi/provider'; import crypto from 'crypto'; import { headers } from 'next/headers'; +import { Client as MCPClient } from '@modelcontextprotocol/sdk/client/index.js'; +import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; export async function getRedirectUri(): Promise { try { @@ -143,38 +145,39 @@ export async function getSkyfiConnectionStatus(): Promise<{ connected: boolean; const timeoutId = setTimeout(() => controller.abort(), 10000); try { - // Try a simple whoami call to verify token validity and get email/budget - const res = await fetch('https://mcp.skyfi.com/mcp', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${tokens.access_token}`, - }, - body: JSON.stringify({ - jsonrpc: '2.0', - id: 1, - method: 'tools/call', - params: { - name: 'skyfi_whoami', - arguments: {}, + // Use the MCP SDK rather than calling tools/call directly. Streamable HTTP + // servers require an initialize handshake (and may issue a session ID) + // before tool calls are accepted. + const transport = new StreamableHTTPClientTransport( + new URL('https://mcp.skyfi.com/mcp'), + { + requestInit: { + headers: { Authorization: `Bearer ${tokens.access_token}` }, + signal: controller.signal, }, - }), - signal: controller.signal, - }); - - clearTimeout(timeoutId); - - if (res.ok) { - const data = await res.json(); - const content = data?.result?.content?.[0]?.text || ''; + }, + ); + const client = new MCPClient({ name: 'QCXSkyFiStatus', version: '1.0.0' }); + + try { + await client.connect(transport); + const result = await client.callTool( + { name: 'skyfi_whoami', arguments: {} }, + undefined, + { signal: controller.signal }, + ); + const content = (result as any)?.content?.[0]?.text || ''; + clearTimeout(timeoutId); return { connected: true, budget: content }; + } finally { + await client.close().catch(() => undefined); } } catch (fetchError) { clearTimeout(timeoutId); - console.warn('[SkyFiAction: getSkyfiConnectionStatus] Failed to query whoami:', fetchError); + console.warn('[SkyfiAction: getSkyfiConnectionStatus] Failed to query whoami:', fetchError); } - return { connected: true }; + return { connected: false }; } catch (error: any) { console.error('[SkyFiAction: getSkyfiConnectionStatus] Error:', error.message); return { connected: false };