1+ import React , { createContext , useCallback , useContext , useEffect , useRef , useState } from "react"
2+ import { useEvent } from "react-use"
3+ import { StateServiceClient , UiServiceClient , ModelsServiceClient } from "../services/grpc-client"
4+ import { EmptyRequest } from "@shared/proto/common"
5+ import { WebviewProviderType as WebviewProviderTypeEnum , WebviewProviderTypeRequest } from "@shared/proto/ui"
16import { DEFAULT_AUTO_APPROVAL_SETTINGS } from "@shared/AutoApprovalSettings"
27import { DEFAULT_BROWSER_SETTINGS } from "@shared/BrowserSettings"
38import { ChatSettings , DEFAULT_CHAT_SETTINGS } from "@shared/ChatSettings"
49import { DEFAULT_PLATFORM , ExtensionMessage , ExtensionState } from "@shared/ExtensionMessage"
510import { TelemetrySetting } from "@shared/TelemetrySetting"
611import { findLastIndex } from "@shared/array"
7- import { EmptyRequest } from "@shared/proto/common"
8- import React , { createContext , useCallback , useContext , useEffect , useRef , useState } from "react"
9- import { useEvent } from "react-use"
1012import {
1113 ApiConfiguration ,
1214 ModelInfo ,
@@ -16,7 +18,6 @@ import {
1618 requestyDefaultModelInfo ,
1719} from "../../../src/shared/api"
1820import { McpMarketplaceCatalog , McpServer , McpViewTab } from "../../../src/shared/mcp"
19- import { ModelsServiceClient , StateServiceClient } from "../services/grpc-client"
2021import { convertTextMateToHljs } from "../utils/textMateToHljs"
2122import { vscode } from "../utils/vscode"
2223
@@ -192,9 +193,6 @@ export const ExtensionStateContextProvider: React.FC<{
192193 switch ( message . type ) {
193194 case "action" : {
194195 switch ( message . action ! ) {
195- case "mcpButtonClicked" :
196- navigateToMcp ( message . tab )
197- break
198196 case "settingsButtonClicked" :
199197 navigateToSettings ( )
200198 break
@@ -271,10 +269,11 @@ export const ExtensionStateContextProvider: React.FC<{
271269
272270 useEvent ( "message" , handleMessage )
273271
274- // Reference to store the state subscription cancellation function
272+ // References to store subscription cancellation functions
275273 const stateSubscriptionRef = useRef < ( ( ) => void ) | null > ( null )
274+ const mcpButtonUnsubscribeRef = useRef < ( ( ) => void ) | null > ( null )
276275
277- // Subscribe to state updates using the new gRPC streaming API
276+ // Subscribe to state updates and UI events using the gRPC streaming API
278277 useEffect ( ( ) => {
279278 // Set up state subscription
280279 stateSubscriptionRef . current = StateServiceClient . subscribeToState ( EmptyRequest . create ( { } ) , {
@@ -346,15 +345,39 @@ export const ExtensionStateContextProvider: React.FC<{
346345 } ,
347346 } )
348347
348+ // Subscribe to MCP button clicked events with webview type
349+ mcpButtonUnsubscribeRef . current = UiServiceClient . subscribeToMcpButtonClicked (
350+ WebviewProviderTypeRequest . create ( {
351+ providerType :
352+ window . WEBVIEW_PROVIDER_TYPE === "sidebar" ? WebviewProviderTypeEnum . SIDEBAR : WebviewProviderTypeEnum . TAB ,
353+ } ) ,
354+ {
355+ onResponse : ( ) => {
356+ console . log ( "[DEBUG] Received mcpButtonClicked event from gRPC stream" )
357+ navigateToMcp ( )
358+ } ,
359+ onError : ( error ) => {
360+ console . error ( "Error in mcpButtonClicked subscription:" , error )
361+ } ,
362+ onComplete : ( ) => {
363+ console . log ( "mcpButtonClicked subscription completed" )
364+ } ,
365+ } ,
366+ )
367+
349368 // Still send the webviewDidLaunch message for other initialization
350369 vscode . postMessage ( { type : "webviewDidLaunch" } )
351370
352- // Clean up subscription when component unmounts
371+ // Clean up subscriptions when component unmounts
353372 return ( ) => {
354373 if ( stateSubscriptionRef . current ) {
355374 stateSubscriptionRef . current ( )
356375 stateSubscriptionRef . current = null
357376 }
377+ if ( mcpButtonUnsubscribeRef . current ) {
378+ mcpButtonUnsubscribeRef . current ( )
379+ mcpButtonUnsubscribeRef . current = null
380+ }
358381 }
359382 } , [ ] )
360383
0 commit comments