feat(ai-mcp): 补充外部客户端命令检测状态

This commit is contained in:
Syngnat
2026-06-08 19:33:05 +08:00
parent d7879d9ef0
commit 5c867fd121
11 changed files with 290 additions and 39 deletions

View File

@@ -15,6 +15,8 @@ describe('AIMCPClientInstallPanel', () => {
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
},
{
@@ -22,6 +24,9 @@ describe('AIMCPClientInstallPanel', () => {
displayName: 'Codex',
installed: true,
matchesCurrent: false,
clientDetected: true,
clientCommand: 'codex',
clientPath: 'C:/Users/mock/AppData/Roaming/npm/codex.cmd',
message: '已检测到 Codex 中的 GoNavi MCP 记录,但与当前 GoNavi 安装路径不一致,建议更新',
configPath: '~/.codex/config.toml',
command: 'gonavi-mcp-server',
@@ -34,6 +39,9 @@ describe('AIMCPClientInstallPanel', () => {
displayName: 'Codex',
installed: true,
matchesCurrent: false,
clientDetected: true,
clientCommand: 'codex',
clientPath: 'C:/Users/mock/AppData/Roaming/npm/codex.cmd',
message: '已检测到 Codex 中的 GoNavi MCP 记录,但与当前 GoNavi 安装路径不一致,建议更新',
configPath: '~/.codex/config.toml',
command: 'gonavi-mcp-server',
@@ -60,31 +68,46 @@ describe('AIMCPClientInstallPanel', () => {
expect(markup).toContain('第 2 步:确认状态后写入');
expect(markup).toContain('未接入');
expect(markup).toContain('需更新');
expect(markup).toContain('命令已检测');
expect(markup).toContain('复制配置路径');
expect(markup).toContain('复制启动命令');
expect(markup).toContain('更新 Codex 配置');
expect(markup).toContain('本机命令状态:已检测到 codex');
expect(markup).toContain('不会下载 Claude Code / Codex');
});
it('shows an already-connected label when the selected client matches the current GoNavi path', () => {
it('shows an already-connected label and supports prewriting config when the client command is not detected locally', () => {
const markup = renderToStaticMarkup(
<AIMCPClientInstallPanel
statuses={[
{
client: 'claude-code',
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
},
{
client: 'codex',
displayName: 'Codex',
installed: true,
matchesCurrent: true,
message: '已检测到 Claude Code 用户级 GoNavi MCP 配置,且与当前 GoNavi 安装路径一致',
clientDetected: true,
clientCommand: 'codex',
message: '已检测到 Codex 用户级 GoNavi MCP 配置,且与当前 GoNavi 安装路径一致',
},
]}
selectedClient="claude-code"
selectedStatus={{
client: 'claude-code',
displayName: 'Claude Code',
installed: true,
matchesCurrent: true,
message: '已检测到 Claude Code 用户级 GoNavi MCP 配置,且与当前 GoNavi 安装路径一致',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
}}
selectedCommandText="gonavi-mcp-server stdio"
darkMode={false}
@@ -101,7 +124,8 @@ describe('AIMCPClientInstallPanel', () => {
/>,
);
expect(markup).toContain('已接入');
expect(markup).toContain('已接入 Claude Code');
expect(markup).toContain('预写入 Claude Code 配置');
expect(markup).toContain('未检测命令');
expect(markup).toContain('未检测到本机 claude 命令');
});
});

View File

@@ -55,6 +55,29 @@ const getStatusTone = (status: AIMCPClientInstallStatus | undefined, darkMode: b
};
};
const resolveClientCommandName = (status: AIMCPClientInstallStatus | undefined) => {
const command = String(status?.clientCommand || '').trim();
if (command) {
return command;
}
return status?.client === 'codex' ? 'codex' : 'claude';
};
const getClientDetectionTone = (status: AIMCPClientInstallStatus | undefined, darkMode: boolean) => {
if (status?.clientDetected) {
return {
label: '命令已检测',
color: '#16a34a',
bg: darkMode ? 'rgba(34,197,94,0.18)' : 'rgba(34,197,94,0.12)',
};
}
return {
label: '未检测命令',
color: '#d97706',
bg: darkMode ? 'rgba(245,158,11,0.18)' : 'rgba(245,158,11,0.12)',
};
};
const getStatusSummary = (status: AIMCPClientInstallStatus | undefined) => {
const label = status?.displayName || '这个客户端';
const messageText = String(status?.message || '');
@@ -80,6 +103,15 @@ const getClientCardDescription = (status: AIMCPClientInstallStatus | undefined)
return '还没有写入 GoNavi MCP 配置。';
};
const getClientDetectionSummary = (status: AIMCPClientInstallStatus | undefined) => {
const label = status?.displayName || '这个客户端';
const commandName = resolveClientCommandName(status);
if (status?.clientDetected) {
return `已检测到本机 ${commandName} 命令,写入后可直接重启 ${label} 验证。`;
}
return `未检测到本机 ${commandName} 命令;如果你只装了桌面端或 CLI 还没加入 PATH也可以先写配置稍后再重启 ${label}`;
};
const resolveActionLabel = (status: AIMCPClientInstallStatus | undefined) => {
const label = status?.displayName || '客户端';
if (status?.matchesCurrent) {
@@ -88,6 +120,9 @@ const resolveActionLabel = (status: AIMCPClientInstallStatus | undefined) => {
if (status?.installed) {
return `更新 ${label} 配置`;
}
if (status?.clientDetected === false) {
return `预写入 ${label} 配置`;
}
return `写入 ${label} 配置`;
};
@@ -149,7 +184,7 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
<div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
<div style={{ fontWeight: 700, fontSize: 13, color: overlayTheme.titleText }}> 1 </div>
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.6 }}>
GoNavi MCP
</div>
</div>
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(220px, 1fr))', gap: 10 }}>
@@ -157,6 +192,7 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
const client = status.client === 'codex' ? 'codex' : 'claude-code';
const active = selectedClient === client;
const tone = getStatusTone(status, darkMode);
const detectionTone = getClientDetectionTone(status, darkMode);
return (
<button
key={status.client}
@@ -172,12 +208,12 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
flexDirection: 'column',
gap: 10,
textAlign: 'left',
minHeight: 118,
minHeight: 138,
transition: 'all 0.2s ease',
opacity: statusLoading ? 0.72 : 1,
}}
>
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12 }}>
<div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 12 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 10, minWidth: 0 }}>
<div
aria-hidden
@@ -199,23 +235,44 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
{status.displayName}
</div>
</div>
<div
style={{
padding: '4px 10px',
borderRadius: 999,
fontSize: 12,
fontWeight: 700,
color: tone.color,
background: tone.bg,
whiteSpace: 'nowrap',
flexShrink: 0,
}}
>
{tone.label}
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 6 }}>
<div
style={{
padding: '4px 10px',
borderRadius: 999,
fontSize: 12,
fontWeight: 700,
color: tone.color,
background: tone.bg,
whiteSpace: 'nowrap',
flexShrink: 0,
}}
>
{tone.label}
</div>
<div
style={{
padding: '4px 10px',
borderRadius: 999,
fontSize: 11,
fontWeight: 700,
color: detectionTone.color,
background: detectionTone.bg,
whiteSpace: 'nowrap',
flexShrink: 0,
}}
>
{detectionTone.label}
</div>
</div>
</div>
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.6 }}>
{getClientCardDescription(status)}
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.6 }}>
{getClientCardDescription(status)}
</div>
<div style={{ fontSize: 11, color: overlayTheme.mutedText, lineHeight: 1.6 }}>
{getClientDetectionSummary(status)}
</div>
</div>
</button>
);
@@ -261,6 +318,16 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
<div style={{ fontSize: 12, color: overlayTheme.titleText, lineHeight: 1.7 }}>
{getStatusSummary(selectedStatus)}
</div>
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.7 }}>
{selectedStatus?.clientDetected
? `已检测到 ${resolveClientCommandName(selectedStatus)}`
: `未检测到 ${resolveClientCommandName(selectedStatus)},仍可先写配置`}
</div>
{selectedStatus?.clientPath && (
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.6, fontFamily: 'var(--gn-font-mono)' }}>
{selectedStatus.clientPath}
</div>
)}
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.7 }}>
{selectedStatus?.message || '未检测到安装状态'}
</div>
@@ -307,7 +374,7 @@ const AIMCPClientInstallPanel: React.FC<AIMCPClientInstallPanelProps> = ({
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12, flexWrap: 'wrap' }}>
<div style={{ fontSize: 12, color: overlayTheme.mutedText, lineHeight: 1.6 }}>
CLI PATH
</div>
<Button
type={selectedStatus?.matchesCurrent ? 'default' : 'primary'}

