-
Notifications
You must be signed in to change notification settings - Fork 55
Add invoke_dsc_resource() MCP tool
#1162
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7509434
Add `invoke_dsc_resource()` tool to MCP
81585e2
add test tracing
9600755
remove test tracing, expand tokio macro and use multithread
e8bbf96
wrap result in object to satisfy mcp
72d39bb
add test tracing
274eb46
remove test tracing, expand tokio macro and use multithread
d9f140c
Fix tool and add tests
00e81ce
fix pipeline
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,109 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::mcp::mcp_server::McpServer; | ||
| use dsc_lib::{ | ||
| configure::config_doc::ExecutionKind, | ||
| dscresources::{ | ||
| dscresource::Invoke, | ||
| invoke_result::{ | ||
| ExportResult, | ||
| GetResult, | ||
| SetResult, | ||
| TestResult, | ||
| }, | ||
| }, | ||
| DscManager, | ||
| }; | ||
| use rmcp::{ErrorData as McpError, Json, tool, tool_router, handler::server::wrapper::Parameters}; | ||
| use rust_i18n::t; | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use tokio::task; | ||
|
|
||
| #[derive(Deserialize, JsonSchema)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub enum DscOperation { | ||
| Get, | ||
| Set, | ||
| Test, | ||
| Export, | ||
| } | ||
|
|
||
| #[derive(Serialize, JsonSchema)] | ||
| #[serde(untagged)] | ||
| pub enum ResourceOperationResult { | ||
| GetResult(GetResult), | ||
| SetResult(SetResult), | ||
| TestResult(TestResult), | ||
| ExportResult(ExportResult), | ||
| } | ||
|
|
||
| #[derive(Serialize, JsonSchema)] | ||
| pub struct InvokeDscResourceResponse { | ||
| pub result: ResourceOperationResult, | ||
| } | ||
|
|
||
| #[derive(Deserialize, JsonSchema)] | ||
| pub struct InvokeDscResourceRequest { | ||
| #[schemars(description = "The operation to perform on the DSC resource")] | ||
| pub operation: DscOperation, | ||
| #[schemars(description = "The type name of the DSC resource to invoke")] | ||
| pub resource_type: String, | ||
| #[schemars(description = "The properties to pass to the DSC resource as JSON. Must match the resource JSON schema from `show_dsc_resource` tool.")] | ||
| pub properties_json: String, | ||
| } | ||
|
|
||
| #[tool_router(router = invoke_dsc_resource_router, vis = "pub")] | ||
| impl McpServer { | ||
| #[tool( | ||
| description = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format", | ||
| annotations( | ||
| title = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format", | ||
| read_only_hint = false, | ||
| destructive_hint = true, | ||
| idempotent_hint = true, | ||
| open_world_hint = true, | ||
| ) | ||
| )] | ||
| pub async fn invoke_dsc_resource(&self, Parameters(InvokeDscResourceRequest { operation, resource_type, properties_json }): Parameters<InvokeDscResourceRequest>) -> Result<Json<InvokeDscResourceResponse>, McpError> { | ||
| let result = task::spawn_blocking(move || { | ||
| let mut dsc = DscManager::new(); | ||
| let Some(resource) = dsc.find_resource(&resource_type, None) else { | ||
| return Err(McpError::invalid_request(t!("mcp.invoke_dsc_resource.resourceNotFound", resource = resource_type), None)); | ||
| }; | ||
| match operation { | ||
| DscOperation::Get => { | ||
| let result = match resource.get(&properties_json) { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ResourceOperationResult::GetResult(result)) | ||
| }, | ||
| DscOperation::Set => { | ||
| let result = match resource.set(&properties_json, false, &ExecutionKind::Actual) { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ResourceOperationResult::SetResult(result)) | ||
| }, | ||
| DscOperation::Test => { | ||
| let result = match resource.test(&properties_json) { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ResourceOperationResult::TestResult(result)) | ||
| }, | ||
| DscOperation::Export => { | ||
| let result = match resource.export(&properties_json) { | ||
| Ok(res) => res, | ||
| Err(e) => return Err(McpError::internal_error(e.to_string(), None)), | ||
| }; | ||
| Ok(ResourceOperationResult::ExportResult(result)) | ||
| } | ||
| } | ||
| }).await.map_err(|e| McpError::internal_error(e.to_string(), None))??; | ||
|
|
||
| Ok(Json(InvokeDscResourceResponse { 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
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
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,59 @@ | ||
| { | ||
| "$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json", | ||
| "type": "Test/Operation", | ||
| "version": "0.1.0", | ||
| "get": { | ||
| "executable": "dsctest", | ||
| "args": [ | ||
| "operation", | ||
| "--operation", | ||
| "get", | ||
| { | ||
| "jsonInputArg": "--input" | ||
| } | ||
| ] | ||
| }, | ||
| "set": { | ||
| "executable": "dsctest", | ||
| "args": [ | ||
| "operation", | ||
| "--operation", | ||
| "set", | ||
| { | ||
| "jsonInputArg": "--input" | ||
| } | ||
| ] | ||
| }, | ||
| "test": { | ||
| "executable": "dsctest", | ||
| "args": [ | ||
| "operation", | ||
| "--operation", | ||
| "trace", | ||
| { | ||
| "jsonInputArg": "--input" | ||
| } | ||
| ] | ||
| }, | ||
| "export": { | ||
| "executable": "dsctest", | ||
| "args": [ | ||
| "operation", | ||
| "--operation", | ||
| "export", | ||
| { | ||
| "jsonInputArg": "--input" | ||
| } | ||
| ] | ||
| }, | ||
| "schema": { | ||
| "command": { | ||
| "executable": "dsctest", | ||
| "args": [ | ||
| "schema", | ||
| "-s", | ||
| "operation" | ||
| ] | ||
| } | ||
| } | ||
| } |
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.