radkit 0.0.5

Rust AI Agent Development Kit
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//! `OpenAPI` Operation Tool - Individual tool for each API operation
//!
//! This module implements `BaseTool` for individual `OpenAPI` operations.

use crate::tools::openapi::{AuthConfig, HeaderOrQuery, OpenApiSpec};
use crate::tools::{BaseTool, FunctionDeclaration, ToolContext, ToolResult};
use openapiv3::{Operation, Parameter, ParameterSchemaOrContent, ReferenceOr, SchemaKind, Type};
use std::collections::HashMap;
use std::fmt::Write as _;
use std::sync::Arc;

type ExtractedParams = (
    Vec<(String, String)>,
    HashMap<String, String>,
    HashMap<String, String>,
);

/// Individual tool for a single `OpenAPI` operation
///
/// Each operation in the `OpenAPI` spec gets its own tool with typed parameters.
pub struct OpenApiOperationTool {
    /// Operation ID (tool name)
    operation_id: String,
    /// Operation description
    description: String,
    /// HTTP method for this operation
    method: String,
    /// Path for this operation
    path: String,
    /// Reference to the `OpenAPI` spec
    spec: Arc<OpenApiSpec>,
    /// Shared HTTP client
    http_client: Arc<reqwest::Client>,
    /// Authentication configuration
    auth: Option<AuthConfig>,
}

/// Helper function to convert JSON value to string (for simple scalar values)
fn value_to_string(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Null => String::new(),
        _ => value.to_string(),
    }
}

/// Encode a query parameter value according to `OpenAPI` style and explode settings
///
/// Handles arrays and objects according to the `OpenAPI` specification.
/// Returns a vector of (key, value) pairs to support exploded arrays.
fn encode_query_param(
    name: &str,
    value: &serde_json::Value,
    style: &openapiv3::QueryStyle,
    explode: bool,
) -> Vec<(String, String)> {
    use openapiv3::QueryStyle;

    match value {
        // Arrays are encoded based on style and explode
        serde_json::Value::Array(arr) => {
            let string_values: Vec<String> = arr.iter().map(value_to_string).collect();

            match (style, explode) {
                // Form style with explode=true (default): ?status=available&status=sold
                (QueryStyle::Form, true) => string_values
                    .into_iter()
                    .map(|v| (name.to_string(), v))
                    .collect(),

                // Form style with explode=false: ?status=available,sold
                (QueryStyle::Form, false) | (QueryStyle::DeepObject, _) => {
                    vec![(name.to_string(), string_values.join(","))]
                }

                // Space-delimited: ?status=available%20sold
                (QueryStyle::SpaceDelimited, _) => {
                    vec![(name.to_string(), string_values.join(" "))]
                }

                // Pipe-delimited: ?status=available|sold
                (QueryStyle::PipeDelimited, _) => {
                    vec![(name.to_string(), string_values.join("|"))]
                }
            }
        }

        // Objects with DeepObject style: ?obj[key1]=value1&obj[key2]=value2
        serde_json::Value::Object(obj) if matches!(style, QueryStyle::DeepObject) => obj
            .iter()
            .map(|(key, val)| (format!("{name}[{key}]"), value_to_string(val)))
            .collect(),

        // For other cases (scalars, objects with non-DeepObject style), use simple encoding
        _ => vec![(name.to_string(), value_to_string(value))],
    }
}

/// Encode a header parameter value (headers don't support explode in the same way)
fn encode_header_param(name: &str, value: &serde_json::Value) -> (String, String) {
    match value {
        // Arrays are comma-separated
        serde_json::Value::Array(arr) => {
            let string_values: Vec<String> = arr.iter().map(value_to_string).collect();
            (name.to_string(), string_values.join(","))
        }
        // Everything else converts to string
        _ => (name.to_string(), value_to_string(value)),
    }
}

/// Percent-encode a string for use in URL path parameters
///
/// This encodes all characters except unreserved characters (A-Z, a-z, 0-9, -, _, ., ~)
/// according to RFC 3986. This prevents special URL characters like /, ?, #, % from
/// breaking the URL structure or causing security issues.
fn percent_encode_path_param(s: &str) -> String {
    let mut encoded = String::new();
    for c in s.chars() {
        match c {
            // Unreserved characters per RFC 3986 - do not encode
            'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => encoded.push(c),
            // Encode everything else
            _ => {
                let mut buf = [0u8; 4];
                for byte in c.encode_utf8(&mut buf).as_bytes() {
                    let _ = write!(&mut encoded, "%{byte:02X}");
                }
            }
        }
    }
    encoded
}

