/**
* mcpService.js
*
* Handles the registration of Model Context Protocol (MCP) tools for the browser.
* This allows AI agents to interact with the Hub UI as a set of structured tools.
*/
class McpService {
constructor() {
this.isSupported = typeof window !== 'undefined' && !!window.navigator?.modelContext;
this.registeredTools = new Set();
if (!this.isSupported) {
console.warn('[MCP] WebMCP is not natively supported in this browser. Tools will not be exposed.');
}
}
/**
* Registers a tool with the browser's model context.
* @param {Object} tool - Tool definition following the MCP spec.
*/
registerTool(tool) {
if (!this.isSupported) return;
try {
window.navigator.modelContext.registerTool(tool);
this.registeredTools.add(tool.name);
console.log(`[MCP] Registered tool: ${tool.name}`);
} catch (error) {
console.error(`[MCP] Failed to register tool ${tool.name}:`, error);
}
}
/**
* Unregisters a tool.
* @param {string} toolName
*/
unregisterTool(toolName) {
if (!this.isSupported) return;
try {
// Note: Native API might use different unregistration logic depending on the draft
if (window.navigator.modelContext.unregisterTool) {
window.navigator.modelContext.unregisterTool(toolName);
}
this.registeredTools.delete(toolName);
console.log(`[MCP] Unregistered tool: ${toolName}`);
} catch (error) {
console.error(`[MCP] Failed to unregister tool ${toolName}:`, error);
}
}
/**
* Returns whether the protocol is active and supported.
*/
isActive() {
return this.isSupported;
}
}
const mcpService = new McpService();
export default mcpService;