1
- "use server" ;
1
+ if ( ! EXPORT_MODE ) {
2
+ ( "use server" ) ;
3
+ }
2
4
import {
3
5
createClient ,
4
6
executeRequest ,
@@ -14,12 +16,15 @@ 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 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
+ } ) ( ) ;
23
28
24
29
const clientsMap = new Map < string , McpClientData > ( ) ;
25
30
@@ -339,7 +344,14 @@ export async function executeMcpAction(
339
344
request : McpRequestMessage ,
340
345
) {
341
346
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
+ }
343
355
if ( ! client ?. client ) {
344
356
throw new Error ( `Client ${ clientId } not found` ) ;
345
357
}
@@ -354,8 +366,35 @@ export async function executeMcpAction(
354
366
// 获取 MCP 配置文件
355
367
export async function getMcpConfigFromFile ( ) : Promise < McpConfigData > {
356
368
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
+ }
359
398
} catch ( error ) {
360
399
logger . error ( `Failed to load MCP config, using default config: ${ error } ` ) ;
361
400
return DEFAULT_MCP_CONFIG ;
@@ -366,8 +405,15 @@ export async function getMcpConfigFromFile(): Promise<McpConfigData> {
366
405
async function updateMcpConfig ( config : McpConfigData ) : Promise < void > {
367
406
try {
368
407
// 确保目录存在
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
+ }
371
417
} catch ( error ) {
372
418
throw error ;
373
419
}
@@ -376,8 +422,19 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
376
422
// 检查 MCP 是否启用
377
423
export async function isMcpEnabled ( ) {
378
424
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
+ }
381
438
} catch ( error ) {
382
439
logger . error ( `Failed to check MCP status: ${ error } ` ) ;
383
440
return false ;
0 commit comments