Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mcp/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { gateToolCall } from "../plugins/permission-plugin.js";
import { scrubSecretShapedToolResultContent } from "../plugins/tool-result-secret-scrub.js";
import { truncateToolResultContent } from "../plugins/result-truncation-plugin.js";
import type { MCPClient } from "./client.js";

function mcpToolName(serverName: string, toolName: string): string {
return `mcp__${serverName}__${toolName}`;
}
import { mcpToolName } from "./tool-name.js";

// MCP results never reach the posix runner, so the secret-scrub and truncation
// middleware in src/plugins never see them. Apply the same scrub-then-truncate
Expand Down
24 changes: 24 additions & 0 deletions src/mcp/tool-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, test } from "bun:test";
import { mcpToolName, mcpToolPrefix, parseMcpToolName } from "./tool-name.js";

describe("mcpToolName", () => {
test("builds the mcp__<server>__<tool> identifier", () => {
expect(mcpToolName("linear", "list_projects")).toBe("mcp__linear__list_projects");
});

test("round-trips with parseMcpToolName", () => {
const name = mcpToolName("railway", "get_logs");
expect(parseMcpToolName(name)).toEqual({ server: "railway", tool: "get_logs" });
});
});

describe("mcpToolPrefix", () => {
test("matches the prefix of a built name for the same server", () => {
const server = "linear";
expect(mcpToolName(server, "list_projects").startsWith(mcpToolPrefix(server))).toBe(true);
});

test("builds mcp__<server>__", () => {
expect(mcpToolPrefix("linear")).toBe("mcp__linear__");
});
});
8 changes: 8 additions & 0 deletions src/mcp/tool-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export function isMcpToolName(name: string): boolean {
return name.startsWith(MCP_PREFIX);
}

export function mcpToolPrefix(serverName: string): string {
return `${MCP_PREFIX}${serverName}__`;
}

export function mcpToolName(serverName: string, toolName: string): string {
return `${mcpToolPrefix(serverName)}${toolName}`;
}

export function parseMcpToolName(name: string): { server: string; tool: string } | null {
if (!isMcpToolName(name)) return null;
const rest = name.slice(MCP_PREFIX.length);
Expand Down
12 changes: 4 additions & 8 deletions src/mcp/tool-permissions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tier } from "../permission/classify.js";
import { isReadOnlyMcpTool } from "./tool-name.js";
import { isReadOnlyMcpTool, mcpToolName, mcpToolPrefix } from "./tool-name.js";

// Subset of MCP ToolAnnotations used for permission tiering (hints from tools/list).
export type McpToolAnnotations = {
Expand All @@ -23,7 +23,7 @@ export function createMcpToolPermissionRegistry(): McpToolPermissionRegistry {
tiers.set(name, tier);
},
removeToolsForServer(serverName) {
const prefix = `mcp__${serverName}__`;
const prefix = mcpToolPrefix(serverName);
for (const key of tiers.keys()) {
if (key.startsWith(prefix)) tiers.delete(key);
}
Expand All @@ -37,10 +37,6 @@ export function createMcpToolPermissionRegistry(): McpToolPermissionRegistry {
};
}

function mcpAgentToolName(serverName: string, toolName: string): string {
return `mcp__${serverName}__${toolName}`;
}

function hasAnnotationHints(annotations: McpToolAnnotations | undefined): boolean {
if (annotations === undefined) return false;
return (
Expand All @@ -60,7 +56,7 @@ export function tierFromMcpTool(
if (hasAnnotationHints(annotations)) {
return annotations!.readOnlyHint === true ? "allow" : "ask";
}
return isReadOnlyMcpTool(mcpAgentToolName(serverName, toolName)) ? "allow" : "ask";
return isReadOnlyMcpTool(mcpToolName(serverName, toolName)) ? "allow" : "ask";
}

export function registerMcpClientTools(
Expand All @@ -69,6 +65,6 @@ export function registerMcpClientTools(
tools: ReadonlyArray<{ name: string; annotations?: McpToolAnnotations }>,
): void {
for (const tool of tools) {
registry.setTier(mcpAgentToolName(serverName, tool.name), tierFromMcpTool(tool.annotations, serverName, tool.name));
registry.setTier(mcpToolName(serverName, tool.name), tierFromMcpTool(tool.annotations, serverName, tool.name));
}
}
Loading