turbomcp_protocol/context/
completion.rs

1//! Completion context types for autocompletion and suggestion systems.
2//!
3//! This module contains types for handling completion requests across various
4//! MCP contexts including prompts, resource templates, and tools.
5
6use std::collections::HashMap;
7
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11/// Context for completion/autocompletion requests
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct CompletionContext {
14    /// Unique completion request ID
15    pub completion_id: String,
16    /// Reference being completed (prompt, resource template, etc.)
17    pub completion_ref: CompletionReference,
18    /// Current argument being completed
19    pub argument_name: Option<String>,
20    /// Partial value being completed
21    pub partial_value: Option<String>,
22    /// Previously resolved arguments
23    pub resolved_arguments: HashMap<String, String>,
24    /// Available completion options
25    pub completions: Vec<CompletionOption>,
26    /// Cursor position for completion
27    pub cursor_position: Option<usize>,
28    /// Maximum number of completions to return
29    pub max_completions: Option<usize>,
30    /// Whether more completions are available
31    pub has_more: bool,
32    /// Total number of available completions
33    pub total_completions: Option<usize>,
34    /// Client capabilities for completion
35    pub client_capabilities: Option<CompletionCapabilities>,
36    /// Completion metadata
37    pub metadata: HashMap<String, serde_json::Value>,
38}
39
40/// Client capabilities for completion
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct CompletionCapabilities {
43    /// Supports paginated completions
44    pub supports_pagination: bool,
45    /// Supports fuzzy matching
46    pub supports_fuzzy: bool,
47    /// Maximum batch size
48    pub max_batch_size: usize,
49    /// Supports rich completion items with descriptions
50    pub supports_descriptions: bool,
51}
52
53/// Reference type for completion context
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub enum CompletionReference {
56    /// Completing a prompt argument
57    Prompt {
58        /// Prompt name
59        name: String,
60        /// Argument being completed
61        argument: String,
62    },
63    /// Completing a resource template parameter
64    ResourceTemplate {
65        /// Template name
66        name: String,
67        /// Parameter being completed
68        parameter: String,
69    },
70    /// Completing a tool argument
71    Tool {
72        /// Tool name
73        name: String,
74        /// Argument being completed
75        argument: String,
76    },
77    /// Custom completion context
78    Custom {
79        /// Custom reference type
80        ref_type: String,
81        /// Reference metadata
82        metadata: HashMap<String, serde_json::Value>,
83    },
84}
85
86/// Completion option with metadata
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct CompletionOption {
89    /// Completion value
90    pub value: String,
91    /// Human-readable label
92    pub label: Option<String>,
93    /// Completion type (value, keyword, function, etc.)
94    pub completion_type: Option<String>,
95    /// Additional documentation
96    pub documentation: Option<String>,
97    /// Sort priority (lower = higher priority)
98    pub sort_priority: Option<i32>,
99    /// Whether this option requires additional input
100    pub insert_text: Option<String>,
101}
102
103impl CompletionContext {
104    /// Create a new completion context
105    pub fn new(completion_ref: CompletionReference) -> Self {
106        Self {
107            completion_id: Uuid::new_v4().to_string(),
108            completion_ref,
109            argument_name: None,
110            partial_value: None,
111            resolved_arguments: HashMap::new(),
112            completions: Vec::new(),
113            cursor_position: None,
114            max_completions: Some(100),
115            has_more: false,
116            total_completions: None,
117            client_capabilities: None,
118            metadata: HashMap::new(),
119        }
120    }
121
122    /// Add a completion option
123    pub fn add_completion(&mut self, option: CompletionOption) {
124        self.completions.push(option);
125    }
126
127    /// Set resolved arguments
128    pub fn with_resolved_arguments(mut self, args: HashMap<String, String>) -> Self {
129        self.resolved_arguments = args;
130        self
131    }
132}