Skip to content

⚡ Create Macro-Driven Tool Discovery System for AI Agent Integration #53

@avrabe

Description

@avrabe

⚡ Create Macro-Driven Tool Discovery System for AI Agent Integration

Problem Statement

Based on analysis of the framework and 2025 AI agent integration patterns, there's a gap in intelligent tool discovery and composition:

Current Limitations:

  • Tools are static and individually defined
  • No automatic tool grouping or workflow discovery
  • AI agents must know exact tool names and parameters
  • Missing tool relationship mapping for complex workflows
  • No support for tool composition patterns recommended in 2025 MCP best practices

AI Agent Integration Challenges:

  • "Avoid mapping every API endpoint to a new MCP tool" - current approach does exactly this
  • Tool grouping and workflow chaining are manual
  • Intentional tool design requires extensive boilerplate
  • Discovery metadata for AI agents is insufficient

Motivation

2025 MCP Best Practices Research

"Intentional Tool Design: Avoid mapping every API endpoint to a new MCP tool. Instead, group related tasks and design higher-level functions."

"Workflow Chaining: Use macros and chaining that trigger complex workflows via a single instruction."

AI Agent Requirements

  • Self-discovering tools that expose their capabilities
  • Semantic tool grouping for related operations
  • Workflow composition for multi-step processes
  • Intent-based tool selection rather than exact name matching
  • Tool relationship mapping for dependency resolution

Framework Competitive Analysis

  • Python MCP SDK: Uses decorators with automatic discovery
  • Other frameworks: Provide tool composition and semantic grouping
  • PulseEngine gap: Missing intelligent discovery layer

Solution Design

1. Enhanced Tool Discovery Macros

#[mcp_tool_group(
    name = "file_operations",
    description = "Complete file management workflow",
    intent = ["file management", "document handling", "storage operations"]
)]
impl FileManager {
    #[mcp_tool(
        workflow_step = "discovery",
        dependencies = [],
        outputs = ["file_list"]
    )]
    pub async fn list_files(&self, path: String) -> Result<Vec<FileInfo>> {
        // Implementation
    }
    
    #[mcp_tool(
        workflow_step = "action", 
        dependencies = ["list_files"],
        outputs = ["processed_file"]
    )]
    pub async fn process_file(&self, file_path: String) -> Result<ProcessedFile> {
        // Implementation  
    }
    
    #[mcp_tool(
        workflow_step = "completion",
        dependencies = ["process_file"],
        outputs = ["workflow_result"]
    )]
    pub async fn finalize_workflow(&self, processed: ProcessedFile) -> Result<WorkflowResult> {
        // Implementation
    }
}

2. Intelligent Tool Discovery

#[derive(Debug, Serialize, Deserialize)]
pub struct ToolDiscoveryInfo {
    pub tool_id: String,
    pub name: String,
    pub description: String,
    pub intent_keywords: Vec<String>,
    pub workflow_group: Option<String>,
    pub dependencies: Vec<String>,
    pub outputs: Vec<String>,
    pub complexity_score: u8,
    pub usage_patterns: Vec<UsagePattern>,
}

pub struct ToolDiscoveryEngine {
    tools: HashMap<String, ToolDiscoveryInfo>,
    workflow_graphs: HashMap<String, WorkflowGraph>,
    intent_index: IntentIndex,
}

impl ToolDiscoveryEngine {
    pub async fn discover_tools_for_intent(&self, intent: &str) -> Result<Vec<ToolRecommendation>> {
        // Semantic matching of intent to available tools
        let candidates = self.intent_index.search(intent);
        
        // Rank by relevance and complexity
        let ranked = self.rank_tools_by_intent(candidates, intent);
        
        // Group into workflows if applicable
        let workflows = self.identify_workflows(&ranked);
        
        Ok(workflows)
    }
    
