Skip to main content

Module agent_handler

Module agent_handler 

Source
Expand description

Agent-as-MCP-Tool

Exposes cortex agents as MCP tools, allowing external MCP clients (like Claude Desktop) to invoke agents directly.

§Architecture

┌─────────────────────────────────────────────────────────────┐
│                     MCP Client (Claude)                     │
└─────────────────────────────────────────────────────────────┘
                             │
                   tools/call "agent_xxx"
                             │
                             ▼
┌─────────────────────────────────────────────────────────────┐
│                     McpServer                               │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              AgentMcpHandler                         │   │
│  │                                                      │   │
│  │  - Wraps AgentTool as MCP ToolHandler               │   │
│  │  - Converts MCP params to AgentToolInput            │   │
│  │  - Converts AgentToolOutput to MCP CallToolResult   │   │
│  └─────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘
                             │
                             ▼
┌─────────────────────────────────────────────────────────────┐
│                     AgentTool / AgentEngine                 │
│  - Executes agent logic                                     │
│  - Returns structured response                              │
└─────────────────────────────────────────────────────────────┘

§Example

use cortexai_mcp::{McpServer, AgentMcpHandler};
use cortexai_agents::agent_tool::AgentTool;

// Create an agent tool
let research_agent = AgentTool::builder("research_agent")
    .description("Researches topics and provides summaries")
    .handler(|input| async move {
        // Agent logic here
        Ok(AgentToolOutput::success("Research results..."))
    });

// Wrap as MCP handler
let mcp_handler = AgentMcpHandler::from_agent_tool(research_agent);

// Add to MCP server
let server = McpServer::builder()
    .name("agent-server")
    .add_tool(mcp_handler)
    .build();

Structs§

AgentMcpConfig
Configuration for an agent MCP handler
AgentMcpHandler
MCP ToolHandler that wraps an agent
AgentMcpHandlerBuilder
Builder for AgentMcpHandler
AgentMcpInput
Input schema for agent MCP tools
AgentMcpOutput
Output structure for agent responses

Functions§

simple_agent
Helper to create a simple agent handler from a closure

Type Aliases§

AgentHandlerFn
Handler type for agent execution