|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::{args::SchemaType, mcp::mcp_server::McpServer, util}; |
| 5 | +use rmcp::{ErrorData as McpError, Json, tool, tool_router, handler::server::wrapper::Parameters}; |
| 6 | +use schemars::{JsonSchema, json_schema}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use serde_json::Value; |
| 9 | +use tokio::task; |
| 10 | + |
| 11 | +fn json_object_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 12 | + json_schema!({"type": "object"}) |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Deserialize, JsonSchema)] |
| 16 | +pub struct ShowSchemaRequest { |
| 17 | + #[schemars(description = "The schema type to retrieve the JSON schema for.")] |
| 18 | + pub r#type: SchemaType, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Serialize, JsonSchema)] |
| 22 | +pub struct ShowSchemaResponse { |
| 23 | + #[schemars(schema_with = "json_object_schema")] |
| 24 | + pub schema: Value, |
| 25 | +} |
| 26 | + |
| 27 | +#[tool_router(router = show_dsc_schema_router, vis = "pub")] |
| 28 | +impl McpServer { |
| 29 | + #[tool( |
| 30 | + description = "Get the JSON schema for a specific part of using DSC, such as a configuration or output from an operation.", |
| 31 | + annotations( |
| 32 | + title = "Get the JSON schema for a specific part of using DSC", |
| 33 | + read_only_hint = true, |
| 34 | + destructive_hint = false, |
| 35 | + idempotent_hint = true, |
| 36 | + open_world_hint = true, |
| 37 | + ) |
| 38 | + )] |
| 39 | + pub async fn show_dsc_schema(&self, Parameters(ShowSchemaRequest { r#type }): Parameters<ShowSchemaRequest>) -> Result<Json<ShowSchemaResponse>, McpError> { |
| 40 | + let result = task::spawn_blocking(move || { |
| 41 | + let schema = util::get_schema(r#type); |
| 42 | + Ok(ShowSchemaResponse { schema: schema.as_value().clone() }) |
| 43 | + }).await.map_err(|e| McpError::internal_error(e.to_string(), None))??; |
| 44 | + |
| 45 | + Ok(Json(result)) |
| 46 | + } |
| 47 | +} |
0 commit comments