adk-tool 3.0.0

Tool system for Rust Agent Development Kit (ADK-Rust) agents (FunctionTool, MCP, Google Search)
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
//! An MCP server that publishes a set of [`adk_core::Tool`]s over Streamable HTTP, with an MCP
//! Server Card (spec 3.5 §7.1, §7.4, §7.9; SEP-2127). **Experimental**: the Server Card
//! extension is itself experimental upstream.
//!
//! One declaration serves every binding: each tool's name, description, parameters schema and
//! [`ToolEffect`] become its MCP tool definition and annotations, the same tool the agent edge
//! exposes as an A2A skill and an AWP endpoint (spec 3.5 §7.4).
//!
//! | Part | Type | Role |
//! |------|------|------|
//! | Server | [`McpServer`] | Lists the tools and answers `tools/call`; an rmcp `ServerHandler` |
//! | Executor | [`McpCallExecutor`] | Runs or refuses every call; by default a [`GovernedExecutor`], which runs a call only when its tool policy permits it |
//! | Tasks | [`McpTaskHost`] | Answers `tasks/get`, `tasks/update` and `tasks/cancel` for calls the executor turned into tasks (SEP-2663); none by default |
//! | Caller | [`McpCallerResolver`] | Turns each HTTP request into the [`McpCaller`] the executor sees |
//! | Router | [`McpRouterBuilder`] | The `axum::Router`: Streamable HTTP, the Server Card at `{endpoint}/server-card` (SEP-2127) with its `/.well-known/mcp/server-card.json` alias (spec 3.5 §7.9), and `/.well-known/ai-catalog.json` |
//! | Card | [`ServerCard`], [`AiCatalog`] | Discovery documents to the SEP-2127 schema |
//!
//! # Example
//!
//! ```rust,no_run
//! use std::sync::Arc;
//!
//! use adk_core::{DeclarativePolicy, RunConfig, Url};
//! use adk_tool::FunctionTool;
//! use adk_tool::mcp::server::{McpRouterBuilder, McpServer};
//! use serde_json::json;
//!
//! # async fn serve() -> Result<(), Box<dyn std::error::Error>> {
//! let quote = Arc::new(
//!     FunctionTool::new("get_quote", "Quotes a solar installation", |_ctx, args| async move {
//!         Ok(json!({ "kw": args["kw"], "price": 4200 }))
//!     })
//!     .with_read_only(true),
//! );
//! let server = McpServer::builder("com.example/solar", "1.0.0")
//!     .description("Quotes for Example Solar installations")
//!     .tool(quote)
//!     .tool_governance(
//!         RunConfig::builder()
//!             .tool_policy(Arc::new(DeclarativePolicy::builder().allow_read_only().build()))
//!             .build(),
//!     )
//!     .build()?;
//! let router = McpRouterBuilder::new(server)
//!     .public_url(Url::parse("http://127.0.0.1:8080/mcp")?)
//!     .build()?;
//! let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await?;
//! axum::serve(listener, router).await?;
//! # Ok(())
//! # }
//! ```

mod caller;
mod card;
mod executor;
mod http;
mod task;

pub use caller::{AnonymousCallers, McpCaller, McpCallerResolver, McpHttpRequest};
pub use card::{
    AI_CATALOG_MEDIA_TYPE, AI_CATALOG_SPEC_VERSION, AI_CATALOG_WELL_KNOWN_PATH, AiCatalog,
    AiCatalogContent, AiCatalogEntry, AiCatalogHost, IconTheme, InputFormat, RemoteTransport,
    SERVER_CARD_ENDPOINT_SUFFIX, SERVER_CARD_MEDIA_TYPE, SERVER_CARD_SCHEMA_URL,
    SERVER_CARD_WELL_KNOWN_PATH, ServerCard, ServerCardHeader, ServerCardIcon, ServerCardInput,
    ServerCardRemote, ServerCardRepository,
};
pub use executor::{
    GovernedExecutor, McpCallExecutor, McpToolCall, McpToolOutcome, UngovernedExecutor,
};
pub use http::{McpRouterBuilder, McpSessions};
pub use task::{McpRequestPeer, McpTaskHost};

use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use adk_core::{
    AdkError, ErrorCategory, ErrorComponent, Result, RetryHint, RunConfig,
    TOOL_ERROR_MESSAGE_LIMIT, Tool, ToolEffect, Url, tool_error_response,
};
use rmcp::model::{
    CallToolRequestParams, CallToolResponse, CallToolResult, CancelTaskParams, ContentBlock,
    CreateTaskResult, ErrorCode, ErrorData, GetTaskParams, GetTaskResult, Implementation,
    InitializeResult, InputRequiredResult, JsonObject, ListToolsResult, PaginatedRequestParams,
    ProtocolVersion, ServerCapabilities, ServerConfig, ToolAnnotations, UpdateTaskParams,
};
use rmcp::service::RequestContext;
use rmcp::{RoleServer, ServerHandler};
use serde_json::{Value, json};

use crate::ToolArgumentValidator;

/// The default bound on one call's run time.
pub const DEFAULT_CALL_TIMEOUT: Duration = Duration::from_secs(60);

/// The longest tool name a refusal quotes back to the caller.
const QUOTED_NAME_LIMIT: usize = 128;