/// Convert `OpenAPI` Schema to JSON Schema
fn convert_schema_to_json_schema(schema: &openapiv3::Schema) -> serde_json::Value {
    let mut json_schema = serde_json::json!({});

    match &schema.schema_kind {
        SchemaKind::Type(Type::String(string_type)) => {
            json_schema["type"] = serde_json::json!("string");
            if !string_type.enumeration.is_empty() {
                let enum_values: Vec<&str> = string_type
                    .enumeration
                    .iter()
                    .filter_map(|v| v.as_deref())
                    .collect();
                if !enum_values.is_empty() {
                    json_schema["enum"] = serde_json::json!(enum_values);
                }
            }
        }
        SchemaKind::Type(Type::Number(_)) => {
            json_schema["type"] = serde_json::json!("number");
        }
        SchemaKind::Type(Type::Integer(_)) => {
            json_schema["type"] = serde_json::json!("integer");
        }
        SchemaKind::Type(Type::Boolean(_)) => {
            json_schema["type"] = serde_json::json!("boolean");
        }
        SchemaKind::Type(Type::Array(array_type)) => {
            json_schema["type"] = serde_json::json!("array");
            if let Some(items) = &array_type.items {
                match items {
                    ReferenceOr::Item(schema) => {
                        json_schema["items"] = convert_schema_to_json_schema(schema);
                    }
                    ReferenceOr::Reference { reference } => {
                        // Keep the $ref as is for now
                        json_schema["items"] = serde_json::json!({"$ref": reference});
                    }
                }
            }
        }
        SchemaKind::Type(Type::Object(_)) => {
            json_schema["type"] = serde_json::json!("object");
        }
        _ => {
            // For complex types or references, use a generic object
            json_schema["type"] = serde_json::json!("object");
        }
    }

    // Add description if available
    if let Some(description) = &schema.schema_data.description {
        json_schema["description"] = serde_json::json!(description);
    }

    json_schema
}

impl OpenApiOperationTool {
    /// Create a new operation tool
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // Owned Strings/Arcs require heap allocation, so this cannot be const.
    pub fn new(
        operation_id: String,
        description: String,
        method: String,
        path: String,
        spec: Arc<OpenApiSpec>,
        http_client: Arc<reqwest::Client>,
        auth: Option<AuthConfig>,
    ) -> Self {
        Self {
            operation_id,
            description,
            method,
            path,
            spec,
            http_client,
            auth,
        }
    }

    /// Find this operation in the `OpenAPI` spec
    /// Uses the stored path and method for direct lookup
    /// Returns (method, path, `path_item`, operation)
    fn find_operation(&self) -> Option<(String, String, &openapiv3::PathItem, &Operation)> {
        // Get the path item
        let path_item_ref = self.spec.spec().paths.paths.get(&self.path)?;
        let path_item = match path_item_ref {
            ReferenceOr::Item(item) => item,
            ReferenceOr::Reference { .. } => return None,
        };

        // Get the operation for this method
        let operation = match self.method.to_uppercase().as_str() {
            "GET" => path_item.get.as_ref()?,
            "POST" => path_item.post.as_ref()?,
            "PUT" => path_item.put.as_ref()?,
            "DELETE" => path_item.delete.as_ref()?,
            "PATCH" => path_item.patch.as_ref()?,
            "HEAD" => path_item.head.as_ref()?,
            "OPTIONS" => path_item.options.as_ref()?,
            "TRACE" => path_item.trace.as_ref()?,
            _ => return None,
        };

        Some((self.method.clone(), self.path.clone(), path_item, operation))
    }

