π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(composio): stop exposing tools of a connection that is not active (#20075)
Arvin Xu9af69c726cc7
diff --git a/apps/server/src/services/aiAgent/pipeline/__tests__/toolDiscovery.parallelReads.test.ts b/apps/server/src/services/aiAgent/pipeline/__tests__/toolDiscovery.parallelReads.test.tsindex e58f07d2868..e9bdc05bebe 100644--- a/apps/server/src/services/aiAgent/pipeline/__tests__/toolDiscovery.parallelReads.test.ts+++ b/apps/server/src/services/aiAgent/pipeline/__tests__/toolDiscovery.parallelReads.test.ts@@ -2,6 +2,8 @@ import { SpanStatusCode } from '@lobechat/observability-otel/api';import type * as ModelBankModule from 'model-bank';import { beforeEach, describe, expect, it, vi } from 'vitest';+import { createServerAgentToolsEngine } from '@/server/modules/Mecha';+import { AiAgentService } from '../../index';// The share-gate filter now runs at the top of `discoverTools`, before@@ -329,4 +331,43 @@ describe('discoverTools - independent reads run together', () => {message: 'composio down',});}, 15_000);++ // A Composio connection that never finished OAuth (or expired) still has a+ // plugin row with the app's full manifest. Only `getComposioManifests`+ // (ACTIVE-gated, source `composio`) may expose it β letting the raw row+ // through advertises every Gmail API to the model while each call fails as+ // `Builtin tool "gmail" is not implemented`.+ it('does not expose a non-ACTIVE Composio plugin row as an installed plugin', async () => {+ const gmailManifest = {+ api: [{ description: 'Fetch emails', name: 'GMAIL_FETCH_EMAILS', parameters: {} }],+ identifier: 'gmail',+ meta: { title: 'Gmail' },+ type: 'default',+ };+ reads.plugins.mockImplementationOnce(async () => {+ reads.started.push('plugins');+ return [+ {+ customParams: { composio: { connectedAccountId: 'ca_pending', status: 'PENDING' } },+ identifier: 'gmail',+ manifest: gmailManifest,+ type: 'plugin',+ },+ {+ customParams: {},+ identifier: 'some-plugin',+ manifest: { api: [], identifier: 'some-plugin', meta: {}, type: 'default' },+ type: 'plugin',+ },+ ];+ });++ const result = await service.execAgent({ agentId: 'agent-1', prompt: 'Read my inbox' });++ expect(result.success).toBe(true);+ const toolsContext = vi.mocked(createServerAgentToolsEngine).mock.calls.at(-1)![0];+ const installedIds = toolsContext.installedPlugins.map((p: any) => p.identifier);+ expect(installedIds).toContain('some-plugin');+ expect(installedIds).not.toContain('gmail');+ }, 15_000);});diff --git a/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts b/apps/server/src/services/aiAgent/pipeline/toolDiscovery.tsindex ea2350417cf..ea974af7a75 100644--- a/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts+++ b/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts@@ -714,8 +714,14 @@ export const discoverTools = async (// Filter out plugin entries that are now handled by real MCP connectors.// `let` because community-MCP plugins may be patched with connector// permissions below (their connector row has no endpoint, so they stay here).+ // Composio connections also leave a plugin row behind (customParams.composio),+ // but they are executable only through `getComposioManifests`, which gates+ // on an ACTIVE connection and tags the tool source as `composio`. Letting a+ // PENDING/EXPIRED row through here exposes the full tool schema with no+ // Composio source, so every call falls to the builtin executor and fails+ // as "not implemented".let pluginsWithoutConnectors = installedPlugins.filter(- (p) => !connectorIdentifierSet.has(p.identifier),+ (p) => !connectorIdentifierSet.has(p.identifier) && !p.customParams?.composio,);log('execAgent: got %d connector manifests', connectorManifests.length);diff --git a/apps/server/src/services/toolExecution/__tests__/builtin.test.ts b/apps/server/src/services/toolExecution/__tests__/builtin.test.tsindex 2e0049ed174..e99346fd52c 100644--- a/apps/server/src/services/toolExecution/__tests__/builtin.test.ts+++ b/apps/server/src/services/toolExecution/__tests__/builtin.test.ts@@ -2,6 +2,7 @@ import type { ChatToolPayload } from '@lobechat/types';import { beforeEach, describe, expect, it, vi } from 'vitest';import { BuiltinToolsExecutor } from '../builtin';+import { hasServerRuntime } from '../serverRuntimes';import type { ToolExecutionContext } from '../types';const mocks = vi.hoisted(() => ({@@ -683,3 +684,49 @@ describe('BuiltinToolsExecutor share-visitor gate', () => {expect(write.error?.code).toBe('SHARE_GATE_BLOCKED');});});++describe('BuiltinToolsExecutor Composio app without an active connection', () => {+ const executor = new BuiltinToolsExecutor({} as any, 'user-1');++ // Only an ACTIVE Composio connection is routed with `source: 'composio'`. A+ // stale activation (or a resumed run whose toolset predates a status change)+ // lands here without that source β the model must learn the app is not+ // connected, not that the tool "is not implemented".+ it('returns COMPOSIO_NOT_CONNECTED instead of throwing "not implemented"', async () => {+ vi.mocked(hasServerRuntime).mockReturnValueOnce(false);++ const result = await executor.execute(+ {+ apiName: 'GMAIL_FETCH_EMAILS',+ arguments: '{"query":"newer_than:3d"}',+ id: 'call-gmail',+ identifier: 'gmail',+ type: 'default' as any,+ },+ context,+ );++ expect(result.success).toBe(false);+ expect(result.error?.code).toBe('COMPOSIO_NOT_CONNECTED');+ expect(result.content).toContain('Gmail is not connected');+ expect(result.content).toContain('Settings β Connectors');+ expect(result.content).not.toMatch(/not implemented/);+ });++ it('still reports an unknown non-Composio builtin as not implemented', async () => {+ vi.mocked(hasServerRuntime).mockReturnValueOnce(false);++ await expect(+ executor.execute(+ {+ apiName: 'doThing',+ arguments: '{}',+ id: 'call-x',+ identifier: 'lobe-missing-tool',+ type: 'default' as any,+ },+ context,+ ),+ ).rejects.toThrow('Builtin tool "lobe-missing-tool" is not implemented');+ });+});diff --git a/apps/server/src/services/toolExecution/builtin.ts b/apps/server/src/services/toolExecution/builtin.tsindex 0b3b3c2a777..26e21d9db6d 100644--- a/apps/server/src/services/toolExecution/builtin.ts+++ b/apps/server/src/services/toolExecution/builtin.ts@@ -1,4 +1,5 @@import { builtinTools } from '@lobechat/builtin-tools';+import { COMPOSIO_APP_TYPES } from '@lobechat/const';import { type LobeChatDatabase } from '@lobechat/database';import {type ChatToolPayload,@@ -19,6 +20,9 @@ import { resolveBuiltinToolWorkIntent } from './workRegistration';const log = debug('lobe-server:builtin-tools-executor');+const COMPOSIO_IDENTIFIERS = new Set(COMPOSIO_APP_TYPES.map((type) => type.identifier));+const isComposioIdentifier = (identifier: string) => COMPOSIO_IDENTIFIERS.has(identifier);+/*** Market rejects a trusted-client token 5 minutes after it was minted, while one* executor can outlive that (the inline step loop keeps it for a whole@@ -264,6 +268,24 @@ export class BuiltinToolsExecutor implements IToolExecutor {});}+ // A Composio app reaching here was not routed as `composio`, which only+ // happens when its connection is not ACTIVE (e.g. a stale activation or a+ // resumed run whose toolset predates a status change). Say so instead of+ // claiming the tool is unimplemented.+ if (isComposioIdentifier(identifier) && !hasServerRuntime(identifier)) {+ const appLabel =+ COMPOSIO_APP_TYPES.find((type) => type.identifier === identifier)?.label ?? identifier;+ const message =+ `${appLabel} is not connected (the Composio connection is pending, expired, or was removed), ` ++ `so "${apiName}" cannot run. Ask the user to reconnect ${appLabel} in Settings β Connectors, ` ++ `then retry in a new message.`;+ return {+ content: message,+ error: { code: 'COMPOSIO_NOT_CONNECTED', message },+ success: false,+ };+ }+// Use server runtime registry (handles both pre-instantiated and per-request runtimes)if (!hasServerRuntime(identifier)) {throw new Error(`Builtin tool "${identifier}" is not implemented`);