/// An MCP server publishing a fixed set of [`Tool`]s (spec 3.5 §7.9).
///
/// `tools/list` describes each tool from its own declaration: name, description, parameters
/// schema as `inputSchema`, an object `response_schema` as `outputSchema`, and annotations
/// from [`Tool::effect`]:
///
/// | Effect | `readOnlyHint` | `idempotentHint` | `destructiveHint` |
/// |--------|----------------|------------------|-------------------|
/// | `ReadOnly` | `true` | `true` | `false` |
/// | `Idempotent` | `false` | `true` | the tool's own hint, else `true` |
/// | `NonIdempotent` | `false` | `false` | the tool's own hint, else `true` |
///
/// `openWorldHint` and `title` come from the tool's [`annotations`](Tool::annotations), else
/// the server's [`open_world`](McpServerBuilder::open_world) default. `tools/call` checks the
/// arguments against the parameters schema, then hands the call to the
/// [`McpCallExecutor`] under the [call timeout](McpServerBuilder::call_timeout).
///
/// The default executor is a [`GovernedExecutor`]: each call goes through
/// `adk_core::authorize_tool_call` under the config passed to
/// [`tool_governance`](McpServerBuilder::tool_governance), and a server given none refuses
/// every call.
///
/// The server is an rmcp `ServerHandler`, so it also serves stdio; there every caller is the
/// [local caller](McpServerBuilder::local_caller), anonymous unless set.
/// [`McpRouterBuilder`] serves it over Streamable HTTP.
///
/// With a [task host](McpServerBuilder::task_host) the server declares the tasks extension
/// (SEP-2663): the executor may answer a call from a client that declared it with
/// [`McpToolOutcome::Task`], and `tasks/get`, `tasks/update` and `tasks/cancel` go to the host.
/// An executor may answer a client on MCP `2026-07-28` or later with
/// [`McpToolOutcome::InputRequired`] (SEP-2322), and receives the answers with the retried
/// call.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
///
/// use adk_tool::FunctionTool;
/// use adk_tool::mcp::server::McpServer;
/// use serde_json::json;
///
/// let server = McpServer::builder("com.example/solar", "1.0.0")
///     .description("Quotes for Example Solar installations")
///     .tool(Arc::new(
///         FunctionTool::new("get_quote", "Quotes an installation", |_ctx, _args| async {
///             Ok(json!({ "price": 4200 }))
///         })
///         .with_read_only(true),
///     ))
///     .build()?;
/// let listed = server.tool_definitions();
/// assert_eq!(listed[0].name, "get_quote");
/// assert_eq!(listed[0].annotations.as_ref().unwrap().read_only_hint, Some(true));
/// # Ok::<(), adk_core::AdkError>(())
/// ```
#[derive(Clone)]
pub struct McpServer {
    inner: Arc<Inner>,
}

struct Inner {
    name: String,
    version: String,
    description: String,
    title: Option<String>,
    website_url: Option<String>,
    repository: Option<ServerCardRepository>,
    icons: Vec<ServerCardIcon>,
    instructions: Option<String>,
    entries: Vec<Entry>,
    index: HashMap<String, usize>,
    executor: Arc<dyn McpCallExecutor>,
    task_host: Option<Arc<dyn McpTaskHost>>,
    local_caller: McpCaller,
    call_timeout: Option<Duration>,
}

/// One published tool: the tool, its MCP definition and its argument check.
struct Entry {
    tool: Arc<dyn Tool>,
    definition: rmcp::model::Tool,
    validator: ToolArgumentValidator,
}

impl std::fmt::Debug for McpServer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let tools: Vec<&str> = self.inner.entries.iter().map(|entry| entry.tool.name()).collect();
        f.debug_struct("McpServer")
            .field("name", &self.inner.name)
            .field("version", &self.inner.version)
            .field("tools", &tools)
            .finish_non_exhaustive()
    }
}

impl McpServer {
    /// Starts a server named `name` (reverse DNS with one `/`, such as `com.example/shop`) at
    /// `version`.
    pub fn builder(name: impl Into<String>, version: impl Into<String>) -> McpServerBuilder {
        McpServerBuilder::new(name, version)
    }

    /// The server's name, as `serverInfo.name` and the card's `name`.
    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// The server's version.
    pub fn version(&self) -> &str {
        &self.inner.version
    }

    /// The published tools, in the order they were added.
    pub fn tools(&self) -> Vec<Arc<dyn Tool>> {
        self.inner.entries.iter().map(|entry| Arc::clone(&entry.tool)).collect()
    }

    /// The MCP definition of every tool, regardless of caller.
    pub fn tool_definitions(&self) -> Vec<rmcp::model::Tool> {
        self.inner.entries.iter().map(|entry| entry.definition.clone()).collect()
    }

    /// The MCP definitions `caller` sees in `tools/list`.
    pub fn tool_definitions_for(&self, caller: &McpCaller) -> Vec<rmcp::model::Tool> {
        self.inner
            .entries
            .iter()
            .filter(|entry| self.inner.executor.is_visible(entry.tool.as_ref(), caller))
            .map(|entry| entry.definition.clone())
            .collect()
    }

