BROKEN: mcp conversion utils, tool name updates

This commit is contained in:
Dallin Romney 2025-06-04 16:48:27 -07:00
parent 0f2cb9417c
commit cd6e2ab23f
4 changed files with 51 additions and 69 deletions

View file

@ -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") {

View file

@ -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<string>();
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<string, number> = {};
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<string, number> = {};
// Warn about duplicate rules
const ruleCounts = new Map<string, number>();
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`,
});

View file

@ -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,

View file

@ -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}`;
}