diff --git a/core/commands/slash/mcp.ts b/core/commands/slash/mcp.ts index 84df38989..a4dad2802 100644 --- a/core/commands/slash/mcp.ts +++ b/core/commands/slash/mcp.ts @@ -1,28 +1,30 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { PromptMessage } from "@modelcontextprotocol/sdk/types.js"; -import { ChatMessage, SlashCommand } from "../../index.js"; +import { ChatMessage, MCPPrompt, SlashCommand } from "../../index.js"; import { renderChatMessage } from "../../util/messageContent.js"; -export function constructMcpSlashCommand( +export function mcpPromptToSlashCommand( client: Client, - name: string, - description?: string, - args?: string[], + prompt: MCPPrompt, ): SlashCommand { return { - name, - description: description ?? "MCP Prompt", + name: prompt.name, + description: prompt.description ?? "MCP Prompt", params: {}, run: async function* (context) { const argsObject: { [key: string]: string } = {}; const userInput = context.input.split(" ").slice(1).join(" "); - if (args) { - args.forEach((arg, i) => { + if (prompt.arguments) { + const argNames = prompt.arguments.map((a) => a.name); + argNames.forEach((arg, i) => { argsObject[arg] = ""; // userInput }); } - const result = await client.getPrompt({ name, arguments: argsObject }); + const result = await client.getPrompt({ + name: prompt.name, + arguments: argsObject, + }); const mcpMessages: PromptMessage[] = result.messages; const messages: ChatMessage[] = mcpMessages.map((msg) => { if (msg.content.type !== "text") { diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index dd04d1915..ee93210fb 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -17,7 +17,7 @@ import { SerializedContinueConfig, Tool, } from "../../"; -import { constructMcpSlashCommand } from "../../commands/slash/mcp"; +import { mcpPromptToSlashCommand } from "../../commands/slash/mcp"; import { MCPManagerSingleton } from "../../context/mcp/MCPManagerSingleton"; import MCPContextProvider from "../../context/providers/MCPContextProvider"; import { ControlPlaneProxyInfo } from "../../control-plane/analytics/IAnalyticsProvider.js"; @@ -26,8 +26,7 @@ import { getControlPlaneEnv } from "../../control-plane/env.js"; import { TeamAnalytics } from "../../control-plane/TeamAnalytics.js"; import ContinueProxy from "../../llm/llms/stubs/ContinueProxy"; import { getConfigDependentToolDefinitions } from "../../tools"; -import { encodeMCPToolUri } from "../../tools/callTool"; -import { getMCPToolName } from "../../tools/mcpToolName"; +import { mcpToolToTool } from "../../tools/mcpTools"; import { getConfigJsonPath, getConfigYamlPath } from "../../util/paths"; import { localPathOrUriToPath } from "../../util/pathToUri"; import { Telemetry } from "../../util/posthog"; @@ -140,6 +139,13 @@ export default async function doLoadConfig(options: { // Rectify model selections for each role newConfig = rectifySelectedModelsFromGlobalContext(newConfig, profileId); + // Add tools that depend on rules etc. + newConfig.tools.push( + ...getConfigDependentToolDefinitions({ + rules: newConfig.rules, + }), + ); + // Add things from MCP servers const mcpManager = MCPManagerSingleton.getInstance(); const mcpServerStatuses = mcpManager.getStatuses(); @@ -151,31 +157,22 @@ export default async function doLoadConfig(options: { }); newConfig.mcpServerStatuses = serializableStatuses; + // Keep track of tool names to avoid duplicates + const toolNames = new Set(); + + newConfig.tools.forEach((tool) => { + toolNames.add(tool.function.name); + }); + for (const server of mcpServerStatuses) { if (server.status === "connected") { - const serverTools: Tool[] = server.tools.map((tool) => ({ - displayTitle: server.name + " " + tool.name, - function: { - description: tool.description, - name: getMCPToolName(server, tool), - parameters: tool.inputSchema, - }, - faviconUrl: server.faviconUrl, - readonly: false, - type: "function" as const, - uri: encodeMCPToolUri(server.id, tool.name), - group: server.name, - originalFunctionName: tool.name, - })); + const serverTools: Tool[] = server.tools.map((tool) => + mcpToolToTool(server, tool), + ); newConfig.tools.push(...serverTools); const serverSlashCommands = server.prompts.map((prompt) => - constructMcpSlashCommand( - server.client, - prompt.name, - prompt.description, - prompt.arguments?.map((a: any) => a.name), - ), + mcpPromptToSlashCommand(server.client, prompt), ); newConfig.slashCommands.push(...serverSlashCommands); @@ -205,45 +202,28 @@ export default async function doLoadConfig(options: { } } - newConfig.tools.push( - ...getConfigDependentToolDefinitions({ - rules: newConfig.rules, - }), - ); + // Warn user about duplicate tool names + // Could change this behavior in a few ways, e.g. to prepend server name to all tool names from that server if ANY are duplicate + // Object.entries(toolNameCounts).forEach(([toolName, count]) => { + // if (count > 1) { + // errors!.push({ + // fatal: false, + // message: `Duplicate (${count}) tools named "${toolName}" detected. Permissions will conflict and usage may be unpredictable`, + // }); + // } + // }); - // Detect duplicate tool names - const counts: Record = {}; - newConfig.tools.forEach((tool) => { - if (counts[tool.function.name]) { - counts[tool.function.name] = counts[tool.function.name] + 1; - } else { - counts[tool.function.name] = 1; - } - }); - - Object.entries(counts).forEach(([toolName, count]) => { - if (count > 1) { - errors!.push({ - fatal: false, - message: `Duplicate (${count}) tools named "${toolName}" detected. Permissions will conflict and usage may be unpredictable`, - }); - } - }); - - const ruleCounts: Record = {}; + // Warn about duplicate rules + const ruleCounts = new Map(); newConfig.rules.forEach((rule) => { if (rule.name) { - if (ruleCounts[rule.name]) { - ruleCounts[rule.name] = ruleCounts[rule.name] + 1; - } else { - ruleCounts[rule.name] = 1; - } + ruleCounts.set(rule.name, (ruleCounts.get(rule.name) || 0) + 1); } }); - Object.entries(ruleCounts).forEach(([ruleName, count]) => { + ruleCounts.forEach((count, ruleName) => { if (count > 1) { - errors!.push({ + errors.push({ fatal: false, message: `Duplicate (${count}) rules named "${ruleName}" detected. This may cause unexpected behavior`, }); diff --git a/core/tools/mcpToolName.vitest.ts b/core/tools/mcpToolName.vitest.ts index 8c20f630a..7b11b26dd 100644 --- a/core/tools/mcpToolName.vitest.ts +++ b/core/tools/mcpToolName.vitest.ts @@ -1,6 +1,6 @@ import { expect, test } from "vitest"; import { MCPServerStatus, MCPTool } from ".."; -import { getMCPToolName } from "./mcpToolName"; +import { getMCPToolName } from "./mcpTools"; const createMcpServer = (name: string): MCPServerStatus => ({ name, diff --git a/core/tools/mcpToolName.ts b/core/tools/mcpTools.ts similarity index 51% rename from core/tools/mcpToolName.ts rename to core/tools/mcpTools.ts index 6440ef5e7..5e4a3f132 100644 --- a/core/tools/mcpToolName.ts +++ b/core/tools/mcpTools.ts @@ -1,9 +1,9 @@ import { MCPServerStatus, MCPTool } from ".."; export function getMCPToolName(server: MCPServerStatus, tool: MCPTool) { - const serverPrefix = server.name.split(" ").join("_").toLowerCase(); - if (tool.name.startsWith(serverPrefix)) { + const serverSuffix = server.name.split(" ").join("_").toLowerCase(); + if (tool.name.endsWith(serverSuffix)) { return tool.name; } - return `${serverPrefix}_${tool.name}`; + return `${tool.name}_${serverSuffix}`; }