|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::mcp::mcp_server::McpServer; |
| 5 | +use dsc_lib::{ |
| 6 | + configure::config_doc::ExecutionKind, |
| 7 | + dscresources::{ |
| 8 | + dscresource::Invoke, |
| 9 | + invoke_result::{ |
| 10 | + ExportResult, |
| 11 | + GetResult, |
| 12 | + SetResult, |
| 13 | + TestResult, |
| 14 | + }, |
| 15 | + }, |
| 16 | + DscManager, |
| 17 | +}; |
| 18 | +use rmcp::{ErrorData as McpError, Json, tool, tool_router, handler::server::wrapper::Parameters}; |
| 19 | +use rust_i18n::t; |
| 20 | +use schemars::JsonSchema; |
| 21 | +use serde::{Deserialize, Serialize}; |
| 22 | +use tokio::task; |
| 23 | + |
| 24 | +#[derive(Deserialize, JsonSchema)] |
| 25 | +#[serde(rename_all = "lowercase")] |
| 26 | +pub enum DscOperation { |
| 27 | + Get, |
| 28 | + Set, |
| 29 | + Test, |
| 30 | + Export, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Serialize, JsonSchema)] |
| 34 | +#[serde(untagged)] |
| 35 | +pub enum ResourceOperationResult { |
| 36 | + GetResult(GetResult), |
| 37 | + SetResult(SetResult), |
| 38 | + TestResult(TestResult), |
| 39 | + ExportResult(ExportResult), |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Serialize, JsonSchema)] |
| 43 | +pub struct InvokeDscResourceResponse { |
| 44 | + pub result: ResourceOperationResult, |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Deserialize, JsonSchema)] |
| 48 | +pub struct InvokeDscResourceRequest { |
| 49 | + #[schemars(description = "The operation to perform on the DSC resource")] |
| 50 | + pub operation: DscOperation, |
| 51 | + #[schemars(description = "The type name of the DSC resource to invoke")] |
| 52 | + pub resource_type: String, |
| 53 | + #[schemars(description = "The properties to pass to the DSC resource as JSON. Must match the resource JSON schema from `show_dsc_resource` tool.")] |
| 54 | + pub properties_json: String, |
| 55 | +} |
| 56 | + |
| 57 | +#[tool_router(router = invoke_dsc_resource_router, vis = "pub")] |
| 58 | +impl McpServer { |
| 59 | + #[tool( |
| 60 | + description = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format", |
| 61 | + annotations( |
| 62 | + title = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format", |
| 63 | + read_only_hint = false, |
| 64 | + destructive_hint = true, |
| 65 | + idempotent_hint = true, |
| 66 | + open_world_hint = true, |
| 67 | + ) |
| 68 | + )] |
| 69 | + pub async fn invoke_dsc_resource(&self, Parameters(InvokeDscResourceRequest { operation, resource_type, properties_json }): Parameters<InvokeDscResourceRequest>) -> Result<Json<InvokeDscResourceResponse>, McpError> { |
| 70 | + let result = task::spawn_blocking(move || { |
| 71 | + let mut dsc = DscManager::new(); |
| 72 | + let Some(resource) = dsc.find_resource(&resource_type, None) else { |
| 73 | + return Err(McpError::invalid_request(t!("mcp.invoke_dsc_resource.resourceNotFound", resource = resource_type), None)); |
| 74 | + }; |
| 75 | + match operation { |
| 76 | + DscOperation::Get => { |
| 77 | + let result = match resource.get(&properties_json) { |
| 78 | + Ok(res) => res, |
| 79 | + Err(e) => return Err(McpError::internal_error(e.to_string(), None)), |
| 80 | + }; |
| 81 | + Ok(ResourceOperationResult::GetResult(result)) |
| 82 | + }, |
| 83 | + DscOperation::Set => { |
| 84 | + let result = match resource.set(&properties_json, false, &ExecutionKind::Actual) { |
| 85 | + Ok(res) => res, |
| 86 | + Err(e) => return Err(McpError::internal_error(e.to_string(), None)), |
| 87 | + }; |
| 88 | + Ok(ResourceOperationResult::SetResult(result)) |
| 89 | + }, |
| 90 | + DscOperation::Test => { |
| 91 | + let result = match resource.test(&properties_json) { |
| 92 | + Ok(res) => res, |
| 93 | + Err(e) => return Err(McpError::internal_error(e.to_string(), None)), |
| 94 | + }; |
| 95 | + Ok(ResourceOperationResult::TestResult(result)) |
| 96 | + }, |
| 97 | + DscOperation::Export => { |
| 98 | + let result = match resource.export(&properties_json) { |
| 99 | + Ok(res) => res, |
| 100 | + Err(e) => return Err(McpError::internal_error(e.to_string(), None)), |
| 101 | + }; |
| 102 | + Ok(ResourceOperationResult::ExportResult(result)) |
| 103 | + } |
| 104 | + } |
| 105 | + }).await.map_err(|e| McpError::internal_error(e.to_string(), None))??; |
| 106 | + |
| 107 | + Ok(Json(InvokeDscResourceResponse { result })) |
| 108 | + } |
| 109 | +} |
0 commit comments