Conversation
If the $result contains some more complex objects (with more properties), Out-String makes 'some' representation of the objects. So some information is lost.
Example:
$result = @(
[pscustomobject]@{A='test'; B="some`nmore`nlines"},
[pscustomobject]@{A='test2'; B="some`nmore`nlines"}
)
$result | Out-string gives
A B
- -
test some…
test2 some…
There was a problem hiding this comment.
Pull request overview
This PR adjusts how Invoke-HandleRequest formats tool results in MCP tools/call responses so that non-text PowerShell objects aren’t lossy-formatted via Out-String.
Changes:
- Introduces
$resultTextto format tool output as plain text for strings/value types and JSON for complex objects. - Updates the MCP response payload to return
$resultTextinstead of piping$resulttoOut-String.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $resultText = if ($null -eq $result) { | ||
| "" | ||
| } | ||
| elseif ($result -is [string]) { | ||
| $result | ||
| } | ||
| elseif ($result -is [ValueType]) { | ||
| $result.ToString() | ||
| } | ||
| else { | ||
| $result | ConvertTo-Json -Depth 100 | ||
| } |
There was a problem hiding this comment.
There are Pester tests for other public entry points (e.g., Register-MCPTool), but none cover Invoke-HandleRequest's tools/call result formatting. Adding tests around this new $resultText branching (null, scalar string, string[], value types, complex objects serialized to JSON) would help prevent MCP response regressions.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| elseif ($result -is [string]) { | ||
| $result | ||
| } | ||
| elseif ($result -is [ValueType]) { | ||
| $result.ToString() | ||
| } | ||
| else { | ||
| $result | ConvertTo-Json -Depth 100 | ||
| } |
There was a problem hiding this comment.
$result -is [string] only matches a scalar string. If a tool writes multiple strings (common in PowerShell), $result becomes string[] and will fall into the JSON branch, so the client receives a literal JSON array string (e.g. ["a","b"]) instead of readable text lines. Consider detecting an enumerable of strings (or value types) and joining with newlines / Out-String for those cases, reserving ConvertTo-Json for non-text objects.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
If the
$resultcontains some more complex objects (with more properties), Out-String makes 'some' representation of the objects. So some information is lost.Example:
That's why
Out-Stringis ok only for text types, but not for objects.