1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! # Completion Handlers (MCP 2025-11-25)
//!
//! Completion handler for the completion/complete request type.
use do_memory_mcp::jsonrpc::{JsonRpcError, JsonRpcRequest, JsonRpcResponse};
use serde_json::json;
use tracing::{error, info};
use super::super::types::{CompletionParams, CompletionResult, CompletionValues};
/// Handle completion/complete request (MCP 2025-11-25)
pub async fn handle_completion_complete(request: JsonRpcRequest) -> Option<JsonRpcResponse> {
// Notifications must not produce a response
request.id.as_ref()?;
info!("Handling completion/complete request");
// Parse completion params
let params: CompletionParams = match request.params {
Some(params) => match serde_json::from_value(params) {
Ok(p) => p,
Err(e) => {
return Some(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id,
result: None,
error: Some(JsonRpcError {
code: -32602,
message: "Invalid params".to_string(),
data: Some(json!({"details": e.to_string()})),
}),
});
}
},
None => {
return Some(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id,
result: None,
error: Some(JsonRpcError {
code: -32602,
message: "Missing params".to_string(),
data: None,
}),
});
}
};
// Generate completions based on reference type and argument
let completions = generate_completions(¶ms);
let result = CompletionResult {
completion: completions,
};
match serde_json::to_value(result) {
Ok(value) => Some(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id,
result: Some(value),
error: None,
}),
Err(e) => {
error!("Failed to serialize completion response: {}", e);
Some(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: request.id,
result: None,
error: Some(JsonRpcError {
code: -32603,
message: "Internal error".to_string(),
data: Some(json!({"details": format!("Response serialization failed: {}", e)})),
}),
})
}
}
}
/// Generate completions based on reference type and argument value
fn generate_completions(params: &CompletionParams) -> CompletionValues {
let argument_value = params.argument.value.clone();
// Get context arguments if available
let _context_args = params.context.as_ref().map(|c| &c.arguments);
// Parse the reference Value to extract prompt name or resource URI
// External tagging format: {"ref/prompt": {"name": "..."}} or {"ref/resource": {"uri": "..."}}
let prompt_name = if let Some(prompt_ref) = params
.reference
.as_object()
.and_then(|o| o.get("ref/prompt"))
{
prompt_ref
.get("name")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
} else {
None
};
// Generate domain completions for query_memory tool
if let Some(ref name) = prompt_name {
// Handle prompt argument completions
match name.as_str() {
"query_memory" => {
// Common domains for query_memory
let domains = [
"web-api",
"data-processing",
"code-generation",
"debugging",
"refactoring",
"testing",
"analysis",
"documentation",
"infrastructure",
"security",
];
let filtered: Vec<String> = domains
.iter()
.filter(|d| d.starts_with(&argument_value))
.map(|s| s.to_string())
.collect();
return CompletionValues {
values: filtered.clone(),
total: Some(filtered.len() as u64),
has_more: Some(false),
};
}
"analyze_patterns" => {
// Task types for analyze_patterns
let task_types = [
"code_generation",
"debugging",
"refactoring",
"testing",
"analysis",
"documentation",
];
let filtered: Vec<String> = task_types
.iter()
.filter(|t| t.starts_with(&argument_value))
.map(|s| s.to_string())
.collect();
return CompletionValues {
values: filtered.clone(),
total: Some(filtered.len() as u64),
has_more: Some(false),
};
}
"advanced_pattern_analysis" => {
// Analysis types
let analysis_types = ["statistical", "predictive", "comprehensive"];
let filtered: Vec<String> = analysis_types
.iter()
.filter(|a| a.starts_with(&argument_value))
.map(|s| s.to_string())
.collect();
return CompletionValues {
values: filtered.clone(),
total: Some(filtered.len() as u64),
has_more: Some(false),
};
}
_ => {
// Generic completions based on argument name
let arg_name = params.argument.name.as_str();
let completions: Vec<&str> = match arg_name {
"domain" => vec!["web-api", "data-processing", "testing"],
"task_type" => vec!["code_generation", "debugging", "refactoring"],
"metric_type" => vec!["all", "performance", "episodes", "system"],
"analysis_type" => vec!["statistical", "predictive", "comprehensive"],
"time_range" => vec!["24h", "7d", "30d", "90d", "all"],
"provider" => vec!["openai", "local", "mistral", "azure", "cohere"],
_ => vec![],
};
let filtered: Vec<String> = completions
.iter()
.filter(|s| s.starts_with(&argument_value))
.map(|s| s.to_string())
.collect();
return CompletionValues {
values: filtered.clone(),
total: Some(filtered.len() as u64),
has_more: Some(false),
};
}
}
}
// Handle resource completions
let _is_resource_ref = params
.reference
.as_object()
.and_then(|o| o.get("ref/resource"))
.is_some();
// For resource URI completions, return empty for now
CompletionValues {
values: vec![],
total: Some(0),
has_more: Some(false),
}
}