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
//! Completion operations for MCP client
//!
//! This module provides autocompletion functionality for prompts and resources,
//! supporting the MCP completion protocol with context and argument validation.
use std::sync::atomic::Ordering;
use turbomcp_protocol::types::{
ArgumentInfo, CompleteRequestParams, CompleteResult, CompletionContext, CompletionReference,
CompletionResponse, PromptReferenceData, ResourceTemplateReferenceData,
};
use turbomcp_protocol::{Error, Result};
impl<T: turbomcp_transport::Transport + 'static> super::super::core::Client<T> {
/// Internal helper for completion operations - DRYed up common logic
async fn complete_internal(
&self,
argument_name: &str,
argument_value: &str,
reference: CompletionReference,
context: Option<CompletionContext>,
) -> Result<CompletionResponse> {
if !self.inner.initialized.load(Ordering::Relaxed) {
return Err(Error::invalid_request("Client not initialized"));
}
let request_params = CompleteRequestParams {
argument: ArgumentInfo {
name: argument_name.to_string(),
value: argument_value.to_string(),
},
reference,
context,
};
let serialized_params = serde_json::to_value(&request_params)?;
let result: CompleteResult = self
.inner
.protocol
.request("completion/complete", Some(serialized_params))
.await?;
Ok(CompletionResponse {
completion: result.completion,
_meta: result._meta,
})
}
/// Request completion suggestions from the server
///
/// Simple completion interface for basic autocompletion needs.
/// Uses a prompt-based reference with hardcoded "partial" argument name.
///
/// # Arguments
///
/// * `handler_name` - The completion handler name
/// * `argument_value` - The partial value to complete
///
/// # Examples
///
/// ```rust,no_run
/// # use turbomcp_client::Client;
/// # use turbomcp_transport::stdio::StdioTransport;
/// # async fn example() -> turbomcp_protocol::Result<()> {
/// let mut client = Client::new(StdioTransport::new());
/// client.initialize().await?;
///
/// let result = client.complete("complete_path", "/usr/b").await?;
/// println!("Completions: {:?}", result.completion.values);
/// # Ok(())
/// # }
/// ```
pub async fn complete(
&self,
handler_name: &str,
argument_value: &str,
) -> Result<CompletionResponse> {
let reference = CompletionReference::Prompt(PromptReferenceData {
name: handler_name.to_string(),
title: None,
});
self.complete_internal("partial", argument_value, reference, None)
.await
}
/// Complete a prompt argument with full MCP protocol support
///
/// This method provides access to the complete MCP completion protocol,
/// allowing specification of argument names, prompt references, and context.
///
/// # Arguments
///
/// * `prompt_name` - Name of the prompt to complete for
/// * `argument_name` - Name of the argument being completed
/// * `argument_value` - Current value for completion matching
/// * `context` - Optional context with previously resolved arguments
///
/// # Examples
///
/// ```rust,no_run
/// # use turbomcp_client::Client;
/// # use turbomcp_transport::stdio::StdioTransport;
/// # use turbomcp_protocol::types::CompletionContext;
/// # use std::collections::HashMap;
/// # async fn example() -> turbomcp_protocol::Result<()> {
/// let mut client = Client::new(StdioTransport::new());
/// client.initialize().await?;
///
/// // Complete with context
/// let mut context_args = HashMap::new();
/// context_args.insert("language".to_string(), "rust".to_string());
/// let context = CompletionContext { arguments: Some(context_args) };
///
/// let completions = client.complete_prompt(
/// "code_review",
/// "framework",
/// "tok",
/// Some(context)
/// ).await?;
///
/// for completion in completions.completion.values {
/// println!("Suggestion: {}", completion);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn complete_prompt(
&self,
prompt_name: &str,
argument_name: &str,
argument_value: &str,
context: Option<CompletionContext>,
) -> Result<CompletionResponse> {
let reference = CompletionReference::Prompt(PromptReferenceData {
name: prompt_name.to_string(),
title: None,
});
self.complete_internal(argument_name, argument_value, reference, context)
.await
}
/// Complete a resource template URI with full MCP protocol support
///
/// This method provides completion for resource template URIs, allowing
/// servers to suggest values for URI template variables.
///
/// # Arguments
///
/// * `resource_uri` - Resource template URI (e.g., "/files/{path}")
/// * `argument_name` - Name of the argument being completed
/// * `argument_value` - Current value for completion matching
/// * `context` - Optional context with previously resolved arguments
///
/// # Examples
///
/// ```rust,no_run
/// # use turbomcp_client::Client;
/// # use turbomcp_transport::stdio::StdioTransport;
/// # async fn example() -> turbomcp_protocol::Result<()> {
/// let mut client = Client::new(StdioTransport::new());
/// client.initialize().await?;
///
/// let completions = client.complete_resource(
/// "/files/{path}",
/// "path",
/// "/home/user/doc",
/// None
/// ).await?;
///
/// for completion in completions.completion.values {
/// println!("Path suggestion: {}", completion);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn complete_resource(
&self,
resource_uri: &str,
argument_name: &str,
argument_value: &str,
context: Option<CompletionContext>,
) -> Result<CompletionResponse> {
let reference = CompletionReference::ResourceTemplate(ResourceTemplateReferenceData {
uri: resource_uri.to_string(),
});
self.complete_internal(argument_name, argument_value, reference, context)
.await
}
}