feat(meta): add _meta field to prompts, resources and paginated result (#558)

This commit is contained in:
Pavel Bezglasny 2025-12-04 02:38:47 +01:00 committed by GitHub
parent eb5a7f7408
commit 81411fc12d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 195 additions and 8 deletions

View file

@ -18,6 +18,8 @@ pub struct PromptAttribute {
pub arguments: Option<Expr>,
/// Optional icons for the prompt
pub icons: Option<Expr>,
/// Optional metadata for the prompt
pub meta: Option<Expr>,
}
pub struct ResolvedPromptAttribute {
@ -26,6 +28,7 @@ pub struct ResolvedPromptAttribute {
pub description: Option<Expr>,
pub arguments: Expr,
pub icons: Option<Expr>,
pub meta: Option<Expr>,
}
impl ResolvedPromptAttribute {
@ -36,6 +39,7 @@ impl ResolvedPromptAttribute {
arguments,
title,
icons,
meta,
} = self;
let description = if let Some(description) = description {
quote! { Some(#description.into()) }
@ -52,6 +56,11 @@ impl ResolvedPromptAttribute {
} else {
quote! { None }
};
let meta = if let Some(meta) = meta {
quote! { Some(#meta) }
} else {
quote! { None }
};
let tokens = quote! {
pub fn #fn_ident() -> rmcp::model::Prompt {
rmcp::model::Prompt {
@ -60,6 +69,7 @@ impl ResolvedPromptAttribute {
arguments: #arguments,
title: #title,
icons: #icons,
meta: #meta,
}
}
};
@ -114,6 +124,7 @@ pub fn prompt(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream>
arguments: arguments.clone(),
title: attribute.title,
icons: attribute.icons,
meta: attribute.meta,
};
let prompt_attr_fn = resolved_prompt_attr.into_fn(prompt_attr_fn_ident.clone())?;

View file

@ -7,6 +7,7 @@ use syn::{Expr, ImplItem, ItemImpl, parse_quote};
#[darling(default)]
pub struct PromptHandlerAttribute {
pub router: Option<Expr>,
pub meta: Option<Expr>,
}
pub fn prompt_handler(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
@ -40,6 +41,12 @@ pub fn prompt_handler(attr: TokenStream, input: TokenStream) -> syn::Result<Toke
}
};
let meta = if let Some(meta) = attribute.meta {
quote! { Some(#meta) }
} else {
quote! { None }
};
// Add list_prompts implementation
let list_prompts_impl: ImplItem = parse_quote! {
async fn list_prompts(
@ -50,6 +57,7 @@ pub fn prompt_handler(attr: TokenStream, input: TokenStream) -> syn::Result<Toke
let prompts = #router_expr.list_all();
Ok(ListPromptsResult {
prompts,
meta: #meta,
next_cursor: None,
})
}

View file

@ -7,6 +7,7 @@ use syn::{Expr, ImplItem, ItemImpl};
#[darling(default)]
pub struct ToolHandlerAttribute {
pub router: Expr,
pub meta: Option<Expr>,
}
impl Default for ToolHandlerAttribute {
@ -16,13 +17,14 @@ impl Default for ToolHandlerAttribute {
self.tool_router
})
.unwrap(),
meta: None,
}
}
}
pub fn tool_handler(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
let attr_args = NestedMeta::parse_meta_list(attr)?;
let ToolHandlerAttribute { router } = ToolHandlerAttribute::from_list(&attr_args)?;
let ToolHandlerAttribute { router, meta } = ToolHandlerAttribute::from_list(&attr_args)?;
let mut item_impl = syn::parse2::<ItemImpl>(input.clone())?;
let tool_call_fn = quote! {
async fn call_tool(
@ -34,13 +36,24 @@ pub fn tool_handler(attr: TokenStream, input: TokenStream) -> syn::Result<TokenS
#router.call(tcc).await
}
};
let result_meta = if let Some(meta) = meta {
quote! { Some(#meta) }
} else {
quote! { None }
};
let tool_list_fn = quote! {
async fn list_tools(
&self,
_request: Option<rmcp::model::PaginatedRequestParam>,
_context: rmcp::service::RequestContext<rmcp::RoleServer>,
) -> Result<rmcp::model::ListToolsResult, rmcp::ErrorData> {
Ok(rmcp::model::ListToolsResult::with_all_items(#router.list_all()))
Ok(rmcp::model::ListToolsResult{
tools: #router.list_all(),
meta: #result_meta,
next_cursor: None,
})
}
};
let tool_call_fn = syn::parse2::<ImplItem>(tool_call_fn)?;

View file

@ -102,7 +102,7 @@ where
let tools = self.tool_router.list_all();
Ok(ServerResult::ListToolsResult(ListToolsResult {
tools,
next_cursor: None,
..Default::default()
}))
}
ClientRequest::GetPromptRequest(request) => {
@ -125,7 +125,7 @@ where
let prompts = self.prompt_router.list_all();
Ok(ServerResult::ListPromptsResult(ListPromptsResult {
prompts,
next_cursor: None,
..Default::default()
}))
}
rest => self.service.handle_request(rest, context).await,

View file

@ -828,6 +828,8 @@ macro_rules! paginated_result {
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct $t {
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<Meta>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<Cursor>,
pub $i_item: $t_item,
@ -838,6 +840,7 @@ macro_rules! paginated_result {
items: $t_item,
) -> Self {
Self {
meta: None,
next_cursor: None,
$i_item: items,
}

View file

@ -258,6 +258,7 @@ mod tests {
mime_type: Some("text/plain".to_string()),
size: Some(100),
icons: None,
meta: None,
});
let json = serde_json::to_string(&resource_link).unwrap();

View file

@ -2,7 +2,7 @@ use base64::engine::{Engine, general_purpose::STANDARD as BASE64_STANDARD};
use serde::{Deserialize, Serialize};
use super::{
AnnotateAble, Annotations, Icon, RawEmbeddedResource, RawImageContent,
AnnotateAble, Annotations, Icon, Meta, RawEmbeddedResource, RawImageContent,
content::{EmbeddedResource, ImageContent},
resource::ResourceContents,
};
@ -25,6 +25,9 @@ pub struct Prompt {
/// Optional list of icons for the prompt
#[serde(skip_serializing_if = "Option::is_none")]
pub icons: Option<Vec<Icon>>,
/// Optional additional metadata for this prompt
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<Meta>,
}
impl Prompt {
@ -44,6 +47,7 @@ impl Prompt {
description: description.map(Into::into),
arguments,
icons: None,
meta: None,
}
}
}

View file

@ -29,6 +29,9 @@ pub struct RawResource {
/// Optional list of icons for the resource
#[serde(skip_serializing_if = "Option::is_none")]
pub icons: Option<Vec<Icon>>,
/// Optional additional metadata for this resource
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<Meta>,
}
pub type Resource = Annotated<RawResource>;
@ -95,6 +98,7 @@ impl RawResource {
mime_type: None,
size: None,
icons: None,
meta: None,
}
}
}
@ -115,6 +119,7 @@ mod tests {
mime_type: Some("text/plain".to_string()),
size: Some(100),
icons: None,
meta: None,
};
let json = serde_json::to_string(&resource).unwrap();

View file

@ -992,6 +992,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"description": {
"description": "Optional description of the resource",
"type": [

View file

@ -992,6 +992,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"description": {
"description": "Optional description of the resource",
"type": [

View file

@ -172,6 +172,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
@ -1031,6 +1039,13 @@
"ListPromptsResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1051,6 +1066,13 @@
"ListResourceTemplatesResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1071,6 +1093,13 @@
"ListResourcesResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1096,6 +1125,13 @@
"ListToolsResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1472,6 +1508,14 @@
"description": "A prompt that can be used to generate text from a model",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this prompt",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"arguments": {
"description": "Optional arguments that can be passed to customize the prompt",
"type": [
@ -1660,6 +1704,14 @@
"description": "A link to a resource that can be fetched separately",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
@ -1816,6 +1868,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"description": {
"description": "Optional description of the resource",
"type": [

View file

@ -172,6 +172,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
@ -1031,6 +1039,13 @@
"ListPromptsResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1051,6 +1066,13 @@
"ListResourceTemplatesResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1071,6 +1093,13 @@
"ListResourcesResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1096,6 +1125,13 @@
"ListToolsResult": {
"type": "object",
"properties": {
"_meta": {
"type": [
"object",
"null"
],
"additionalProperties": true
},
"nextCursor": {
"type": [
"string",
@ -1472,6 +1508,14 @@
"description": "A prompt that can be used to generate text from a model",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this prompt",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"arguments": {
"description": "Optional arguments that can be passed to customize the prompt",
"type": [
@ -1660,6 +1704,14 @@
"description": "A link to a resource that can be fetched separately",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
@ -1816,6 +1868,14 @@
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
"_meta": {
"description": "Optional additional metadata for this resource",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"description": {
"description": "Optional description of the resource",
"type": [

View file

@ -110,7 +110,10 @@ impl Counter {
#[prompt_router]
impl Counter {
/// This is an example prompt that takes one required argument, message
#[prompt(name = "example_prompt")]
#[prompt(
name = "example_prompt",
meta = Meta(rmcp::object!({"meta_key": "meta_value"}))
)]
async fn example_prompt(
&self,
Parameters(args): Parameters<ExamplePromptArgs>,
@ -161,8 +164,8 @@ impl Counter {
}
}
#[tool_handler]
#[prompt_handler]
#[tool_handler(meta = Meta(rmcp::object!({"tool_meta_key": "tool_meta_value"})))]
#[prompt_handler(meta = Meta(rmcp::object!({"router_meta_key": "router_meta_value"})))]
impl ServerHandler for Counter {
fn get_info(&self) -> ServerInfo {
ServerInfo {
@ -188,6 +191,7 @@ impl ServerHandler for Counter {
self._create_resource_text("memo://insights", "memo-name"),
],
next_cursor: None,
meta: None,
})
}
@ -226,6 +230,7 @@ impl ServerHandler for Counter {
Ok(ListResourceTemplatesResult {
next_cursor: None,
resource_templates: Vec::new(),
meta: None,
})
}

View file

@ -125,6 +125,7 @@ impl ServerHandler for SamplingDemoServer {
icons: None,
meta: None,
}],
meta: None,
next_cursor: None,
})
}