    /// Merge path-level and operation-level parameters
    ///
    /// Path-level parameters apply to all operations under that path.
    /// Operation-level parameters can override path-level ones.
    /// A parameter is uniquely identified by (name, location) pair.
    fn merge_parameters<'a>(
        path_params: &'a [ReferenceOr<Parameter>],
        operation_params: &'a [ReferenceOr<Parameter>],
    ) -> Vec<&'a ReferenceOr<Parameter>> {
        let mut merged = Vec::new();
        let mut seen = std::collections::HashSet::new();

        // Add operation-level parameters first (they take precedence)
        for param_ref in operation_params {
            if let ReferenceOr::Item(param) = param_ref {
                let key = Self::get_parameter_key(param);
                seen.insert(key);
            }
            merged.push(param_ref);
        }

        // Add path-level parameters that aren't overridden
        for param_ref in path_params {
            if let ReferenceOr::Item(param) = param_ref {
                let key = Self::get_parameter_key(param);
                if !seen.contains(&key) {
                    merged.push(param_ref);
                }
            } else {
                // For references, we can't check uniqueness without resolving,
                // so we include them (may result in duplicates if spec is invalid)
                merged.push(param_ref);
            }
        }

        merged
    }

    /// Get a unique key for a parameter (name + location)
    fn get_parameter_key(param: &Parameter) -> (String, String) {
        let (name, location) = match param {
            Parameter::Query { parameter_data, .. } => {
                (parameter_data.name.clone(), "query".to_string())
            }
            Parameter::Header { parameter_data, .. } => {
                (parameter_data.name.clone(), "header".to_string())
            }
            Parameter::Path { parameter_data, .. } => {
                (parameter_data.name.clone(), "path".to_string())
            }
            Parameter::Cookie { parameter_data, .. } => {
                (parameter_data.name.clone(), "cookie".to_string())
            }
        };
        (name, location)
    }

    /// Build the full URL with path parameters
    fn build_url(&self, path: &str, args: &HashMap<String, serde_json::Value>) -> String {
        let mut url = format!("{}{}", self.spec.base_url(), path);

        // Replace path parameters like {petId} with actual values
        // Use percent-encoding to prevent special characters from breaking the URL structure
        for (key, value) in args {
            let placeholder = format!("{{{key}}}");
            if url.contains(&placeholder) {
                let value_str = value_to_string(value);
                // Percent-encode the value to handle special URL characters (/, ?, #, %, etc.)
                let encoded_value = percent_encode_path_param(&value_str);
                url = url.replace(&placeholder, &encoded_value);
            }
        }

        url
    }

    /// Extract parameters from path and operation definitions
    ///
    /// Merges path-level and operation-level parameters before extraction.
    /// Returns (`query_params`, `header_params`, `cookie_params`).
    /// Query params use Vec to support exploded arrays with duplicate keys.
    fn extract_parameters(
        path_item: &openapiv3::PathItem,
        operation: &Operation,
        args: &HashMap<String, serde_json::Value>,
    ) -> ExtractedParams {
        let mut query_params = Vec::new();
        let mut header_params = HashMap::new();
        let mut cookie_params = HashMap::new();

        // Merge path-level and operation-level parameters
        let all_params = Self::merge_parameters(&path_item.parameters, &operation.parameters);

        for param_ref in all_params {
            let param = match param_ref {
                ReferenceOr::Item(p) => p,
                // TODO(Phase 2): Resolve $ref parameter references
                ReferenceOr::Reference { .. } => continue,
            };

            match param {
                Parameter::Query {
                    parameter_data,
                    style,
                    ..
                } => {
                    if let Some(value) = args.get(&parameter_data.name) {
                        // Determine explode setting (default is true for query params)
                        let explode = parameter_data.explode.unwrap_or(true);

                        // Encode according to OpenAPI style and explode settings
                        let encoded =
                            encode_query_param(&parameter_data.name, value, style, explode);
                        query_params.extend(encoded);
                    }
                }
                Parameter::Header { parameter_data, .. } => {
                    if let Some(value) = args.get(&parameter_data.name) {
                        let (name, encoded_value) =
                            encode_header_param(&parameter_data.name, value);
                        header_params.insert(name, encoded_value);
                    }
                }
                Parameter::Path { .. } => {
                    // Path parameters are handled in build_url
                }
                Parameter::Cookie { parameter_data, .. } => {
                    if let Some(value) = args.get(&parameter_data.name) {
                        cookie_params.insert(parameter_data.name.clone(), value_to_string(value));
                    }
                }
            }
        }

        (query_params, header_params, cookie_params)
    }
}

#[cfg_attr(all(target_os = "wasi", target_env = "p1"), async_trait::async_trait(?Send))]
#[cfg_attr(
    not(all(target_os = "wasi", target_env = "p1")),
    async_trait::async_trait
)]
impl BaseTool for OpenApiOperationTool {
    fn name(&self) -> &str {
        &self.operation_id
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn declaration(&self) -> FunctionDeclaration {
        // Find the operation to extract parameter schema
        let Some((_, _, path_item, operation)) = self.find_operation() else {
            // Fallback to empty schema if operation not found
            return FunctionDeclaration::new(
                self.operation_id.clone(),
                self.description.clone(),
                serde_json::json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            );
        };

        let mut properties = serde_json::Map::new();
        let mut required = Vec::new();

        // Merge path-level and operation-level parameters
        let all_params = Self::merge_parameters(&path_item.parameters, &operation.parameters);

        // Extract parameter schemas from merged parameters
        for param_ref in all_params {
            let param_data = match param_ref {
                ReferenceOr::Item(p) => match p {
                    Parameter::Query { parameter_data, .. }
                    | Parameter::Header { parameter_data, .. }
                    | Parameter::Path { parameter_data, .. }
                    | Parameter::Cookie { parameter_data, .. } => parameter_data,
                },
                // For $ref parameters, we'll skip for now but could resolve later
                ReferenceOr::Reference { .. } => continue,
            };

            // Convert schema
            let param_schema = match &param_data.format {
                ParameterSchemaOrContent::Schema(schema_ref) => match schema_ref {
                    ReferenceOr::Item(schema) => convert_schema_to_json_schema(schema),
                    ReferenceOr::Reference { reference } => {
                        serde_json::json!({"$ref": reference})
                    }
                },
                ParameterSchemaOrContent::Content(_) => {
                    // Content is more complex, default to object for now
                    let mut default_schema = serde_json::json!({"type": "object"});
                    if let Some(desc) = &param_data.description {
                        default_schema["description"] = serde_json::json!(desc);
                    }
                    default_schema
                }
            };

            properties.insert(param_data.name.clone(), param_schema);

            // Mark as required
            if param_data.required {
                required.push(param_data.name.clone());
            }
        }

        // Add request body as "body" parameter for methods that support it
        if let Some(request_body) = &operation.request_body {
            let body_required = match request_body {
                ReferenceOr::Item(rb) => rb.required,
                ReferenceOr::Reference { .. } => false,
            };

            properties.insert(
                "body".to_string(),
                serde_json::json!({
                    "type": "object",
                    "description": "Request body"
                }),
            );

            if body_required {
                required.push("body".to_string());
            }
        }

        FunctionDeclaration::new(
            self.operation_id.clone(),
            self.description.clone(),
            serde_json::json!({
                "type": "object",
                "properties": properties,
                "required": required
            }),
        )
    }

    async fn run_async(
        &self,
        args: HashMap<String, serde_json::Value>,
        _context: &ToolContext<'_>,
    ) -> ToolResult {
        // Find the operation in the spec
        let Some((method, path, path_item, operation)) = self.find_operation() else {
            return ToolResult::error(format!(
                "Operation '{}' not found in OpenAPI spec",
                self.operation_id
            ));
        };

        // Extract parameters (merges path-level and operation-level parameters)
        let (query_params, header_params, cookie_params) =
            Self::extract_parameters(path_item, operation, &args);

        // Build URL with path parameters
        let url = self.build_url(&path, &args);

        // Build the HTTP request
        let mut request_builder = match method.as_str() {
            "GET" => self.http_client.get(&url),
            "POST" => self.http_client.post(&url),
            "PUT" => self.http_client.put(&url),
            "DELETE" => self.http_client.delete(&url),
            "PATCH" => self.http_client.patch(&url),
            "HEAD" => self.http_client.head(&url),
            _ => {
                return ToolResult::error(format!("Unsupported HTTP method: {method}"));
            }
        };

        // Add query parameters
        if !query_params.is_empty() {
            request_builder = request_builder.query(&query_params);
        }

        // Add header parameters
        for (name, value) in header_params {
            request_builder = request_builder.header(name, value);
        }

        // Add cookie parameters
        if !cookie_params.is_empty() {
            let cookie_header = cookie_params
                .iter()
                .map(|(k, v)| format!("{k}={v}"))
                .collect::<Vec<_>>()
                .join("; ");
            request_builder = request_builder.header("Cookie", cookie_header);
        }

        // Add authentication
        if let Some(auth) = &self.auth {
            request_builder = match auth {
                AuthConfig::Basic { username, password } => {
                    request_builder.basic_auth(username, Some(password))
                }
                AuthConfig::ApiKey {
                    location: HeaderOrQuery::Header,
                    name,
                    value,
                } => request_builder.header(name, value),
                AuthConfig::ApiKey {
                    location: HeaderOrQuery::Query,
                    name,
                    value,
                } => request_builder.query(&[(name, value)]),
            };
        }

        // Add request body for POST/PUT/PATCH
        if matches!(method.as_str(), "POST" | "PUT" | "PATCH") {
            if let Some(body) = args.get("body") {
                request_builder = request_builder
                    .header("Content-Type", "application/json")
                    .json(body);
            }
        }

        // Execute the request
        let response = match request_builder.send().await {
            Ok(resp) => resp,
            Err(e) => {
                return ToolResult::error(format!("HTTP request failed: {e}"));
            }
        };

        let status = response.status();
        let status_code = status.as_u16();

        // Read response body
        let body_text = match response.text().await {
            Ok(text) => text,
            Err(e) => {
                return ToolResult::error(format!("Failed to read response body: {e}"));
            }
        };

        // Try to parse as JSON, fallback to plain text
        let result_value = serde_json::from_str::<serde_json::Value>(&body_text).map_or_else(
            |_| {
                serde_json::json!({
                    "status": status_code,
                    "body": body_text.clone(),
                })
            },
            |json| {
                serde_json::json!({
                    "status": status_code,
                    "body": json,
                })
            },
        );

        // Check if the request was successful
        if status.is_success() {
            ToolResult::success(result_value)
        } else {
            ToolResult::error(format!(
                "HTTP {} {}: {}",
                status_code,
                status.canonical_reason().unwrap_or("Unknown"),
                body_text
            ))
        }
    }
}

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

    #[test]
    fn test_build_url_with_special_characters() {
        // Create a minimal OpenApiSpec for testing
        let spec_json = r#"{
            "openapi": "3.0.0",
            "info": {"title": "Test API", "version": "1.0.0"},
            "servers": [{"url": "https://api.example.com"}],
            "paths": {
                "/pets/{petId}": {
                    "get": {
                        "operationId": "test_op",
                        "responses": {
                            "200": {"description": "Success"}
                        }
                    }
                }
            }
        }"#;
        let openapi_spec = Arc::new(
            OpenApiSpec::from_str(spec_json, "https://api.example.com".to_string()).unwrap(),
        );

        let http_client = Arc::new(reqwest::Client::new());

        let tool = OpenApiOperationTool::new(
            "test_op".to_string(),
            "Test operation".to_string(),
            "GET".to_string(),
            "/pets/{petId}".to_string(),
            openapi_spec,
            http_client,
            None,
        );

        // Test with normal value
        let mut args = HashMap::new();
        args.insert("petId".to_string(), serde_json::json!("123"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/123");

        // Test with value containing slash (should be encoded as %2F)
        args.insert("petId".to_string(), serde_json::json!("foo/bar"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%2Fbar");

        // Test with value containing question mark (should be encoded as %3F)
        args.insert("petId".to_string(), serde_json::json!("foo?bar"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%3Fbar");

        // Test with value containing hash (should be encoded as %23)
        args.insert("petId".to_string(), serde_json::json!("foo#bar"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%23bar");

        // Test with value containing percent (should be encoded as %25)
        args.insert("petId".to_string(), serde_json::json!("foo%bar"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%25bar");

        // Test with value containing spaces (should be encoded as %20)
        args.insert("petId".to_string(), serde_json::json!("foo bar"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%20bar");

        // Test with multiple special characters
        args.insert("petId".to_string(), serde_json::json!("foo/bar?baz#qux"));
        let url = tool.build_url("/pets/{petId}", &args);
        assert_eq!(url, "https://api.example.com/pets/foo%2Fbar%3Fbaz%23qux");
    }

    #[test]
    fn test_build_url_with_multiple_path_params() {
        // Create a minimal OpenApiSpec for testing
        let spec_json = r#"{
            "openapi": "3.0.0",
            "info": {"title": "Test API", "version": "1.0.0"},
            "servers": [{"url": "https://api.example.com"}],
            "paths": {
                "/users/{userId}/pets/{petId}": {
                    "get": {
                        "operationId": "test_op",
                        "responses": {
                            "200": {"description": "Success"}
                        }
                    }
                }
            }
        }"#;
        let openapi_spec = Arc::new(
            OpenApiSpec::from_str(spec_json, "https://api.example.com".to_string()).unwrap(),
        );

        let http_client = Arc::new(reqwest::Client::new());

        let tool = OpenApiOperationTool::new(
            "test_op".to_string(),
            "Test operation".to_string(),
            "GET".to_string(),
            "/users/{userId}/pets/{petId}".to_string(),
            openapi_spec,
            http_client,
            None,
        );

        let mut args = HashMap::new();
        args.insert("userId".to_string(), serde_json::json!("user/123"));
        args.insert("petId".to_string(), serde_json::json!("pet?456"));

        let url = tool.build_url("/users/{userId}/pets/{petId}", &args);
        assert_eq!(
            url,
            "https://api.example.com/users/user%2F123/pets/pet%3F456"
        );
    }

    #[test]
    fn test_path_level_parameters() {
        // Test that path-level parameters are included in the tool declaration
        let spec_json = r#"{
            "openapi": "3.0.0",
            "info": {"title": "Test API", "version": "1.0.0"},
            "servers": [{"url": "https://api.example.com"}],
            "paths": {
                "/pets/{petId}": {
                    "parameters": [
                        {
                            "name": "petId",
                            "in": "path",
                            "required": true,
                            "schema": {"type": "string"}
                        },
                        {
                            "name": "version",
                            "in": "query",
                            "required": false,
                            "schema": {"type": "string"}
                        }
                    ],
                    "get": {
                        "operationId": "getPet",
                        "parameters": [
                            {
                                "name": "includeDetails",
                                "in": "query",
                                "required": false,
                                "schema": {"type": "boolean"}
                            }
                        ],
                        "responses": {
                            "200": {"description": "Success"}
                        }
                    }
                }
            }
        }"#;
        let openapi_spec = Arc::new(
            OpenApiSpec::from_str(spec_json, "https://api.example.com".to_string()).unwrap(),
        );

        let http_client = Arc::new(reqwest::Client::new());

        let tool = OpenApiOperationTool::new(
            "getPet".to_string(),
            "Get a pet".to_string(),
            "GET".to_string(),
            "/pets/{petId}".to_string(),
            openapi_spec,
            http_client,
            None,
        );

        // Get the declaration and check it includes both path and operation parameters
        let declaration = tool.declaration();
        let schema = declaration.parameters();

        // Should have petId (path-level), version (path-level), and includeDetails (operation-level)
        let properties = schema.get("properties").unwrap().as_object().unwrap();
        assert!(
            properties.contains_key("petId"),
            "Missing path parameter petId"
        );
        assert!(
            properties.contains_key("version"),
            "Missing path-level query parameter version"
        );
        assert!(
            properties.contains_key("includeDetails"),
            "Missing operation-level query parameter includeDetails"
        );

        // Check required parameters
        let required = schema.get("required").unwrap().as_array().unwrap();
        assert!(
            required.contains(&serde_json::json!("petId")),
            "petId should be required"
        );
        assert!(
            !required.contains(&serde_json::json!("version")),
            "version should not be required"
        );
        assert!(
            !required.contains(&serde_json::json!("includeDetails")),
            "includeDetails should not be required"
        );
    }

    #[test]
    fn test_operation_parameters_override_path_parameters() {
        // Test that operation-level parameters override path-level ones
        let spec_json = r#"{
            "openapi": "3.0.0",
            "info": {"title": "Test API", "version": "1.0.0"},
            "servers": [{"url": "https://api.example.com"}],
            "paths": {
                "/items/{itemId}": {
                    "parameters": [
                        {
                            "name": "itemId",
                            "in": "path",
                            "required": true,
                            "schema": {"type": "string"},
                            "description": "Path-level description"
                        },
                        {
                            "name": "format",
                            "in": "query",
                            "required": false,
                            "schema": {"type": "string", "enum": ["json", "xml"]}
                        }
                    ],
                    "get": {
                        "operationId": "getItem",
                        "parameters": [
                            {
                                "name": "format",
                                "in": "query",
                                "required": true,
                                "schema": {"type": "string", "enum": ["json", "xml", "yaml"]},
                                "description": "Operation-level override"
                            }
                        ],
                        "responses": {
                            "200": {"description": "Success"}
                        }
                    }
                }
            }
        }"#;
        let openapi_spec = Arc::new(
            OpenApiSpec::from_str(spec_json, "https://api.example.com".to_string()).unwrap(),
        );

        let http_client = Arc::new(reqwest::Client::new());

        let tool = OpenApiOperationTool::new(
            "getItem".to_string(),
            "Get an item".to_string(),
            "GET".to_string(),
            "/items/{itemId}".to_string(),
            openapi_spec,
            http_client,
            None,
        );

        let declaration = tool.declaration();
        let schema = declaration.parameters();
        let properties = schema.get("properties").unwrap().as_object().unwrap();

        // Should have both itemId and format
        assert_eq!(properties.len(), 2, "Should have exactly 2 parameters");

        // format should be required (from operation-level override)
        let required = schema.get("required").unwrap().as_array().unwrap();
        assert!(
            required.contains(&serde_json::json!("format")),
            "format should be required (operation-level override)"
        );

        // The enum should have yaml (from operation-level, not path-level)
        let format_schema = properties.get("format").unwrap();
        let format_enum = format_schema.get("enum").unwrap().as_array().unwrap();
        assert!(
            format_enum.contains(&serde_json::json!("yaml")),
            "format enum should include 'yaml' from operation-level parameter"
        );
    }

    #[test]
    fn test_encode_query_param_array_form_explode() {
        use openapiv3::QueryStyle;

        // Array with form style and explode=true (default)
        let value = serde_json::json!(["available", "sold"]);
        let result = encode_query_param("status", &value, &QueryStyle::Form, true);

        assert_eq!(result.len(), 2);
        assert_eq!(result[0], ("status".to_string(), "available".to_string()));
        assert_eq!(result[1], ("status".to_string(), "sold".to_string()));
    }

    #[test]
    fn test_encode_query_param_array_form_no_explode() {
        use openapiv3::QueryStyle;

        // Array with form style and explode=false
        let value = serde_json::json!(["available", "sold"]);
        let result = encode_query_param("status", &value, &QueryStyle::Form, false);

        assert_eq!(result.len(), 1);
        assert_eq!(
            result[0],
            ("status".to_string(), "available,sold".to_string())
        );
    }

    #[test]
    fn test_encode_query_param_array_pipe_delimited() {
        use openapiv3::QueryStyle;

        // Array with pipe-delimited style
        let value = serde_json::json!(["red", "green", "blue"]);
        let result = encode_query_param("colors", &value, &QueryStyle::PipeDelimited, false);

        assert_eq!(result.len(), 1);
        assert_eq!(
            result[0],
            ("colors".to_string(), "red|green|blue".to_string())
        );
    }

    #[test]
    fn test_encode_query_param_array_space_delimited() {
        use openapiv3::QueryStyle;

        // Array with space-delimited style
        let value = serde_json::json!(["red", "green", "blue"]);
        let result = encode_query_param("colors", &value, &QueryStyle::SpaceDelimited, false);

        assert_eq!(result.len(), 1);
        assert_eq!(
            result[0],
            ("colors".to_string(), "red green blue".to_string())
        );
    }

    #[test]
    fn test_encode_query_param_deep_object() {
        use openapiv3::QueryStyle;

        // Object with DeepObject style
        let value = serde_json::json!({"name": "John", "age": "30"});
        let result = encode_query_param("user", &value, &QueryStyle::DeepObject, true);

        assert_eq!(result.len(), 2);
        // Order may vary, so check both possibilities
        assert!(
            result.contains(&("user[name]".to_string(), "John".to_string()))
                || result.contains(&("user[age]".to_string(), "30".to_string()))
        );
    }

    #[test]
    fn test_encode_query_param_scalar() {
        use openapiv3::QueryStyle;

        // Scalar values are encoded simply
        let value = serde_json::json!("available");
        let result = encode_query_param("status", &value, &QueryStyle::Form, true);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("status".to_string(), "available".to_string()));
    }

    #[test]
    fn test_encode_header_param_array() {
        // Arrays in headers are comma-separated
        let value = serde_json::json!(["gzip", "deflate"]);
        let (name, encoded) = encode_header_param("Accept-Encoding", &value);

        assert_eq!(name, "Accept-Encoding");
        assert_eq!(encoded, "gzip,deflate");
    }

    #[test]
    fn test_encode_header_param_scalar() {
        let value = serde_json::json!("application/json");
        let (name, encoded) = encode_header_param("Content-Type", &value);

        assert_eq!(name, "Content-Type");
        assert_eq!(encoded, "application/json");
    }
}