-
Notifications
You must be signed in to change notification settings - Fork 54
Add invoke_dsc_config() MCP tool
#1174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gijsreyn
wants to merge
10
commits into
PowerShell:main
Choose a base branch
from
Gijsreyn:gh-1093/main/invoke-mcp-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
013deb6
Add invoke_dsc_config() MCP tool
Gijsreyn c63b8f3
Merge branch 'main' of https://github.com/Gijsreyn/operation-methods …
Gijsreyn 4b7c82a
Format
Gijsreyn 75871a1
Add line break
Gijsreyn 0277279
Forgot tool count
Gijsreyn 9635eab
Merge branch 'main' into gh-1093/main/invoke-mcp-config
Gijsreyn 4b20ca9
Merge branch 'main' into gh-1093/main/invoke-mcp-config
Gijsreyn 23b4e37
Merge branch 'main' into gh-1093/main/invoke-mcp-config
Gijsreyn 5bb3af8
Test with YAML
Gijsreyn 46f4cbf
Merge branch 'main' into gh-1093/main/invoke-mcp-config
Gijsreyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::mcp::mcp_server::McpServer; | ||
| use dsc_lib::{ | ||
| configure::{ | ||
| config_doc::Configuration, | ||
| config_result::{ | ||
| ConfigurationExportResult, ConfigurationGetResult, ConfigurationSetResult, | ||
| ConfigurationTestResult, | ||
| }, | ||
| Configurator, | ||
| }, | ||
| progress::ProgressFormat, | ||
| }; | ||
| use rmcp::{handler::server::wrapper::Parameters, tool, tool_router, ErrorData as McpError, Json}; | ||
| use rust_i18n::t; | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use tokio::task; | ||
|
|
||
| #[derive(Deserialize, JsonSchema)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub enum ConfigOperation { | ||
| Get, | ||
| Set, | ||
| Test, | ||
| Export, | ||
| } | ||
|
|
||
| #[derive(Serialize, JsonSchema)] | ||
| #[serde(untagged)] | ||
| pub enum ConfigOperationResult { | ||
| GetResult(Box<ConfigurationGetResult>), | ||
| SetResult(Box<ConfigurationSetResult>), | ||
| TestResult(Box<ConfigurationTestResult>), | ||
| ExportResult(Box<ConfigurationExportResult>), | ||
| } | ||
|
|
||
| #[derive(Serialize, JsonSchema)] | ||
| pub struct InvokeDscConfigResponse { | ||
| pub result: ConfigOperationResult, | ||
| } | ||
|
|
||
| #[derive(Deserialize, JsonSchema)] | ||
| pub struct InvokeDscConfigRequest { | ||
| #[schemars(description = "The operation to perform on the DSC configuration")] | ||
| pub operation: ConfigOperation, | ||
| #[schemars(description = "The DSC configuration document as JSON or YAML string")] | ||
| pub configuration: String, | ||
| #[schemars( | ||
| description = "Optional parameters to pass to the configuration as JSON or YAML string" | ||
| )] | ||
| pub parameters: Option<String>, | ||
| } | ||
|
|
||
| #[tool_router(router = invoke_dsc_config_router, vis = "pub")] | ||
| impl McpServer { | ||
| #[tool( | ||
| description = "Invoke a DSC configuration operation (Get, Set, Test, Export) with optional parameters", | ||
| annotations( | ||
| title = "Invoke a DSC configuration operation (Get, Set, Test, Export) with optional parameters", | ||
| read_only_hint = false, | ||
| destructive_hint = true, | ||
| idempotent_hint = true, | ||
| open_world_hint = true, | ||
| ) | ||
| )] | ||
| pub async fn invoke_dsc_config( | ||
| &self, | ||
| Parameters(InvokeDscConfigRequest { | ||
| operation, | ||
| configuration, | ||
| parameters, | ||
| }): Parameters<InvokeDscConfigRequest>, | ||
| ) -> Result<Json<InvokeDscConfigResponse>, McpError> { | ||
| let result = task::spawn_blocking(move || { | ||
| let config: Configuration = match serde_json::from_str(&configuration) { | ||
| Ok(config) => config, | ||
| Err(_) => { | ||
| match serde_yaml::from_str::<serde_yaml::Value>(&configuration) { | ||
| Ok(yaml_value) => match serde_json::to_value(yaml_value) { | ||
| Ok(json_value) => match serde_json::from_value(json_value) { | ||
| Ok(config) => config, | ||
| Err(e) => { | ||
| return Err(McpError::invalid_request( | ||
| format!( | ||
| "{}: {e}", | ||
| t!("mcp.invoke_dsc_config.invalidConfiguration") | ||
| ), | ||
| None, | ||
| )) | ||
| } | ||
| }, | ||
| Err(e) => { | ||
| return Err(McpError::invalid_request( | ||
| format!( | ||
| "{}: {e}", | ||
| t!("mcp.invoke_dsc_config.failedConvertJson") | ||
| ), | ||
| None, | ||
| )) | ||
| } | ||
| }, | ||
| Err(e) => { | ||
| return Err(McpError::invalid_request( | ||
| format!( | ||
| "{}: {e}", | ||
| t!("mcp.invoke_dsc_config.invalidConfiguration") | ||
| ), | ||
| None, | ||
| )) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let config_json = match serde_json::to_string(&config) { | ||
| Ok(json) => json, | ||
| Err(e) => { | ||
| return Err(McpError::internal_error( | ||
| format!("{}: {e}", t!("mcp.invoke_dsc_config.failedSerialize")), | ||
| None, | ||
| )) | ||
| } | ||
| }; | ||
|
|
||
| let mut configurator = match Configurator::new(&config_json, ProgressFormat::None) { | ||
| Ok(configurator) => configurator, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
|
|
||
| configurator.context.dsc_version = Some(env!("CARGO_PKG_VERSION").to_string()); | ||
|
|
||
| let parameters_value: Option<serde_json::Value> = if let Some(params_str) = parameters { | ||
| let params_json = match serde_json::from_str(¶ms_str) { | ||
| Ok(json) => json, | ||
| Err(_) => { | ||
| match serde_yaml::from_str::<serde_yaml::Value>(¶ms_str) { | ||
| Ok(yaml) => match serde_json::to_value(yaml) { | ||
| Ok(json) => json, | ||
| Err(e) => { | ||
| return Err(McpError::invalid_request( | ||
| format!( | ||
| "{}: {e}", | ||
| t!("mcp.invoke_dsc_config.failedConvertJson") | ||
| ), | ||
| None, | ||
| )) | ||
| } | ||
| }, | ||
| Err(e) => { | ||
| return Err(McpError::invalid_request( | ||
| format!( | ||
| "{}: {e}", | ||
| t!("mcp.invoke_dsc_config.invalidParameters") | ||
| ), | ||
| None, | ||
| )) | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Wrap parameters in a "parameters" field for configurator.set_context() | ||
| Some(serde_json::json!({ | ||
| "parameters": params_json | ||
| })) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| if let Err(e) = configurator.set_context(parameters_value.as_ref()) { | ||
| return Err(McpError::invalid_request( | ||
| format!("{}: {e}", t!("mcp.invoke_dsc_config.failedSetParameters")), | ||
| None, | ||
| )); | ||
| } | ||
|
|
||
| match operation { | ||
| ConfigOperation::Get => { | ||
| let result = match configurator.invoke_get() { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ConfigOperationResult::GetResult(Box::new(result))) | ||
| } | ||
| ConfigOperation::Set => { | ||
| let result = match configurator.invoke_set(false) { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ConfigOperationResult::SetResult(Box::new(result))) | ||
| } | ||
| ConfigOperation::Test => { | ||
| let result = match configurator.invoke_test() { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ConfigOperationResult::TestResult(Box::new(result))) | ||
| } | ||
| ConfigOperation::Export => { | ||
| let result = match configurator.invoke_export() { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ConfigOperationResult::ExportResult(Box::new(result))) | ||
| } | ||
| } | ||
| }) | ||
| .await | ||
| .map_err(|e| McpError::internal_error(e.to_string(), None))??; | ||
|
|
||
| Ok(Json(InvokeDscConfigResponse { result })) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.