1use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10use serde_json::{Map, Value};
11
12pub const SERVER_CARD_SCHEMA_URL: &str =
14 "https://static.modelcontextprotocol.io/schemas/v1/server-card.schema.json";
15
16pub const SERVER_CARD_MEDIA_TYPE: &str = "application/mcp-server-card+json";
18
19pub const AI_CATALOG_MEDIA_TYPE: &str = "application/ai-catalog+json";
21
22pub const AI_CATALOG_SPEC_VERSION: &str = "1.0";
24
25pub const SERVER_CARD_ENDPOINT_SUFFIX: &str = "/server-card";
28
29pub const SERVER_CARD_WELL_KNOWN_PATH: &str = "/.well-known/mcp/server-card.json";
31
32pub const AI_CATALOG_WELL_KNOWN_PATH: &str = "/.well-known/ai-catalog.json";
34
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58#[serde(rename_all = "camelCase")]
59#[non_exhaustive]
60pub struct ServerCard {
61 #[serde(rename = "$schema")]
63 pub schema: String,
64 pub name: String,
66 pub version: String,
68 pub description: String,
70 #[serde(skip_serializing_if = "Option::is_none")]
72 pub title: Option<String>,
73 #[serde(skip_serializing_if = "Option::is_none")]
75 pub website_url: Option<String>,
76 #[serde(skip_serializing_if = "Option::is_none")]
78 pub repository: Option<ServerCardRepository>,
79 #[serde(skip_serializing_if = "Option::is_none")]
81 pub icons: Option<Vec<ServerCardIcon>>,
82 #[serde(skip_serializing_if = "Option::is_none")]
84 pub remotes: Option<Vec<ServerCardRemote>>,
85 #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
87 pub meta: Option<Map<String, Value>>,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
102#[serde(rename_all = "camelCase")]
103#[non_exhaustive]
104pub struct ServerCardRepository {
105 pub url: String,
107 pub source: String,
109 #[serde(skip_serializing_if = "Option::is_none")]
111 pub subfolder: Option<String>,
112 #[serde(skip_serializing_if = "Option::is_none")]
114 pub id: Option<String>,
115}
116
117impl ServerCardRepository {
118 pub fn new(url: impl Into<String>, source: impl Into<String>) -> Self {
120 Self { url: url.into(), source: source.into(), subfolder: None, id: None }
121 }
122
123 pub fn with_subfolder(mut self, subfolder: impl Into<String>) -> Self {
125 self.subfolder = Some(subfolder.into());
126 self
127 }
128
129 pub fn with_id(mut self, id: impl Into<String>) -> Self {
131 self.id = Some(id.into());
132 self
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
150#[serde(rename_all = "camelCase")]
151#[non_exhaustive]
152pub struct ServerCardIcon {
153 pub src: String,
155 #[serde(skip_serializing_if = "Option::is_none")]
157 pub mime_type: Option<String>,
158 #[serde(skip_serializing_if = "Option::is_none")]
160 pub sizes: Option<Vec<String>>,
161 #[serde(skip_serializing_if = "Option::is_none")]
163 pub theme: Option<IconTheme>,
164}
165
166impl ServerCardIcon {
167 pub fn new(src: impl Into<String>) -> Self {
169 Self { src: src.into(), mime_type: None, sizes: None, theme: None }
170 }
171
172 pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
174 self.mime_type = Some(mime_type.into());
175 self
176 }
177
178 pub fn with_sizes(mut self, sizes: impl IntoIterator<Item = impl Into<String>>) -> Self {
180 self.sizes = Some(sizes.into_iter().map(Into::into).collect());
181 self
182 }
183
184 pub fn with_theme(mut self, theme: IconTheme) -> Self {
186 self.theme = Some(theme);
187 self
188 }
189}
190
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
193#[serde(rename_all = "lowercase")]
194pub enum IconTheme {
195 Light,
197 Dark,
199}
200
201#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
203#[serde(rename_all = "kebab-case")]
204pub enum RemoteTransport {
205 StreamableHttp,
207 Sse,
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
224#[serde(rename_all = "camelCase")]
225#[non_exhaustive]
226pub struct ServerCardRemote {
227 #[serde(rename = "type")]
229 pub transport: RemoteTransport,
230 pub url: String,
232 #[serde(skip_serializing_if = "Option::is_none")]
234 pub headers: Option<Vec<ServerCardHeader>>,
235 #[serde(skip_serializing_if = "Option::is_none")]
237 pub variables: Option<BTreeMap<String, ServerCardInput>>,
238 #[serde(skip_serializing_if = "Option::is_none")]
240 pub supported_protocol_versions: Option<Vec<String>>,
241}
242
243impl ServerCardRemote {
244 pub fn new(transport: RemoteTransport, url: impl Into<String>) -> Self {
246 Self {
247 transport,
248 url: url.into(),
249 headers: None,
250 variables: None,
251 supported_protocol_versions: None,
252 }
253 }
254
255 pub fn with_header(mut self, header: ServerCardHeader) -> Self {
257 self.headers.get_or_insert_with(Vec::new).push(header);
258 self
259 }
260
261 pub fn with_variable(mut self, name: impl Into<String>, input: ServerCardInput) -> Self {
263 self.variables.get_or_insert_with(BTreeMap::new).insert(name.into(), input);
264 self
265 }
266
267 pub fn with_protocol_versions(
269 mut self,
270 versions: impl IntoIterator<Item = impl Into<String>>,
271 ) -> Self {
272 self.supported_protocol_versions = Some(versions.into_iter().map(Into::into).collect());
273 self
274 }
275}
276
277#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
288#[serde(rename_all = "camelCase")]
289#[non_exhaustive]
290pub struct ServerCardInput {
291 #[serde(skip_serializing_if = "Option::is_none")]
293 pub description: Option<String>,
294 #[serde(skip_serializing_if = "Option::is_none")]
296 pub is_required: Option<bool>,
297 #[serde(skip_serializing_if = "Option::is_none")]
299 pub is_secret: Option<bool>,
300 #[serde(skip_serializing_if = "Option::is_none")]
302 pub format: Option<InputFormat>,
303 #[serde(skip_serializing_if = "Option::is_none")]
305 pub default: Option<String>,
306 #[serde(skip_serializing_if = "Option::is_none")]
308 pub placeholder: Option<String>,
309 #[serde(skip_serializing_if = "Option::is_none")]
311 pub value: Option<String>,
312 #[serde(skip_serializing_if = "Option::is_none")]
314 pub choices: Option<Vec<String>>,
315}
316
317impl ServerCardInput {
318 pub fn new() -> Self {
320 Self::default()
321 }
322
323 pub fn with_description(mut self, description: impl Into<String>) -> Self {
325 self.description = Some(description.into());
326 self
327 }
328
329 pub fn required(mut self) -> Self {
331 self.is_required = Some(true);
332 self
333 }
334
335 pub fn secret(mut self) -> Self {
337 self.is_secret = Some(true);
338 self
339 }
340
341 pub fn with_value(mut self, value: impl Into<String>) -> Self {
343 self.value = Some(value.into());
344 self
345 }
346}
347
348#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
350#[serde(rename_all = "lowercase")]
351pub enum InputFormat {
352 String,
354 Number,
356 Boolean,
358 Filepath,
360}
361
362#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
378#[serde(rename_all = "camelCase")]
379#[non_exhaustive]
380pub struct ServerCardHeader {
381 pub name: String,
383 #[serde(flatten)]
385 pub input: ServerCardInput,
386 #[serde(skip_serializing_if = "Option::is_none")]
388 pub variables: Option<BTreeMap<String, ServerCardInput>>,
389}
390
391impl ServerCardHeader {
392 pub fn new(name: impl Into<String>) -> Self {
394 Self { name: name.into(), input: ServerCardInput::default(), variables: None }
395 }
396
397 pub fn with_description(mut self, description: impl Into<String>) -> Self {
399 self.input.description = Some(description.into());
400 self
401 }
402
403 pub fn with_value(mut self, value: impl Into<String>) -> Self {
405 self.input.value = Some(value.into());
406 self
407 }
408
409 pub fn with_variable(mut self, name: impl Into<String>, input: ServerCardInput) -> Self {
411 self.variables.get_or_insert_with(BTreeMap::new).insert(name.into(), input);
412 self
413 }
414
415 pub fn required(mut self) -> Self {
417 self.input.is_required = Some(true);
418 self
419 }
420
421 pub fn secret(mut self) -> Self {
423 self.input.is_secret = Some(true);
424 self
425 }
426}
427
428#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
446#[serde(rename_all = "camelCase")]
447#[non_exhaustive]
448pub struct AiCatalog {
449 pub spec_version: String,
451 #[serde(skip_serializing_if = "Option::is_none")]
453 pub host: Option<AiCatalogHost>,
454 pub entries: Vec<AiCatalogEntry>,
456}
457
458impl AiCatalog {
459 pub fn new(entries: impl IntoIterator<Item = AiCatalogEntry>) -> Self {
461 Self {
462 spec_version: AI_CATALOG_SPEC_VERSION.to_string(),
463 host: None,
464 entries: entries.into_iter().collect(),
465 }
466 }
467
468 pub fn with_host(mut self, host: AiCatalogHost) -> Self {
470 self.host = Some(host);
471 self
472 }
473}
474
475#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
486#[serde(rename_all = "camelCase")]
487#[non_exhaustive]
488pub struct AiCatalogHost {
489 pub display_name: String,
491 #[serde(skip_serializing_if = "Option::is_none")]
493 pub identifier: Option<String>,
494 #[serde(skip_serializing_if = "Option::is_none")]
496 pub documentation_url: Option<String>,
497 #[serde(skip_serializing_if = "Option::is_none")]
499 pub logo_url: Option<String>,
500}
501
502impl AiCatalogHost {
503 pub fn new(display_name: impl Into<String>) -> Self {
505 Self {
506 display_name: display_name.into(),
507 identifier: None,
508 documentation_url: None,
509 logo_url: None,
510 }
511 }
512
513 pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
515 self.identifier = Some(identifier.into());
516 self
517 }
518
519 pub fn with_documentation_url(mut self, url: impl Into<String>) -> Self {
521 self.documentation_url = Some(url.into());
522 self
523 }
524}
525
526#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
543#[serde(rename_all = "camelCase")]
544#[non_exhaustive]
545pub struct AiCatalogEntry {
546 pub identifier: String,
548 #[serde(rename = "type")]
550 pub artifact_type: String,
551 #[serde(flatten)]
553 pub content: AiCatalogContent,
554 #[serde(skip_serializing_if = "Option::is_none")]
556 pub tags: Option<Vec<String>>,
557}
558
559impl AiCatalogEntry {
560 pub fn server_card_url(identifier: impl Into<String>, url: impl Into<String>) -> Self {
562 Self {
563 identifier: identifier.into(),
564 artifact_type: SERVER_CARD_MEDIA_TYPE.to_string(),
565 content: AiCatalogContent::Url(url.into()),
566 tags: None,
567 }
568 }
569
570 pub fn server_card_inline(
576 identifier: impl Into<String>,
577 card: &ServerCard,
578 ) -> serde_json::Result<Self> {
579 Ok(Self {
580 identifier: identifier.into(),
581 artifact_type: SERVER_CARD_MEDIA_TYPE.to_string(),
582 content: AiCatalogContent::Data(serde_json::to_value(card)?),
583 tags: None,
584 })
585 }
586
587 pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
589 self.tags = Some(tags.into_iter().map(Into::into).collect());
590 self
591 }
592}
593
594#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
596#[serde(rename_all = "lowercase")]
597pub enum AiCatalogContent {
598 Url(String),
600 Data(Value),
602}
603
604pub(crate) fn catalog_identifier(card_name: &str) -> String {
608 let (namespace, name) = card_name.split_once('/').unwrap_or(("", card_name));
609 let publisher: Vec<&str> = namespace.split('.').rev().collect();
610 format!("urn:air:{}:mcp:{name}", publisher.join("."))
611}
612
613#[cfg(test)]
614mod tests {
615 use super::*;
616 use serde_json::json;
617
618 #[test]
619 fn the_catalog_identifier_reverses_the_namespace() {
620 assert_eq!(catalog_identifier("com.example/weather"), "urn:air:example.com:mcp:weather");
621 assert_eq!(catalog_identifier("io.github.acme/shop"), "urn:air:acme.github.io:mcp:shop");
622 }
623
624 #[test]
625 fn a_header_flattens_its_input_properties() {
626 let header = ServerCardHeader::new("Authorization")
627 .with_value("Bearer {token}")
628 .with_variable("token", ServerCardInput::new().required().secret())
629 .required()
630 .secret();
631 assert_eq!(
632 serde_json::to_value(&header).unwrap(),
633 json!({
634 "name": "Authorization",
635 "isRequired": true,
636 "isSecret": true,
637 "value": "Bearer {token}",
638 "variables": { "token": { "isRequired": true, "isSecret": true } },
639 })
640 );
641 }
642
643 #[test]
644 fn an_entry_carries_exactly_one_of_url_or_data() {
645 let by_url = AiCatalogEntry::server_card_url("urn:air:example.com:mcp:shop", "https://x/y");
646 assert_eq!(
647 serde_json::to_value(&by_url).unwrap(),
648 json!({
649 "identifier": "urn:air:example.com:mcp:shop",
650 "type": SERVER_CARD_MEDIA_TYPE,
651 "url": "https://x/y",
652 })
653 );
654 let card = ServerCard {
655 schema: SERVER_CARD_SCHEMA_URL.into(),
656 name: "com.example/shop".into(),
657 version: "1.0.0".into(),
658 description: "Shop".into(),
659 title: None,
660 website_url: None,
661 repository: None,
662 icons: None,
663 remotes: None,
664 meta: None,
665 };
666 let inline =
667 AiCatalogEntry::server_card_inline("urn:air:example.com:mcp:shop", &card).unwrap();
668 let value = serde_json::to_value(&inline).unwrap();
669 assert_eq!(value["data"]["name"], "com.example/shop");
670 assert!(value.get("url").is_none());
671 let back: AiCatalogEntry = serde_json::from_value(value).unwrap();
672 assert_eq!(back, inline);
673 }
674}