use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
pub const SERVER_CARD_SCHEMA_URL: &str =
"https://static.modelcontextprotocol.io/schemas/v1/server-card.schema.json";
pub const SERVER_CARD_MEDIA_TYPE: &str = "application/mcp-server-card+json";
pub const AI_CATALOG_MEDIA_TYPE: &str = "application/ai-catalog+json";
pub const AI_CATALOG_SPEC_VERSION: &str = "1.0";
pub const SERVER_CARD_ENDPOINT_SUFFIX: &str = "/server-card";
pub const SERVER_CARD_WELL_KNOWN_PATH: &str = "/.well-known/mcp/server-card.json";
pub const AI_CATALOG_WELL_KNOWN_PATH: &str = "/.well-known/ai-catalog.json";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCard {
#[serde(rename = "$schema")]
pub schema: String,
pub name: String,
pub version: String,
pub description: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub website_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<ServerCardRepository>,
#[serde(skip_serializing_if = "Option::is_none")]
pub icons: Option<Vec<ServerCardIcon>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub remotes: Option<Vec<ServerCardRemote>>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<Map<String, Value>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCardRepository {
pub url: String,
pub source: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub subfolder: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
}
impl ServerCardRepository {
pub fn new(url: impl Into<String>, source: impl Into<String>) -> Self {
Self { url: url.into(), source: source.into(), subfolder: None, id: None }
}
pub fn with_subfolder(mut self, subfolder: impl Into<String>) -> Self {
self.subfolder = Some(subfolder.into());
self
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCardIcon {
pub src: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sizes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub theme: Option<IconTheme>,
}
impl ServerCardIcon {
pub fn new(src: impl Into<String>) -> Self {
Self { src: src.into(), mime_type: None, sizes: None, theme: None }
}
pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.mime_type = Some(mime_type.into());
self
}
pub fn with_sizes(mut self, sizes: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.sizes = Some(sizes.into_iter().map(Into::into).collect());
self
}
pub fn with_theme(mut self, theme: IconTheme) -> Self {
self.theme = Some(theme);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IconTheme {
Light,
Dark,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RemoteTransport {
StreamableHttp,
Sse,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCardRemote {
#[serde(rename = "type")]
pub transport: RemoteTransport,
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<Vec<ServerCardHeader>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<BTreeMap<String, ServerCardInput>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub supported_protocol_versions: Option<Vec<String>>,
}
impl ServerCardRemote {
pub fn new(transport: RemoteTransport, url: impl Into<String>) -> Self {
Self {
transport,
url: url.into(),
headers: None,
variables: None,
supported_protocol_versions: None,
}
}
pub fn with_header(mut self, header: ServerCardHeader) -> Self {
self.headers.get_or_insert_with(Vec::new).push(header);
self
}
pub fn with_variable(mut self, name: impl Into<String>, input: ServerCardInput) -> Self {
self.variables.get_or_insert_with(BTreeMap::new).insert(name.into(), input);
self
}
pub fn with_protocol_versions(
mut self,
versions: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.supported_protocol_versions = Some(versions.into_iter().map(Into::into).collect());
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCardInput {
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_required: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_secret: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<InputFormat>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub placeholder: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub choices: Option<Vec<String>>,
}
impl ServerCardInput {
pub fn new() -> Self {
Self::default()
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn required(mut self) -> Self {
self.is_required = Some(true);
self
}
pub fn secret(mut self) -> Self {
self.is_secret = Some(true);
self
}
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.value = Some(value.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InputFormat {
String,
Number,
Boolean,
Filepath,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ServerCardHeader {
pub name: String,
#[serde(flatten)]
pub input: ServerCardInput,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<BTreeMap<String, ServerCardInput>>,
}
impl ServerCardHeader {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into(), input: ServerCardInput::default(), variables: None }
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.input.description = Some(description.into());
self
}
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.input.value = Some(value.into());
self
}
pub fn with_variable(mut self, name: impl Into<String>, input: ServerCardInput) -> Self {
self.variables.get_or_insert_with(BTreeMap::new).insert(name.into(), input);
self
}
pub fn required(mut self) -> Self {
self.input.is_required = Some(true);
self
}
pub fn secret(mut self) -> Self {
self.input.is_secret = Some(true);
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AiCatalog {
pub spec_version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub host: Option<AiCatalogHost>,
pub entries: Vec<AiCatalogEntry>,
}
impl AiCatalog {
pub fn new(entries: impl IntoIterator<Item = AiCatalogEntry>) -> Self {
Self {
spec_version: AI_CATALOG_SPEC_VERSION.to_string(),
host: None,
entries: entries.into_iter().collect(),
}
}
pub fn with_host(mut self, host: AiCatalogHost) -> Self {
self.host = Some(host);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AiCatalogHost {
pub display_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub identifier: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub logo_url: Option<String>,
}
impl AiCatalogHost {
pub fn new(display_name: impl Into<String>) -> Self {
Self {
display_name: display_name.into(),
identifier: None,
documentation_url: None,
logo_url: None,
}
}
pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
self.identifier = Some(identifier.into());
self
}
pub fn with_documentation_url(mut self, url: impl Into<String>) -> Self {
self.documentation_url = Some(url.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AiCatalogEntry {
pub identifier: String,
#[serde(rename = "type")]
pub artifact_type: String,
#[serde(flatten)]
pub content: AiCatalogContent,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
}
impl AiCatalogEntry {
pub fn server_card_url(identifier: impl Into<String>, url: impl Into<String>) -> Self {
Self {
identifier: identifier.into(),
artifact_type: SERVER_CARD_MEDIA_TYPE.to_string(),
content: AiCatalogContent::Url(url.into()),
tags: None,
}
}
pub fn server_card_inline(
identifier: impl Into<String>,
card: &ServerCard,
) -> serde_json::Result<Self> {
Ok(Self {
identifier: identifier.into(),
artifact_type: SERVER_CARD_MEDIA_TYPE.to_string(),
content: AiCatalogContent::Data(serde_json::to_value(card)?),
tags: None,
})
}
pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.tags = Some(tags.into_iter().map(Into::into).collect());
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AiCatalogContent {
Url(String),
Data(Value),
}
pub(crate) fn catalog_identifier(card_name: &str) -> String {
let (namespace, name) = card_name.split_once('/').unwrap_or(("", card_name));
let publisher: Vec<&str> = namespace.split('.').rev().collect();
format!("urn:air:{}:mcp:{name}", publisher.join("."))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn the_catalog_identifier_reverses_the_namespace() {
assert_eq!(catalog_identifier("com.example/weather"), "urn:air:example.com:mcp:weather");
assert_eq!(catalog_identifier("io.github.acme/shop"), "urn:air:acme.github.io:mcp:shop");
}
#[test]
fn a_header_flattens_its_input_properties() {
let header = ServerCardHeader::new("Authorization")
.with_value("Bearer {token}")
.with_variable("token", ServerCardInput::new().required().secret())
.required()
.secret();
assert_eq!(
serde_json::to_value(&header).unwrap(),
json!({
"name": "Authorization",
"isRequired": true,
"isSecret": true,
"value": "Bearer {token}",
"variables": { "token": { "isRequired": true, "isSecret": true } },
})
);
}
#[test]
fn an_entry_carries_exactly_one_of_url_or_data() {
let by_url = AiCatalogEntry::server_card_url("urn:air:example.com:mcp:shop", "https://x/y");
assert_eq!(
serde_json::to_value(&by_url).unwrap(),
json!({
"identifier": "urn:air:example.com:mcp:shop",
"type": SERVER_CARD_MEDIA_TYPE,
"url": "https://x/y",
})
);
let card = ServerCard {
schema: SERVER_CARD_SCHEMA_URL.into(),
name: "com.example/shop".into(),
version: "1.0.0".into(),
description: "Shop".into(),
title: None,
website_url: None,
repository: None,
icons: None,
remotes: None,
meta: None,
};
let inline =
AiCatalogEntry::server_card_inline("urn:air:example.com:mcp:shop", &card).unwrap();
let value = serde_json::to_value(&inline).unwrap();
assert_eq!(value["data"]["name"], "com.example/shop");
assert!(value.get("url").is_none());
let back: AiCatalogEntry = serde_json::from_value(value).unwrap();
assert_eq!(back, inline);
}
}