1- "use server" ;
1+ if ( ! EXPORT_MODE ) {
2+ ( "use server" ) ;
3+ }
24import {
35 createClient ,
46 executeRequest ,
@@ -14,12 +16,15 @@ import {
1416 ServerConfig ,
1517 ServerStatusResponse ,
1618} from "./types" ;
17- import fs from "fs/promises" ;
18- import path from "path" ;
19- import { getServerSideConfig } from "../config/server" ;
2019
2120const logger = new MCPClientLogger ( "MCP Actions" ) ;
22- const CONFIG_PATH = path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
21+
22+ const CONFIG_PATH = EXPORT_MODE
23+ ? "/mcp/config.json"
24+ : await ( async ( ) => {
25+ const path = await import ( "path" ) ;
26+ return path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
27+ } ) ( ) ;
2328
2429const clientsMap = new Map < string , McpClientData > ( ) ;
2530
@@ -339,7 +344,14 @@ export async function executeMcpAction(
339344 request : McpRequestMessage ,
340345) {
341346 try {
342- const client = clientsMap . get ( clientId ) ;
347+ let client = clientsMap . get ( clientId ) ;
348+ if ( ! client ) {
349+ client = [ ...clientsMap . values ( ) ] . find (
350+ ( c ) =>
351+ c . tools ?. tools &&
352+ c . tools . tools . find ( ( t ) => t . name == request . params ?. name ) ,
353+ ) ;
354+ }
343355 if ( ! client ?. client ) {
344356 throw new Error ( `Client ${ clientId } not found` ) ;
345357 }
@@ -354,8 +366,35 @@ export async function executeMcpAction(
354366// 获取 MCP 配置文件
355367export async function getMcpConfigFromFile ( ) : Promise < McpConfigData > {
356368 try {
357- const configStr = await fs . readFile ( CONFIG_PATH , "utf-8" ) ;
358- return JSON . parse ( configStr ) ;
369+ if ( EXPORT_MODE ) {
370+ const res = await fetch ( CONFIG_PATH ) ;
371+ const config : McpConfigData = await res . json ( ) ;
372+ const storage = localStorage ;
373+ const storedConfig_str = storage . getItem ( "McpConfig" ) ;
374+ if ( storedConfig_str ) {
375+ const storedConfig : McpConfigData = JSON . parse ( storedConfig_str ) ;
376+ const mcpServers = config . mcpServers ;
377+ if ( storedConfig . mcpServers ) {
378+ for ( const mcpId in config . mcpServers ) {
379+ if ( mcpId in mcpServers ) {
380+ mcpServers [ mcpId ] = {
381+ ...mcpServers [ mcpId ] ,
382+ ...storedConfig . mcpServers [ mcpId ] ,
383+ } ;
384+ } else {
385+ mcpServers [ mcpId ] = storedConfig . mcpServers [ mcpId ] ;
386+ }
387+ }
388+ }
389+
390+ config . mcpServers = mcpServers ;
391+ }
392+ return config ;
393+ } else {
394+ const fs = await import ( "fs/promises" ) ;
395+ const configStr = await fs . readFile ( CONFIG_PATH , "utf-8" ) ;
396+ return JSON . parse ( configStr ) ;
397+ }
359398 } catch ( error ) {
360399 logger . error ( `Failed to load MCP config, using default config: ${ error } ` ) ;
361400 return DEFAULT_MCP_CONFIG ;
@@ -366,8 +405,15 @@ export async function getMcpConfigFromFile(): Promise<McpConfigData> {
366405async function updateMcpConfig ( config : McpConfigData ) : Promise < void > {
367406 try {
368407 // 确保目录存在
369- await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
370- await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
408+ if ( EXPORT_MODE ) {
409+ const storage = localStorage ;
410+ storage . setItem ( "McpConfig" , JSON . stringify ( config ) ) ;
411+ } else {
412+ const fs = await import ( "fs/promises" ) ;
413+ const path = await import ( "path" ) ;
414+ await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
415+ await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
416+ }
371417 } catch ( error ) {
372418 throw error ;
373419 }
@@ -376,8 +422,19 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
376422// 检查 MCP 是否启用
377423export async function isMcpEnabled ( ) {
378424 try {
379- const serverConfig = getServerSideConfig ( ) ;
380- return serverConfig . enableMcp ;
425+ const config = await getMcpConfigFromFile ( ) ;
426+ if ( typeof config . enableMcp === "boolean" ) {
427+ return config . enableMcp ;
428+ }
429+ if ( EXPORT_MODE ) {
430+ const { getClientConfig } = await import ( "../config/client" ) ;
431+ const clientConfig = getClientConfig ( ) ;
432+ return clientConfig ?. enableMcp === true ;
433+ } else {
434+ const { getServerSideConfig } = await import ( "../config/server" ) ;
435+ const serverConfig = getServerSideConfig ( ) ;
436+ return serverConfig . enableMcp ;
437+ }
381438 } catch ( error ) {
382439 logger . error ( `Failed to check MCP status: ${ error } ` ) ;
383440 return false ;
0 commit comments