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
use crate::common::{ExecutionSupportDsl, ExprList, IconDsl};
use quote::ToTokens;
use syn::{parse::Parse, punctuated::Punctuated, Error, Expr, ExprLit, Lit, Meta, Token};
const VALID_ROLES: [&str; 2] = ["assistant", "user"];
#[derive(Debug)]
pub(crate) struct GenericMcpMacroAttributes {
pub name: Option<String>,
pub description: Option<String>,
pub meta: Option<String>, // Store raw JSON string instead of parsed Map
pub title: Option<String>,
pub icons: Option<Vec<IconDsl>>,
pub mime_type: Option<String>,
pub size: Option<i64>,
pub uri: Option<String>,
pub uri_template: Option<String>,
pub audience: Option<Vec<String>>,
// tool specific
pub destructive_hint: Option<bool>,
pub idempotent_hint: Option<bool>,
pub open_world_hint: Option<bool>,
pub read_only_hint: Option<bool>,
pub execution: Option<ExecutionSupportDsl>,
}
impl Parse for GenericMcpMacroAttributes {
fn parse(attributes: syn::parse::ParseStream) -> syn::Result<Self> {
let mut instance = Self {
name: None,
description: None,
meta: None,
title: None,
icons: None,
mime_type: None,
size: None,
uri: None,
uri_template: None,
audience: None,
destructive_hint: None,
idempotent_hint: None,
open_world_hint: None,
read_only_hint: None,
execution: None,
};
let meta_list: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated(attributes)?;
for meta in meta_list {
match meta {
Meta::NameValue(meta_name_value) => {
let ident = meta_name_value.path.get_ident().unwrap();
let ident_str = ident.to_string();
match ident_str.as_str() {
// string literal or concat!()
"name" | "description" | "title" => {
let value = match &meta_name_value.value {
Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) => lit_str.value(),
Expr::Macro(expr_macro) => {
let mac = &expr_macro.mac;
if mac.path.is_ident("concat") {
let args: ExprList = syn::parse2(mac.tokens.clone())?;
let mut result = String::new();
for expr in args.exprs {
if let Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) = expr
{
result.push_str(&lit_str.value());
} else {
return Err(Error::new_spanned(
expr,
"Only string literals are allowed inside concat!()",
));
}
}
result
} else {
return Err(Error::new_spanned(
expr_macro,
"Expected a string literal or concat!(...)",
));
}
}
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a string literal or concat!(...)",
));
}
};
match ident_str.as_str() {
"name" => instance.name = Some(value),
"description" => instance.description = Some(value),
"title" => instance.title = Some(value),
_ => {}
}
}
// string literals
"mime_type" | "uri" | "uri_template" => {
let value = match &meta_name_value.value {
Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) => lit_str.value(),
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a string literal",
));
}
};
match ident_str.as_str() {
"mime_type" => instance.mime_type = Some(value),
"uri" => instance.uri = Some(value),
"uri_template" => instance.uri_template = Some(value),
_ => {}
}
}
// i64
"size" => {
let value = match &meta_name_value.value {
Expr::Lit(ExprLit {
lit: Lit::Int(lit_int),
..
}) => lit_int.base10_parse::<i64>()?,
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a integer literal",
));
}
};
instance.size = Some(value);
}
"meta" => {
let value = match &meta_name_value.value {
Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) => lit_str.value(),
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a JSON object as a string literal",
));
}
};
// Validate that the string is a valid JSON object
let parsed: serde_json::Value =
serde_json::from_str(&value).map_err(|e| {
Error::new_spanned(
&meta_name_value.value,
format!("Expected a valid JSON object: {e}"),
)
})?;
if !parsed.is_object() {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a JSON object",
));
}
instance.meta = Some(value);
}
"audience" => {
let values = match &meta_name_value.value {
Expr::Array(expr_array) => {
let mut result = Vec::new();
for elem in &expr_array.elems {
match elem {
Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}) => {
if !VALID_ROLES.contains(&lit_str.value().as_str())
{
return Err(Error::new_spanned(
elem,
format!(
"valid audience values are : {}",
VALID_ROLES.join(" , ")
),
));
}
result.push(lit_str.value());
}
_ => {
return Err(Error::new_spanned(
elem,
"Expected a string literal in array",
));
}
}
}
result
}
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected an array of string literals, e.g. [\"system\", \"user\"]",
));
}
};
instance.audience = Some(values);
}
"icons" => {
// Check if the value is an array (Expr::Array)
if let Expr::Array(array_expr) = &meta_name_value.value {
let icon_list: Punctuated<IconDsl, Token![,]> = array_expr
.elems
.iter()
.map(|elem| syn::parse2::<IconDsl>(elem.to_token_stream()))
.collect::<Result<_, _>>()?;
instance.icons = Some(icon_list.into_iter().collect());
} else {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected an array for the 'icons' attribute",
));
}
}
// for tools annotations
"destructive_hint" | "idempotent_hint" | "open_world_hint"
| "read_only_hint" => {
let value = match &meta_name_value.value {
Expr::Lit(ExprLit {
lit: Lit::Bool(lit_bool),
..
}) => lit_bool.value,
_ => {
return Err(Error::new_spanned(
&meta_name_value.value,
"Expected a boolean literal",
));
}
};
match ident_str.as_str() {
"destructive_hint" => instance.destructive_hint = Some(value),
"idempotent_hint" => instance.idempotent_hint = Some(value),
"open_world_hint" => instance.open_world_hint = Some(value),
"read_only_hint" => instance.read_only_hint = Some(value),
_ => {}
}
}
other => {
eprintln!("other: {:?}", other)
}
}
}
Meta::List(meta_list) => {
let ident = meta_list.path.get_ident().unwrap();
let ident_str = ident.to_string();
if ident_str == "execution" {
let nested = meta_list
.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
let mut task_support = None;
for meta in nested {
if let Meta::NameValue(nv) = meta {
if nv.path.is_ident("task_support") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = &nv.value
{
let value = s.value();
task_support = Some(match value.as_str() {
"forbidden" => ExecutionSupportDsl::Forbidden,
"optional" => ExecutionSupportDsl::Optional,
"required" => ExecutionSupportDsl::Required,
_ => return Err(Error::new_spanned(&nv.value, "task_support must be one of: forbidden, optional, required")),
});
}
}
}
}
instance.execution = task_support;
}
}
_ => {}
}
}
Ok(instance)
}
}