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。

CR9: MCP 服务器名每轮只读一次

nana1ad671d664af
diff --git a/apps/desktop/src/main/companion-cloud/__tests__/mcpServerNames.test.ts b/apps/desktop/src/main/companion-cloud/__tests__/mcpServerNames.test.ts
new file mode 100644
index 0000000000..ef33fecae5
--- /dev/null
+++ b/apps/desktop/src/main/companion-cloud/__tests__/mcpServerNames.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it, vi } from 'vitest';
+import { createMcpServerNamesReader } from '../mcpServerNames.js';
+
+describe('createMcpServerNamesReader', () => {
+ it('reads the server list once for every companion of a cycle, again after it, and again for another owner', async () => {
+ const list = vi.fn(async () => [{ id: 'mcp-1', name: 'Docs' }]);
+ let owner = 'a'; let clock = 0;
+ const read = createMcpServerNamesReader(list, () => owner, { ttlMs: 1000, now: () => clock });
+ expect(await Promise.all([read(), read(), read()])).toEqual(Array(3).fill({ 'mcp-1': 'Docs' }));
+ expect(list).toHaveBeenCalledTimes(1);
+ clock = 1001; await read();
+ expect(list).toHaveBeenCalledTimes(2);
+ owner = 'b'; await read();
+ expect(list).toHaveBeenCalledTimes(3);
+ });
+
+ it('gives no names for a list that cannot be read', async () => {
+ const read = createMcpServerNamesReader(async () => { throw new Error('db'); }, () => 'a');
+ expect(await read()).toEqual({});
+ });
+});
diff --git a/apps/desktop/src/main/companion-cloud/host.ts b/apps/desktop/src/main/companion-cloud/host.ts
index 380b0d3ade..433099423c 100644
--- a/apps/desktop/src/main/companion-cloud/host.ts
+++ b/apps/desktop/src/main/companion-cloud/host.ts
@@ -34,6 +34,7 @@ import type { TeammateTodo } from '@cindy/maker-shared/teammate-todo';
import { portableTodo, travellingTodos } from './travel.js';
import type { CompanionMaterializeHost } from './materialize.js';
import { companionHomes } from './homes.js';
+import { createMcpServerNamesReader } from './mcpServerNames.js';
import { companionCapabilitiesPatch, portableCompanionCapabilities, type CompanionArrivalNotice } from './capabilities.js';
import { MIRROR_ATTACHMENT_MAX_BYTES, type CompanionMirrorCursor, type CompanionMirrorRow } from './mirror.js';
import { replyArtifactAttachments, replyTextWithFiles } from './replyArtifacts.js';
@@ -63,6 +64,8 @@ function profileTemplateId(capabilitiesJson: string | null, identitySource: stri
return inferBotTemplatePresetId(identitySource);
}
+const readMcpServerNames = createMcpServerNamesReader(listCustomMcpServers, activeOwnerScopeKey);
+
export async function readCompanionValues(botId: string, selfDeviceId: string | null = null): Promise<CompanionPreferenceValue[]> {
const userDataDir = ownerScopedUserDataPath();
const [row] = await getDbClient().drizzle
@@ -86,7 +89,7 @@ export async function readCompanionValues(botId: string, selfDeviceId: string |
const config = JSON.parse(row.capabilities ?? '{}') as unknown;
if (!config || typeof config !== 'object' || Array.isArray(config)) throw new Error('CAPABILITIES_UNREADABLE');
// Names of this computer's own MCP servers, so a computer without them can still say which one it is.
- const mcpNames = Object.fromEntries((await listCustomMcpServers().catch(() => [])).map(server => [server.id, server.name]));
+ const mcpNames = await readMcpServerNames();
values.push({ key: COMPANION_CAPABILITIES_KEY, value: portableCompanionCapabilities(
config as Record<string, unknown>, readExplicitBotModelChain(config as Record<string, unknown>), mcpNames) });
} catch { values.push({ key: COMPANION_CAPABILITIES_KEY, value: null, unread: true }); }
diff --git a/apps/desktop/src/main/companion-cloud/mcpServerNames.ts b/apps/desktop/src/main/companion-cloud/mcpServerNames.ts
new file mode 100644
index 0000000000..e6ab3a8269
--- /dev/null
+++ b/apps/desktop/src/main/companion-cloud/mcpServerNames.ts
@@ -0,0 +1,19 @@
+/** Names of this computer's MCP servers for the capabilities upload, read once per cycle instead of once per companion. */
+export function createMcpServerNamesReader(
+ list: () => Promise<Array<{ id: string; name: string }>>,
+ owner: () => string,
+ options: { ttlMs?: number; now?: () => number } = {},
+): () => Promise<Record<string, string>> {
+ const ttlMs = options.ttlMs ?? 5_000;
+ const now = options.now ?? Date.now;
+ let last: { owner: string; at: number; value: Promise<Record<string, string>> } | null = null;
+ return () => {
+ const current = owner();
+ if (!last || last.owner !== current || now() - last.at > ttlMs) {
+ // A server list that cannot be read costs the names for this cycle only, as before.
+ last = { owner: current, at: now(), value: list()
+ .then(servers => Object.fromEntries(servers.map(server => [server.id, server.name]))).catch(() => ({})) };
+ }
+ return last.value;
+ };
+}