View File

@@ -47,6 +47,8 @@ describe('AISettingsMCPSection', () => {
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
},
{
@@ -54,6 +56,9 @@ describe('AISettingsMCPSection', () => {
displayName: 'Codex',
installed: false,
matchesCurrent: false,
clientDetected: true,
clientCommand: 'codex',
clientPath: 'C:/Users/mock/AppData/Roaming/npm/codex.cmd',
message: '未检测到 Codex 用户级 GoNavi MCP 配置',
},
]}
@@ -63,6 +68,8 @@ describe('AISettingsMCPSection', () => {
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
}}
selectedMCPClientCommandText=""
@@ -89,6 +96,7 @@ describe('AISettingsMCPSection', () => {
);
expect(markup).toContain('把 GoNavi MCP 接入外部 AI 客户端');
expect(markup).toContain('未检测命令');
expect(markup).toContain('常见启动方式模板');
expect(markup).toContain('Node 脚本');
expect(markup).toContain('新增 MCP 服务');

View File

@@ -43,6 +43,8 @@ if (typeof window !== 'undefined' && (!(window as any).go?.app?.App || !(window
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
configPath: 'C:/Users/mock/.claude.json',
command: 'C:/Program Files/GoNavi/GoNavi.exe',
@@ -53,6 +55,9 @@ if (typeof window !== 'undefined' && (!(window as any).go?.app?.App || !(window
displayName: 'Codex',
installed: true,
matchesCurrent: false,
clientDetected: true,
clientCommand: 'codex',
clientPath: 'C:/Users/mock/AppData/Roaming/npm/codex.cmd',
message: '已检测到 Codex 中的 GoNavi MCP 记录,但与当前 GoNavi 安装路径不一致,建议更新',
configPath: 'C:/Users/mock/.codex/config.toml',
command: 'C:/Old/GoNavi.exe',

View File

@@ -597,6 +597,9 @@ export interface AIMCPClientInstallStatus {
displayName: string;
installed: boolean;
matchesCurrent: boolean;
clientDetected?: boolean;
clientCommand?: string;
clientPath?: string;
message: string;
configPath?: string;
command?: string;

View File

@@ -27,6 +27,9 @@ describe('mcpClientInstallStatus helpers', () => {
displayName: 'Codex',
installed: true,
matchesCurrent: true,
clientDetected: false,
clientCommand: 'codex',
clientPath: '',
message: '已检测到 Codex 用户级 GoNavi MCP 配置,且与当前 GoNavi 安装路径一致',
args: [],
},
@@ -54,6 +57,32 @@ describe('mcpClientInstallStatus helpers', () => {
expect(pickPreferredMCPClient(statuses)).toBe('codex');
});
it('prefers a locally detected client command when neither client has existing GoNavi MCP config', () => {
const statuses: AIMCPClientInstallStatus[] = [
{
client: 'claude-code',
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
},
{
client: 'codex',
displayName: 'Codex',
installed: false,
matchesCurrent: false,
clientDetected: true,
clientCommand: 'codex',
clientPath: 'C:/Users/mock/AppData/Roaming/npm/codex.cmd',
message: '未检测到 Codex 用户级 GoNavi MCP 配置',
},
];
expect(pickPreferredMCPClient(statuses)).toBe('codex');
});
it('keeps the user-selected client when it is still present in the latest status list', () => {
expect(pickPreferredMCPClient(EMPTY_MCP_CLIENT_STATUSES, 'codex')).toBe('codex');
});

View File

@@ -8,6 +8,8 @@ export const EMPTY_MCP_CLIENT_STATUSES: AIMCPClientInstallStatus[] = [
displayName: 'Claude Code',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'claude',
message: '未检测到 Claude Code 用户级 GoNavi MCP 配置',
},
{
@@ -15,6 +17,8 @@ export const EMPTY_MCP_CLIENT_STATUSES: AIMCPClientInstallStatus[] = [
displayName: 'Codex',
installed: false,
matchesCurrent: false,
clientDetected: false,
clientCommand: 'codex',
message: '未检测到 Codex 用户级 GoNavi MCP 配置',
},
];
@@ -36,16 +40,19 @@ const hasStatusError = (status: AIMCPClientInstallStatus): boolean =>
/|||/u.test(String(status.message || ''));
const getMCPClientPriority = (status: AIMCPClientInstallStatus): number => {
if (hasStatusError(status)) {
if (status.installed && !status.matchesCurrent) {
return 0;
}
if (status.installed && !status.matchesCurrent) {
if (status.matchesCurrent) {
return 1;
}
if (status.matchesCurrent) {
if (status.clientDetected) {
return 2;
}
return 3;
if (hasStatusError(status)) {
return 3;
}
return 4;
};
export const normalizeMCPClientStatuses = (items?: AIMCPClientInstallStatus[]): AIMCPClientInstallStatus[] => {
@@ -67,6 +74,9 @@ export const normalizeMCPClientStatuses = (items?: AIMCPClientInstallStatus[]):
...base,
...item,
displayName: item.displayName || base.displayName,
clientDetected: item.clientDetected ?? base.clientDetected ?? false,
clientCommand: item.clientCommand || base.clientCommand,
clientPath: item.clientPath || '',
message: item.message || base.message,
args: Array.isArray(item.args) ? item.args : (base.args || []),
});

View File

@@ -27,6 +27,9 @@ export namespace ai {
displayName: string;
installed: boolean;
matchesCurrent: boolean;
clientDetected: boolean;
clientCommand?: string;
clientPath?: string;
message: string;
configPath?: string;
command?: string;
@@ -42,6 +45,9 @@ export namespace ai {
this.displayName = source["displayName"];
this.installed = source["installed"];
this.matchesCurrent = source["matchesCurrent"];
this.clientDetected = source["clientDetected"];
this.clientCommand = source["clientCommand"];
this.clientPath = source["clientPath"];
this.message = source["message"];
this.configPath = source["configPath"];
this.command = source["command"];
@@ -1320,4 +1326,3 @@ export namespace sync {
}
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"strconv"
@@ -15,6 +16,8 @@ import (
const (
gonaviMCPServerID = "gonavi"
defaultCodexMCPStartupTimeoutSecond = 60
claudeCodeClientCommandName = "claude"
codexClientCommandName = "codex"
)
var claudeCodeConfigPathFunc = func() (string, error) {
@@ -42,6 +45,7 @@ var codexConfigPathFunc = func() (string, error) {
}
var localMCPExecutablePathFunc = os.Executable
var localCLICommandPathFunc = exec.LookPath
type claudeCodeMCPServerConfig struct {
Type string `json:"type"`
@@ -166,13 +170,33 @@ func resolveLocalMCPCommand(executablePath string) (string, []string, error) {
}
}
func detectLocalCLICommand(commandName string) (bool, string) {
commandName = strings.TrimSpace(commandName)
if commandName == "" {
return false, ""
}
resolvedPath, err := localCLICommandPathFunc(commandName)
if err != nil {
return false, ""
}
resolvedPath = strings.TrimSpace(resolvedPath)
if resolvedPath == "" {
return false, ""
}
return true, filepath.Clean(resolvedPath)
}
func inspectClaudeCodeMCPInstallStatus(expectedCommand string, expectedArgs []string, expectedErr error) ai.MCPClientInstallStatus {
configPath, pathErr := claudeCodeConfigPathFunc()
clientDetected, clientPath := detectLocalCLICommand(claudeCodeClientCommandName)
status := ai.MCPClientInstallStatus{
Client: "claude-code",
DisplayName: "Claude Code",
ConfigPath: strings.TrimSpace(configPath),
Message: "未检测到 Claude Code 用户级 GoNavi MCP 配置",
Client: "claude-code",
DisplayName: "Claude Code",
ClientDetected: clientDetected,
ClientCommand: claudeCodeClientCommandName,
ClientPath: clientPath,
ConfigPath: strings.TrimSpace(configPath),
Message: "未检测到 Claude Code 用户级 GoNavi MCP 配置",
}
if pathErr != nil {
status.Message = fmt.Sprintf("定位 Claude Code 配置失败: %v", pathErr)
@@ -214,11 +238,15 @@ func inspectClaudeCodeMCPInstallStatus(expectedCommand string, expectedArgs []st
func inspectCodexMCPInstallStatus(expectedCommand string, expectedArgs []string, expectedErr error) ai.MCPClientInstallStatus {
configPath, pathErr := codexConfigPathFunc()
clientDetected, clientPath := detectLocalCLICommand(codexClientCommandName)
status := ai.MCPClientInstallStatus{
Client: "codex",
DisplayName: "Codex",
ConfigPath: strings.TrimSpace(configPath),
Message: "未检测到 Codex 用户级 GoNavi MCP 配置",
Client: "codex",
DisplayName: "Codex",
ClientDetected: clientDetected,
ClientCommand: codexClientCommandName,
ClientPath: clientPath,
ConfigPath: strings.TrimSpace(configPath),
Message: "未检测到 Codex 用户级 GoNavi MCP 配置",
}
if pathErr != nil {
status.Message = fmt.Sprintf("定位 Codex 配置失败: %v", pathErr)

View File

@@ -2,6 +2,7 @@ package aiservice
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"reflect"
@@ -274,3 +275,71 @@ func TestUpsertCodexMCPServerConfigReplacesExistingBlockAndNestedSections(t *tes
t.Fatalf("expected unrelated project config to be preserved, got %s", text)
}
}
func TestInspectClaudeCodeMCPInstallStatusIncludesLocalCLIAvailability(t *testing.T) {
originalConfigPathFunc := claudeCodeConfigPathFunc
originalCLIPathFunc := localCLICommandPathFunc
t.Cleanup(func() {
claudeCodeConfigPathFunc = originalConfigPathFunc
localCLICommandPathFunc = originalCLIPathFunc
})
tempDir := t.TempDir()
claudeCodeConfigPathFunc = func() (string, error) {
return filepath.Join(tempDir, ".claude.json"), nil
}
localCLICommandPathFunc = func(file string) (string, error) {
if file != claudeCodeClientCommandName {
t.Fatalf("expected lookup for %q, got %q", claudeCodeClientCommandName, file)
}
return `C:\Users\mock\AppData\Roaming\npm\claude.CMD`, nil
}
status := inspectClaudeCodeMCPInstallStatus(`C:\Program Files\GoNavi\GoNavi.exe`, []string{"mcp-server"}, nil)
if !status.ClientDetected {
t.Fatal("expected Claude Code command detection to be true")
}
if status.ClientCommand != claudeCodeClientCommandName {
t.Fatalf("expected client command %q, got %q", claudeCodeClientCommandName, status.ClientCommand)
}
if status.ClientPath != `C:\Users\mock\AppData\Roaming\npm\claude.CMD` {
t.Fatalf("unexpected client path: %q", status.ClientPath)
}
if status.Installed {
t.Fatal("expected MCP config to remain uninstalled when config file is absent")
}
}
func TestInspectCodexMCPInstallStatusKeepsMissingCLISignalSeparateFromConfigState(t *testing.T) {
originalConfigPathFunc := codexConfigPathFunc
originalCLIPathFunc := localCLICommandPathFunc
t.Cleanup(func() {
codexConfigPathFunc = originalConfigPathFunc
localCLICommandPathFunc = originalCLIPathFunc
})
tempDir := t.TempDir()
codexConfigPathFunc = func() (string, error) {
return filepath.Join(tempDir, "config.toml"), nil
}
localCLICommandPathFunc = func(file string) (string, error) {
if file != codexClientCommandName {
t.Fatalf("expected lookup for %q, got %q", codexClientCommandName, file)
}
return "", errors.New("not found")
}
status := inspectCodexMCPInstallStatus(`C:\Program Files\GoNavi\GoNavi.exe`, []string{"mcp-server"}, nil)
if status.ClientDetected {
t.Fatal("expected codex command detection to be false")
}
if status.ClientCommand != codexClientCommandName {
t.Fatalf("expected client command %q, got %q", codexClientCommandName, status.ClientCommand)
}
if status.ClientPath != "" {
t.Fatalf("expected missing codex command path to be empty, got %q", status.ClientPath)
}
if status.Message != "未检测到 Codex 用户级 GoNavi MCP 配置" {
t.Fatalf("unexpected config message: %q", status.Message)
}
}

View File

@@ -152,6 +152,9 @@ type MCPClientInstallStatus struct {
DisplayName string `json:"displayName"`
Installed bool `json:"installed"`
MatchesCurrent bool `json:"matchesCurrent"`
ClientDetected bool `json:"clientDetected"`
ClientCommand string `json:"clientCommand,omitempty"`
ClientPath string `json:"clientPath,omitempty"`
Message string `json:"message"`
ConfigPath string `json:"configPath,omitempty"`
Command string `json:"command,omitempty"`