mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { MCPClient } from '@/lib/MCPClient';
|
|
import type { MCPServer } from '@/contexts/AISettingsContext';
|
|
|
|
/**
|
|
* Hook to check the connection status of an MCP server.
|
|
* Returns 'connected', 'offline', or 'checking'.
|
|
*/
|
|
export function useMCPServerStatus(server: MCPServer): 'connected' | 'offline' | 'checking' {
|
|
const [status, setStatus] = useState<'connected' | 'offline' | 'checking'>('checking');
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
async function checkConnection() {
|
|
setStatus('checking');
|
|
|
|
try {
|
|
const client = new MCPClient(server);
|
|
await client.connect();
|
|
|
|
// Try to list tools to verify the connection is working
|
|
await client.listTools();
|
|
|
|
await client.close();
|
|
|
|
if (!cancelled) {
|
|
setStatus('connected');
|
|
}
|
|
} catch {
|
|
if (!cancelled) {
|
|
setStatus('offline');
|
|
}
|
|
}
|
|
}
|
|
|
|
checkConnection();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [server]); // Re-check when server config changes
|
|
|
|
return status;
|
|
}
|