1use super::*;
12use rmcp::model::{
13 GetPromptRequestParams, GetPromptResponse, GetPromptResult, ListPromptsResult,
14 ListResourcesResult, PaginatedRequestParams, ReadResourceRequestParams, ReadResourceResponse,
15 ReadResourceResult, ResourceContents,
16};
17use rmcp::service::RequestContext;
18use rmcp::RoleServer;
19
20pub(super) fn schema_lang(lang: &'static str) -> &'static str {
23 if schema_localization_enabled() {
24 lang
25 } else {
26 "en"
27 }
28}
29
30pub(super) fn localized_router(lang: &str) -> ToolRouter<PikoServer> {
33 let mut router = PikoServer::tool_router();
34 let texts = pikopiko::report::tools::for_lang(lang);
35 for (name, route) in router.map.iter_mut() {
36 let description = match name.as_ref() {
37 "get_spec" => texts.get_spec,
38 "list_synths" => texts.list_synths,
39 "check_mml" => texts.check_mml,
40 "describe_mml" => texts.describe_mml,
41 "build_mml" => texts.build_mml,
42 "render_mml" => texts.render_mml,
43 "edit_timbre" => texts.edit_timbre,
44 "measure_tone" => texts.measure_tone,
45 "get_tones" => texts.get_tones,
46 "format_mml" => texts.format_mml,
47 _ => continue,
48 };
49 route.attr.description = Some(Cow::Borrowed(description));
50 let mut schema = (*route.attr.input_schema).clone();
51 if let Some(Value::Object(props)) = schema.get_mut("properties") {
52 for (prop, v) in props.iter_mut() {
53 if let (Some(d), Value::Object(o)) = (pikopiko::report::tools::param(prop, lang), v)
54 {
55 o.insert("description".into(), Value::String(d.into()));
56 }
57 }
58 }
59 route.attr.input_schema = Arc::new(schema);
60 }
61 router
62}
63
64#[tool_router]
65impl PikoServer {
66 #[tool(
68 description = "get_spec",
69 annotations(read_only_hint = true, open_world_hint = false)
70 )]
71 pub(super) async fn get_spec(&self) -> Result<CallToolResult, McpError> {
72 Ok(CallToolResult::success(vec![ContentBlock::text(
73 pikopiko::spec_for(self.config.lang),
74 )]))
75 }
76
77 #[tool(
78 description = "list_synths",
79 annotations(read_only_hint = true, open_world_hint = false)
80 )]
81 pub(super) async fn list_synths(
82 &self,
83 args: Parameters<ListSynthsArgs>,
84 ) -> Result<CallToolResult, McpError> {
85 Ok(self.do_list_synths(&args.0))
86 }
87
88 #[tool(
89 description = "get_tones",
90 annotations(read_only_hint = true, idempotent_hint = true, open_world_hint = false)
91 )]
92 pub(super) async fn get_tones(
93 &self,
94 args: Parameters<GetTonesArgs>,
95 ) -> Result<CallToolResult, McpError> {
96 Ok(self.do_get_tones(&args.0))
97 }
98
99 #[tool(
100 description = "format_mml",
101 annotations(read_only_hint = true, idempotent_hint = true, open_world_hint = false)
102 )]
103 pub(super) async fn format_mml(
104 &self,
105 args: Parameters<FormatArgs>,
106 ) -> Result<CallToolResult, McpError> {
107 Ok(self.do_format(&args.0))
108 }
109
110 #[tool(
111 description = "check_mml",
112 annotations(read_only_hint = true, open_world_hint = false)
113 )]
114 pub(super) async fn check_mml(
115 &self,
116 args: Parameters<MmlArgs>,
117 ) -> Result<CallToolResult, McpError> {
118 Ok(self.do_check(&args.0))
119 }
120
121 #[tool(
122 description = "describe_mml",
123 annotations(read_only_hint = true, open_world_hint = false)
124 )]
125 pub(super) async fn describe_mml(
126 &self,
127 args: Parameters<MmlArgs>,
128 ) -> Result<CallToolResult, McpError> {
129 Ok(self.do_describe(&args.0))
130 }
131
132 #[tool(
133 description = "build_mml",
134 annotations(read_only_hint = true, open_world_hint = false)
135 )]
136 pub(super) async fn build_mml(
137 &self,
138 args: Parameters<BuildArgs>,
139 ) -> Result<CallToolResult, McpError> {
140 Ok(self.do_build(&args.0))
141 }
142
143 #[tool(
144 description = "edit_timbre",
145 annotations(read_only_hint = true, idempotent_hint = true, open_world_hint = false)
146 )]
147 pub(super) async fn edit_timbre(
148 &self,
149 args: Parameters<EditTimbreArgs>,
150 ) -> Result<CallToolResult, McpError> {
151 Ok(self.do_edit_timbre(&args.0))
152 }
153
154 #[tool(
155 description = "measure_tone",
156 annotations(
157 read_only_hint = false,
158 destructive_hint = false,
159 idempotent_hint = true,
160 open_world_hint = false
161 )
162 )]
163 pub(super) async fn measure_tone(
164 &self,
165 args: Parameters<MeasureToneArgs>,
166 ) -> Result<CallToolResult, McpError> {
167 Ok(self.do_measure_tone(&args.0))
168 }
169
170 #[tool(
171 description = "render_mml",
172 annotations(
173 read_only_hint = false,
174 destructive_hint = false,
175 idempotent_hint = true,
176 open_world_hint = false
177 )
178 )]
179 pub(super) async fn render_mml(
180 &self,
181 args: Parameters<RenderArgs>,
182 ) -> Result<CallToolResult, McpError> {
183 let this = self.clone();
185 let args = args.0;
186 tokio::task::spawn_blocking(move || this.do_render(&args))
187 .await
188 .map_err(|_| {
189 McpError::internal_error(self.tr("mcp-tool-render-aborted", &[]), None)
191 })
192 }
193}
194
195#[tool_handler(router = self.router)]
196impl ServerHandler for PikoServer {
197 async fn list_resources(
198 &self,
199 _request: Option<PaginatedRequestParams>,
200 _context: RequestContext<RoleServer>,
201 ) -> Result<ListResourcesResult, McpError> {
202 Ok(ListResourcesResult::with_all_items(self.resource_list()))
203 }
204
205 async fn read_resource(
206 &self,
207 request: ReadResourceRequestParams,
208 _context: RequestContext<RoleServer>,
209 ) -> Result<ReadResourceResponse, McpError> {
210 match self.resource_read(&request.uri) {
211 Some((mime, text)) => {
212 Ok(ReadResourceResult::new(vec![
213 ResourceContents::text(text, request.uri).with_mime_type(mime)
214 ])
215 .into())
216 }
217 None => Err(McpError::resource_not_found(
218 self.tr("mcp-resource-not-found", &[("uri", request.uri.clone())]),
219 None,
220 )),
221 }
222 }
223
224 async fn list_prompts(
225 &self,
226 _request: Option<PaginatedRequestParams>,
227 _context: RequestContext<RoleServer>,
228 ) -> Result<ListPromptsResult, McpError> {
229 Ok(ListPromptsResult::with_all_items(self.prompt_list()))
230 }
231
232 async fn get_prompt(
233 &self,
234 request: GetPromptRequestParams,
235 _context: RequestContext<RoleServer>,
236 ) -> Result<GetPromptResponse, McpError> {
237 let args = request.arguments.unwrap_or_default();
238 match self.prompt_get(&request.name, &args) {
239 Ok((description, messages)) => Ok(GetPromptResult::new(messages)
240 .with_description(description)
241 .into()),
242 Err(text) => Err(McpError::invalid_params(text, None)),
243 }
244 }
245
246 fn get_info(&self) -> ServerConfig {
247 ServerConfig::new(
248 ServerCapabilities::builder()
249 .enable_tools()
250 .enable_resources()
251 .enable_prompts()
252 .build(),
253 )
254 .with_server_info(Implementation::new(
255 "pikopiko-mcp",
256 env!("CARGO_PKG_VERSION"),
257 ))
258 .with_instructions(pikopiko::report::tools::for_lang(schema_lang(self.config.lang)).guide)
259 }
260}