CompletionDefinition

Trait CompletionDefinition 

Source
pub trait CompletionDefinition:
    HasCompletionMetadata
    + HasCompletionContext
    + HasCompletionHandling {
    // Provided method
    fn to_complete_request(&self) -> CompleteRequest { ... }
}
Expand description

Complete MCP Completion Creation - Build intelligent autocomplete providers for enhanced UX.

This trait represents a complete, working MCP completion provider that enhances client user experience with context-aware suggestions. When you implement the required traits, you automatically get CompletionDefinition for free via blanket implementation.

§What This Enables

Completions implementing CompletionDefinition provide smart autocomplete that:

  • 🎯 Context-aware suggestions based on current input
  • Real-time completion as users type
  • 🔍 Filtered results matching partial input
  • 📝 Enhanced UX for resource URIs, tool names, argument values

§Complete Working Example

// This struct will automatically implement CompletionDefinition!
struct ResourceUriCompletion {
    available_resources: Vec<String>,
}

impl HasCompletionMetadata for ResourceUriCompletion {
    fn reference(&self) -> &CompletionReference {
        static REF: std::sync::OnceLock<CompletionReference> = std::sync::OnceLock::new();
        REF.get_or_init(|| CompletionReference::Resource { uri: "resource://".to_string() })
    }

    fn argument(&self) -> &CompleteArgument {
        static ARG: std::sync::OnceLock<CompleteArgument> = std::sync::OnceLock::new();
        ARG.get_or_init(|| CompleteArgument { name: "uri".to_string(), value: String::new() })
    }
}

impl HasCompletionContext for ResourceUriCompletion {
    fn context(&self) -> Option<&CompletionContext> { None }
}

impl HasCompletionHandling for ResourceUriCompletion {
    // Implementation provides intelligent URI completion
}

// 🎉 ResourceUriCompletion now automatically implements CompletionDefinition!
// Clients get smart autocomplete for resource URIs as they type

§Real-World Completion Ideas

  • Resource Completions: File paths, API endpoints, database URIs
  • Tool Completions: Available tool names, common argument patterns
  • Data Completions: Enum values, configuration options, format choices
  • Context Completions: User names, project names, environment configs

§How It Works in MCP

  1. Registration: Completion providers register during server startup
  2. User Input: Client detects user typing in completion-enabled fields
  3. Request: Client calls completion/complete with current context
  4. Processing: Your completion logic generates relevant suggestions
  5. Filtering: Framework filters suggestions based on partial input
  6. Display: Client shows intelligent autocomplete dropdown to user

The framework handles real-time triggering, context passing, and result filtering!

Provided Methods§

Source

fn to_complete_request(&self) -> CompleteRequest

Convert this completion definition to a protocol CompleteRequest

Implementors§