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,18 @@ 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 (
351
+ ! client &&
352
+ request . params ?. name &&
353
+ typeof request . params . name === "string"
354
+ ) {
355
+ // Use a tool-to-client mapping that's maintained when tools are initialized
356
+ const toolName = request . params . name ;
357
+ client = [ ...clientsMap . values ( ) ] . find (
358
+ ( c ) => c . tools ?. tools && c . tools . tools . some ( ( t ) => t . name == toolName ) ,
359
+ ) ;
360
+ }
343
361
if ( ! client ?. client ) {
344
362
throw new Error ( `Client ${ clientId } not found` ) ;
345
363
}
@@ -354,8 +372,30 @@ export async function executeMcpAction(
354
372
// 获取 MCP 配置文件
355
373
export async function getMcpConfigFromFile ( ) : Promise < McpConfigData > {
356
374
try {
357
- const configStr = await fs . readFile ( CONFIG_PATH , "utf-8" ) ;
358
- return JSON . parse ( configStr ) ;
375
+ if ( EXPORT_MODE ) {
376
+ const res = await fetch ( await getConfigPath ( ) ) ;
377
+ const config : McpConfigData = await res . json ( ) ;
378
+ const storage = localStorage ;
379
+ const storedConfig_str = storage . getItem ( "McpConfig" ) ;
380
+ if ( storedConfig_str ) {
381
+ const storedConfig : McpConfigData = JSON . parse ( storedConfig_str ) ;
382
+ // Create a merged configuration that combines both sources
383
+ const merged = { ...config . mcpServers } ;
384
+ if ( storedConfig . mcpServers ) {
385
+ // Ensure we process all servers from stored config
386
+ for ( const id in storedConfig . mcpServers ) {
387
+ merged [ id ] = { ...merged [ id ] , ...storedConfig . mcpServers [ id ] } ;
388
+ }
389
+ }
390
+
391
+ config . mcpServers = merged ;
392
+ }
393
+ return config ;
394
+ } else {
395
+ const fs = await import ( "fs/promises" ) ;
396
+ const configStr = await fs . readFile ( await getConfigPath ( ) , "utf-8" ) ;
397
+ return JSON . parse ( configStr ) ;
398
+ }
359
399
} catch ( error ) {
360
400
logger . error ( `Failed to load MCP config, using default config: ${ error } ` ) ;
361
401
return DEFAULT_MCP_CONFIG ;
@@ -366,8 +406,25 @@ export async function getMcpConfigFromFile(): Promise<McpConfigData> {
366
406
async function updateMcpConfig ( config : McpConfigData ) : Promise < void > {
367
407
try {
368
408
// 确保目录存在
369
- await fs . mkdir ( path . dirname ( CONFIG_PATH ) , { recursive : true } ) ;
370
- await fs . writeFile ( CONFIG_PATH , JSON . stringify ( config , null , 2 ) ) ;
409
+ if ( EXPORT_MODE ) {
410
+ try {
411
+ const storage = localStorage ;
412
+ storage . setItem ( "McpConfig" , JSON . stringify ( config ) ) ;
413
+ } catch ( storageError ) {
414
+ logger . warn (
415
+ `Failed to save MCP config to localStorage: ${ storageError } ` ,
416
+ ) ;
417
+ // Continue execution without storage
418
+ }
419
+ } else {
420
+ const fs = await import ( "fs/promises" ) ;
421
+ const path = await import ( "path" ) ;
422
+ await fs . mkdir ( path . dirname ( await getConfigPath ( ) ) , { recursive : true } ) ;
423
+ await fs . writeFile (
424
+ await getConfigPath ( ) ,
425
+ JSON . stringify ( config , null , 2 ) ,
426
+ ) ;
427
+ }
371
428
} catch ( error ) {
372
429
throw error ;
373
430
}
@@ -376,8 +433,19 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
376
433
// 检查 MCP 是否启用
377
434
export async function isMcpEnabled ( ) {
378
435
try {
379
- const serverConfig = getServerSideConfig ( ) ;
380
- return serverConfig . enableMcp ;
436
+ const config = await getMcpConfigFromFile ( ) ;
437
+ if ( typeof config . enableMcp === "boolean" ) {
438
+ return config . enableMcp ;
439
+ }
440
+ if ( EXPORT_MODE ) {
441
+ const { getClientConfig } = await import ( "../config/client" ) ;
442
+ const clientConfig = getClientConfig ( ) ;
443
+ return clientConfig ?. enableMcp === true ;
444
+ } else {
445
+ const { getServerSideConfig } = await import ( "../config/server" ) ;
446
+ const serverConfig = getServerSideConfig ( ) ;
447
+ return serverConfig . enableMcp ;
448
+ }
381
449
} catch ( error ) {
382
450
logger . error ( `Failed to check MCP status: ${ error } ` ) ;
383
451
return false ;
0 commit comments