feat: carry mcp primitives content as a system prompt

This commit is contained in:
Kadxy 2025-01-09 10:20:56 +08:00
parent 77be190d76
commit f2a2b40d2c
2 changed files with 21 additions and 14 deletions

View File

@ -260,8 +260,6 @@ export const MCP_PRIMITIVES_TEMPLATE = `
{{ primitives }} {{ primitives }}
`; `;
// String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.
// Here are the functions available in JSONSchema format:
export const MCP_SYSTEM_TEMPLATE = ` export const MCP_SYSTEM_TEMPLATE = `
You are an AI assistant with access to system tools. Your role is to help users by combining natural language understanding with tool operations when needed. You are an AI assistant with access to system tools. Your role is to help users by combining natural language understanding with tool operations when needed.
@ -269,7 +267,13 @@ You are an AI assistant with access to system tools. Your role is to help users
{{ MCP_PRIMITIVES }} {{ MCP_PRIMITIVES }}
2. WHEN TO USE TOOLS: 2. WHEN TO USE TOOLS:
- When users ask any questions that can be answered by available tools, you should use the tools to answer the user's question. - ALWAYS USE TOOLS when they can help answer user questions
- DO NOT just describe what you could do - TAKE ACTION immediately
- If you're not sure whether to use a tool, USE IT
- Common triggers for tool use:
* Questions about files or directories
* Requests to check, list, or manipulate system resources
* Any query that can be answered with available tools
3. HOW TO USE TOOLS: 3. HOW TO USE TOOLS:
A. Tool Call Format: A. Tool Call Format:
@ -287,24 +291,25 @@ You are an AI assistant with access to system tools. Your role is to help users
C. Important Rules: C. Important Rules:
- Only ONE tool call per message - Only ONE tool call per message
- Always use the exact primitive name from available tools - ALWAYS TAKE ACTION instead of just describing what you could do
- Include the correct clientId in code block language tag - Include the correct clientId in code block language tag
- Verify arguments match the primitive's requirements - Verify arguments match the primitive's requirements
4. INTERACTION FLOW: 4. INTERACTION FLOW:
A. Understand user's request A. When user makes a request:
B. If tools are needed: - IMMEDIATELY use appropriate tool if available
- Explain what you plan to do - DO NOT ask if user wants you to use the tool
- Make the appropriate tool call - DO NOT just describe what you could do
- Wait for the response B. After receiving tool response:
- Explain the results in user-friendly terms - Explain results clearly
- Take next appropriate action if needed
C. If tools fail: C. If tools fail:
- Explain the error clearly - Explain the error
- Suggest alternatives or ask for clarification - Try alternative approach immediately
5. EXAMPLE INTERACTION: 5. EXAMPLE INTERACTION:
User: "What files do I have on my desktop?" User: "What files do I have on my desktop?"
Assistant: "I'll first check which directories I have access to. Assistant: "I'll check which directories I have access to.
\`\`\`json:mcp:filesystem \`\`\`json:mcp:filesystem
{ {
"method": "tools/call", "method": "tools/call",

View File

@ -203,6 +203,7 @@ async function getMcpSystemPrompt(): Promise<string> {
primitives = primitives.filter((i) => primitives = primitives.filter((i) =>
i.primitives.some((p) => p.type === "tool"), i.primitives.some((p) => p.type === "tool"),
); );
let primitivesString = ""; let primitivesString = "";
primitives.forEach((i) => { primitives.forEach((i) => {
primitivesString += MCP_PRIMITIVES_TEMPLATE.replace( primitivesString += MCP_PRIMITIVES_TEMPLATE.replace(
@ -210,9 +211,10 @@ async function getMcpSystemPrompt(): Promise<string> {
i.clientId, i.clientId,
).replace( ).replace(
"{{ primitives }}", "{{ primitives }}",
i.primitives.map((p) => JSON.stringify(p)).join("\n"), i.primitives.map((p) => JSON.stringify(p, null, 2)).join("\n"),
); );
}); });
return MCP_SYSTEM_TEMPLATE.replace("{{ MCP_PRIMITIVES }}", primitivesString); return MCP_SYSTEM_TEMPLATE.replace("{{ MCP_PRIMITIVES }}", primitivesString);
} }