rig_mcp_server/completion.rs
1//! Argument completion: what a completer is asked, and the boxed error it may return.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use futures::future::BoxFuture;
7
8/// The error a completer returns, boxed. It is logged by kind only and never
9/// sent to the client.
10pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
11
12/// The function a server awaits for `completion/complete`.
13pub(crate) type Completer = Arc<
14 dyn Fn(CompletionRequest) -> BoxFuture<'static, Result<Vec<String>, BoxError>> + Send + Sync,
15>;
16
17/// What a client asks completions for.
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[non_exhaustive]
20pub enum CompletionTarget {
21 /// An argument of the prompt with this name.
22 Prompt(String),
23 /// A resource, by the URI the server serves it at, or a resource template,
24 /// by its URI template (such as `memo://{id}`); the argument is then a variable.
25 Resource(String),
26}
27
28/// One `completion/complete` request, as a completer sees it.
29///
30/// Build one with [`CompletionRequest::new`] to call a completer directly in a
31/// unit test.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct CompletionRequest {
35 /// The prompt or resource the argument belongs to.
36 pub target: CompletionTarget,
37 /// The name of the argument being completed.
38 pub argument: String,
39 /// What the client has typed for the argument so far.
40 pub value: String,
41 /// The arguments the client has already resolved, by name. Empty when the
42 /// client sent no context.
43 pub context: HashMap<String, String>,
44}
45
46impl CompletionRequest {
47 /// A request for `argument` of `target`, with the value typed so far and the
48 /// arguments already resolved.
49 pub fn new(
50 target: CompletionTarget,
51 argument: impl Into<String>,
52 value: impl Into<String>,
53 context: HashMap<String, String>,
54 ) -> Self {
55 Self {
56 target,
57 argument: argument.into(),
58 value: value.into(),
59 context,
60 }
61 }
62}