    /// The Server Card for the Streamable HTTP endpoint at `endpoint` (SEP-2127).
    ///
    /// The card's name, version, title and description are the server's `serverInfo`, and its
    /// one remote lists the protocol versions the server negotiates, so the card agrees with
    /// what a client sees once connected. `headers` describes headers a client must send, such
    /// as `Authorization`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::Url;
    /// use adk_tool::mcp::server::{McpServer, SERVER_CARD_SCHEMA_URL};
    ///
    /// let server = McpServer::builder("com.example/solar", "1.0.0")
    ///     .description("Quotes for Example Solar installations")
    ///     .build()?;
    /// let card = server.server_card(&Url::parse("https://agent.example.com/mcp").unwrap(), &[]);
    /// assert_eq!(card.schema, SERVER_CARD_SCHEMA_URL);
    /// assert_eq!(card.remotes.unwrap()[0].url, "https://agent.example.com/mcp");
    /// # Ok::<(), adk_core::AdkError>(())
    /// ```
    pub fn server_card(&self, endpoint: &Url, headers: &[ServerCardHeader]) -> ServerCard {
        let inner = &self.inner;
        let versions = ServerHandler::supported_protocol_versions(self);
        let mut remote = ServerCardRemote::new(RemoteTransport::StreamableHttp, endpoint.as_str())
            .with_protocol_versions(versions.iter().map(ToString::to_string));
        for header in headers {
            remote = remote.with_header(header.clone());
        }
        ServerCard {
            schema: SERVER_CARD_SCHEMA_URL.to_string(),
            name: inner.name.clone(),
            version: inner.version.clone(),
            description: inner.description.clone(),
            title: inner.title.clone(),
            website_url: inner.website_url.clone(),
            repository: inner.repository.clone(),
            icons: (!inner.icons.is_empty()).then(|| inner.icons.clone()),
            remotes: Some(vec![remote]),
            meta: None,
        }
    }

    /// The AI Catalog entry pointing at this server's card at `card_url`.
    ///
    /// Its identifier is `urn:air:{reversed namespace}:mcp:{name}`: `com.example/solar` becomes
    /// `urn:air:example.com:mcp:solar`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::Url;
    /// use adk_tool::mcp::server::McpServer;
    ///
    /// let server = McpServer::builder("com.example/solar", "1.0.0")
    ///     .description("Quotes for Example Solar installations")
    ///     .build()?;
    /// let card_url = Url::parse("https://agent.example.com/mcp/server-card").unwrap();
    /// let entry = server.ai_catalog_entry(&card_url);
    /// assert_eq!(entry.identifier, "urn:air:example.com:mcp:solar");
    /// # Ok::<(), adk_core::AdkError>(())
    /// ```
    pub fn ai_catalog_entry(&self, card_url: &Url) -> AiCatalogEntry {
        AiCatalogEntry::server_card_url(
            card::catalog_identifier(&self.inner.name),
            card_url.as_str(),
        )
    }

    /// The tool `name`, when `caller` may see it.
    fn visible(&self, name: &str, caller: &McpCaller) -> Option<&Entry> {
        let entry = &self.inner.entries[*self.inner.index.get(name)?];
        self.inner.executor.is_visible(entry.tool.as_ref(), caller).then_some(entry)
    }

    /// The caller of `context`: the one the router resolved for an HTTP request, else the
    /// [local caller](McpServerBuilder::local_caller).
    fn caller_of(&self, context: &RequestContext<RoleServer>) -> McpCaller {
        match context.extensions.get::<axum::http::request::Parts>() {
            Some(parts) => {
                parts.extensions.get::<McpCaller>().cloned().unwrap_or_else(McpCaller::anonymous)
            }
            None => self.inner.local_caller.clone(),
        }
    }

    /// The task host, or the refusal of a `tasks/*` request on a server without one.
    fn task_host(&self) -> std::result::Result<&Arc<dyn McpTaskHost>, ErrorData> {
        self.inner.task_host.as_ref().ok_or_else(|| {
            ErrorData::new(ErrorCode::METHOD_NOT_FOUND, "this server keeps no tasks", None)
        })
    }