    pub async fn suggest_tool_chain(&self, starting_tool: &str, desired_outcome: &str) -> Result<Vec<ToolChain>> {
        // Find workflow paths from starting tool to outcome
        let graph = self.workflow_graphs.get(starting_tool)?;
        let paths = graph.find_paths_to_outcome(desired_outcome);
        
        Ok(paths)
    }
}

3. Semantic Tool Composition

#[mcp_workflow(
    name = "data_processing_pipeline",
    description = "Complete data ingestion, processing, and output workflow",
    intent = ["data processing", "ETL", "data pipeline"]
)]
pub struct DataProcessingWorkflow;

impl DataProcessingWorkflow {
    #[mcp_workflow_entrypoint]
    pub async fn execute_pipeline(&self, input: DataInput) -> Result<WorkflowResult> {
        // Auto-generated workflow execution
        let ingested = self.ingest_data(input).await?;
        let processed = self.process_data(ingested).await?;
        let output = self.output_data(processed).await?;
        
        Ok(WorkflowResult {
            data: output,
            workflow_id: self.workflow_id(),
            execution_time: self.execution_duration(),
            tools_used: self.get_tools_chain(),
        })
    }
    
    // Individual steps auto-discovered from tool_group
    async fn ingest_data(&self, input: DataInput) -> Result<IngestedData> { ... }
    async fn process_data(&self, data: IngestedData) -> Result<ProcessedData> { ... }
    async fn output_data(&self, data: ProcessedData) -> Result<OutputData> { ... }
}

4. AI Agent Discovery API

// New endpoint for AI agent tool discovery
#[mcp_discovery_endpoint]
impl DiscoveryHandler {
    pub async fn discover_tools(&self, query: ToolDiscoveryQuery) -> Result<DiscoveryResponse> {
        match query {
            ToolDiscoveryQuery::ByIntent(intent) => {
                self.discovery_engine.discover_tools_for_intent(&intent).await
            }
            ToolDiscoveryQuery::ByCapability(capability) => {
                self.discovery_engine.find_tools_with_capability(&capability).await
            }
            ToolDiscoveryQuery::WorkflowChain { start, end } => {
                self.discovery_engine.suggest_tool_chain(&start, &end).await
            }
        }
    }
    
    pub async fn get_workflow_suggestions(&self, context: WorkflowContext) -> Result<Vec<WorkflowSuggestion>> {
        // AI-friendly workflow recommendations
        self.discovery_engine.suggest_workflows_for_context(context).await
    }
}

5. Auto-Generated Tool Schema with Relationships

// Enhanced tool schema generation
pub struct EnhancedToolSchema {
    pub basic_schema: ToolSchema,
    pub discovery_metadata: ToolDiscoveryInfo,
    pub relationships: ToolRelationships,
    pub ai_hints: AIAgentHints,
}

#[derive(Debug, Serialize)]
pub struct AIAgentHints {
    pub when_to_use: String,
    pub common_patterns: Vec<String>,
    pub related_workflows: Vec<String>,
    pub complexity_guidance: ComplexityGuidance,
}

Implementation Plan

Phase 1: Core Discovery Engine (Week 1)

  • Create pulseengine-mcp-discovery crate
  • Implement ToolDiscoveryEngine with semantic matching
  • Add #[mcp_tool_group] macro for grouping tools
  • Create tool relationship mapping system
  • Basic intent-based tool discovery

Phase 2: Workflow Composition (Week 2)

  • Implement #[mcp_workflow] macro for complex workflows
  • Add workflow dependency resolution
  • Create workflow execution engine
  • Add tool chaining and composition logic
  • Implement workflow graph generation

Phase 3: AI Agent Integration (Week 3)

  • Create discovery endpoint for AI agents
  • Implement semantic tool search API
  • Add workflow suggestion system
  • Create AI-friendly tool metadata generation
  • Add intent keyword indexing

