11import { defineStore } from 'pinia'
2+ import { watch } from 'vue'
23import type {
34 ChatCompletionRequestContent ,
45 ChatCompletionPromptMessage
@@ -18,6 +19,7 @@ type McpMethodType =
1819 | { type : 'call' ; fn : ( ) => any }
1920 | { type : 'templates/list' ; fn : ( ) => any }
2021 | string
22+ export type McpServerApi = MCPAPI | undefined
2123
2224export function getAllowedPrimitive ( item : McpObject ) : AllowedPrimitive [ ] {
2325 if ( ! item ) return [ ]
@@ -27,11 +29,11 @@ export function getAllowedPrimitive(item: McpObject): AllowedPrimitive[] {
2729 ) as AllowedPrimitive [ ]
2830}
2931
30- export function getRawServers ( ) : MCPAPI | undefined {
32+ export function getRawServers ( ) : McpServerApi {
3133 return window . mcpServers ?. get ( )
3234}
3335
34- export function getServers ( ) : MCPAPI | undefined {
36+ export function getServers ( ) : McpServerApi {
3537 const mcpServers = getRawServers ( )
3638 const stdioServers = useStdioStore ( ) . configValues
3739
@@ -65,12 +67,17 @@ export interface McpCoreType {
6567 method : McpMethodType
6668}
6769
70+ function getObjectKeys ( o : unknown ) {
71+ return o && typeof o === 'object' && ! Array . isArray ( o ) ? Object . keys ( o ) : [ ]
72+ }
73+
6874export const useMcpStore = defineStore ( 'mcpStore' , {
6975 // TODO: fix any to type
7076 state : ( ) : any => ( {
7177 version : 1 ,
7278 serverTools : [ ] ,
7379 loading : true ,
80+ checkList : getObjectKeys ( getServers ( ) ) as string [ ] ,
7481 selected : undefined as string [ ] | undefined ,
7582 selectedChips : { } // { key : 0 | 1 | 2}
7683 } ) ,
@@ -108,6 +115,16 @@ export const useMcpStore = defineStore('mcpStore', {
108115 } ,
109116
110117 actions : {
118+ watchServerUpdate ( ) {
119+ watch ( getServers , ( newVal : McpServerApi , oldVal : McpServerApi ) => {
120+ const newKeys = getObjectKeys ( newVal )
121+ const oldKeys = getObjectKeys ( oldVal )
122+ const retainedKeys = this . checkList . filter ( ( key : string ) => newKeys . includes ( key ) )
123+ const addedKeys = newKeys . filter ( ( key ) => ! oldKeys . includes ( key ) )
124+ this . checkList = [ ...retainedKeys , ...addedKeys ]
125+ } )
126+ } ,
127+
111128 getAllByServer : function ( serverName : string ) : McpCoreType [ ] {
112129 const mcpServers = getServers ( )
113130 if ( ! mcpServers ) {
0 commit comments