Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor: refactor with electron

Innei849ee3daa3c4
diff --git a/src/services/mcp.test.ts b/src/services/mcp.test.ts
index a42ca353163..54b39f07c4c 100644
--- a/src/services/mcp.test.ts
+++ b/src/services/mcp.test.ts
@@ -1,10 +1,20 @@
import { ChatToolPayload } from '@lobechat/types';
import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk';
import { act } from '@testing-library/react';
+import superjson from 'superjson';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mcpService } from './mcp';
+const mockElectronIpc = {
+ mcp: {
+ callTool: vi.fn(),
+ getStreamableMcpServerManifest: vi.fn(),
+ getStdioMcpServerManifest: vi.fn(),
+ validMcpServerInstallable: vi.fn(),
+ },
+};
+
// Mock dependencies
vi.mock('@lobechat/const', () => ({
CURRENT_VERSION: '1.0.0',
@@ -25,22 +35,6 @@ vi.mock('@lobechat/utils', () => ({
}));
vi.mock('@/libs/trpc/client', () => ({
- desktopClient: {
- mcp: {
- callTool: {
- mutate: vi.fn(),
- },
- getStreamableMcpServerManifest: {
- query: vi.fn(),
- },
- getStdioMcpServerManifest: {
- query: vi.fn(),
- },
- validMcpServerInstallable: {
- mutate: vi.fn(),
- },
- },
- },
toolsClient: {
mcp: {
callTool: {
@@ -53,6 +47,10 @@ vi.mock('@/libs/trpc/client', () => ({
},
}));
+vi.mock('@/utils/electron/ipc', () => ({
+ ensureElectronIpc: () => mockElectronIpc,
+}));
+
vi.mock('./discover', () => ({
discoverService: {
reportPluginCall: vi.fn().mockResolvedValue(undefined),
@@ -83,7 +81,7 @@ describe('MCPService', () => {
describe('invokeMcpToolCall', () => {
it('should invoke tool call with installed plugin', async () => {
- const { desktopClient, toolsClient } = await import('@/libs/trpc/client');
+ const { toolsClient } = await import('@/libs/trpc/client');
const { discoverService } = await import('./discover');
const mockPlugin = {
@@ -567,8 +565,7 @@ describe('MCPService', () => {
});
describe('getStdioMcpServerManifest', () => {
- it('should call desktopClient with stdio parameters', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
+ it('should call ipc mcp.getStdioMcpServerManifest with stdio parameters', async () => {
const mockManifest: LobeChatPluginManifest = {
identifier: 'stdio-server',
version: '1',
@@ -581,7 +578,9 @@ describe('MCPService', () => {
},
],
};
- vi.mocked(desktopClient.mcp.getStdioMcpServerManifest.query).mockResolvedValue(mockManifest);
+ vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mockResolvedValue(
+ superjson.serialize(mockManifest) as any,
+ );
const stdioParams = {
command: 'node',
@@ -599,14 +598,11 @@ describe('MCPService', () => {
const result = await mcpService.getStdioMcpServerManifest(stdioParams, metadata);
expect(result).toEqual(mockManifest);
- expect(desktopClient.mcp.getStdioMcpServerManifest.query).toHaveBeenCalledWith(
- { ...stdioParams, metadata },
- { signal: undefined },
- );
+ const callArg = vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({ ...stdioParams, metadata });
});
it('should handle abort signal for stdio manifest', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
const mockManifest: LobeChatPluginManifest = {
identifier: 'python-server',
version: '1',
@@ -619,7 +615,9 @@ describe('MCPService', () => {
},
],
};
- vi.mocked(desktopClient.mcp.getStdioMcpServerManifest.query).mockResolvedValue(mockManifest);
+ vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mockResolvedValue(
+ superjson.serialize(mockManifest) as any,
+ );
const stdioParams = {
command: 'python',
@@ -630,14 +628,15 @@ describe('MCPService', () => {
const abortController = new AbortController();
await mcpService.getStdioMcpServerManifest(stdioParams, undefined, abortController.signal);
- expect(desktopClient.mcp.getStdioMcpServerManifest.query).toHaveBeenCalledWith(
- { ...stdioParams, metadata: undefined },
- { signal: abortController.signal },
- );
+ // IPC client does not support AbortSignal yet
+ const callArg = vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({
+ ...stdioParams,
+ metadata: undefined,
+ });
});
it('should work without optional parameters', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
const mockManifest: LobeChatPluginManifest = {
identifier: 'npm-server',
version: '1',
@@ -650,7 +649,9 @@ describe('MCPService', () => {
},
],
};
- vi.mocked(desktopClient.mcp.getStdioMcpServerManifest.query).mockResolvedValue(mockManifest);
+ vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mockResolvedValue(
+ superjson.serialize(mockManifest) as any,
+ );
const stdioParams = {
command: 'npm',
@@ -660,23 +661,23 @@ describe('MCPService', () => {
const result = await mcpService.getStdioMcpServerManifest(stdioParams);
expect(result).toEqual(mockManifest);
- expect(desktopClient.mcp.getStdioMcpServerManifest.query).toHaveBeenCalledWith(
- { ...stdioParams, metadata: undefined },
- { signal: undefined },
- );
+ const callArg = vi.mocked(mockElectronIpc.mcp.getStdioMcpServerManifest).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({
+ ...stdioParams,
+ metadata: undefined,
+ });
});
});
describe('checkInstallation', () => {
it('should check MCP plugin installation status', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
const mockInstallResult = {
platform: 'linux',
success: true,
packageInstalled: true,
};
- vi.mocked(desktopClient.mcp.validMcpServerInstallable.mutate).mockResolvedValue(
- mockInstallResult,
+ vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mockResolvedValue(
+ superjson.serialize(mockInstallResult) as any,
);
const manifest = {
@@ -695,14 +696,13 @@ describe('MCPService', () => {
const result = await mcpService.checkInstallation(manifest as any);
expect(result).toEqual(mockInstallResult);
- expect(desktopClient.mcp.validMcpServerInstallable.mutate).toHaveBeenCalledWith(
- { deploymentOptions: manifest.deploymentOptions },
- { signal: undefined },
- );
+ const callArg = vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({
+ deploymentOptions: manifest.deploymentOptions,
+ });
});
it('should handle installation check with abort signal', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
const mockInstallResult = {
platform: 'linux',
success: false,
@@ -712,8 +712,8 @@ describe('MCPService', () => {
{ name: 'npm', installed: false, meetRequirement: false },
],
};
- vi.mocked(desktopClient.mcp.validMcpServerInstallable.mutate).mockResolvedValue(
- mockInstallResult,
+ vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mockResolvedValue(
+ superjson.serialize(mockInstallResult) as any,
);
const manifest = {
@@ -732,22 +732,22 @@ describe('MCPService', () => {
const result = await mcpService.checkInstallation(manifest as any, abortController.signal);
expect(result).toEqual(mockInstallResult);
- expect(desktopClient.mcp.validMcpServerInstallable.mutate).toHaveBeenCalledWith(
- { deploymentOptions: manifest.deploymentOptions },
- { signal: abortController.signal },
- );
+ // IPC client does not support AbortSignal yet
+ const callArg = vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({
+ deploymentOptions: manifest.deploymentOptions,
+ });
});
it('should handle multiple deployment options', async () => {
- const { desktopClient } = await import('@/libs/trpc/client');
const mockInstallResult = {
platform: 'linux',
success: true,
packageInstalled: true,
isRecommended: true,
};
- vi.mocked(desktopClient.mcp.validMcpServerInstallable.mutate).mockResolvedValue(
- mockInstallResult,
+ vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mockResolvedValue(
+ superjson.serialize(mockInstallResult) as any,
);
const manifest = {
@@ -774,10 +774,10 @@ describe('MCPService', () => {
const result = await mcpService.checkInstallation(manifest as any);
expect(result).toEqual(mockInstallResult);
- expect(desktopClient.mcp.validMcpServerInstallable.mutate).toHaveBeenCalledWith(
- { deploymentOptions: manifest.deploymentOptions },
- expect.any(Object),
- );
+ const callArg = vi.mocked(mockElectronIpc.mcp.validMcpServerInstallable).mock.calls[0][0];
+ expect(superjson.deserialize(callArg as any)).toEqual({
+ deploymentOptions: manifest.deploymentOptions,
+ });
});
});
-});
\ No newline at end of file
+});
diff --git a/src/services/mcp.ts b/src/services/mcp.ts
index 187db3f5333..517974a579f 100644
--- a/src/services/mcp.ts
+++ b/src/services/mcp.ts
@@ -3,9 +3,11 @@ import { ChatToolPayload, CheckMcpInstallResult, CustomPluginMetadata } from '@l
import { isLocalOrPrivateUrl, safeParseJSON } from '@lobechat/utils';
import { PluginManifest } from '@lobehub/market-sdk';
import { CallReportRequest } from '@lobehub/market-types';
+import superjson from 'superjson';
import { MCPToolCallResult } from '@/libs/mcp';
-import { desktopClient, lambdaClient, toolsClient } from '@/libs/trpc/client';
+import { lambdaClient, toolsClient } from '@/libs/trpc/client';
+import { ensureElectronIpc } from '@/utils/electron/ipc';
import { discoverService } from './discover';
@@ -76,16 +78,18 @@ class MCPService {
};
}
+ const isStdio = plugin?.customParams?.mcp?.type === 'stdio';
+ const isCloud = plugin?.customParams?.mcp?.type === 'cloud';
+
const data = {
- args,
+ // For desktop IPC, always pass a record/object for tool "arguments"
+ // (IPC layer will superjson serialize the whole payload).
+ args: isDesktop && isStdio ? (safeParseJSON(args) ?? {}) : args,
env: connection?.type === 'stdio' ? params.env : (pluginSettings ?? connection?.env),
params,
toolName: apiName,
};
- const isStdio = plugin?.customParams?.mcp?.type === 'stdio';
- const isCloud = plugin?.customParams?.mcp?.type === 'cloud';
-
// Record call start time
const callStartTime = Date.now();
let success = false;
@@ -109,8 +113,11 @@ class MCPService {
toolName: apiName,
});
} else if (isDesktop && isStdio) {
- // For desktop and stdio, use the desktopClient
- result = await desktopClient.mcp.callTool.mutate(data, { signal });
+ // For desktop and stdio, use IPC (main process)
+ // Note: IPC doesn't support AbortSignal yet
+ const serialized = superjson.serialize(data);
+ const serializedResult = await ensureElectronIpc().mcp.callTool(serialized as any);
+ result = superjson.deserialize(serializedResult as any) as any;
} else {
// For other types, use the toolsClient
result = await toolsClient.mcp.callTool.mutate(data, { signal });
@@ -188,10 +195,15 @@ class MCPService {
},
signal?: AbortSignal,
) {
- // If in Desktop mode and URL is local address, use desktopClient
+ // If in Desktop mode and URL is local address, use IPC (main process)
// This avoids accessing user local services through remote server in production
if (isDesktop && isLocalOrPrivateUrl(params.url)) {
- return desktopClient.mcp.getStreamableMcpServerManifest.query(params, { signal });
+ // Note: IPC doesn't support AbortSignal yet
+ const serialized = superjson.serialize(params);
+ const serializedResult = await ensureElectronIpc().mcp.getStreamableMcpServerManifest(
+ serialized as any,
+ );
+ return superjson.deserialize(serializedResult as any) as any;
}
// Otherwise use toolsClient (via server relay)
@@ -206,12 +218,15 @@ class MCPService {
name: string;
},
metadata?: CustomPluginMetadata,
- signal?: AbortSignal,
+ _signal?: AbortSignal,
) {
- return desktopClient.mcp.getStdioMcpServerManifest.query(
- { ...stdioParams, metadata },
- { signal },
+ void _signal;
+ // Note: IPC doesn't support AbortSignal yet
+ const serialized = superjson.serialize({ ...stdioParams, metadata });
+ const serializedResult = await ensureElectronIpc().mcp.getStdioMcpServerManifest(
+ serialized as any,
);
+ return superjson.deserialize(serializedResult as any) as any;
}
/**
@@ -222,13 +237,18 @@ class MCPService {
*/
async checkInstallation(
manifest: PluginManifest,
- signal?: AbortSignal,
+ _signal?: AbortSignal,
): Promise<CheckMcpInstallResult> {
+ void _signal;
// Pass all deployment options to main process for checking
- return desktopClient.mcp.validMcpServerInstallable.mutate(
- { deploymentOptions: manifest.deploymentOptions as any },
- { signal },
+ // Note: IPC doesn't support AbortSignal yet
+ const serialized = superjson.serialize({
+ deploymentOptions: manifest.deploymentOptions as any,
+ });
+ const serializedResult = await ensureElectronIpc().mcp.validMcpServerInstallable(
+ serialized as any,
);
+ return superjson.deserialize(serializedResult as any) as any;
}
}