Newer
Older
cortex-hub / frontend / src / shared / components / WebMcpProvider.js
import React, { createContext, useContext, useEffect, useState } from 'react';
import mcpService from '../../../services/mcpService';

const WebMcpContext = createContext(null);

export const WebMcpProvider = ({ children }) => {
    const [isMcpActive, setIsMcpActive] = useState(false);

    useEffect(() => {
        // Check if WebMCP is supported/active
        if (mcpService.isActive()) {
            setIsMcpActive(true);
            console.log('[MCP] WebMCP Provider initialized and active.');
        }
    }, []);

    const value = {
        isMcpActive,
        registerTool: (tool) => mcpService.registerTool(tool),
        unregisterTool: (toolName) => mcpService.unregisterTool(toolName)
    };

    return (
        <WebMcpContext.Provider value={value}>
            {children}
        </WebMcpContext.Provider>
    );
};

export const useWebMcp = () => {
    const context = useContext(WebMcpContext);
    if (!context) {
        throw new Error('useWebMcp must be used within a WebMcpProvider');
    }
    return context;
};