feat(mcp): turn off a global server for Pi from the project switch

A Pi project entry replaces the global one and is skipped without a
command or url, so a switch-only entry left the server on in Pi and the
project page could not turn it off there. Reporters want a server off in
one project while every other project keeps loading it.

For a project under mcp.projects, the switch now writes the global
server's command, or its url without the query, with enabled: false to
the project's .pi/mcp.json. A disabled server never starts, so args, env
and headers stay out of a file that may be committed, and every sync
rewrites the entry from the global server. A project's own config cannot
see the global server, so pi there is still refused. The migration that
dropped pi from switch-only entries is gone.

Refs #305
This commit is contained in:
Willie
2026-10-01 16:08:23 +08:00
parent 886e3672a2
commit f837f0e94d
27 changed files with 175 additions and 124 deletions
@@ -51,9 +51,11 @@ describe('MCP project view', () => {
await waitFor(() => expect(mcpApi.save).toHaveBeenLastCalledWith({ name: 'context7', replace: true, server: { disabled: true }, project: '/work/app' }));
});
it('says Pi keeps loading a server turned off here, since Pi has no per-project switch', () => {
// Pi's switch carries the global server's command, so Pi turns it off here like the others.
it('turns a server off here for Pi too', () => {
view({ docs: { command: 'npx', targets: ['opencode', 'pi'] } }, { targets: ['opencode', 'pi'], servers: { docs: { disabled: true } } });
expect(screen.getByText('Still loads in Pi, which has no per-project switch.')).toBeInTheDocument();
expect(screen.getByRole('img', { name: 'Off in OpenCode, Pi' })).toBeInTheDocument();
expect(screen.queryByText(/Still loads in/)).not.toBeInTheDocument();
});
it("offers to import servers found in this project's Agent files, reading that project's file", async () => {
+3 -3
View File
@@ -45,7 +45,7 @@ function projectViewModel(data: MCPList, root: string, offered: readonly string[
const unmanaged = data.unmanaged.filter((u) => u.project === root);
// A switch for a global server belongs to the list above; everything else is the project's own.
const own = Object.fromEntries(Object.entries(servers).filter(([n, s]) => !(s.disabled && globals[n])));
// A switch that names no targets follows the project, and Pi has none.
// A switch that names no targets follows the project. Pi's needs a global server's command or url.
const targetsOf = (n: string) => own[n]?.targets ?? (own[n]?.disabled ? targets.filter((x) => x !== 'pi') : targets);
const offTargets = mcpOffTargets;
const switchable = (server: MCPServer) => switchTargets(server, defaults, targets);
@@ -187,12 +187,12 @@ export default function MCPProjectView({ data, root, offered, onChanged, onRemov
const off = Boolean(entry?.disabled);
const to = switchable(server);
// Off shows as Agent logos. A sentence is kept for the switch that cannot be used, which needs a reason.
const written = (entry?.targets ?? to).filter((x) => mcpOffTargets.includes(x));
const written = (entry?.targets ?? to).filter((x) => mcpOffTargets.includes(x) || x === 'pi');
const reason = entry && !off ? t('mcp.projects.overridden')
: off ? (written.length === 0 ? t('mcp.projects.noSwitchHere') : '')
: to.length > 0 ? ''
// Some Agent could turn it off, just none this project uses.
: switchTargets(server, defaults, mcpOffTargets).length > 0 ? t('mcp.projects.noSwitchHere') : t('mcp.projects.noSwitch');
: switchTargets(server, defaults, [...mcpOffTargets, 'pi']).length > 0 ? t('mcp.projects.noSwitchHere') : t('mcp.projects.noSwitch');
// What the page has to admit: the badge says off, yet an Agent without a switch keeps
// loading the server here, and a list saved with the entry does not follow the project.
const stillOn = off ? targets.filter((x) => (server.targets ?? defaults).includes(x) && !written.includes(x)) : [];
+6 -2
View File
@@ -74,9 +74,13 @@ describe('MCP view helpers', () => {
describe('switchTargets', () => {
const context7 = { command: 'npx', targets: ['claude', 'opencode', 'kilocode', 'pi'] };
// Pi has no per-project switch, so it is never among them.
it('turns a global server off only for the Agents the project uses', () => {
expect(switchTargets(context7, [], ['opencode', 'pi'])).toEqual(['opencode']);
expect(switchTargets(context7, [], ['opencode', 'pi'])).toEqual(['opencode', 'pi']);
});
// Pi's switch carries the global server's command or url, so a switch entry alone cannot reach it.
it('leaves Pi out for a switch entry, which has no command or url', () => {
expect(switchTargets({ disabled: true }, ['opencode', 'pi'], ['opencode', 'pi'])).toEqual(['opencode']);
});
it('follows the default targets of a server that names none', () => {
+3 -3
View File
@@ -282,9 +282,10 @@ export const isResolvable = (change: MCPChange) =>
/** Whether importing can settle a conflict. It reads the Agent's global file, which never holds a project's switch. */
export const canImportConflict = (change: MCPChange) => isResolvable(change) && !change.switch;
/** Agents a project can turn a global server off for: those it uses, that the server reaches and that have a switch. */
/** Agents a project can turn a global server off for: those it uses, that the server reaches and that have a switch.
* Pi takes one when given the global server itself, whose command or url its switch carries. */
export const switchTargets = (server: MCPServer, defaults: string[], projectTargets: readonly string[]) =>
(server.targets ?? defaults).filter((x) => mcpOffTargets.includes(x) && projectTargets.includes(x));
(server.targets ?? defaults).filter((x) => (mcpOffTargets.includes(x) || (x === 'pi' && Boolean(server.command || server.url))) && projectTargets.includes(x));
/** The Agents a server of one scope goes to. A switch that names none follows the scope's, where the Agent has a switch, as sync works it out. */
export const reachOf = (server: MCPServer, defaults: string[]) =>
@@ -337,7 +338,6 @@ const noticeKeys: Record<string, string> = {
const legacyNoticeKeys: Record<string, string> = {
'Pi now uses its built-in MCP; the next sync updates the config': 'mcp.notice.piExtension',
'piOptionsPrune is no longer used; the next sync removes it': 'mcp.notice.piOptionsPrune',
'Pi cannot turn off a server per project; the next sync removes pi from these entries': 'mcp.notice.piSwitch',
'directTools, includeTools and excludeTools become tool settings; the next sync converts them': 'mcp.notice.piTools',
'directTools, includeTools or excludeTools the server already covers, or that are not tool lists, are dropped; the next sync removes them': 'mcp.notice.piToolsDropped',
};
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi nutzt jetzt sein integriertes MCP; der nächste Sync aktualisiert die Konfiguration: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune wird nicht mehr verwendet; der nächste Sync entfernt es: {names}",
"mcp.notice.adapterOptions": "Das integrierte MCP von Pi liest {fields} nicht; der nächste Sync entfernt sie: {names}",
"mcp.notice.piSwitch": "Pi kann einen Server nicht in nur einem Projekt ausschalten; der nächste Sync entfernt Pi aus diesen Einträgen: {names}",
"mcp.notice.piTools": "directTools, includeTools und excludeTools werden zu Tool-Einstellungen; der nächste Sync wandelt sie um: {names}",
"mcp.notice.piToolsDropped": "directTools, includeTools oder excludeTools, die der Server schon abdeckt oder die keine Tool-Listen sind, entfernt der nächste Sync: {names}",
"mcp.notice.toolPolicy": "{agent} kann diesen Teil der Tool-Richtlinie nicht anwenden ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi now uses its built-in MCP; the next sync updates the config: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune is no longer used; the next sync removes it: {names}",
"mcp.notice.adapterOptions": "Pi's built-in MCP does not read {fields}; the next sync removes them: {names}",
"mcp.notice.piSwitch": "Pi cannot turn off a server in one project; the next sync removes Pi from these entries: {names}",
"mcp.notice.piTools": "directTools, includeTools and excludeTools become tool settings; the next sync converts them: {names}",
"mcp.notice.piToolsDropped": "directTools, includeTools or excludeTools that the server already covers, or that are not tool lists, are dropped by the next sync: {names}",
"mcp.notice.toolPolicy": "{agent} cannot apply this part of the tool policy ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi ahora usa su MCP integrado; la próxima sincronización actualiza la configuración: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune ya no se usa; la próxima sincronización lo quita: {names}",
"mcp.notice.adapterOptions": "El MCP integrado de Pi no lee {fields}; la próxima sincronización los quita: {names}",
"mcp.notice.piSwitch": "Pi no puede desactivar un servidor en un solo proyecto; la próxima sincronización quita Pi de estas entradas: {names}",
"mcp.notice.piTools": "directTools, includeTools y excludeTools pasan a ser ajustes de herramientas; la próxima sincronización los convierte: {names}",
"mcp.notice.piToolsDropped": "La próxima sincronización quita los directTools, includeTools o excludeTools que el servidor ya cubre o que no son listas de herramientas: {names}",
"mcp.notice.toolPolicy": "{agent} no puede aplicar esta parte de la política de herramientas ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi اکنون از MCP داخلی خود استفاده می‌کند؛ همگام‌سازی بعدی پیکربندی را به‌روز می‌کند: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune دیگر استفاده نمی‌شود؛ همگام‌سازی بعدی آن را حذف می‌کند: {names}",
"mcp.notice.adapterOptions": "MCP داخلی Pi این‌ها را نمی‌خواند: {fields}؛ همگام‌سازی بعدی آن‌ها را حذف می‌کند: {names}",
"mcp.notice.piSwitch": "Pi نمی‌تواند یک سرور را فقط در یک پروژه خاموش کند؛ همگام‌سازی بعدی Pi را از این مدخل‌ها حذف می‌کند: {names}",
"mcp.notice.piTools": "directTools، includeTools و excludeTools به تنظیمات ابزار تبدیل می‌شوند؛ همگام‌سازی بعدی آن‌ها را تبدیل می‌کند: {names}",
"mcp.notice.piToolsDropped": "همگام‌سازی بعدی directTools، includeTools یا excludeTools را که سرور از قبل پوشش می‌دهد یا فهرست ابزار نیستند حذف می‌کند: {names}",
"mcp.notice.toolPolicy": "{agent} نمی‌تواند این بخش از سیاست ابزار را اعمال کند ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi utilise désormais son MCP intégré ; la prochaine synchronisation met à jour la configuration : {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune n'est plus utilisé ; la prochaine synchronisation le supprime : {names}",
"mcp.notice.adapterOptions": "Le MCP intégré de Pi ne lit pas {fields} ; la prochaine synchronisation les supprime : {names}",
"mcp.notice.piSwitch": "Pi ne peut pas désactiver un serveur dans un seul projet ; la prochaine synchronisation retire Pi de ces entrées : {names}",
"mcp.notice.piTools": "directTools, includeTools et excludeTools deviennent des réglages d'outils ; la prochaine synchronisation les convertit : {names}",
"mcp.notice.piToolsDropped": "La prochaine synchronisation supprime les directTools, includeTools ou excludeTools que le serveur couvre déjà ou qui ne sont pas des listes d'outils : {names}",
"mcp.notice.toolPolicy": "{agent} ne peut pas appliquer cette partie de la politique d'outils ({parts}) : {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi sekarang memakai MCP bawaannya; sinkronisasi berikutnya memperbarui konfigurasi: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune tidak dipakai lagi; sinkronisasi berikutnya menghapusnya: {names}",
"mcp.notice.adapterOptions": "MCP bawaan Pi tidak membaca {fields}; sinkronisasi berikutnya menghapusnya: {names}",
"mcp.notice.piSwitch": "Pi tidak bisa mematikan server di satu proyek saja; sinkronisasi berikutnya menghapus Pi dari entri ini: {names}",
"mcp.notice.piTools": "directTools, includeTools, dan excludeTools menjadi pengaturan alat; sinkronisasi berikutnya mengonversinya: {names}",
"mcp.notice.piToolsDropped": "Sinkronisasi berikutnya menghapus directTools, includeTools, atau excludeTools yang sudah dicakup server atau yang bukan daftar alat: {names}",
"mcp.notice.toolPolicy": "{agent} tidak bisa menerapkan bagian kebijakan alat ini ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi は内蔵 MCP を使うようになりました。次の sync で config を更新します: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune は使われなくなりました。次の sync で削除します: {names}",
"mcp.notice.adapterOptions": "Pi の内蔵 MCP は {fields} を読みません。次の sync で削除します: {names}",
"mcp.notice.piSwitch": "Pi は 1 つのプロジェクトだけでサーバーをオフにできません。次の sync でこれらのエントリから Pi を外します: {names}",
"mcp.notice.piTools": "directTools、includeTools、excludeTools はツール設定に変わります。次の sync で変換します: {names}",
"mcp.notice.piToolsDropped": "サーバーがすでに設定済みの、またはツールのリストでない directTools、includeTools、excludeTools は次の sync で削除されます: {names}",
"mcp.notice.toolPolicy": "{agent} はツールポリシーのこの部分({parts})を適用できません: {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi는 이제 내장 MCP를 사용합니다. 다음 sync에서 설정을 업데이트합니다: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune은 더 이상 사용되지 않습니다. 다음 sync에서 제거합니다: {names}",
"mcp.notice.adapterOptions": "Pi 내장 MCP는 {fields}을(를) 읽지 않습니다. 다음 sync에서 제거합니다: {names}",
"mcp.notice.piSwitch": "Pi는 한 프로젝트에서만 서버를 끌 수 없습니다. 다음 sync에서 이 항목들에서 Pi를 제거합니다: {names}",
"mcp.notice.piTools": "directTools, includeTools, excludeTools는 도구 설정으로 바뀝니다. 다음 sync에서 변환합니다: {names}",
"mcp.notice.piToolsDropped": "서버가 이미 설정했거나 도구 목록이 아닌 directTools, includeTools, excludeTools는 다음 sync에서 제거됩니다: {names}",
"mcp.notice.toolPolicy": "{agent}은(는) 도구 정책의 이 부분({parts})을 적용할 수 없습니다: {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "O Pi agora usa o MCP integrado; a próxima sincronização atualiza a configuração: {names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune não é mais usado; a próxima sincronização o remove: {names}",
"mcp.notice.adapterOptions": "O MCP integrado do Pi não lê {fields}; a próxima sincronização os remove: {names}",
"mcp.notice.piSwitch": "O Pi não consegue desligar um servidor em um só projeto; a próxima sincronização remove o Pi destas entradas: {names}",
"mcp.notice.piTools": "directTools, includeTools e excludeTools viram configurações de ferramentas; a próxima sincronização os converte: {names}",
"mcp.notice.piToolsDropped": "A próxima sincronização remove directTools, includeTools ou excludeTools que o servidor já cobre ou que não são listas de ferramentas: {names}",
"mcp.notice.toolPolicy": "{agent} não consegue aplicar esta parte da política de ferramentas ({parts}): {names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi 现在使用内置 MCP,下次同步会更新配置:{names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune 已不再使用,下次同步会移除:{names}",
"mcp.notice.adapterOptions": "Pi 内置 MCP 不会读取 {fields},下次同步会移除:{names}",
"mcp.notice.piSwitch": "Pi 无法在单个项目中关闭 server,下次同步会从这些条目中移除 Pi:{names}",
"mcp.notice.piTools": "directTools、includeTools 和 excludeTools 会转为工具设置,下次同步会转换:{names}",
"mcp.notice.piToolsDropped": "server 已涵盖、或不是工具列表的 directTools、includeTools 或 excludeTools,下次同步会移除:{names}",
"mcp.notice.toolPolicy": "{agent} 无法应用这部分工具策略({parts}):{names}",
-1
View File
@@ -1430,7 +1430,6 @@
"mcp.notice.piExtension": "Pi 現在使用內建 MCP,下次同步會更新設定:{names}",
"mcp.notice.piOptionsPrune": "piOptionsPrune 已不再使用,下次同步會移除:{names}",
"mcp.notice.adapterOptions": "Pi 內建 MCP 不會讀取 {fields},下次同步會移除:{names}",
"mcp.notice.piSwitch": "Pi 無法在單一專案關閉 server,下次同步會從這些項目移除 Pi:{names}",
"mcp.notice.piTools": "directTools、includeTools 與 excludeTools 會轉成工具設定,下次同步會轉換:{names}",
"mcp.notice.piToolsDropped": "server 已涵蓋、或不是工具清單的 directTools、includeTools 或 excludeTools,下次同步會移除:{names}",
"mcp.notice.toolPolicy": "{agent} 無法套用這部分工具政策({parts}):{names}",