    /// Runs one call: arguments checked, then the executor under the timeout and the request's
    /// cancellation.
    async fn call(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<CallToolResponse, ErrorData> {
        let parts = context.extensions.get::<axum::http::request::Parts>();
        let caller = self.caller_of(&context);
        let name = request.name.as_ref();
        let Some(entry) = self.visible(name, &caller) else {
            tracing::debug!(tool.name = %quoted(name), caller.id = caller.id(), "mcp call to an unknown tool");
            return Err(ErrorData::invalid_params(
                format!("unknown tool '{}'", quoted(name)),
                None,
            ));
        };
        let arguments = Value::Object(request.arguments.unwrap_or_default());
        if let Err(error) = entry.validator.validate(name, &arguments) {
            return Ok(failed_result(name, &error).into());
        }
        let session_id = parts
            .and_then(|parts| parts.headers.get("mcp-session-id"))
            .and_then(|value| value.to_str().ok())
            .map(str::to_string);
        let capabilities = context.client_capabilities();
        let accepts_task = self.inner.task_host.is_some()
            && capabilities.as_ref().is_some_and(|capabilities| capabilities.supports_tasks());
        // ISO `YYYY-MM-DD` versions compare lexically the same as chronologically.
        let accepts_input_required = context
            .protocol_version()
            .is_some_and(|version| version.as_str() >= ProtocolVersion::V_2026_07_28.as_str());
        let can_elicit = !accepts_input_required
            && capabilities
                .as_ref()
                .and_then(|capabilities| capabilities.elicitation.as_ref())
                .is_some_and(|elicitation| elicitation.form.is_some() || elicitation.url.is_none());
        let peer = McpRequestPeer::new(
            context.peer.clone(),
            context.meta.get_progress_token(),
            can_elicit,
        );
        let call = McpToolCall {
            tool: Arc::clone(&entry.tool),
            arguments,
            caller,
            request_id: request_id_string(&context.id),
            session_id,
            accepts_task,
            accepts_input_required,
            input_responses: request.input_responses,
            request_state: request.request_state,
            peer: Some(peer),
        };
        let caller_id = call.caller.id().to_string();
        let run = self.inner.executor.execute(call);
        let bounded = async {
            match self.inner.call_timeout {
                Some(limit) => tokio::time::timeout(limit, run)
                    .await
                    .unwrap_or_else(|_| Err(timeout_error(entry.tool.effect(), limit))),
                None => run.await,
            }
        };
        let outcome = tokio::select! {
            outcome = bounded => outcome,
            () = context.ct.cancelled() => Err(AdkError::new(
                ErrorComponent::Tool,
                ErrorCategory::Cancelled,
                "mcp.server.call_cancelled",
                "the client cancelled the call",
            )),
        };
        match outcome {
            Ok(McpToolOutcome::Completed(value)) => Ok(completed_result(value).into()),
            Ok(McpToolOutcome::Failed(error)) => {
                tracing::debug!(tool.name = name, caller.id = %caller_id, error = %error.diagnostics(), "mcp tool failed");
                Ok(failed_result(name, &error).into())
            }
            Ok(McpToolOutcome::Task(task)) if accepts_task => {
                tracing::debug!(tool.name = name, caller.id = %caller_id, task.id = %task.task_id, "mcp call continues as a task");
                Ok(CallToolResponse::Task(CreateTaskResult::new(task)))
            }
            Ok(McpToolOutcome::InputRequired { input_requests, request_state })
                if accepts_input_required =>
            {
                let requests = (!input_requests.is_empty()).then_some(input_requests);
                if requests.is_none() && request_state.is_none() {
                    return Err(ErrorData::internal_error(
                        "the call asked for input with neither questions nor state",
                        None,
                    ));
                }
                Ok(CallToolResponse::InputRequired(InputRequiredResult::new(
                    requests,
                    request_state,
                )))
            }
            Ok(McpToolOutcome::Task(_) | McpToolOutcome::InputRequired { .. }) => {
                tracing::warn!(tool.name = name, caller.id = %caller_id, "mcp executor answered with a result the client cannot take");
                Err(ErrorData::internal_error(
                    "the call needs a client that takes MCP tasks or input_required results",
                    None,
                ))
            }
            Err(error) => {
                tracing::info!(tool.name = name, caller.id = %caller_id, error = %error.diagnostics(), "mcp call refused");
                Err(refusal(&error))
            }
        }
    }
}

impl ServerHandler for McpServer {
    fn get_info(&self) -> ServerConfig {
        let inner = &self.inner;
        let mut implementation =
            Implementation::new(&inner.name, &inner.version).with_description(&inner.description);
        if let Some(title) = &inner.title {
            implementation = implementation.with_title(title);
        }
        if let Some(url) = &inner.website_url {
            implementation = implementation.with_website_url(url);
        }
        if !inner.icons.is_empty() {
            implementation = implementation.with_icons(inner.icons.iter().map(rmcp_icon).collect());
        }
        let capabilities = if inner.task_host.is_some() {
            ServerCapabilities::builder().enable_tools().enable_tasks().build()
        } else {
            ServerCapabilities::builder().enable_tools().build()
        };
        let config = InitializeResult::new(capabilities).with_server_info(implementation);
        match &inner.instructions {
            Some(instructions) => config.with_instructions(instructions),
            None => config,
        }
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<ListToolsResult, ErrorData> {
        let caller = self.caller_of(&context);
        Ok(ListToolsResult::with_all_items(self.tool_definitions_for(&caller)))
    }

    fn get_tool(&self, name: &str) -> Option<rmcp::model::Tool> {
        let index = *self.inner.index.get(name)?;
        Some(self.inner.entries[index].definition.clone())
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<CallToolResponse, ErrorData> {
        self.call(request, context).await
    }

    async fn get_task(
        &self,
        request: GetTaskParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<GetTaskResult, ErrorData> {
        let caller = self.caller_of(&context);
        let task = self.task_host()?.get(&request.task_id, &caller).await.map_err(|error| {
            tracing::debug!(task.id = %quoted(&request.task_id), caller.id = caller.id(), error = %error.diagnostics(), "mcp tasks/get refused");
            refusal(&error)
        })?;
        Ok(GetTaskResult::new(task))
    }

    async fn update_task(
        &self,
        request: UpdateTaskParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<(), ErrorData> {
        let caller = self.caller_of(&context);
        self.task_host()?
            .update(&request.task_id, &caller, request.input_responses)
            .await
            .map_err(|error| {
                tracing::info!(task.id = %quoted(&request.task_id), caller.id = caller.id(), error = %error.diagnostics(), "mcp tasks/update refused");
                refusal(&error)
            })
    }

    async fn cancel_task(
        &self,
        request: CancelTaskParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<(), ErrorData> {
        let caller = self.caller_of(&context);
        self.task_host()?.cancel(&request.task_id, &caller).await.map_err(|error| {
            tracing::debug!(task.id = %quoted(&request.task_id), caller.id = caller.id(), error = %error.diagnostics(), "mcp tasks/cancel refused");
            refusal(&error)
        })
    }
}

/// Builds an [`McpServer`].
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
///
/// use adk_tool::mcp::server::{McpServer, ServerCardRepository};
///
/// let server = McpServer::builder("com.example/solar", "1.0.0")
///     .description("Quotes for Example Solar installations")
///     .title("Example Solar")
///     .website_url("https://example.com")
///     .repository(ServerCardRepository::new("https://github.com/example/solar", "github"))
///     .open_world(false)
///     .call_timeout(Some(Duration::from_secs(20)))
///     .build()?;
/// assert_eq!(server.name(), "com.example/solar");
/// # Ok::<(), adk_core::AdkError>(())
/// ```
pub struct McpServerBuilder {
    name: String,
    version: String,
    description: Option<String>,
    title: Option<String>,
    website_url: Option<String>,
    repository: Option<ServerCardRepository>,
    icons: Vec<ServerCardIcon>,
    instructions: Option<String>,
    tools: Vec<Arc<dyn Tool>>,
    executor: Option<Arc<dyn McpCallExecutor>>,
    governance: Option<RunConfig>,
    task_host: Option<Arc<dyn McpTaskHost>>,
    local_caller: McpCaller,
    open_world: bool,
    call_timeout: Option<Duration>,
}

impl McpServerBuilder {
    fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            description: None,
            title: None,
            website_url: None,
            repository: None,
            icons: Vec::new(),
            instructions: None,
            tools: Vec::new(),
            executor: None,
            governance: None,
            task_host: None,
            local_caller: McpCaller::anonymous(),
            open_world: true,
            call_timeout: Some(DEFAULT_CALL_TIMEOUT),
        }
    }

    /// What the server does, 1 to 100 characters; required.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// The display name, 1 to 100 characters.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// The server's homepage or documentation.
    pub fn website_url(mut self, url: impl Into<String>) -> Self {
        self.website_url = Some(url.into());
        self
    }

    /// Where the server's source lives.
    pub fn repository(mut self, repository: ServerCardRepository) -> Self {
        self.repository = Some(repository);
        self
    }

    /// Adds an icon.
    pub fn icon(mut self, icon: ServerCardIcon) -> Self {
        self.icons.push(icon);
        self
    }

    /// Instructions for the client's model, sent in the `initialize` result.
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.instructions = Some(instructions.into());
        self
    }

    /// Publishes `tool`.
    pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
        self.tools.push(tool);
        self
    }

    /// Publishes every tool in `tools`.
    pub fn tools(mut self, tools: impl IntoIterator<Item = Arc<dyn Tool>>) -> Self {
        self.tools.extend(tools);
        self
    }

    /// Governs every call through `adk_core::authorize_tool_call` under `config`, with a
    /// [`GovernedExecutor`]: its `tool_policy`, `governance` kill switches, confirmation
    /// handler, approvals, approval store, action ledger and audit trail apply as they do to an
    /// agent's calls.
    ///
    /// Without it, and without an [`executor`](Self::executor), the server refuses every call.
    /// A config without a `tool_policy` refuses every call too.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::sync::Arc;
    ///
    /// use adk_core::{DeclarativePolicy, GovernanceControl, RunConfig};
    /// use adk_tool::mcp::server::McpServer;
    ///
    /// let control = GovernanceControl::new();
    /// let policy = DeclarativePolicy::builder()
    ///     .allow_read_only()
    ///     .require_approval("book_install", "a person confirms each booking")
    ///     .build();
    /// let server = McpServer::builder("com.example/solar", "1.0.0")
    ///     .description("Quotes and bookings for Example Solar")
    ///     .tool_governance(
    ///         RunConfig::builder().tool_policy(Arc::new(policy)).governance(control.clone()).build(),
    ///     )
    ///     .build()?;
    /// control.freeze("incident 4012"); // every call is now refused
    /// # Ok::<(), adk_core::AdkError>(())
    /// ```
    pub fn tool_governance(mut self, config: RunConfig) -> Self {
        self.governance = Some(config);
        self
    }

    /// Runs calls through `executor` instead of a [`GovernedExecutor`].
    ///
    /// The executor owns authorization: it routes each call through
    /// `adk_core::authorize_tool_call` itself, as `adk-server`'s executors do, or is an
    /// [`UngovernedExecutor`], the explicit opt-out under which every visible tool runs for any
    /// caller.
    pub fn executor(mut self, executor: Arc<dyn McpCallExecutor>) -> Self {
        self.executor = Some(executor);
        self
    }

    /// Follows the tasks the executor starts with `host`, and declares the tasks extension
    /// (SEP-2663) so clients that support it may receive a task for a call.
    pub fn task_host(mut self, host: Arc<dyn McpTaskHost>) -> Self {
        self.task_host = Some(host);
        self
    }

    /// The caller of requests that reach the server without an HTTP request, such as over
    /// stdio; [`McpCaller::anonymous`] by default.
    ///
    /// A stdio server serves the one client that started it, so the host that starts it names
    /// who that is, for example `McpCaller::authenticated("local:ana")`.
    pub fn local_caller(mut self, caller: McpCaller) -> Self {
        self.local_caller = caller;
        self
    }

    /// The `openWorldHint` of tools that declare none; `true` by default, as in MCP.
    pub fn open_world(mut self, open_world: bool) -> Self {
        self.open_world = open_world;
        self
    }

    /// Bounds each call's run time; [`DEFAULT_CALL_TIMEOUT`] by default, `None` for no bound.
    ///
    /// A call over the bound is refused with a `Timeout` error, and the executor's future is
    /// dropped.
    pub fn call_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.call_timeout = timeout;
        self
    }

