Skip to content

Commit 220ad69

Browse files
authored
Improve tool selection with additional tool descriptions (#1718)
Fixes OPS-3138.
1 parent 4e80c37 commit 220ad69

4 files changed

Lines changed: 61 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ 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 { getAdditionalToolDescriptions } 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+
const descriptions = getAdditionalToolDescriptions(selectedTools);
13+
return descriptions.length > 0 ? descriptions.join('\n\n') : '';
14+
}
15+
1016
export const getMcpSystemPrompt = async ({
1117
queryClassification,
1218
selectedTools,
@@ -58,7 +64,12 @@ export const getMcpSystemPrompt = async ({
5864

5965
const allPrompts = await Promise.all(promptPromises);
6066

61-
return allPrompts.join('\n\n');
67+
const additionalToolNotes = buildAdditionalToolNotes(selectedTools);
68+
const finalPrompts = additionalToolNotes
69+
? [...allPrompts, additionalToolNotes]
70+
: allPrompts;
71+
72+
return finalPrompts.join('\n\n');
6273
};
6374

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

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

Lines changed: 12 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 { getAdditionalToolDescriptions } from './external-tool-descriptions';
1011
import { sanitizeMessages } from './tool-utils';
1112
import { QueryClassification } from './types';
1213

@@ -202,6 +203,16 @@ const getSystemPrompt = async (
202203
const toolsMessage = toolList
203204
.map((t) => `- ${t.name}: ${t.description}`)
204205
.join('\n');
206+
const additionalToolNotes = getAdditionalToolDescriptions(
207+
toolList.map((t) => t.name),
208+
);
209+
210+
const additionalToolNotesSection =
211+
additionalToolNotes.length > 0
212+
? `\n\n### IMPORTANT TOOL USAGE NOTES:\n\n${additionalToolNotes.join(
213+
'\n\n',
214+
)}\n`
215+
: '';
205216
return (
206217
"Given the following conversation history and the list of available tools, select the tools that are most relevant to answer the user's request. " +
207218
`IMPORTANT: Tables tools should always be included in the output if the user asks a question involving those table names: ${openopsTablesNames.join(
@@ -211,7 +222,7 @@ const getSystemPrompt = async (
211222
'Include ALL relevant categories that apply. ' +
212223
`${
213224
uiContext ? `${await buildUIContextSection(uiContext)}\n` : ''
214-
} Tools: ${toolsMessage}`
225+
} Tools: ${toolsMessage}${additionalToolNotesSection}`
215226
);
216227
};
217228

0 commit comments

Comments
 (0)