Add support for MCP server instructions behind config option (#13432)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
christine betts
2025-11-26 13:08:47 -05:00
committed by GitHub
parent 558c8ece2c
commit bc365f1eaa
9 changed files with 129 additions and 1 deletions

View File

@@ -193,4 +193,55 @@ describe('McpClientManager', () => {
);
});
});
describe('getMcpInstructions', () => {
it('should only return instructions from servers with useInstructions: true', async () => {
// Override McpClient mock for this test to return distinct objects based on config
vi.mocked(McpClient).mockImplementation(
(name, config) =>
({
connect: vi.fn(),
discover: vi.fn(),
disconnect: vi.fn(),
getServerConfig: vi.fn().mockReturnValue(config),
getInstructions: vi
.fn()
.mockReturnValue(`Instructions for ${name}`),
}) as unknown as McpClient,
);
const manager = new McpClientManager({} as ToolRegistry, mockConfig);
// 1. Configured server with useInstructions: true
mockConfig.getMcpServers.mockReturnValue({
'enabled-server': {
useInstructions: true,
},
'disabled-server': {
useInstructions: false,
},
'default-server': {
// undefined should be treated as false
},
});
await manager.startConfiguredMcpServers();
const instructions = manager.getMcpInstructions();
expect(instructions).toContain(
"# Instructions for MCP Server 'enabled-server'",
);
expect(instructions).toContain('Instructions for enabled-server');
expect(instructions).not.toContain(
"# Instructions for MCP Server 'disabled-server'",
);
expect(instructions).not.toContain('Instructions for disabled-server');
expect(instructions).not.toContain(
"# Instructions for MCP Server 'default-server'",
);
expect(instructions).not.toContain('Instructions for default-server');
});
});
});

View File

@@ -323,4 +323,20 @@ export class McpClientManager {
}
return mcpServers;
}
getMcpInstructions(): string {
const instructions: string[] = [];
for (const [name, client] of this.clients) {
// Only include instructions if explicitly enabled in config
if (client.getServerConfig().useInstructions) {
const clientInstructions = client.getInstructions();
if (clientInstructions) {
instructions.push(
`# Instructions for MCP Server '${name}'\n${clientInstructions}`,
);
}
}
}
return instructions.join('\n\n');
}
}

View File

@@ -218,6 +218,10 @@ export class McpClient {
getServerConfig(): MCPServerConfig {
return this.serverConfig;
}
getInstructions(): string | undefined {
return this.client?.getInstructions();
}
}
/**