Skip to content

Commit c63b8f3

Browse files
committed
Merge branch 'main' of https://github.com/Gijsreyn/operation-methods into gh-1093/main/invoke-mcp-config
2 parents 013deb6 + d311aae commit c63b8f3

8 files changed

Lines changed: 463 additions & 5 deletions

File tree

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
---
2+
description: Reference for the 'trim' DSC configuration document function
3+
ms.date: 01/10/2025
4+
ms.topic: reference
5+
title: trim
6+
---
7+
8+
# trim
9+
10+
## Synopsis
11+
12+
Removes all leading and trailing white-space characters from the specified string.
13+
14+
## Syntax
15+
16+
```Syntax
17+
trim(<stringToTrim>)
18+
```
19+
20+
## Description
21+
22+
The `trim()` function removes all leading and trailing white-space characters from
23+
the input string. White-space characters include spaces, tabs, newlines, carriage
24+
returns, and other Unicode whitespace characters. The function preserves internal
25+
whitespace within the string. Use it for cleaning user input, normalizing
26+
configuration values, or preparing strings for comparison.
27+
28+
## Examples
29+
30+
### Example 1 - Clean user input
31+
32+
The following example removes leading and trailing spaces from a parameter value.
33+
34+
```yaml
35+
# trim.example.1.dsc.config.yaml
36+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
37+
parameters:
38+
userName:
39+
type: string
40+
defaultValue: ' admin '
41+
resources:
42+
- name: Clean user input
43+
type: Microsoft.DSC.Debug/Echo
44+
properties:
45+
output:
46+
rawInput: "[parameters('userName')]"
47+
cleanedInput: "[trim(parameters('userName'))]"
48+
```
49+
50+
```bash
51+
dsc config get --file trim.example.1.dsc.config.yaml
52+
```
53+
54+
```yaml
55+
results:
56+
- name: Clean user input
57+
type: Microsoft.DSC.Debug/Echo
58+
result:
59+
actualState:
60+
output:
61+
rawInput: ' admin '
62+
cleanedInput: admin
63+
messages: []
64+
hadErrors: false
65+
```
66+
67+
### Example 2 - Normalize file paths
68+
69+
The following example demonstrates using `trim()` with [`concat()`][01] to clean
70+
path components before building a complete file path.
71+
72+
```yaml
73+
# trim.example.2.dsc.config.yaml
74+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
75+
parameters:
76+
baseDir:
77+
type: string
78+
defaultValue: ' /var/log '
79+
fileName:
80+
type: string
81+
defaultValue: ' app.log '
82+
resources:
83+
- name: Build clean file path
84+
type: Microsoft.DSC.Debug/Echo
85+
properties:
86+
output:
87+
filePath: "[concat(trim(parameters('baseDir')), '/', trim(parameters('fileName')))]"
88+
```
89+
90+
```bash
91+
dsc config get --file trim.example.2.dsc.config.yaml
92+
```
93+
94+
```yaml
95+
results:
96+
- name: Build clean file path
97+
type: Microsoft.DSC.Debug/Echo
98+
result:
99+
actualState:
100+
output:
101+
filePath: /var/log/app.log
102+
messages: []
103+
hadErrors: false
104+
```
105+
106+
### Example 3 - Clean configuration values for comparison
107+
108+
The following example uses `trim()` to normalize strings before comparing them with
109+
the [`equals()`][02] function.
110+
111+
```yaml
112+
# trim.example.3.dsc.config.yaml
113+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
114+
parameters:
115+
expectedEnv:
116+
type: string
117+
defaultValue: production
118+
actualEnv:
119+
type: string
120+
defaultValue: ' production '
121+
resources:
122+
- name: Environment comparison
123+
type: Microsoft.DSC.Debug/Echo
124+
properties:
125+
output:
126+
matches: "[equals(trim(parameters('actualEnv')), parameters('expectedEnv'))]"
127+
```
128+
129+
```bash
130+
dsc config get --file trim.example.3.dsc.config.yaml
131+
```
132+
133+
```yaml
134+
results:
135+
- name: Environment comparison
136+
type: Microsoft.DSC.Debug/Echo
137+
result:
138+
actualState:
139+
output:
140+
matches: true
141+
messages: []
142+
hadErrors: false
143+
```
144+
145+
### Example 4 - Process multi-line configuration
146+
147+
The following example shows how `trim()` handles tabs, newlines, and various
148+
whitespace characters.
149+
150+
```yaml
151+
# trim.example.4.dsc.config.yaml
152+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
153+
resources:
154+
- name: Whitespace handling
155+
type: Microsoft.DSC.Debug/Echo
156+
properties:
157+
output:
158+
spaces: "[trim(' content ')]"
159+
mixed: "[trim(' \t\n content \n\t ')]"
160+
internal: "[trim(' multiple spaces inside ')]"
161+
```
162+
163+
```bash
164+
dsc config get --file trim.example.4.dsc.config.yaml
165+
```
166+
167+
```yaml
168+
results:
169+
- name: Whitespace handling
170+
type: Microsoft.DSC.Debug/Echo
171+
result:
172+
actualState:
173+
output:
174+
spaces: content
175+
mixed: content
176+
internal: multiple spaces inside
177+
messages: []
178+
hadErrors: false
179+
```
180+
181+
### Example 5 - Combine with case conversion
182+
183+
The following example demonstrates using `trim()` with [`toLower()`][00] to both
184+
clean and normalize a string value.
185+
186+
```yaml
187+
# trim.example.5.dsc.config.yaml
188+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
189+
parameters:
190+
serviceName:
191+
type: string
192+
defaultValue: ' WEB-SERVER '
193+
resources:
194+
- name: Clean and normalize service name
195+
type: Microsoft.DSC.Debug/Echo
196+
properties:
197+
output:
198+
original: "[parameters('serviceName')]"
199+
normalized: "[toLower(trim(parameters('serviceName')))]"
200+
```
201+
202+
```bash
203+
dsc config get --file trim.example.5.dsc.config.yaml
204+
```
205+
206+
```yaml
207+
results:
208+
- name: Clean and normalize service name
209+
type: Microsoft.DSC.Debug/Echo
210+
result:
211+
actualState:
212+
output:
213+
original: ' WEB-SERVER '
214+
normalized: web-server
215+
messages: []
216+
hadErrors: false
217+
```
218+
219+
## Parameters
220+
221+
### stringToTrim
222+
223+
The string value to remove leading and trailing whitespace from.
224+
225+
```yaml
226+
Type: string
227+
Required: true
228+
Position: 1
229+
```
230+
231+
## Output
232+
233+
The `trim()` function returns the input string with all leading and trailing
234+
white-space characters removed. Internal whitespace is preserved.
235+
236+
```yaml
237+
Type: string
238+
```
239+
240+
## Related functions
241+
242+
- [`toLower()`][00] - Converts a string to lower case
243+
- [`concat()`][01] - Concatenates strings together
244+
- [`equals()`][02] - Compares two values for equality
245+
- [`startsWith()`][03] - Checks if a string starts with a value
246+
- [`endsWith()`][04] - Checks if a string ends with a value
247+
- [`substring()`][05] - Extracts a portion of a string
248+
- [`replace()`][06] - Replaces text in a string
249+
- [`parameters()`][07] - Retrieves parameter values
250+
251+
<!-- Link reference definitions -->
252+
[00]: ./toLower.md
253+
[01]: ./concat.md
254+
[02]: ./equals.md
255+
[03]: ./startsWith.md
256+
[04]: ./endsWith.md
257+
[05]: ./substring.md
258+
[06]: ./replace.md
259+
[07]: ./parameters.md

