CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 37m50s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
149 lines
5.0 KiB
Rust
149 lines
5.0 KiB
Rust
//! Lossless Resource-only projection shared by the handler and existing wire builders.
|
|
use rmcp::model::{
|
|
CacheScope, ListResourceTemplatesResult, ListResourcesResult, ReadResourceResult, ResultType,
|
|
};
|
|
use serde::{Serialize, de::DeserializeOwned};
|
|
use serde_json::{Map, Value};
|
|
|
|
pub(super) const RESOURCE_FIELDS: &[&str] = &[
|
|
"uri",
|
|
"name",
|
|
"title",
|
|
"description",
|
|
"mimeType",
|
|
"size",
|
|
"_meta",
|
|
];
|
|
pub(super) const TEMPLATE_FIELDS: &[&str] = &[
|
|
"uriTemplate",
|
|
"name",
|
|
"title",
|
|
"description",
|
|
"mimeType",
|
|
"_meta",
|
|
];
|
|
pub(super) const CONTENT_FIELDS: &[&str] = &["uri", "mimeType", "text", "blob", "_meta"];
|
|
|
|
/// Only unrepresentable fields plus order/identity witnesses, never a saved success response.
|
|
pub(super) struct Projection {
|
|
extras: Vec<Map<String, Value>>,
|
|
identities: Vec<Map<String, Value>>,
|
|
}
|
|
|
|
fn invalid() -> serde_json::Error {
|
|
<serde_json::Error as serde::de::Error>::custom("invalid resource projection")
|
|
}
|
|
|
|
impl Projection {
|
|
fn project<T: DeserializeOwned>(
|
|
raw: &[Value],
|
|
fields: &[&str],
|
|
) -> Result<(Vec<T>, Self), serde_json::Error> {
|
|
let mut extras = Vec::with_capacity(raw.len());
|
|
let mut identities = Vec::with_capacity(raw.len());
|
|
let mut typed = Vec::with_capacity(raw.len());
|
|
for raw in raw {
|
|
let mut visible = raw.as_object().ok_or_else(invalid)?.clone();
|
|
let extra = visible
|
|
.iter()
|
|
.filter(|(key, value)| !fields.contains(&key.as_str()) || value.is_null())
|
|
.map(|(key, value)| (key.clone(), value.clone()))
|
|
.collect();
|
|
let identity = visible
|
|
.iter()
|
|
.filter(|(key, _)| matches!(key.as_str(), "uri" | "uriTemplate" | "name"))
|
|
.map(|(key, value)| (key.clone(), value.clone()))
|
|
.collect();
|
|
// Opaque descriptor JSON must not acquire SDK annotation/icon schema validation.
|
|
visible.remove("annotations");
|
|
visible.remove("icons");
|
|
typed.push(serde_json::from_value(Value::Object(visible))?);
|
|
extras.push(extra);
|
|
identities.push(identity);
|
|
}
|
|
Ok((typed, Self { extras, identities }))
|
|
}
|
|
|
|
pub(super) fn restore(self, serialized: &mut Value) -> Result<(), serde_json::Error> {
|
|
let array = serialized.as_array_mut().ok_or_else(invalid)?;
|
|
if array.len() != self.extras.len() {
|
|
return Err(invalid());
|
|
}
|
|
for ((item, extras), identity) in array.iter_mut().zip(self.extras).zip(self.identities) {
|
|
let item = item.as_object_mut().ok_or_else(invalid)?;
|
|
if identity
|
|
.iter()
|
|
.any(|(key, value)| item.get(key) != Some(value))
|
|
{
|
|
return Err(invalid());
|
|
}
|
|
item.extend(extras);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub(super) fn list(
|
|
resources: &[Value],
|
|
next_cursor: Option<String>,
|
|
version: &str,
|
|
) -> Result<(ListResourcesResult, Projection), serde_json::Error> {
|
|
let (resources, extras) = Projection::project(resources, RESOURCE_FIELDS)?;
|
|
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
|
Ok((
|
|
ListResourcesResult {
|
|
resources,
|
|
next_cursor,
|
|
result_type: modern.then_some(ResultType::COMPLETE),
|
|
ttl_ms: modern.then_some(0),
|
|
cache_scope: modern.then_some(CacheScope::Private),
|
|
meta: None,
|
|
},
|
|
extras,
|
|
))
|
|
}
|
|
|
|
pub(super) fn templates(
|
|
templates: &[Value],
|
|
next_cursor: Option<String>,
|
|
version: &str,
|
|
) -> Result<(ListResourceTemplatesResult, Projection), serde_json::Error> {
|
|
let (resource_templates, extras) = Projection::project(templates, TEMPLATE_FIELDS)?;
|
|
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
|
Ok((
|
|
ListResourceTemplatesResult {
|
|
resource_templates,
|
|
next_cursor,
|
|
result_type: modern.then_some(ResultType::COMPLETE),
|
|
ttl_ms: modern.then_some(0),
|
|
cache_scope: modern.then_some(CacheScope::Private),
|
|
meta: None,
|
|
},
|
|
extras,
|
|
))
|
|
}
|
|
|
|
pub(super) fn read(
|
|
contents: &Value,
|
|
version: &str,
|
|
) -> Result<(ReadResourceResult, Projection), serde_json::Error> {
|
|
let (contents, extras) =
|
|
Projection::project(contents.as_array().ok_or_else(invalid)?, CONTENT_FIELDS)?;
|
|
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
|
let mut result = ReadResourceResult::new(contents);
|
|
result.result_type = modern.then_some(ResultType::COMPLETE);
|
|
result.ttl_ms = modern.then_some(0);
|
|
result.cache_scope = modern.then_some(CacheScope::Private);
|
|
Ok((result, extras))
|
|
}
|
|
|
|
pub(super) fn serialize(
|
|
result: impl Serialize,
|
|
extras: Projection,
|
|
field: &str,
|
|
) -> Result<Value, serde_json::Error> {
|
|
let mut result = serde_json::to_value(result)?;
|
|
extras.restore(result.get_mut(field).ok_or_else(invalid)?)?;
|
|
Ok(result)
|
|
}
|