    /// Builds the server.
    ///
    /// # Errors
    ///
    /// Returns an `InvalidInput` error when the name, version, description or title break the
    /// SEP-2127 card schema, when two tools share a name, when a tool's parameters schema is
    /// not a JSON Schema object of type `object`, or when both an executor and a governance
    /// config are set.
    pub fn build(self) -> Result<McpServer> {
        check_name(&self.name)?;
        check_version(&self.version)?;
        let description = self.description.ok_or_else(|| {
            invalid("mcp.server.description", "set the server's description with `description(..)`")
        })?;
        check_text("description", &description)?;
        if let Some(title) = &self.title {
            check_text("title", title)?;
        }
        let mut entries = Vec::with_capacity(self.tools.len());
        let mut index = HashMap::with_capacity(self.tools.len());
        for tool in self.tools {
            let name = tool.name().to_string();
            if name.is_empty() {
                return Err(invalid("mcp.server.tool_name", "a published tool has an empty name"));
            }
            if index.insert(name.clone(), entries.len()).is_some() {
                return Err(invalid(
                    "mcp.server.duplicate_tool",
                    format!("two published tools are named '{name}'; MCP tool names are unique"),
                ));
            }
            let input_schema = input_schema(tool.as_ref())?;
            let validator = ToolArgumentValidator::new(&Value::Object(input_schema.clone()))?;
            let definition = definition(tool.as_ref(), input_schema, self.open_world);
            entries.push(Entry { tool, definition, validator });
        }
        let executor = match (self.executor, self.governance) {
            (Some(_), Some(_)) => {
                return Err(invalid(
                    "mcp.server.executor_and_governance",
                    "set `executor(..)` or `tool_governance(..)`, not both: a custom executor \
                     governs its own calls, so the governance config would be ignored",
                ));
            }
            (Some(executor), None) => executor,
            (None, config) => {
                Arc::new(GovernedExecutor::new(self.name.clone(), config.unwrap_or_default()))
                    as Arc<dyn McpCallExecutor>
            }
        };
        Ok(McpServer {
            inner: Arc::new(Inner {
                name: self.name,
                version: self.version,
                description,
                title: self.title,
                website_url: self.website_url,
                repository: self.repository,
                icons: self.icons,
                instructions: self.instructions,
                entries,
                index,
                executor,
                task_host: self.task_host,
                local_caller: self.local_caller,
                call_timeout: self.call_timeout,
            }),
        })
    }
}

/// The tool's MCP definition from its own declaration (spec 3.5 §7.4).
fn definition(tool: &dyn Tool, input_schema: JsonObject, open_world: bool) -> rmcp::model::Tool {
    let declared = tool.annotations();
    let hint = |key: &str| declared.and_then(|hints| hints.get(key)).and_then(Value::as_bool);
    let title = declared.and_then(|hints| hints.get("title")).and_then(Value::as_str);
    let annotations = match title {
        Some(title) => ToolAnnotations::with_title(title),
        None => ToolAnnotations::new(),
    };
    let annotations = match tool.effect() {
        ToolEffect::ReadOnly => annotations.read_only(true).idempotent(true).destructive(false),
        ToolEffect::Idempotent => annotations
            .read_only(false)
            .idempotent(true)
            .destructive(hint("destructiveHint").unwrap_or(true)),
        // `NonIdempotent`, and any effect added later, is described as the least safe.
        _ => annotations
            .read_only(false)
            .idempotent(false)
            .destructive(hint("destructiveHint").unwrap_or(true)),
    }
    .open_world(hint("openWorldHint").unwrap_or(open_world));
    let mut definition = rmcp::model::Tool::new(
        tool.name().to_string(),
        tool.description().to_string(),
        Arc::new(input_schema),
    )
    .with_annotations(annotations);
    if let Some(title) = title {
        definition = definition.with_title(title);
    }
    if let Some(Value::Object(output)) = tool.response_schema()
        && output.get("type").and_then(Value::as_str) == Some("object")
    {
        definition = definition.with_raw_output_schema(Arc::new(output));
    }
    definition
}

/// The tool's parameters schema as an MCP `inputSchema`: a JSON object of type `object`.
fn input_schema(tool: &dyn Tool) -> Result<JsonObject> {
    match tool.parameters_schema() {
        None => Ok(JsonObject::from_iter([("type".to_string(), json!("object"))])),
        Some(Value::Object(mut schema)) => match schema.get("type") {
            None => {
                schema.insert("type".to_string(), json!("object"));
                Ok(schema)
            }
            Some(Value::String(kind)) if kind == "object" => Ok(schema),
            Some(other) => Err(invalid(
                "mcp.server.input_schema",
                format!(
                    "tool '{}' declares parameters of type {other}; an MCP tool's parameters are \
                     a JSON object, so wrap them in an object schema",
                    tool.name()
                ),
            )),
        },
        Some(_) => Err(invalid(
            "mcp.server.input_schema",
            format!(
                "tool '{}' declares a parameters schema that is not a JSON object",
                tool.name()
            ),
        )),
    }
}

/// The result of a call that returned `value`: the value as text, and as structured content
/// when it is an object.
fn completed_result(value: Value) -> CallToolResult {
    let text = match &value {
        Value::String(text) => text.clone(),
        other => other.to_string(),
    };
    let mut result = CallToolResult::success(vec![ContentBlock::text(text)]);
    if value.is_object() {
        result.structured_content = Some(value);
    }
    result
}

/// The `isError` result of a call to `tool` that ran and failed with `error`.
fn failed_result(tool: &str, error: &AdkError) -> CallToolResult {
    let response = tool_error_response(tool, error, false);
    let text = response["error"].as_str().unwrap_or("the tool failed").to_string();
    CallToolResult::error(vec![ContentBlock::text(text)])
}

/// The JSON-RPC error a refused call is answered with.
///
/// Only the standard JSON-RPC codes are used: `-32602` when the call as made cannot be served
/// (`InvalidInput`, `NotFound`, `Unsupported`), `-32603` otherwise. The message names the
/// error's component, category and code, and adds the error's own message only for a rejection,
/// whose message is guidance for the caller; `data` carries the category, the code and whether
/// a retry may succeed.
fn refusal(error: &AdkError) -> ErrorData {
    let code = match error.category {
        ErrorCategory::InvalidInput | ErrorCategory::NotFound | ErrorCategory::Unsupported => {
            ErrorCode::INVALID_PARAMS
        }
        ErrorCategory::Unauthorized
        | ErrorCategory::Forbidden
        | ErrorCategory::RateLimited
        | ErrorCategory::Timeout
        | ErrorCategory::Unavailable
        | ErrorCategory::Cancelled
        | ErrorCategory::Internal
        | ErrorCategory::ResourceExhausted
        | ErrorCategory::Conflict => ErrorCode::INTERNAL_ERROR,
    };
    let message = if error.category.is_rejection() {
        let mut chars = error.message.chars();
        let head: String = chars.by_ref().take(TOOL_ERROR_MESSAGE_LIMIT).collect();
        let cut = if chars.next().is_some() { "… (truncated)" } else { "" };
        format!("{}: {head}{cut}", error.redacted())
    } else {
        error.redacted()
    };
    let mut data = json!({
        "category": error.category.to_string(),
        "code": error.code,
        "retryable": error.is_retryable(),
    });
    if let Some(after) = error.retry.retry_after_ms {
        data["retryAfterMs"] = json!(after);
    }
    ErrorData::new(code, message, Some(data))
}

/// The refusal of a call that ran past `limit`; one that may have had its effect is not marked
/// retryable.
fn timeout_error(effect: ToolEffect, limit: Duration) -> AdkError {
    let error = AdkError::timeout(
        ErrorComponent::Tool,
        "mcp.server.call_timeout",
        format!("the call ran past the server's {} ms limit", limit.as_millis()),
    );
    if effect.is_retry_safe() { error } else { error.with_retry(RetryHint::default()) }
}

/// The JSON-RPC request id as a string.
fn request_id_string(id: &rmcp::model::RequestId) -> String {
    match id {
        rmcp::model::NumberOrString::Number(number) => number.to_string(),
        rmcp::model::NumberOrString::String(text) => text.to_string(),
    }
}

/// `name` cut to [`QUOTED_NAME_LIMIT`] characters, for quoting a caller's input back.
fn quoted(name: &str) -> Cow<'_, str> {
    match name.char_indices().nth(QUOTED_NAME_LIMIT) {
        Some((end, _)) => Cow::Owned(format!("{}…", &name[..end])),
        None => Cow::Borrowed(name),
    }
}