dsc/src/mcp/invoke_dsc_resource.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum DscOperation {
2828
Set,
2929
Test,
3030
Export,
31+
Delete,
3132
}
3233

3334
#[derive(Serialize, JsonSchema)]
@@ -37,6 +38,7 @@ pub enum ResourceOperationResult {
3738
SetResult(SetResult),
3839
TestResult(TestResult),
3940
ExportResult(ExportResult),
41+
DeleteResult { success: bool },
4042
}
4143

4244
#[derive(Serialize, JsonSchema)]
@@ -57,9 +59,9 @@ pub struct InvokeDscResourceRequest {
5759
#[tool_router(router = invoke_dsc_resource_router, vis = "pub")]
5860
impl McpServer {
5961
#[tool(
60-
description = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format",
62+
description = "Invoke a DSC resource operation (Get, Set, Test, Export, Delete) with specified properties in JSON format",
6163
annotations(
62-
title = "Invoke a DSC resource operation (Get, Set, Test, Export) with specified properties in JSON format",
64+
title = "Invoke a DSC resource operation (Get, Set, Test, Export, Delete) with specified properties in JSON format",
6365
read_only_hint = false,
6466
destructive_hint = true,
6567
idempotent_hint = true,
@@ -94,6 +96,12 @@ impl McpServer {
9496
};
9597
Ok(ResourceOperationResult::TestResult(result))
9698
},
99+
DscOperation::Delete => {
100+
match resource.delete(&properties_json) {
101+
Ok(()) => Ok(ResourceOperationResult::DeleteResult { success: true }),
102+
Err(e) => Err(McpError::internal_error(e.to_string(), None)),
103+
}
104+
},
97105
DscOperation::Export => {
98106
let result = match resource.export(&properties_json) {
99107
Ok(res) => res,

dsc/tests/dsc_functions.tests.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,31 @@ Describe 'tests for function expressions' {
831831
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
832832
$out.results[0].result.actualState.output | Should -Be $expected
833833
}
834+
835+
It 'trim function works for: <expression>' -TestCases @(
836+
@{ expression = "[trim(' hello')]"; expected = 'hello' }
837+
@{ expression = "[trim('hello ')]"; expected = 'hello' }
838+
@{ expression = "[trim(' hello world ')]"; expected = 'hello world' }
839+
@{ expression = "[trim('hello')]"; expected = 'hello' }
840+
@{ expression = "[trim('')]"; expected = '' }
841+
@{ expression = "[trim(' ')]"; expected = '' }
842+
@{ expression = "[trim(' hello world ')]"; expected = 'hello world' }
843+
@{ expression = "[trim(' café ')]"; expected = 'café' }
844+
@{ expression = "[trim(' a ')]"; expected = 'a' }
845+
@{ expression = "[trim(concat(' hello', ' '))]"; expected = 'hello' }
846+
) {
847+
param($expression, $expected)
848+
849+
$escapedExpression = $expression -replace "'", "''"
850+
$config_yaml = @"
851+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
852+
resources:
853+
- name: Echo
854+
type: Microsoft.DSC.Debug/Echo
855+
properties:
856+
output: '$escapedExpression'
857+
"@
858+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
859+
$out.results[0].result.actualState.output | Should -Be $expected
860+
}
834861
}

dsc/tests/dsc_mcp.tests.ps1

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Describe 'Tests for MCP server' {
7171
}
7272

7373
$tools = @{
74-
'invoke_dsc_config' = $false
7574
'invoke_dsc_resource' = $false
7675
'list_dsc_functions' = $false
7776
'list_dsc_resources' = $false
@@ -307,7 +306,7 @@ Describe 'Tests for MCP server' {
307306
@{ operation = 'test'; property = 'desiredState' }
308307
@{ operation = 'export'; property = 'actualState' }
309308
) {
310-
param($operation)
309+
param($operation, $property)
311310

312311
$mcpRequest = @{
313312
jsonrpc = "2.0"
@@ -335,6 +334,32 @@ Describe 'Tests for MCP server' {
335334
$response.result.structuredContent.result.$property.hello | Should -BeExactly "World" -Because $because
336335
}
337336

337+
It 'Calling invoke_dsc_resource for delete operation' {
338+
$mcpRequest = @{
339+
jsonrpc = "2.0"
340+
id = 12
341+
method = "tools/call"
342+
params = @{
343+
name = "invoke_dsc_resource"
344+
arguments = @{
345+
type = 'Test/Operation'
346+
operation = 'delete'
347+
resource_type = 'Test/Operation'
348+
properties_json = (@{
349+
hello = "World"
350+
action = 'delete'
351+
} | ConvertTo-Json -Depth 20)
352+
}
353+
}
354+
}
355+
356+
$response = Send-McpRequest -request $mcpRequest
357+
$response.id | Should -Be 12
358+
$because = ($response | ConvertTo-Json -Depth 20 | Out-String)
359+
($response.result.structuredContent.psobject.properties | Measure-Object).Count | Should -Be 1 -Because $because
360+
$response.result.structuredContent.result.success | Should -Be $true -Because $because
361+
}
362+
338363
It 'Calling invoke_dsc_config for operation: <operation>' -TestCases @(
339364
@{ operation = 'get' }
340365
@{ operation = 'set' }
@@ -585,4 +610,4 @@ greeting: Hello from YAML parameters
585610
$response.error.code | Should -Be -32600
586611
$response.error.message | Should -Match 'Invalid parameters'
587612
}
588-
}
613+
}

lib/dsc-lib/locales/en-us.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ description = "Converts the specified string to lower case"
512512
[functions.toUpper]
513513
description = "Converts the specified string to upper case"
514514

515+
[functions.trim]
516+
description = "Removes all leading and trailing white-space characters from the specified string"
517+
515518
[functions.true]
516519
description = "Returns the boolean value true"
517520
invoked = "true function"

0 commit comments

Comments
 (0)