fix(core): reuse connected MCP client in subagent invocations

Subsequent subagent invocations attempt to connect to an already connected MCP client, throwing an error. This commit adds a status check before calling client.connect(), enabling client.discoverInto() to successfully populate the new subagent registry.
This commit is contained in:
Akhilesh Kumar
2026-03-16 17:14:35 +00:00
parent e8d4b73d4d
commit e76d684210
2 changed files with 6 additions and 3 deletions
@@ -14,7 +14,7 @@ import {
type MockedObject,
} from 'vitest';
import { McpClientManager } from './mcp-client-manager.js';
import { McpClient, MCPDiscoveryState } from './mcp-client.js';
import { McpClient, MCPDiscoveryState, MCPServerStatus } from './mcp-client.js';
import type { ToolRegistry } from './tool-registry.js';
import type { Config, GeminiCLIExtension } from '../config/config.js';
import type { PromptRegistry } from '../prompts/prompt-registry.js';
@@ -38,7 +38,7 @@ describe('McpClientManager', () => {
connect: vi.fn(),
discoverInto: vi.fn(),
disconnect: vi.fn(),
getStatus: vi.fn(),
getStatus: vi.fn().mockReturnValue(MCPServerStatus.DISCONNECTED),
getServerConfig: vi.fn(),
getServerName: vi.fn().mockReturnValue('test-server'),
} as unknown as McpClient);
@@ -13,6 +13,7 @@ import type { ToolRegistry } from './tool-registry.js';
import {
McpClient,
MCPDiscoveryState,
MCPServerStatus,
populateMcpServerCommand,
} from './mcp-client.js';
import { getErrorMessage, isAuthenticationError } from '../utils/errors.js';
@@ -458,7 +459,9 @@ export class McpClientManager {
: undefined);
try {
await client.connect();
if (client.getStatus() === MCPServerStatus.DISCONNECTED) {
await client.connect();
}
if (targetRegistries) {
await client.discoverInto(this.cliConfig, targetRegistries);
}