Phase 4: Advanced Features (Week 4)

  • Add machine learning-based tool recommendation
  • Implement usage pattern analysis
  • Create tool performance optimization hints
  • Add A/B testing for tool discovery
  • Comprehensive documentation and examples

Features to Implement

1. Semantic Tool Grouping

#[mcp_tool_group(
    intent = ["file operations", "document management"],
    complexity = "medium",
    related_groups = ["data_processing", "storage_management"]
)]
impl DocumentManager {
    // Tools automatically grouped and discoverable
}

2. Intent-Based Discovery

// AI agent can query: "I need to process financial data"
let tools = discovery_engine
    .discover_tools_for_intent("financial data processing")
    .await?;

// Returns ranked list of relevant tools and workflows

3. Workflow Composition

// Automatically compose workflows from available tools
let workflow = discovery_engine
    .compose_workflow("data ingestion", "report generation")
    .await?;

// Returns optimized workflow with tool dependencies

4. Tool Relationship Mapping

pub struct ToolRelationship {
    pub relationship_type: RelationshipType, // Prerequisite, Alternative, Complement
    pub related_tool: String,
    pub confidence_score: f32,
    pub usage_frequency: u32,
}

Acceptance Criteria

Discovery System

  • Tools can be discovered by intent keywords
  • Tool grouping works automatically via macros
  • Workflow composition generates valid tool chains
  • Tool relationships are accurately mapped
  • Performance: <10ms for tool discovery queries

AI Agent Integration

  • AI agents can discover tools without knowing exact names
  • Workflow suggestions are semantically relevant
  • Tool complexity is communicated clearly
  • Usage patterns guide AI agent decisions
  • Discovery API is well-documented for AI integration

Developer Experience

  • Macro-driven approach requires minimal boilerplate
  • Tool discovery works out-of-the-box
  • Clear documentation with examples
  • Migration path from current tool definitions
  • Performance impact is negligible

Framework Integration

  • Works with existing #[mcp_tools] macro
  • Compatible with all transport layers
  • Integrates with security middleware
  • Maintains backward compatibility
  • Extensible for future AI capabilities

Configuration Examples

Simple Tool Discovery

#[mcp_server(name = "Smart Server", discovery = true)]
struct SmartServer;

#[mcp_tool_group(intent = ["data analysis", "statistics"])]
impl DataAnalyzer {
    #[mcp_tool(complexity = "low")]
    pub async fn basic_stats(&self, data: Vec<f64>) -> Result<Stats> { ... }
    
    #[mcp_tool(complexity = "high", depends_on = ["basic_stats"])]  
    pub async fn advanced_analysis(&self, stats: Stats) -> Result<Analysis> { ... }
}

AI Agent Query Examples

// Natural language tool discovery
let response = client
    .post("/mcp/discover")
    .json(&json!({
        "intent": "I need to analyze financial data and generate a report",
        "context": {
            "data_type": "financial",
            "output_format": "report"
        }
    }))
    .send()
    .await?;

// Returns structured workflow recommendations

References & Research

2025 MCP Best Practices

AI Agent Integration Patterns

Current Framework Analysis

  • Existing tool macro: mcp-macros/src/mcp_tool.rs
  • Tool discovery gaps: Missing semantic layer
  • AI agent feedback: "Need better tool discoverability"

Success Metrics

  1. Discovery Performance: AI agents find relevant tools in <10ms
  2. Workflow Accuracy: 90%+ of suggested workflows are useful
  3. Developer Productivity: 50% reduction in tool definition boilerplate
  4. AI Integration: 95% of tool discoveries result in successful execution
  5. Framework Adoption: Used by 80%+ of new MCP server implementations

This creates a competitive advantage by making PulseEngine MCP the most AI-agent-friendly framework available, addressing the key 2025 requirement for intelligent tool composition.

Priority: High - Key differentiator for AI agent integration
Effort: High - Novel system requiring careful design
Impact: High - Major competitive advantage and AI agent enablement

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions