Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(bots): match Pi MCP catalog to bridge transport support

zqchris86b55cc0ab62
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index 14302811a2..a7152f89ca 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -1680,14 +1680,39 @@ describe('Bot canonical Session lifecycle', () => {
await expect(selectBotCapability({ ...input, id: 'shared-docs', joined: true })).resolves.toMatchObject({ ok: true, joined: true });
});
+ it.each([
+ { id: 'pi-sse', transport: 'sse' as const, url: 'https://example.invalid/mcp' },
+ { id: 'pi-public-http', transport: 'http' as const, url: 'http://example.invalid/mcp' },
+ ])('keeps Pi-incompatible $id discoverable but unjoinable and removable', async (config) => {
+ h.customMcpConfigs.push({ ...config, name: config.id, headers: {} });
+ await refreshCustomMcpProviders();
+ await invoke('local-db:bots:update', {
+ id: 'bot-1', capabilities: { mcpServers: [config.id], mcpMode: 'allowlist' },
+ });
+ const created = await invoke('local-db:bots:create-canonical-session', {
+ botId: 'bot-1', expectedCanonicalSessionId: null, expectedProfileVersion: 2,
+ });
+ expect(created.session.agentKind).toBe('pi');
+ const input = { callerSessionId: created.session.id, kind: 'mcp' as const, id: config.id };
+ await expect(findBotCapabilities(input)).resolves.toMatchObject({
+ capabilities: expect.arrayContaining([{ id: config.id, name: config.id,
+ description: config.transport, available: false, joined: true }]),
+ });
+ await expect(selectBotCapability({ ...input, joined: true })).resolves.toMatchObject({ ok: false, errorCode: 'CAPABILITY_UNAVAILABLE' });
+ await expect(selectBotCapability({ ...input, joined: false })).resolves.toMatchObject({ ok: true, joined: false });
+ });
+
it('revalidates saved MCP transports on fallback and restores them when switching back', async () => {
- const configs = (['http', 'sse'] as const).map((transport) => ({
- id: transport, name: transport, transport, url: `https://example.invalid/${transport}`,
- headers: { Authorization: 'FAKE_SECRET' }, updatedAt: 1,
- }));
+ const configs = [
+ { id: 'https', transport: 'http' as const, url: 'https://example.invalid/mcp' },
+ { id: 'sse', transport: 'sse' as const, url: 'https://example.invalid/sse' },
+ { id: 'public-http', transport: 'http' as const, url: 'http://example.invalid/mcp' },
+ { id: 'local-http', transport: 'http' as const, url: 'http://localhost:4321/mcp' },
+ ].map((config) => ({ ...config, name: config.id, headers: { Authorization: 'FAKE_SECRET' }, updatedAt: 1 }));
+ const configured = configs.map((config) => config.id);
const providers = configs.map((config) => new CustomMcpProvider(config, () => 'FAKE_TOKEN'));
await invoke('local-db:bots:update', {
- id: 'bot-1', capabilities: { mcpServers: ['http', 'sse'], mcpMode: 'allowlist' },
+ id: 'bot-1', capabilities: { mcpServers: configured, mcpMode: 'allowlist' },
});
const created = await invoke('local-db:bots:create-canonical-session', {
botId: 'bot-1', expectedCanonicalSessionId: null, expectedProfileVersion: 2,
@@ -1702,23 +1727,19 @@ describe('Bot canonical Session lifecycle', () => {
const catalog = buildBotMcpCatalog({
agentKind: actualRoute, providers, builtinNames: [], customServers: configs,
});
- for (const provider of providers) {
- const context = { agentKind: actualRoute, workingDir: created.session.workingDir };
- const serialized = actualRoute === 'codex'
- ? provider.toCodexMcpConfig(context) : provider.toClaudeSdkConfig(context);
- expect(catalog.find((entry) => entry.name === provider.name)?.available).toBe(serialized !== null);
- }
expect(JSON.stringify(catalog)).not.toMatch(/FAKE_SECRET|FAKE_TOKEN|example.invalid|Authorization/);
return catalog;
},
});
expect(snapshot).toMatchObject({
- configuredMcpServers: ['http', 'sse'],
- resolvedMcpServers: agentKind === 'codex' ? ['http'] : ['http', 'sse'],
- unavailableMcpServers: agentKind === 'codex' ? ['sse'] : [],
+ configuredMcpServers: configured,
+ resolvedMcpServers: agentKind === 'pi' ? ['https', 'local-http']
+ : agentKind === 'codex' ? ['https', 'public-http', 'local-http'] : configured,
+ unavailableMcpServers: agentKind === 'pi' ? ['sse', 'public-http']
+ : agentKind === 'codex' ? ['sse'] : [],
});
expect(opts.botRuntimeProfile?.mcpPolicy.catalog).toContainEqual(expect.objectContaining({
- name: 'sse', available: agentKind !== 'codex',
+ name: 'sse', available: agentKind === 'claude-code',
}));
}
});
diff --git a/apps/desktop/src/main/maker-host/botMcpCatalog.ts b/apps/desktop/src/main/maker-host/botMcpCatalog.ts
index c7818502e7..c51476b1e0 100644
--- a/apps/desktop/src/main/maker-host/botMcpCatalog.ts
+++ b/apps/desktop/src/main/maker-host/botMcpCatalog.ts
@@ -1,10 +1,11 @@
import type { McpProvider, McpProviderContext } from '@cindy/maker-core';
import type { McpTransport } from '../../shared/customMcp.js';
+import { isPiCustomMcpProviderAvailable } from '../mcp-integrations/piMcpTransport.js';
/** Freeze only metadata; compatibility is recomputed for each actual start route. */
export function buildBotMcpCatalog(input: {
agentKind: McpProviderContext['agentKind'];
- providers: readonly Pick<McpProvider, 'name'>[];
+ providers: readonly McpProvider[];
builtinNames: readonly string[];
customServers: readonly { id: string; transport: McpTransport; updatedAt: number }[];
}) {
@@ -13,11 +14,15 @@ export function buildBotMcpCatalog(input: {
return [...new Map(input.providers.map((provider) => {
const builtin = builtinNames.has(provider.name);
const custom = customServers.get(provider.name);
+ // Evaluate registered user configs using Pi's actual serialization and URL gate.
+ // Builtins retain their SDK bridge path and are not instantiated by catalog queries.
+ const available = builtin || (input.agentKind === 'pi' && custom
+ ? isPiCustomMcpProviderAvailable(provider)
+ : input.agentKind !== 'codex' || custom?.transport !== 'sse');
return [provider.name, {
name: provider.name,
source: builtin ? 'builtin' as const : 'custom' as const,
- // CustomMcpProvider.toCodexMcpConfig cannot serialize the SSE transport.
- available: builtin || input.agentKind !== 'codex' || custom?.transport !== 'sse',
+ available,
generation: builtin ? 'builtin:1'
: custom ? `${custom.transport}:${custom.updatedAt}` : 'custom:unknown',
}];
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/mcpHandlers.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/mcpHandlers.test.ts
index 80b25b8730..592c40d68b 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/mcpHandlers.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/mcpHandlers.test.ts
@@ -90,6 +90,8 @@ describe('mcp:custom:* CRUD handlers', () => {
const configs: CustomMcpConfig[] = [
validConfig,
{ ...validConfig, id: 'events', transport: 'sse' },
+ { ...validConfig, id: 'public-http', url: 'http://example.test/mcp' },
+ { ...validConfig, id: 'local-http', url: 'http://localhost:4321/mcp' },
{ ...validConfig, id: 'cindy_memory' },
{ ...validConfig, id: '__proto__' },
{ ...validConfig, id: 'bad-headers', headers: { 'X-Test': 'line\nbreak' } },
@@ -118,7 +120,9 @@ describe('mcp:custom:* CRUD handlers', () => {
const result = await harness.invoke(MAKER_INVOKE.MCP_CUSTOM_LIST, { agentKind });
expect(deps.listMcpServers).toHaveBeenCalledWith({ agentKind });
expect(result).toEqual({ agentKind, servers: rawList.map((server) => ({
- ...server, available: server.id === 'mytools' || (server.id === 'events' && agentKind !== 'codex'),
+ ...server, available: ['mytools', 'local-http'].includes(server.id)
+ || (server.id === 'events' && agentKind === 'claude-code')
+ || (server.id === 'public-http' && agentKind !== 'pi'),
})) });
},
);
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/piEnvironment.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/piEnvironment.test.ts
index ea22b5288c..aec2026d56 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/piEnvironment.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/piEnvironment.test.ts
@@ -22,6 +22,8 @@ vi.mock('electron', () => ({
}));
import type { Logger, McpProvider } from '@cindy/maker-core';
+import { CustomMcpProvider } from '../custom-mcp-provider.js';
+import { buildBotMcpCatalog } from '../../maker-host/botMcpCatalog.js';
import {
CODEX_ALLOWED_BUILTIN_PLUGIN_IDS_KEY,
CODEX_DISABLED_BUILTIN_PLUGIN_IDS_KEY,
@@ -722,6 +724,36 @@ describe('piEnvironment per-session identity', () => {
config!.disposeSessionCtx!();
});
+ it('advertises only custom transports the Pi bridge can actually assemble', async () => {
+ const configs = [
+ { id: 'https', transport: 'http' as const, url: 'https://example.test/mcp' },
+ { id: 'sse', transport: 'sse' as const, url: 'https://example.test/events' },
+ { id: 'public-http', transport: 'http' as const, url: 'http://example.test/mcp' },
+ { id: 'local-http', transport: 'http' as const, url: 'http://localhost:4321/mcp' },
+ { id: 'ipv4', transport: 'http' as const, url: 'http://127.0.0.1:4321/mcp' },
+ { id: 'ipv6', transport: 'http' as const, url: 'http://[::1]:4321/mcp' },
+ { id: 'other-loopback', transport: 'http' as const, url: 'http://127.0.0.2:4321/mcp' },
+ { id: 'credentials', transport: 'http' as const, url: 'https://user:[email protected]/mcp' },
+ { id: 'invalid-url', transport: 'http' as const, url: 'not a url' },
+ ].map((config) => ({ ...config, name: config.id, headers: {}, updatedAt: 1 }));
+ const builtin = makeProvider('cindy_memory');
+ const sdkFactory = vi.spyOn(builtin, 'toClaudeSdkConfig');
+ const providers = [builtin, ...configs.map((config) => new CustomMcpProvider(config, () => 'TOKEN_CANARY'))];
+ const catalog = buildBotMcpCatalog({ agentKind: 'pi', providers,
+ builtinNames: ['cindy_memory'], customServers: configs });
+ expect(sdkFactory).not.toHaveBeenCalled();
+ const available = catalog.filter((entry) => entry.available).map((entry) => entry.name);
+ expect(available).toEqual(['cindy_memory', 'https', 'local-http', 'ipv4', 'ipv6']);
+ expect(JSON.stringify(catalog)).not.toMatch(/TOKEN_CANARY|example.test|user:canary/);
+
+ const config = await getPiExtraSpawnConfig(providers, noopLogger(), { workingDir: '', memoryEnabled: true });
+ try {
+ expect(config?.mcpBridge?.servers.map((server) => server.name)).toEqual(available);
+ } finally {
+ config?.disposeSessionCtx?.();
+ }
+ });
+
it('snapshots remote MCP lifecycle changes for new sessions while old leases keep their startup config', async () => {
let enabled = true;
let url = 'https://old.example.test/mcp';
diff --git a/apps/desktop/src/main/mcp-integrations/piEnvironment.ts b/apps/desktop/src/main/mcp-integrations/piEnvironment.ts
index 28e66795d5..4bc516905c 100644
--- a/apps/desktop/src/main/mcp-integrations/piEnvironment.ts
+++ b/apps/desktop/src/main/mcp-integrations/piEnvironment.ts
@@ -57,6 +57,7 @@ import { pluginIdForKnownProviderName } from '../maker-host/plugins/builtin-plug
// 直接取 plugins 模块的 registry 单例,不经 maker-host/index.ts —— 后者 import pi-host,
// 从 mcp-integrations 反向 import 会成环。
import { createPluginRegistry } from '../maker-host/plugins/index.js';
+import { isAllowedRemoteMcpUrl } from './piMcpTransport.js';
interface StartedPiBridge {
bridge: CodexHttpBridge | null;
@@ -112,22 +113,6 @@ function selectMcpEnvForServers(
);
}
-function isLoopbackMcpHostname(hostname: string): boolean {
- const normalized = hostname.toLowerCase();
- return (
- normalized === 'localhost' ||
- normalized === '127.0.0.1' ||
- normalized === '::1' ||
- normalized === '[::1]'
- );
-}
-
-function isAllowedRemoteMcpUrl(url: URL): boolean {
- if (url.username || url.password) return false;
- if (url.protocol === 'https:') return true;
- return url.protocol === 'http:' && isLoopbackMcpHostname(url.hostname);
-}
-
function shutdownGeneration(started: StartedPiBridge): Promise<void> {
if (!started.shutdownPromise) {
started.shutdownPromise = (started.bridge?.shutdown() ?? Promise.resolve())
diff --git a/apps/desktop/src/main/mcp-integrations/piMcpTransport.ts b/apps/desktop/src/main/mcp-integrations/piMcpTransport.ts
new file mode 100644
index 0000000000..7e2477f719
--- /dev/null
+++ b/apps/desktop/src/main/mcp-integrations/piMcpTransport.ts
@@ -0,0 +1,26 @@
+import type { McpProvider } from '@cindy/maker-core';
+
+/** Pi direct MCP URLs must match the bridge's HTTPS / explicit loopback boundary. */
+export function isAllowedRemoteMcpUrl(url: URL): boolean {
+ if (url.username || url.password) return false;
+ if (url.protocol === 'https:') return true;
+ const hostname = url.hostname.toLowerCase();
+ return url.protocol === 'http:' && (
+ hostname === 'localhost' || hostname === '127.0.0.1' ||
+ hostname === '::1' || hostname === '[::1]'
+ );
+}
+
+/**
+ * Registered user MCPs are HTTP/SSE configs, never in-process SDK servers.
+ * Pi consumes their Codex serialization; a Claude SSE config cannot be its SDK fallback.
+ * Inspect only the serialized route, without starting a bridge or returning secrets.
+ */
+export function isPiCustomMcpProviderAvailable(provider: McpProvider): boolean {
+ try {
+ const config = provider.toCodexMcpConfig?.({ agentKind: 'pi', workingDir: '', vendorOptions: {} });
+ return config?.type === 'http' && isAllowedRemoteMcpUrl(new URL(config.url));
+ } catch {
+ return false;
+ }
+}