Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Traits and helpers for type extraction from request arguments

use crate::Context;
use crate::error::{Error, ErrorCode};
use crate::shared::{ArcSlice, ArcStr};
use crate::types::helpers::TypeCategory;
use crate::types::request::RequestParamsMeta;
use crate::types::{Meta, ProgressToken};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::collections::HashMap;

#[cfg(feature = "tasks")]
use crate::types::RelatedTaskMetadata;

/// Fallback names for a handler whose argument names are not known.
///
/// A handler accepts at most five value-carrying arguments, so the table is
/// never indexed past its end; the extra slots only keep [`ArgNames::get`]
/// total.
const POSITIONAL: [&str; 8] = [
    "arg0", "arg1", "arg2", "arg3", "arg4", "arg5", "arg6", "arg7",
];

/// The fallback name of the `index`-th argument slot.
#[inline]
pub(crate) fn positional_name(index: usize) -> &'static str {
    match POSITIONAL.get(index) {
        Some(name) => name,
        None => "",
    }
}

/// The slot `name` is the fallback name of, if it is one.
#[inline]
pub(crate) fn positional_slot(name: &str) -> Option<usize> {
    POSITIONAL.iter().position(|positional| *positional == name)
}

/// The names of a tool or prompt handler's arguments, in declaration order.
///
/// Arguments are read from the request's `arguments` map **by name**: the
/// *n*-th value-carrying parameter of the handler is looked up under the
/// *n*-th name here. Parameters that come from request metadata instead --
/// [`Meta`], [`Context`], or a DI-injected `Dc<T>` -- carry no name and do not
/// consume a slot.
///
/// The names are also exactly the property names the handler's generated
/// `inputSchema` (or prompt `arguments` list) advertises, which is what keeps
/// what a peer is told to send and what the handler reads from drifting apart.
///
/// A handler registered without declared names -- a bare closure passed to
/// [`crate::App::map_tool`], whose parameter names Rust does not preserve --
/// falls back to the positional `arg0`, `arg1`, ... form, and its generated
/// schema advertises those same names.
///
/// # Examples
///
/// ```
/// use neva::types::ArgNames;
///
/// let names = ArgNames::new(["age", "name"]);
/// assert_eq!(names.get(0), "age");
/// assert_eq!(names.get(1), "name");
///
/// // Undeclared, or past the end of what was declared: positional fallback.
/// assert_eq!(ArgNames::default().get(1), "arg1");
/// assert_eq!(names.get(2), "arg2");
/// ```
#[derive(Debug, Default, Clone)]
pub struct ArgNames {
    /// The declared names, if the handler's are known.
    names: Option<ArcSlice<ArcStr>>,
    /// How many argument slots the handler has, named or not.
    arity: usize,
}

impl ArgNames {
    /// Creates [`ArgNames`] from the handler's declared argument names.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// let names = ArgNames::new(["age", "name"]);
    /// assert_eq!(names.len(), 2);
    /// ```
    #[inline]
    pub fn new<I, S>(names: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let names = names
            .into_iter()
            .map(|name| ArcStr::from(name.into()))
            .collect::<Vec<_>>();
        Self {
            arity: names.len(),
            names: Some(ArcSlice::from(names)),
        }
    }

    /// Creates undeclared [`ArgNames`] for a handler with `arity` argument
    /// slots, which read the positional `arg0`, `arg1`, ... names.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// let names = ArgNames::positional(2);
    /// assert_eq!(names.get(0), "arg0");
    /// assert!(!names.is_declared());
    /// ```
    #[inline]
    pub fn positional(arity: usize) -> Self {
        Self { names: None, arity }
    }

    /// Returns `true` when the handler's own argument names are known, rather
    /// than the positional fallback being in use.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// assert!(ArgNames::new(["city"]).is_declared());
    /// assert!(!ArgNames::positional(1).is_declared());
    /// ```
    #[inline]
    pub fn is_declared(&self) -> bool {
        self.names.is_some()
    }

    /// Returns how many argument slots the handler has, named or not.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// assert_eq!(ArgNames::positional(3).arity(), 3);
    /// assert_eq!(ArgNames::new(["a", "b"]).arity(), 2);
    /// ```
    #[inline]
    pub fn arity(&self) -> usize {
        self.arity
    }

    /// Returns the name of the argument in the `index`-th slot, falling back
    /// to the positional `argN` form when no name was declared for it.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// assert_eq!(ArgNames::new(["city"]).get(0), "city");
    /// assert_eq!(ArgNames::default().get(0), "arg0");
    /// ```
    #[inline]
    pub fn get(&self, index: usize) -> &str {
        self.names
            .as_deref()
            .and_then(|names| names.get(index))
            .map_or_else(|| positional_name(index), |name| &**name)
    }

