1
- "use server" ;
1
+ if ( ! EXPORT_MODE ) {
2
+ ( "use server" ) ;
3
+ }
2
4
import {
3
5
createClient ,
4
6
executeRequest ,
@@ -14,12 +16,17 @@ import {
14
16
ServerConfig ,
15
17
ServerStatusResponse ,
16
18
} from "./types" ;
17
- import fs from "fs/promises" ;
18
- import path from "path" ;
19
- import { getServerSideConfig } from "../config/server" ;
20
19
21
20
const logger = new MCPClientLogger ( "MCP Actions" ) ;
22
- const CONFIG_PATH = path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
21
+
22
+ const getConfigPath = async ( ) => {
23
+ if ( EXPORT_MODE ) {
24
+ return "/mcp/config.json" ;
25
+ } else {
26
+ const path = await import ( "path" ) ;
27
+ return path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
28
+ }
29
+ } ;
23
30
24
31
const clientsMap = new Map < string , McpClientData > ( ) ;
25
32
@@ -339,7 +346,14 @@ export async function executeMcpAction(
339
346
request : McpRequestMessage ,
340
347
) {
341
348
try {
342
- const client = clientsMap . get ( clientId ) ;
349
+ let client = clientsMap . get ( clientId ) ;
350
+ if ( ! client ) {
351
+ client = [ ...clientsMap . values ( ) ] . find (
352
+ ( c ) =>
353
+ c . tools ?. tools &&
354
+ c . tools . tools . find ( ( t ) => t . name == request . params ?. name ) ,
355
+ ) ;
356
+ }
343
357
if ( ! client ?. client ) {
344
358
throw new Error ( `Client ${ clientId } not found` ) ;
345
359
}
@@ -354,8 +368,28 @@ export async function executeMcpAction(
354
368
// 获取 MCP 配置文件
355
369
export async function getMcpConfigFromFile ( ) : Promise < McpConfigData > {
356
370
try {
357
- const configStr = await fs . readFile ( CONFIG_PATH , "utf-8" ) ;
358
- return JSON . parse ( configStr ) ;
371
+ if ( EXPORT_MODE ) {
372
+ const res = await fetch ( await getConfigPath ( ) ) ;
373
+ const config : McpConfigData = await res . json ( ) ;
374
+ const storage = localStorage ;
375
+ const storedConfig_str = storage . getItem ( "McpConfig" ) ;
376
+ if ( storedConfig_str ) {
377
+ const storedConfig : McpConfigData = JSON . parse ( storedConfig_str ) ;
378
+ const merged = { ...config . mcpServers } ;
379
+ if ( storedConfig . mcpServers ) {
380
+ for ( const id in storedConfig . mcpServers ) {
381
+ merged [ id ] = { ...merged [ id ] , ...storedConfig . mcpServers [ id ] } ;
382
+ }
383
+ }
384
+
385
+ config . mcpServers = merged ;
386
+ }
387
+ return config ;
388
+ } else {
389
+ const fs = await import ( "fs/promises" ) ;
390
+ const configStr = await fs . readFile ( await getConfigPath ( ) , "utf-8" ) ;
391
+ return JSON . parse ( configStr ) ;
392
+ }
359
393
} catch ( error ) {
360
394
logger . error ( `Failed to load MCP config, using default config: ${ error } ` ) ;
361
395
return DEFAULT_MCP_CONFIG ;
@@ -366,8 +400,25 @@ export async function getMcpConfigFromFile(): Promise<McpConfigData> {
366
400
async function updateMcpConfig ( config : McpConfigData ) : Promise < void > {
367
401
try {
368
402
// 确保目录存在
369
- await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
370
- await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
403
+ if ( EXPORT_MODE ) {
404
+ try {
405
+ const storage = localStorage ;
406
+ storage . setItem ( "McpConfig" , JSON . stringify ( config ) ) ;
407
+ } catch ( storageError ) {
408
+ logger . warn (
409
+ `Failed to save MCP config to localStorage: ${ storageError } ` ,
410
+ ) ;
411
+ // Continue execution without storage
412
+ }
413
+ } else {
414
+ const fs = await import ( "fs/promises" ) ;
415
+ const path = await import ( "path" ) ;
416
+ await fs . mkdir ( path . dirname ( await getConfigPath ( ) ) , { recursive : true } ) ;
417
+ await fs . writeFile (
418
+ await getConfigPath ( ) ,
419
+ JSON . stringify ( config , null , 2 ) ,
420
+ ) ;
421
+ }
371
422
} catch ( error ) {
372
423
throw error ;
373
424
}
@@ -376,8 +427,19 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
376
427
// 检查 MCP 是否启用
377
428
export async function isMcpEnabled ( ) {
378
429
try {
379
- const serverConfig = getServerSideConfig ( ) ;
380
- return serverConfig . enableMcp ;
430
+ const config = await getMcpConfigFromFile ( ) ;
431
+ if ( typeof config . enableMcp === "boolean" ) {
432
+ return config . enableMcp ;
433
+ }
434
+ if ( EXPORT_MODE ) {
435
+ const { getClientConfig } = await import ( "../config/client" ) ;
436
+ const clientConfig = getClientConfig ( ) ;
437
+ return clientConfig ?. enableMcp === true ;
438
+ } else {
439
+ const { getServerSideConfig } = await import ( "../config/server" ) ;
440
+ const serverConfig = getServerSideConfig ( ) ;
441
+ return serverConfig . enableMcp ;
442
+ }
381
443
} catch ( error ) {
382
444
logger . error ( `Failed to check MCP status: ${ error } ` ) ;
383
445
return false ;
0 commit comments