fix: Only register MCP tools when connected via torch
- Tools only register when connected to MCP (has torch) - Store current page for tool activation when torch is obtained - Add onTorchConnected to activate page tools after MCP connection - Add onTorchDisconnected to clear tools when losing torch - Page changes only update tools if connected, otherwise store for later
This commit is contained in:
@@ -8,9 +8,11 @@
|
||||
import {
|
||||
initWebMCP,
|
||||
getRegisteredTools as getWebMCPTools,
|
||||
clearAllTools as clearWebMCPTools
|
||||
clearAllTools as clearWebMCPTools,
|
||||
getWebMCP
|
||||
} from './webmcp'
|
||||
import { useToolsStore } from '../stores/tools'
|
||||
import { useCanvasStore } from '../stores/canvas'
|
||||
import {
|
||||
createGlobalHandlers,
|
||||
createCanvasHandlers,
|
||||
@@ -158,6 +160,14 @@ const pageCategories: Record<PageName, ToolCategory[]> = {
|
||||
let currentPage: PageName | null = null
|
||||
let isInitialized = false
|
||||
|
||||
/**
|
||||
* Check if connected to MCP
|
||||
*/
|
||||
function isConnectedToMCP(): boolean {
|
||||
const canvasStore = useCanvasStore()
|
||||
return canvasStore.isConnected
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the tool registry with Vue router
|
||||
*/
|
||||
@@ -182,14 +192,22 @@ export function clearSourceCodeCredentials() {
|
||||
|
||||
/**
|
||||
* Activate tools for a specific page
|
||||
* Only works when connected to MCP
|
||||
*/
|
||||
export async function activatePageTools(pageName: PageName) {
|
||||
export async function activatePageTools(pageName: PageName, force: boolean = false) {
|
||||
if (!isInitialized) {
|
||||
console.warn('[ToolRegistry] Not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
if (currentPage === pageName) {
|
||||
// Only register tools if connected to MCP
|
||||
if (!isConnectedToMCP()) {
|
||||
console.log(`[ToolRegistry] Not connected to MCP, storing page "${pageName}" for later`)
|
||||
currentPage = pageName
|
||||
return
|
||||
}
|
||||
|
||||
if (!force && currentPage === pageName) {
|
||||
console.log(`[ToolRegistry] Already on "${pageName}", skipping`)
|
||||
return
|
||||
}
|
||||
@@ -238,6 +256,7 @@ export async function activatePageTools(pageName: PageName) {
|
||||
|
||||
/**
|
||||
* Initialize tools on page refresh
|
||||
* Just stores the current page, actual registration happens when connected
|
||||
*/
|
||||
export async function initToolsOnRefresh(pageName: PageName) {
|
||||
if (!isInitialized) {
|
||||
@@ -245,11 +264,44 @@ export async function initToolsOnRefresh(pageName: PageName) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`[ToolRegistry] Initializing on refresh for "${pageName}"`)
|
||||
console.log(`[ToolRegistry] Storing current page "${pageName}" for when connected`)
|
||||
currentPage = pageName
|
||||
|
||||
// Only activate if already connected
|
||||
if (isConnectedToMCP()) {
|
||||
internalClearAllTools()
|
||||
currentPage = null
|
||||
await activatePageTools(pageName)
|
||||
await activatePageTools(pageName, true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when torch is obtained and MCP is connected
|
||||
* Activates tools for the current page
|
||||
*/
|
||||
export async function onTorchConnected() {
|
||||
if (!isInitialized) {
|
||||
console.warn('[ToolRegistry] Not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
if (!currentPage) {
|
||||
console.warn('[ToolRegistry] No current page set')
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`[ToolRegistry] Torch connected, activating tools for "${currentPage}"`)
|
||||
internalClearAllTools()
|
||||
await activatePageTools(currentPage, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when torch is lost and MCP is disconnected
|
||||
* Clears all tools
|
||||
*/
|
||||
export function onTorchDisconnected() {
|
||||
console.log('[ToolRegistry] Torch disconnected, clearing tools')
|
||||
internalClearAllTools()
|
||||
syncStoreWithActiveTools()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useTorchStore } from '../stores/torch'
|
||||
import { autoConnect, disconnectWebMCP } from './webmcp'
|
||||
import { onTorchConnected, onTorchDisconnected } from './toolRegistry'
|
||||
import { endpoints } from '../config/endpoints'
|
||||
|
||||
let torchWs: WebSocket | null = null
|
||||
@@ -150,7 +151,8 @@ async function connectToMCP(): Promise<void> {
|
||||
console.log('[Torch] Connecting to MCP...')
|
||||
const success = await autoConnect()
|
||||
if (success) {
|
||||
console.log('[Torch] Connected to MCP')
|
||||
console.log('[Torch] Connected to MCP, activating tools')
|
||||
await onTorchConnected()
|
||||
} else {
|
||||
console.error('[Torch] Failed to connect to MCP')
|
||||
}
|
||||
@@ -161,6 +163,7 @@ async function connectToMCP(): Promise<void> {
|
||||
*/
|
||||
function disconnectFromMCP(): void {
|
||||
console.log('[Torch] Disconnecting from MCP...')
|
||||
onTorchDisconnected()
|
||||
disconnectWebMCP()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user