    /// Returns the number of declared names, or `0` if none were declared.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// assert_eq!(ArgNames::new(["a", "b"]).len(), 2);
    /// assert_eq!(ArgNames::default().len(), 0);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.names.as_deref().map_or(0, <[ArcStr]>::len)
    }

    /// Declares `names` for the handler these [`ArgNames`] describe, keeping
    /// the handler's own arity so a miscounted declaration stays detectable.
    #[inline]
    pub(crate) fn declare<I, S>(&self, names: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            arity: self.arity,
            ..Self::new(names)
        }
    }

    /// The slot `name` belongs to, if it names one.
    ///
    /// The inverse of [`Self::get`]: the declared names when there are any,
    /// the positional fallback otherwise.
    #[inline]
    pub(crate) fn slot_of(&self, name: &str) -> Option<usize> {
        match self.names.as_deref() {
            Some(names) => names.iter().position(|declared| &**declared == name),
            None => positional_slot(name),
        }
        .filter(|slot| *slot < self.arity)
    }

    /// The first name declared more than once, if any.
    ///
    /// Two slots reading the same key is not a mistake that announces itself:
    /// the schema collapses to one property, and both parameters quietly
    /// receive the same value rather than the call failing.
    #[inline]
    pub(crate) fn duplicate(&self) -> Option<&str> {
        let names = self.names.as_deref()?;
        let mut seen = std::collections::HashSet::with_capacity(names.len());
        names
            .iter()
            .map(|name| &**name)
            .find(|name| !seen.insert(*name))
    }

    /// Returns `true` when no names were declared.
    ///
    /// # Examples
    ///
    /// ```
    /// use neva::types::ArgNames;
    ///
    /// assert!(ArgNames::default().is_empty());
    /// assert!(!ArgNames::new(["a"]).is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Represents a payload that needs the type to be extracted from
pub(crate) enum Payload<'a> {
    /// Tool or Prompt argument
    Args(serde_json::Value),

    /// Request metadata ("_meta")
    Meta(&'a Option<RequestParamsMeta>),
}

/// Represents an extraction sources
pub(crate) enum Source {
    /// Tool or Prompt arguments
    Args,
    /// Request metadata ("_meta")
    Meta,
}

/// A trait that type needs to implement to be extractable from [`crate::types::Request`]
pub(crate) trait RequestArgument: Sized {
    type Error;

    /// Extracts a type value from [`Payload`]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error>;

    /// Returns a [`Source`] that the type needs to be extracted from
    #[inline]
    fn source() -> Source {
        Source::Args
    }
}

/// The parts of a request's params that argument extraction reads.
///
/// Implemented by `tools/call` and `prompts/get` params so both share one set
/// of tuple extraction impls.
pub(crate) trait HandlerArgs {
    /// Splits the params into its `arguments` map and its `_meta`.
    fn into_parts(self) -> (Option<HashMap<String, Value>>, Option<RequestParamsMeta>);
}

/// Builds a handler's argument tuple from the params of a `tools/call` or
/// `prompts/get` request.
///
/// This is the extraction counterpart of [`ArgNames`]: `P` carries the values
/// a peer sent, `names` says which name each handler slot reads.
///
/// # Examples
///
/// ```
/// use neva::types::{ArgNames, CallToolRequestParams, FromHandlerArgs};
/// use serde_json::json;
///
/// // A peer may send the arguments in any order -- extraction is by name.
/// let params = CallToolRequestParams::new("greet")
///     .with_args([("age", json!(30)), ("name", json!("John"))]);
///
/// let (name, age) = <(String, i32)>::from_args(params, &ArgNames::new(["name", "age"]))?;
///
/// assert_eq!(name, "John");
/// assert_eq!(age, 30);
/// # Ok::<(), neva::error::Error>(())
/// ```
pub trait FromHandlerArgs<P>: Sized {
    /// Extracts the handler's arguments from `params`.
    fn from_args(params: P, names: &ArgNames) -> Result<Self, Error>;
}

impl<'a> Payload<'a> {
    /// Returns arguments value for type extraction
    #[inline]
    pub(crate) fn expect_args(self) -> serde_json::Value {
        match self {
            Payload::Args(val) => val,
            _ => unreachable!("Expected Args variant"),
        }
    }

    /// Returns an optional [`RequestParamsMeta`] for type extraction
    #[inline]
    pub(crate) fn expect_meta(self) -> &'a Option<RequestParamsMeta> {
        match self {
            Payload::Meta(meta) => meta,
            _ => unreachable!("Expected Meta variant"),
        }
    }
}

impl<T: DeserializeOwned> RequestArgument for T {
    type Error = Error;

    #[inline]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error> {
        let arg = payload.expect_args();
        T::deserialize(arg).map_err(Error::from)
    }
}

impl RequestArgument for Meta<RequestParamsMeta> {
    type Error = Error;

    #[inline]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error> {
        let meta = payload.expect_meta();
        meta.clone()
            .ok_or(Error::new(ErrorCode::InvalidParams, "Missing metadata"))
            .map(Meta)
    }

    #[inline]
    fn source() -> Source {
        Source::Meta
    }
}

