Skip to content

Commit 4d45f03

Browse files
azure-sdkbenbp
andauthored
Log output from mcp installer script as json-rpc (#37820)
Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent 47da73f commit 4d45f03

File tree

1 file changed

+84
-11
lines changed

1 file changed

+84
-11
lines changed

eng/common/mcp/azure-sdk-mcp.ps1

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,54 @@ $ErrorActionPreference = "Stop"
2020

2121
$toolInstallDirectory = $InstallDirectory ? $InstallDirectory : (Get-CommonInstallDirectory)
2222

23+
$mcpMode = $Run
24+
25+
# Log to console or MCP client json-rpc
26+
function log([object]$message, [switch]$warn, [switch]$err) {
27+
[string]$messageString = $message
28+
29+
# Assume we are in an MCP client context when `-Run` is specified
30+
# otherwise print to console normally
31+
if (!$mcpMode) {
32+
if ($err) {
33+
Write-Error $messageString -ErrorAction Continue
34+
}
35+
elseif ($warn) {
36+
Write-Warning $messageString
37+
}
38+
else {
39+
Write-Host $messageString
40+
}
41+
return;
42+
}
43+
44+
$level = switch ($message) {
45+
{ $_ -is [System.Management.Automation.ErrorRecord] } { 'error' }
46+
{ $_ -is [System.Management.Automation.WarningRecord] } { 'warning' }
47+
default { 'notice' }
48+
}
49+
50+
# If message stringifies as a valid error message we want to print it
51+
# otherwise print stack for calls to external binaries
52+
if ($messageString -eq 'System.Management.Automation.RemoteException') {
53+
$messageString = $message.ScriptStackTrace
54+
}
55+
56+
# Log json-rpc messages so the MCP client doesn't print
57+
# '[warning] Failed to parse message:'
58+
$messageObject = @{
59+
jsonrpc = "2.0"
60+
method = "notifications/message"
61+
params = @{
62+
level = $level
63+
logger = "installer"
64+
data = $messageString
65+
}
66+
}
67+
68+
Write-Host ($messageObject | ConvertTo-Json -Depth 100 -Compress)
69+
}
70+
2371
if ($UpdateVsCodeConfig) {
2472
$vscodeConfigPath = Join-Path $PSScriptRoot ".." ".." ".." ".vscode" "mcp.json"
2573
if (Test-Path $vscodeConfigPath) {
@@ -45,7 +93,7 @@ if ($UpdateVsCodeConfig) {
4593
}
4694
}
4795
$vscodeConfig.servers = $orderedServers
48-
Write-Host "Updating vscode mcp config at $vscodeConfigPath"
96+
log "Updating vscode mcp config at $vscodeConfigPath"
4997
$vscodeConfig | ConvertTo-Json -Depth 10 | Set-Content -Path $vscodeConfigPath -Force
5098
}
5199

@@ -54,12 +102,36 @@ if ($UpdateVsCodeConfig) {
54102
$tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath()
55103
$guid = [System.Guid]::NewGuid()
56104
$tempInstallDirectory = Join-Path $tmp "azsdk-install-$($guid)"
57-
$tempExe = Install-Standalone-Tool `
58-
-Version $Version `
59-
-FileName $FileName `
60-
-Package $Package `
61-
-Directory $tempInstallDirectory `
62-
-Repository $Repository
105+
106+
if ($mcpMode) {
107+
try {
108+
# Swallow all output and re-log so we can wrap any
109+
# output from the inner function as json-rpc
110+
$tempExe = Install-Standalone-Tool `
111+
-Version $Version `
112+
-FileName $FileName `
113+
-Package $Package `
114+
-Directory $tempInstallDirectory `
115+
-Repository $Repository `
116+
*>&1
117+
| Tee-Object -Variable _
118+
| ForEach-Object { log $_; $_ }
119+
| Select-Object -Last 1
120+
}
121+
catch {
122+
log -err $_
123+
exit 1
124+
}
125+
}
126+
else {
127+
$tempExe = Install-Standalone-Tool `
128+
-Version $Version `
129+
-FileName $FileName `
130+
-Package $Package `
131+
-Directory $tempInstallDirectory `
132+
-Repository $Repository `
133+
134+
}
63135

64136
if (-not (Test-Path $toolInstallDirectory)) {
65137
New-Item -ItemType Directory -Path $toolInstallDirectory -Force | Out-Null
@@ -68,12 +140,13 @@ $exeName = Split-Path $tempExe -Leaf
68140
$exeDestination = Join-Path $toolInstallDirectory $exeName
69141
Copy-Item -Path $tempExe -Destination $exeDestination -Force
70142

71-
Write-Host "Package $package is installed at $exeDestination"
143+
log "Package $package is installed at $exeDestination"
72144
if (!$UpdatePathInProfile) {
73-
Write-Warning "To add the tool to PATH for new shell sessions, re-run with -UpdatePathInProfile to modify the shell profile file."
74-
} else {
145+
log -warn "To add the tool to PATH for new shell sessions, re-run with -UpdatePathInProfile to modify the shell profile file."
146+
}
147+
else {
75148
Add-InstallDirectoryToPathInProfile -InstallDirectory $toolInstallDirectory
76-
Write-Warning "'$exeName' will be available in PATH for new shell sessions."
149+
log -warn "'$exeName' will be available in PATH for new shell sessions."
77150
}
78151

79152
if ($Run) {

0 commit comments

Comments
 (0)