Skip to content

Commit 934b457

Browse files
committed
Improve tool selection with additinal tool descriptions
1 parent d79539f commit 934b457

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

packages/server/api/src/app/ai/chat/prompts.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ import { ChatFlowContext, CODE_BLOCK_NAME, isNil } from '@openops/shared';
33
import { ToolSet } from 'ai';
44
import { readFile } from 'fs/promises';
55
import { join } from 'path';
6+
import { getAdditionalDescriptionsForSelectedTools } from '../mcp/external-tool-descriptions';
67
import { hasToolProvider } from '../mcp/tool-utils';
78
import { QueryClassification } from '../mcp/types';
89
import { MCPChatContext } from './ai-chat.service';
910

11+
function buildAdditionalToolNotes(selectedTools: ToolSet | undefined): string {
12+
if (!selectedTools) {
13+
return '';
14+
}
15+
16+
const descriptions = getAdditionalDescriptionsForSelectedTools(selectedTools);
17+
return descriptions.length > 0 ? descriptions.join('\n\n') : '';
18+
}
19+
1020
export const getMcpSystemPrompt = async ({
1121
queryClassification,
1222
selectedTools,
@@ -58,7 +68,12 @@ export const getMcpSystemPrompt = async ({
5868

5969
const allPrompts = await Promise.all(promptPromises);
6070

61-
return allPrompts.join('\n\n');
71+
const additionalToolNotes = buildAdditionalToolNotes(selectedTools);
72+
const finalPrompts = additionalToolNotes
73+
? [...allPrompts, additionalToolNotes]
74+
: allPrompts;
75+
76+
return finalPrompts.join('\n\n');
6277
};
6378

6479
export const getBlockSystemPrompt = async (
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const MCP_TOOL_ADDITIONAL_DESCRIPTIONS: Record<string, string> = {
2+
'session-sql': `## IMPORTANT: Tool Usage Note for 'session-sql'
3+
4+
The 'session-sql' tool from AWS billing and cost management MCP server does NOT work with OpenOps tables.
5+
6+
When the user asks about OpenOps tables, database schema, or table operations, DO NOT use 'session-sql'. This tool is only for AWS billing and cost management related SQL queries.`,
7+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MCP_TOOL_ADDITIONAL_DESCRIPTIONS } from './external-tool-descriptions-data';
2+
3+
export function getToolAdditionalDescription(
4+
toolName: string,
5+
): string | undefined {
6+
return MCP_TOOL_ADDITIONAL_DESCRIPTIONS[toolName];
7+
}
8+
9+
export function getAdditionalDescriptionsForSelectedTools(
10+
selectedTools: Record<string, unknown> | undefined,
11+
): string[] {
12+
if (!selectedTools) {
13+
return [];
14+
}
15+
16+
const descriptions: string[] = [];
17+
for (const toolName of Object.keys(selectedTools)) {
18+
const description = getToolAdditionalDescription(toolName);
19+
if (description) {
20+
descriptions.push(description);
21+
}
22+
}
23+
24+
return descriptions;
25+
}

packages/server/api/src/app/ai/mcp/llm-query-router.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { projectService } from '../../project/project-service';
77
import { getChatTools } from '../chat/ai-chat.service';
88
import { buildUIContextSection } from '../chat/prompts.service';
99
import { getAdditionalQueryClassificationDescriptions } from './extensions';
10+
import { getToolAdditionalDescription } from './external-tool-descriptions';
1011
import { sanitizeMessages } from './tool-utils';
1112
import { QueryClassification } from './types';
1213

@@ -202,6 +203,20 @@ const getSystemPrompt = async (
202203
const toolsMessage = toolList
203204
.map((t) => `- ${t.name}: ${t.description}`)
204205
.join('\n');
206+
const additionalToolNotes: string[] = [];
207+
for (const tool of toolList) {
208+
const additionalDescription = getToolAdditionalDescription(tool.name);
209+
if (additionalDescription) {
210+
additionalToolNotes.push(additionalDescription);
211+
}
212+
}
213+
214+
const additionalToolNotesSection =
215+
additionalToolNotes.length > 0
216+
? `\n\n## IMPORTANT TOOL USAGE NOTES:\n\n${additionalToolNotes.join(
217+
'\n\n',
218+
)}\n`
219+
: '';
205220
return (
206221
"Given the following conversation history and the list of available tools, select the tools that are most relevant to answer the user's request. " +
207222
`IMPORTANT: Tables tools should always be included in the output if the user asks a question involving those table names: ${openopsTablesNames.join(
@@ -211,7 +226,7 @@ const getSystemPrompt = async (
211226
'Include ALL relevant categories that apply. ' +
212227
`${
213228
uiContext ? `${await buildUIContextSection(uiContext)}\n` : ''
214-
} Tools: ${toolsMessage}`
229+
} Tools: ${toolsMessage}${additionalToolNotesSection}`
215230
);
216231
};
217232

0 commit comments

Comments
 (0)