impl RequestArgument for Meta<ProgressToken> {
    type Error = Error;

    #[inline]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error> {
        let meta = payload.expect_meta();
        meta.as_ref()
            .and_then(|meta| meta.progress_token.clone())
            .ok_or(Error::new(
                ErrorCode::InvalidParams,
                "Missing progress token",
            ))
            .map(Meta)
    }

    #[inline]
    fn source() -> Source {
        Source::Meta
    }
}

#[cfg(feature = "tasks")]
impl RequestArgument for Meta<RelatedTaskMetadata> {
    type Error = Error;

    #[inline]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error> {
        let meta = payload.expect_meta();
        meta.as_ref()
            .and_then(|meta| meta.task.clone())
            .ok_or(Error::new(
                ErrorCode::InvalidParams,
                "Missing progress token",
            ))
            .map(Meta)
    }

    #[inline]
    fn source() -> Source {
        Source::Meta
    }
}

impl RequestArgument for Context {
    type Error = Error;

    #[inline]
    fn extract(payload: Payload<'_>) -> Result<Self, Self::Error> {
        let meta = payload.expect_meta();
        meta.as_ref()
            .and_then(|meta| meta.context.clone())
            .ok_or(Error::new(ErrorCode::InvalidParams, "Missing MCP context"))
    }

    #[inline]
    fn source() -> Source {
        Source::Meta
    }
}

/// Extracts one handler argument.
///
/// Metadata-sourced types read `meta` and leave `slot` alone; everything else
/// consumes the next name and reads the value a peer sent under it. An absent
/// key is offered to the type as `null`, so an `Option<T>` argument resolves
/// to `None` instead of failing.
///
/// Whether an argument may be omitted is decided by its *type*
/// ([`TypeCategory::is_optional`]), never by whether a synthetic `null`
/// happens to deserialize into it: `serde_json::Value` and `()` accept `null`
/// quite happily, and inferring optionality from that would let a required
/// argument through as `Null` against the schema that declares it required.
#[inline]
pub(crate) fn extract_arg<T: RequestArgument<Error = Error> + TypeCategory>(
    meta: &Option<RequestParamsMeta>,
    args: Option<&HashMap<String, Value>>,
    names: &ArgNames,
    slot: &mut usize,
) -> Result<T, Error> {
    match T::source() {
        Source::Meta => T::extract(Payload::Meta(meta)),
        Source::Args => {
            let name = names.get(*slot);
            *slot += 1;
            match args.and_then(|args| args.get(name)) {
                Some(value) => T::extract(Payload::Args(value.clone())).map_err(|err| {
                    Error::new(
                        ErrorCode::InvalidParams,
                        format!("invalid value for argument `{name}`: {err}"),
                    )
                }),
                None if T::is_optional() => T::extract(Payload::Args(Value::Null)),
                None => Err(Error::new(
                    ErrorCode::InvalidParams,
                    format!("missing required argument `{name}`"),
                )),
            }
        }
    }
}

impl<P: HandlerArgs> FromHandlerArgs<P> for () {
    #[inline]
    fn from_args(_: P, _: &ArgNames) -> Result<Self, Error> {
        Ok(())
    }
}

macro_rules! impl_from_handler_args {
    ($($T:ident),+) => {
        impl<P: HandlerArgs, $($T: RequestArgument<Error = Error> + TypeCategory),+> FromHandlerArgs<P> for ($($T,)+) {
            #[inline]
            fn from_args(params: P, names: &ArgNames) -> Result<Self, Error> {
                let (args, meta) = params.into_parts();
                let args = args.as_ref();
                // Tuple elements are evaluated left to right, so `slot` walks
                // the declared names in the handler's own parameter order.
                let mut slot = 0;
                let tuple = (
                    $(
                        extract_arg::<$T>(&meta, args, names, &mut slot)?,
                    )+
                );
                Ok(tuple)
            }
        }
    };
}

impl_from_handler_args! { T1 }
impl_from_handler_args! { T1, T2 }
impl_from_handler_args! { T1, T2, T3 }
impl_from_handler_args! { T1, T2, T3, T4 }
impl_from_handler_args! { T1, T2, T3, T4, T5 }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_returns_declared_names() {
        let names = ArgNames::new(["age", "name"]);

        assert_eq!(names.get(0), "age");
        assert_eq!(names.get(1), "name");
        assert_eq!(names.len(), 2);
        assert!(!names.is_empty());
    }

    #[test]
    fn it_falls_back_to_positional_names() {
        let names = ArgNames::default();

        assert_eq!(names.get(0), "arg0");
        assert_eq!(names.get(4), "arg4");
        assert!(names.is_empty());
    }

    #[test]
    fn it_falls_back_past_the_declared_names() {
        let names = ArgNames::new(["age"]);

        assert_eq!(names.get(0), "age");
        assert_eq!(names.get(1), "arg1");
    }
}