/// The card icon as an rmcp `serverInfo` icon.
fn rmcp_icon(icon: &ServerCardIcon) -> rmcp::model::Icon {
    let mut converted = rmcp::model::Icon::new(&icon.src);
    if let Some(mime_type) = &icon.mime_type {
        converted = converted.with_mime_type(mime_type);
    }
    if let Some(sizes) = &icon.sizes {
        converted = converted.with_sizes(sizes.clone());
    }
    if let Some(theme) = icon.theme {
        converted = converted.with_theme(match theme {
            IconTheme::Light => rmcp::model::IconTheme::Light,
            IconTheme::Dark => rmcp::model::IconTheme::Dark,
        });
    }
    converted
}

fn invalid(code: &'static str, message: impl Into<String>) -> AdkError {
    AdkError::new(ErrorComponent::Tool, ErrorCategory::InvalidInput, code, message)
}

/// The SEP-2127 name rule: 3 to 200 characters, `namespace/name`, exactly one `/`.
fn check_name(name: &str) -> Result<()> {
    let valid = (3..=200).contains(&name.len())
        && name.split_once('/').is_some_and(|(namespace, server)| {
            !namespace.is_empty()
                && !server.is_empty()
                && namespace.bytes().all(|b| b.is_ascii_alphanumeric() || b".-".contains(&b))
                && server.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
        });
    if valid {
        Ok(())
    } else {
        Err(invalid(
            "mcp.server.name",
            format!(
                "server name '{name}' is not a reverse-DNS name with one '/', such as \
                 'com.example/shop' (SEP-2127)"
            ),
        ))
    }
}

