1
- "use server" ;
1
+ if ( EXPORT_MODE ) {
2
+ ( "use client" ) ;
3
+ } else {
4
+ ( "use server" ) ;
5
+ }
2
6
import {
3
7
createClient ,
4
8
executeRequest ,
@@ -14,12 +18,15 @@ import {
14
18
ServerConfig ,
15
19
ServerStatusResponse ,
16
20
} from "./types" ;
17
- import fs from "fs/promises" ;
18
- import path from "path" ;
19
- import { getServerSideConfig } from "../config/server" ;
20
21
21
22
const logger = new MCPClientLogger ( "MCP Actions" ) ;
22
- const CONFIG_PATH = path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
23
+
24
+ const CONFIG_PATH = EXPORT_MODE
25
+ ? "/mcp/config.json"
26
+ : await ( async ( ) => {
27
+ const path = await import ( "path" ) ;
28
+ return path . join ( process . cwd ( ) , "app/mcp/mcp_config.json" ) ;
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,35 @@ 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 ( CONFIG_PATH ) ;
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 mcpServers = config . mcpServers ;
379
+ if ( storedConfig . mcpServers ) {
380
+ for ( const mcpId in config . mcpServers ) {
381
+ if ( mcpId in mcpServers ) {
382
+ mcpServers [ mcpId ] = {
383
+ ...mcpServers [ mcpId ] ,
384
+ ...storedConfig . mcpServers [ mcpId ] ,
385
+ } ;
386
+ } else {
387
+ mcpServers [ mcpId ] = storedConfig . mcpServers [ mcpId ] ;
388
+ }
389
+ }
390
+ }
391
+
392
+ config . mcpServers = mcpServers ;
393
+ }
394
+ return config ;
395
+ } else {
396
+ const fs = await import ( "fs/promises" ) ;
397
+ const configStr = await fs . readFile ( CONFIG_PATH , "utf-8" ) ;
398
+ return JSON . parse ( configStr ) ;
399
+ }
359
400
} catch ( error ) {
360
401
logger . error ( `Failed to load MCP config, using default config: ${ error } ` ) ;
361
402
return DEFAULT_MCP_CONFIG ;
@@ -366,8 +407,15 @@ export async function getMcpConfigFromFile(): Promise<McpConfigData> {
366
407
async function updateMcpConfig ( config : McpConfigData ) : Promise < void > {
367
408
try {
368
409
// 确保目录存在
369
- await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
370
- await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
410
+ if ( EXPORT_MODE ) {
411
+ const storage = localStorage ;
412
+ storage . setItem ( "McpConfig" , JSON . stringify ( config ) ) ;
413
+ } else {
414
+ const fs = await import ( "fs/promises" ) ;
415
+ const path = await import ( "path" ) ;
416
+ await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
417
+ await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
418
+ }
371
419
} catch ( error ) {
372
420
throw error ;
373
421
}
@@ -376,8 +424,19 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
376
424
// 检查 MCP 是否启用
377
425
export async function isMcpEnabled ( ) {
378
426
try {
379
- const serverConfig = getServerSideConfig ( ) ;
380
- return serverConfig . enableMcp ;
427
+ const config = await getMcpConfigFromFile ( ) ;
428
+ if ( typeof config . enableMcp === "boolean" ) {
429
+ return config . enableMcp ;
430
+ }
431
+ if ( EXPORT_MODE ) {
432
+ const { getClientConfig } = await import ( "../config/client" ) ;
433
+ const clientConfig = getClientConfig ( ) ;
434
+ return clientConfig ?. enableMcp === true ;
435
+ } else {
436
+ const { getServerSideConfig } = await import ( "../config/server" ) ;
437
+ const serverConfig = getServerSideConfig ( ) ;
438
+ return serverConfig . enableMcp ;
439
+ }
381
440
} catch ( error ) {
382
441
logger . error ( `Failed to check MCP status: ${ error } ` ) ;
383
442
return false ;
0 commit comments