Skip to content

Commit d522346

Browse files
committed
fix(agents): apply custom tool limits to mcp tools
1 parent 7954d02 commit d522346

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

src/cortex-agents/src/agent.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,19 @@ impl AgentInfo {
127127
}
128128

129129
pub fn is_tool_enabled(&self, tool: &str) -> bool {
130-
self.tools.get(tool).copied().unwrap_or(true)
130+
if let Some(enabled) = self.tools.get(tool) {
131+
return *enabled;
132+
}
133+
134+
if tool.eq_ignore_ascii_case("mcp") {
135+
return self.tools.get("mcp").copied().unwrap_or(true);
136+
}
137+
138+
if tool.to_ascii_lowercase().starts_with("mcp__") {
139+
return self.tools.get("mcp").copied().unwrap_or(true);
140+
}
141+
142+
true
131143
}
132144

133145
pub fn with_max_steps(mut self, max_steps: usize) -> Self {

src/cortex-agents/src/custom/registry.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ fn custom_agent_to_agent_info(agent: &CustomAgentConfig) -> AgentInfo {
184184
}
185185
}
186186

187+
for tool in &allowed {
188+
if tool.to_ascii_lowercase().starts_with("mcp__") {
189+
info = info.enable_tool(tool);
190+
}
191+
}
192+
193+
if !matches!(
194+
agent.tools,
195+
ToolsConfig::Category(ToolCategory::Mcp | ToolCategory::All)
196+
) && !allowed.iter().any(|t| t.eq_ignore_ascii_case("mcp"))
197+
{
198+
info = info.disable_tool("mcp");
199+
}
200+
187201
info
188202
}
189203

@@ -378,6 +392,63 @@ mod tests {
378392
assert!(info.model.is_none()); // Should not have a model set
379393
}
380394

395+
#[test]
396+
fn test_read_only_agent_disables_mcp_tools() {
397+
let mut registry = CustomAgentRegistry::new();
398+
registry.register(CustomAgentConfig::new("readonly").with_tools(ToolsConfig::read_only()));
399+
400+
let info = registry.to_agent_info("readonly").unwrap();
401+
402+
assert!(!info.is_tool_enabled("mcp"));
403+
assert!(!info.is_tool_enabled("mcp__postgres__query"));
404+
assert!(!info.is_tool_enabled("MCP__postgres__query"));
405+
assert!(info.is_tool_enabled("read"));
406+
}
407+
408+
#[test]
409+
fn test_mcp_and_all_tool_configs_allow_mcp_tools() {
410+
let mut registry = CustomAgentRegistry::new();
411+
registry.register(
412+
CustomAgentConfig::new("mcp-agent")
413+
.with_tools(ToolsConfig::Category(ToolCategory::Mcp)),
414+
);
415+
registry.register(CustomAgentConfig::new("all-agent").with_tools(ToolsConfig::all()));
416+
417+
let mcp_info = registry.to_agent_info("mcp-agent").unwrap();
418+
assert!(mcp_info.is_tool_enabled("mcp__postgres__query"));
419+
420+
let all_info = registry.to_agent_info("all-agent").unwrap();
421+
assert!(all_info.is_tool_enabled("mcp__postgres__query"));
422+
}
423+
424+
#[test]
425+
fn test_custom_tools_list_can_allow_mcp_tools() {
426+
let mut registry = CustomAgentRegistry::new();
427+
registry.register(
428+
CustomAgentConfig::new("custom-mcp").with_tools(ToolsConfig::custom(["Read", "mcp"])),
429+
);
430+
431+
let info = registry.to_agent_info("custom-mcp").unwrap();
432+
433+
assert!(info.is_tool_enabled("mcp__postgres__query"));
434+
assert!(info.is_tool_enabled("Read"));
435+
assert!(!info.is_tool_enabled("Execute"));
436+
}
437+
438+
#[test]
439+
fn test_custom_tools_list_can_allow_single_mcp_tool() {
440+
let mut registry = CustomAgentRegistry::new();
441+
registry.register(
442+
CustomAgentConfig::new("single-mcp")
443+
.with_tools(ToolsConfig::custom(["Read", "mcp__postgres__query"])),
444+
);
445+
446+
let info = registry.to_agent_info("single-mcp").unwrap();
447+
448+
assert!(info.is_tool_enabled("mcp__postgres__query"));
449+
assert!(!info.is_tool_enabled("mcp__postgres__write"));
450+
}
451+
381452
#[test]
382453
fn test_tools_to_permission() {
383454
// Read-only should have read_only permission

0 commit comments

Comments
 (0)