/// A version of at most 255 characters that is not a range (SEP-2127).
fn check_version(version: &str) -> Result<()> {
    let is_range = version.contains(['^', '~', '>', '<', '=', '*', ' '])
        || version.split('.').any(|part| part.eq_ignore_ascii_case("x"));
    if version.is_empty() || version.len() > 255 || is_range {
        return Err(invalid(
            "mcp.server.version",
            format!(
                "server version '{version}' must be one version of at most 255 characters, not a range, such as '1.0.2'"
            ),
        ));
    }
    Ok(())
}

/// A card text field of 1 to 100 characters.
fn check_text(field: &str, value: &str) -> Result<()> {
    let length = value.chars().count();
    if (1..=100).contains(&length) {
        Ok(())
    } else {
        Err(invalid(
            "mcp.server.card_text",
            format!("the server's {field} has {length} characters; SEP-2127 allows 1 to 100"),
        ))
    }
}

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

    fn tool(name: &str) -> Arc<dyn Tool> {
        Arc::new(FunctionTool::new(name, "a tool", |_ctx, _args| async { Ok(json!({})) }))
    }

    fn server() -> McpServerBuilder {
        McpServer::builder("com.example/shop", "1.0.0").description("Shop")
    }

    #[test]
    fn names_follow_the_card_schema() {
        for good in ["com.example/shop", "io.github.acme-co/shop_v2.1", "a/b"] {
            assert!(check_name(good).is_ok(), "{good}");
        }
        for bad in ["shop", "com.example/", "/shop", "com/example/shop", "com.example/sh op", "ab"]
        {
            assert!(check_name(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn versions_reject_ranges() {
        for good in ["1.0.0", "2.1.0-alpha", "2026.10"] {
            assert!(check_version(good).is_ok(), "{good}");
        }
        for bad in ["", "^1.2.3", "~1.2.3", ">=1.2.3", "1.x", "1.*"] {
            assert!(check_version(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn the_description_is_required_and_bounded() {
        let missing = McpServer::builder("com.example/shop", "1.0.0").build().unwrap_err();
        assert_eq!(missing.code, "mcp.server.description");
        let long = server().description("x".repeat(101)).build().unwrap_err();
        assert_eq!(long.code, "mcp.server.card_text");
    }

    #[test]
    fn duplicate_tool_names_are_refused() {
        let error = server().tool(tool("quote")).tool(tool("quote")).build().unwrap_err();
        assert_eq!(error.code, "mcp.server.duplicate_tool");
    }

    #[test]
    fn a_tool_without_parameters_takes_an_empty_object() {
        let built = server().tool(tool("quote")).build().unwrap();
        assert_eq!(
            Value::Object((*built.tool_definitions()[0].input_schema).clone()),
            json!({ "type": "object" })
        );
    }

    #[test]
    fn refusals_keep_internal_messages_back() {
        let internal = AdkError::unavailable(
            ErrorComponent::Tool,
            "orders.db",
            "connect postgres://admin:secret@db.internal failed",
        );
        let data = refusal(&internal);
        assert_eq!(data.code, ErrorCode::INTERNAL_ERROR);
        assert_eq!(data.message, "tool.unavailable (orders.db)");
        assert_eq!(
            data.data,
            Some(json!({ "category": "unavailable", "code": "orders.db", "retryable": true }))
        );

        let denied = AdkError::new(
            ErrorComponent::Tool,
            ErrorCategory::Forbidden,
            "awp.capability.tier",
            "this capability needs the known tier",
        );
        assert_eq!(
            refusal(&denied).message,
            "tool.forbidden (awp.capability.tier): this capability needs the known tier"
        );
        let invalid_input = invalid("x.y", "bad");
        assert_eq!(refusal(&invalid_input).code, ErrorCode::INVALID_PARAMS);
    }

    #[test]
    fn a_timed_out_non_idempotent_call_is_not_retryable() {
        let limit = Duration::from_secs(1);
        assert!(!timeout_error(ToolEffect::NonIdempotent, limit).is_retryable());
        assert!(timeout_error(ToolEffect::ReadOnly, limit).is_retryable());
    }

    #[test]
    fn quoted_names_are_bounded() {
        let long = "a".repeat(500);
        assert_eq!(quoted(&long).chars().count(), QUOTED_NAME_LIMIT + 1);
        assert_eq!(quoted("quote"), "quote");
    }
}