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.

πŸ› fix(agent-runtime): skip client-executor marking when gateway is configured (#13787)

Arvin Xue11c89fc48fb
diff --git a/src/server/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts b/src/server/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
index 312046b67b4..d1f429cb7ea 100644
--- a/src/server/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
+++ b/src/server/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
@@ -1,3 +1,4 @@
+import { LocalSystemManifest } from '@lobechat/builtin-tool-local-system';
import { RemoteDeviceManifest } from '@lobechat/builtin-tool-remote-device';
import type * as ModelBankModule from 'model-bank';
import { beforeEach, describe, expect, it, vi } from 'vitest';
@@ -11,6 +12,7 @@ const {
mockGetAgentConfig,
mockGetEnabledPluginManifests,
mockMessageCreate,
+ mockPluginQuery,
mockQueryDeviceList,
} = vi.hoisted(() => ({
mockCreateOperation: vi.fn(),
@@ -19,6 +21,7 @@ const {
mockGetAgentConfig: vi.fn(),
mockGetEnabledPluginManifests: vi.fn(),
mockMessageCreate: vi.fn(),
+ mockPluginQuery: vi.fn(),
mockQueryDeviceList: vi.fn(),
}));
@@ -51,7 +54,7 @@ vi.mock('@/server/services/agent', () => ({
vi.mock('@/database/models/plugin', () => ({
PluginModel: vi.fn().mockImplementation(() => ({
- query: vi.fn().mockResolvedValue([]),
+ query: mockPluginQuery,
})),
}));
@@ -160,6 +163,7 @@ describe('AiAgentService.execAgent - device tool pipeline (LOBE-5636)', () => {
success: true,
});
mockQueryDeviceList.mockResolvedValue([]);
+ mockPluginQuery.mockResolvedValue([]);
mockGenerateToolsDetailed.mockReturnValue({ enabledToolIds: [], tools: [] });
mockGetEnabledPluginManifests.mockReturnValue(new Map());
service = new AiAgentService(mockDb, userId);
@@ -278,6 +282,75 @@ describe('AiAgentService.execAgent - device tool pipeline (LOBE-5636)', () => {
});
});
+ describe('toolExecutorMap gating on gatewayConfigured (regression for #13769)', () => {
+ it('should mark local-system as client when gateway is NOT configured (standalone Electron)', async () => {
+ const { deviceProxy } = await import('@/server/services/toolExecution/deviceProxy');
+ vi.spyOn(deviceProxy, 'isConfigured', 'get').mockReturnValue(false);
+
+ mockGetEnabledPluginManifests.mockReturnValue(
+ new Map([[LocalSystemManifest.identifier, LocalSystemManifest]]),
+ );
+ mockGetAgentConfig.mockResolvedValue(createBaseAgentConfig());
+
+ await service.execAgent({ agentId: 'agent-1', prompt: 'Hello' });
+
+ const executorMap = mockCreateOperation.mock.calls[0][0].toolSet.executorMap;
+ expect(executorMap[LocalSystemManifest.identifier]).toBe('client');
+ });
+
+ it('should NOT mark local-system as client when gateway IS configured (cloud)', async () => {
+ const { deviceProxy } = await import('@/server/services/toolExecution/deviceProxy');
+ vi.spyOn(deviceProxy, 'isConfigured', 'get').mockReturnValue(true);
+ mockQueryDeviceList.mockResolvedValue([
+ { deviceId: 'dev-1', deviceName: 'My PC', platform: 'win32' },
+ ]);
+
+ mockGetEnabledPluginManifests.mockReturnValue(
+ new Map([[LocalSystemManifest.identifier, LocalSystemManifest]]),
+ );
+ mockGetAgentConfig.mockResolvedValue(createBaseAgentConfig());
+
+ await service.execAgent({ agentId: 'agent-1', prompt: 'Hello' });
+
+ const executorMap = mockCreateOperation.mock.calls[0][0].toolSet.executorMap;
+ expect(executorMap[LocalSystemManifest.identifier]).toBeUndefined();
+ });
+
+ it('should mark stdio MCP plugin as client only when gateway is NOT configured', async () => {
+ const stdioPlugin = {
+ customParams: { mcp: { type: 'stdio' } },
+ identifier: 'my-stdio-mcp',
+ } as any;
+ const stdioManifest = {
+ api: [{ description: 't', name: 'a', parameters: {} }],
+ identifier: 'my-stdio-mcp',
+ meta: { title: 'Stdio' },
+ };
+
+ mockPluginQuery.mockResolvedValue([stdioPlugin]);
+ mockGetEnabledPluginManifests.mockReturnValue(new Map([['my-stdio-mcp', stdioManifest]]));
+ mockGetAgentConfig.mockResolvedValue(createBaseAgentConfig({ plugins: ['my-stdio-mcp'] }));
+
+ const { deviceProxy } = await import('@/server/services/toolExecution/deviceProxy');
+
+ // Gateway NOT configured β†’ should mark as client
+ vi.spyOn(deviceProxy, 'isConfigured', 'get').mockReturnValue(false);
+ await service.execAgent({ agentId: 'agent-1', prompt: 'Hello' });
+ let executorMap = mockCreateOperation.mock.calls[0][0].toolSet.executorMap;
+ expect(executorMap['my-stdio-mcp']).toBe('client');
+
+ // Gateway configured β†’ should NOT mark as client
+ mockCreateOperation.mockClear();
+ vi.spyOn(deviceProxy, 'isConfigured', 'get').mockReturnValue(true);
+ mockQueryDeviceList.mockResolvedValue([
+ { deviceId: 'dev-1', deviceName: 'PC', platform: 'win32' },
+ ]);
+ await service.execAgent({ agentId: 'agent-1', prompt: 'Hello' });
+ executorMap = mockCreateOperation.mock.calls[0][0].toolSet.executorMap;
+ expect(executorMap['my-stdio-mcp']).toBeUndefined();
+ });
+ });
+
describe('toolManifestMap fully derived from ToolsEngine', () => {
it('should derive manifestMap entirely from getEnabledPluginManifests', async () => {
const mockManifest = {
diff --git a/src/server/services/aiAgent/index.ts b/src/server/services/aiAgent/index.ts
index ddcd9e20fc9..199b62a2dee 100644
--- a/src/server/services/aiAgent/index.ts
+++ b/src/server/services/aiAgent/index.ts
@@ -623,13 +623,17 @@ export class AiAgentService {
// require local IPC / subprocess capabilities:
// - local-system builtin: Electron IPC for file + command execution
// - stdio MCP plugins: subprocess lives on the user's machine
- // Dispatcher in RuntimeExecutors reads this to route via Agent Gateway WS.
- if (manifestMap.has(LocalSystemManifest.identifier)) {
- toolExecutorMap[LocalSystemManifest.identifier] = 'client';
- }
- for (const plugin of installedPlugins) {
- if (plugin.customParams?.mcp?.type === 'stdio' && manifestMap.has(plugin.identifier)) {
- toolExecutorMap[plugin.identifier] = 'client';
+ // Only applies when gateway is NOT configured (standalone Electron). When
+ // deviceContext is present, tools are routed via the RemoteDevice proxy,
+ // so marking them as `client` would misdispatch through dispatchClientTool.
+ if (!gatewayConfigured) {
+ if (manifestMap.has(LocalSystemManifest.identifier)) {
+ toolExecutorMap[LocalSystemManifest.identifier] = 'client';
+ }
+ for (const plugin of installedPlugins) {
+ if (plugin.customParams?.mcp?.type === 'stdio' && manifestMap.has(plugin.identifier)) {
+ toolExecutorMap[plugin.identifier] = 'client';
+ }
}
}