Skip to content

Fix the response from MCP server.#10

Open
stej wants to merge 3 commits intodfinke:mainfrom
stej:FixIncorrectResponse
Open

Fix the response from MCP server.#10
stej wants to merge 3 commits intodfinke:mainfrom
stej:FixIncorrectResponse

Conversation

@stej
Copy link

@stej stej commented Feb 16, 2026

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`neven more`nlines"}
)
$result | Out-string
A     B
-     -
test  some…
test2 some…

That's why Out-String is ok only for text types, but not for objects.

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…
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $resultText to format tool output as plain text for strings/value types and JSON for complex objects.
  • Updates the MCP response payload to return $resultText instead of piping $result to Out-String.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +49 to +60
$resultText = if ($null -eq $result) {
""
}
elseif ($result -is [string]) {
$result
}
elseif ($result -is [ValueType]) {
$result.ToString()
}
else {
$result | ConvertTo-Json -Depth 100
}
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment on lines +52 to +60
elseif ($result -is [string]) {
$result
}
elseif ($result -is [ValueType]) {
$result.ToString()
}
else {
$result | ConvertTo-Json -Depth 100
}
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

@dfinke dfinke added the enhancement New feature or request label Feb 16, 2026
stej and others added 2 commits March 17, 2026 09:51
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants