@myrialabs/clopen 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/auth/permissions.ts +41 -2
- package/backend/chat/stream-manager.ts +71 -0
- package/backend/database/migrations/032_seed_copilot_provider.ts +19 -0
- package/backend/database/migrations/033_seed_codex_provider.ts +19 -0
- package/backend/database/migrations/034_seed_qwen_provider.ts +119 -0
- package/backend/database/migrations/035_add_owner_to_db_client_connections.ts +34 -0
- package/backend/database/migrations/index.ts +29 -1
- package/backend/database/queries/db-client-connection-queries.ts +160 -54
- package/backend/database/queries/engine-queries.ts +12 -0
- package/backend/database/queries/project-queries.ts +10 -5
- package/backend/engine/README.md +1702 -0
- package/backend/engine/adapters/claude/models.ts +141 -0
- package/backend/engine/adapters/claude/stream.ts +1 -1
- package/backend/engine/adapters/codex/credential.ts +179 -0
- package/backend/engine/adapters/codex/error-handler.ts +65 -0
- package/backend/engine/adapters/codex/index.ts +1 -0
- package/backend/engine/adapters/codex/message-converter.ts +646 -0
- package/backend/engine/adapters/codex/models.ts +105 -0
- package/backend/engine/adapters/codex/patch-rollout.ts +169 -0
- package/backend/engine/adapters/codex/session-fork.ts +149 -0
- package/backend/engine/adapters/codex/stream.ts +310 -0
- package/backend/engine/adapters/codex/usage-rollout.ts +145 -0
- package/backend/engine/adapters/copilot/error-handler.ts +94 -0
- package/backend/engine/adapters/copilot/index.ts +1 -0
- package/backend/engine/adapters/copilot/message-converter.ts +973 -0
- package/backend/engine/adapters/copilot/models.ts +92 -0
- package/backend/engine/adapters/copilot/session-fork.ts +111 -0
- package/backend/engine/adapters/copilot/stream.ts +573 -0
- package/backend/engine/adapters/opencode/config.ts +56 -104
- package/backend/engine/adapters/opencode/error-handler.ts +71 -0
- package/backend/engine/adapters/opencode/models.ts +79 -0
- package/backend/engine/adapters/opencode/presets.ts +91 -0
- package/backend/engine/adapters/opencode/stream.ts +5 -115
- package/backend/engine/adapters/qwen/credential.ts +78 -0
- package/backend/engine/adapters/qwen/environment.ts +84 -0
- package/backend/engine/adapters/qwen/error-handler.ts +71 -0
- package/backend/engine/adapters/qwen/index.ts +1 -0
- package/backend/engine/adapters/qwen/message-converter.ts +1055 -0
- package/backend/engine/adapters/qwen/models.ts +81 -0
- package/backend/engine/adapters/qwen/presets.ts +51 -0
- package/backend/engine/adapters/qwen/session-fork.ts +97 -0
- package/backend/engine/adapters/qwen/stream.ts +380 -0
- package/backend/engine/index.ts +35 -0
- package/backend/engine/install-recipes.ts +80 -1
- package/backend/engine/install-runner.ts +4 -0
- package/backend/mcp/README.md +84 -14
- package/backend/mcp/config.ts +218 -17
- package/backend/mcp/index.ts +2 -0
- package/backend/mcp/project-context.ts +55 -0
- package/backend/mcp/servers/browser-automation/actions.ts +6 -1
- package/backend/mcp/servers/helper.ts +13 -0
- package/backend/middleware/error-handler.ts +16 -2
- package/backend/preview/browser/browser-preview-service.ts +4 -4
- package/backend/ws/README.md +128 -3
- package/backend/ws/access.ts +56 -0
- package/backend/ws/chat/stream.ts +36 -12
- package/backend/ws/db-client/access.ts +19 -0
- package/backend/ws/db-client/connections.ts +31 -17
- package/backend/ws/db-client/io.ts +7 -9
- package/backend/ws/db-client/query.ts +83 -31
- package/backend/ws/db-client/schema.ts +9 -4
- package/backend/ws/db-client/structure.ts +22 -10
- package/backend/ws/engine/access.ts +39 -0
- package/backend/ws/engine/claude/accounts.ts +15 -10
- package/backend/ws/engine/codex/accounts.ts +516 -0
- package/backend/ws/engine/codex/index.ts +14 -0
- package/backend/ws/engine/codex/status.ts +87 -0
- package/backend/ws/engine/copilot/accounts.ts +122 -0
- package/backend/ws/engine/copilot/index.ts +13 -0
- package/backend/ws/engine/copilot/status.ts +54 -0
- package/backend/ws/engine/index.ts +7 -1
- package/backend/ws/engine/opencode/providers.ts +1 -1
- package/backend/ws/engine/qwen/accounts.ts +137 -0
- package/backend/ws/engine/qwen/index.ts +15 -0
- package/backend/ws/engine/qwen/presets.ts +40 -0
- package/backend/ws/engine/qwen/status.ts +54 -0
- package/backend/ws/files/read.ts +3 -1
- package/backend/ws/files/state.ts +3 -0
- package/backend/ws/files/watch.ts +7 -1
- package/backend/ws/git/branch.ts +13 -19
- package/backend/ws/git/commit-message.ts +3 -4
- package/backend/ws/git/commit.ts +5 -7
- package/backend/ws/git/conflict.ts +7 -10
- package/backend/ws/git/diff.ts +7 -10
- package/backend/ws/git/log.ts +3 -4
- package/backend/ws/git/remote.ts +30 -43
- package/backend/ws/git/staging.ts +13 -19
- package/backend/ws/git/status.ts +7 -11
- package/backend/ws/messages/crud.ts +7 -13
- package/backend/ws/preview/access.ts +45 -0
- package/backend/ws/preview/browser/cleanup.ts +3 -11
- package/backend/ws/preview/browser/console.ts +5 -40
- package/backend/ws/preview/browser/interact.ts +2 -6
- package/backend/ws/preview/browser/native-ui.ts +5 -18
- package/backend/ws/preview/browser/stats.ts +2 -12
- package/backend/ws/preview/browser/tab-info.ts +4 -20
- package/backend/ws/preview/browser/tab.ts +4 -27
- package/backend/ws/preview/browser/webcodecs.ts +6 -47
- package/backend/ws/projects/crud.ts +4 -10
- package/backend/ws/projects/status.ts +2 -0
- package/backend/ws/sessions/crud.ts +22 -33
- package/backend/ws/settings/crud.ts +13 -13
- package/backend/ws/snapshot/restore.ts +5 -2
- package/backend/ws/snapshot/timeline.ts +3 -1
- package/backend/ws/system-tools/access.ts +28 -0
- package/backend/ws/system-tools/install.ts +13 -4
- package/backend/ws/system-tools/status.ts +5 -2
- package/backend/ws/terminal/access.ts +56 -0
- package/backend/ws/terminal/persistence.ts +12 -6
- package/backend/ws/terminal/session.ts +35 -14
- package/backend/ws/terminal/stream.ts +4 -1
- package/dist/assets/{index-DqAGCTFT.js → index-D8V2wj9B.js} +2 -2
- package/dist/assets/{main-DsED0rLr.js → main-C9nYVmnw.js} +206 -198
- package/dist/assets/main-hspyFOoK.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +4 -1
- package/shared/constants/engines.ts +45 -140
- package/shared/types/database/schema.ts +1 -0
- package/shared/types/unified/common.ts +1 -1
- package/shared/types/unified/engine.ts +29 -1
- package/shared/types/unified/index.ts +2 -0
- package/shared/types/unified/message.ts +1 -1
- package/shared/types/unified/tool.ts +17 -2
- package/shared/utils/ws-server.ts +14 -0
- package/dist/assets/main-eWY9R8g9.css +0 -1
|
@@ -20,7 +20,7 @@ import { getClopenDir } from '$backend/utils/paths';
|
|
|
20
20
|
import { resolveBinary, resolveBinaryWithRefresh } from '$backend/utils/cli';
|
|
21
21
|
import { resolveStaticCurlAsset } from '$backend/utils/static-curl';
|
|
22
22
|
|
|
23
|
-
export type ToolId = 'git' | 'claude' | 'opencode' | 'chrome' | 'cloudflared';
|
|
23
|
+
export type ToolId = 'git' | 'claude' | 'opencode' | 'copilot' | 'codex' | 'qwen' | 'chrome' | 'cloudflared';
|
|
24
24
|
|
|
25
25
|
export interface ManualInstruction {
|
|
26
26
|
label: string;
|
|
@@ -409,6 +409,82 @@ async function resolveOpenCodeRecipe(): Promise<Recipe> {
|
|
|
409
409
|
return base;
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
+
async function resolveCopilotRecipe(): Promise<Recipe> {
|
|
413
|
+
const base: Recipe = {
|
|
414
|
+
tool: 'copilot',
|
|
415
|
+
autoInstallable: true,
|
|
416
|
+
missingPrereqs: [],
|
|
417
|
+
manualInstructions: [{
|
|
418
|
+
label: 'bun',
|
|
419
|
+
command: 'bun add -g @github/copilot',
|
|
420
|
+
docs: 'https://docs.github.com/en/copilot/how-tos/copilot-cli/set-up-copilot-cli/install-copilot-cli'
|
|
421
|
+
}],
|
|
422
|
+
command: ['bun', 'add', '-g', '@github/copilot'],
|
|
423
|
+
displayCommand: 'bun add -g @github/copilot'
|
|
424
|
+
};
|
|
425
|
+
return base;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async function resolveQwenRecipe(): Promise<Recipe> {
|
|
429
|
+
const manualInstructions: ManualInstruction[] = [{
|
|
430
|
+
label: 'bun',
|
|
431
|
+
command: 'bun add -g @qwen-code/qwen-code',
|
|
432
|
+
docs: 'https://github.com/QwenLM/qwen-code'
|
|
433
|
+
}];
|
|
434
|
+
|
|
435
|
+
// Qwen Code CLI ships bundled with `@qwen-code/sdk` from v0.1.1+ so the
|
|
436
|
+
// SDK path doesn't need a standalone install. We still expose this recipe
|
|
437
|
+
// for users who prefer the standalone CLI for `qwen-oauth` / shell use.
|
|
438
|
+
if (!resolveBinary('bun')) {
|
|
439
|
+
return {
|
|
440
|
+
tool: 'qwen',
|
|
441
|
+
autoInstallable: false,
|
|
442
|
+
unavailableReason: 'Bun is required to install the Qwen Code CLI. Install Bun first (https://bun.sh).',
|
|
443
|
+
missingPrereqs: [],
|
|
444
|
+
manualInstructions
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return {
|
|
449
|
+
tool: 'qwen',
|
|
450
|
+
autoInstallable: true,
|
|
451
|
+
missingPrereqs: [],
|
|
452
|
+
manualInstructions,
|
|
453
|
+
command: ['bun', 'add', '-g', '@qwen-code/qwen-code'],
|
|
454
|
+
displayCommand: 'bun add -g @qwen-code/qwen-code'
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async function resolveCodexRecipe(): Promise<Recipe> {
|
|
459
|
+
const manualInstructions: ManualInstruction[] = [{
|
|
460
|
+
label: 'bun',
|
|
461
|
+
command: 'bun add -g @openai/codex',
|
|
462
|
+
docs: 'https://developers.openai.com/codex'
|
|
463
|
+
}];
|
|
464
|
+
|
|
465
|
+
// Codex CLI is installed via `bun add -g`. Bun must be on PATH for the
|
|
466
|
+
// auto-installer to work; otherwise we fall back to the manual instructions
|
|
467
|
+
// (the user installs Bun first via the existing Bun recipe / website).
|
|
468
|
+
if (!resolveBinary('bun')) {
|
|
469
|
+
return {
|
|
470
|
+
tool: 'codex',
|
|
471
|
+
autoInstallable: false,
|
|
472
|
+
unavailableReason: 'Bun is required to install the Codex CLI. Install Bun first (https://bun.sh).',
|
|
473
|
+
missingPrereqs: [],
|
|
474
|
+
manualInstructions
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return {
|
|
479
|
+
tool: 'codex',
|
|
480
|
+
autoInstallable: true,
|
|
481
|
+
missingPrereqs: [],
|
|
482
|
+
manualInstructions,
|
|
483
|
+
command: ['bun', 'add', '-g', '@openai/codex'],
|
|
484
|
+
displayCommand: 'bun add -g @openai/codex'
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
|
|
412
488
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
413
489
|
// Chrome recipe — Puppeteer download (macOS/Windows) or Google Chrome (Linux)
|
|
414
490
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -660,6 +736,9 @@ export async function resolveRecipe(tool: ToolId): Promise<Recipe> {
|
|
|
660
736
|
case 'git': return resolveGitRecipe();
|
|
661
737
|
case 'claude': return resolveClaudeRecipe();
|
|
662
738
|
case 'opencode': return resolveOpenCodeRecipe();
|
|
739
|
+
case 'copilot': return resolveCopilotRecipe();
|
|
740
|
+
case 'codex': return resolveCodexRecipe();
|
|
741
|
+
case 'qwen': return resolveQwenRecipe();
|
|
663
742
|
case 'chrome': return resolveChromeRecipe();
|
|
664
743
|
case 'cloudflared': return resolveCloudflaredRecipe();
|
|
665
744
|
}
|
|
@@ -361,6 +361,10 @@ export function getSession(sessionId: string) {
|
|
|
361
361
|
return session ? sessionSnapshot(session) : null;
|
|
362
362
|
}
|
|
363
363
|
|
|
364
|
+
export function getSessionOwner(sessionId: string): string | null {
|
|
365
|
+
return sessions.get(sessionId)?.startedBy ?? null;
|
|
366
|
+
}
|
|
367
|
+
|
|
364
368
|
export function getActiveSessionForTool(tool: ToolId) {
|
|
365
369
|
const id = activeByTool.get(tool);
|
|
366
370
|
if (!id) return null;
|
package/backend/mcp/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Custom MCP Tools
|
|
2
2
|
|
|
3
|
-
Custom MCP (Model Context Protocol) tools for adding specialized functionality to
|
|
3
|
+
Custom MCP (Model Context Protocol) tools for adding specialized functionality to all AI engines (**Claude Code**, **Open Code**, **Codex**, **Copilot**, …). Servers are defined once and shared across every engine via a single-source-of-truth architecture.
|
|
4
4
|
|
|
5
5
|
## 📚 Table of Contents
|
|
6
6
|
|
|
@@ -22,10 +22,11 @@ Custom MCP (Model Context Protocol) tools for adding specialized functionality t
|
|
|
22
22
|
System for adding custom tools to AI engines with type-safe TypeScript definitions. Tools are defined once using `defineServer()` and automatically available to both Claude Code (in-process) and Open Code (remote HTTP MCP server).
|
|
23
23
|
|
|
24
24
|
**Features:**
|
|
25
|
-
- Single source of truth — define tools once, use in
|
|
25
|
+
- Single source of truth — define tools once, use in every engine
|
|
26
26
|
- In-process execution for Claude Code via `createSdkMcpServer`
|
|
27
|
-
- Remote HTTP MCP server for Open Code
|
|
28
|
-
|
|
27
|
+
- Remote HTTP MCP server (Streamable HTTP) for Open Code, Codex, and
|
|
28
|
+
Copilot via `createRemoteMcpServer` mounted at `/mcp`
|
|
29
|
+
- All engines execute handlers in-process (no subprocess, no bridge)
|
|
29
30
|
- Type-safe with TypeScript
|
|
30
31
|
- Auto metadata extraction and registration
|
|
31
32
|
- Configuration-based enable/disable
|
|
@@ -537,7 +538,22 @@ const mcpConfig = getOpenCodeMcpConfig();
|
|
|
537
538
|
```
|
|
538
539
|
|
|
539
540
|
#### `resolveOpenCodeToolName(toolName)`
|
|
540
|
-
Resolve
|
|
541
|
+
Resolve a remote-MCP tool name to `mcp__server__tool` format (single source
|
|
542
|
+
of truth — used by every non-Claude engine adapter).
|
|
543
|
+
|
|
544
|
+
The remote-MCP namespace key is always `clopen-mcp`, but engines disagree on
|
|
545
|
+
the separator they use to join the namespace and the bare tool name:
|
|
546
|
+
|
|
547
|
+
| Engine | Tool name shape | Separator |
|
|
548
|
+
| --------- | -------------------------------- | --------- |
|
|
549
|
+
| Open Code | `clopen-mcp_open_new_tab` | `_` |
|
|
550
|
+
| Copilot | `clopen-mcp-open_new_tab` | `-` |
|
|
551
|
+
| Codex | `open_new_tab` (bare) | n/a |
|
|
552
|
+
|
|
553
|
+
The resolver strips **both** `clopen-mcp_` and `clopen-mcp-` prefixes and
|
|
554
|
+
falls back to the bare-name path. **Adapters must call this helper instead
|
|
555
|
+
of building a per-engine resolver** — if a future SDK introduces yet another
|
|
556
|
+
separator, extend the prefix list here, not at the call site.
|
|
541
557
|
|
|
542
558
|
```typescript
|
|
543
559
|
import { resolveOpenCodeToolName } from '$backend/mcp';
|
|
@@ -545,6 +561,9 @@ import { resolveOpenCodeToolName } from '$backend/mcp';
|
|
|
545
561
|
resolveOpenCodeToolName("clopen-mcp_get_temperature");
|
|
546
562
|
// Returns: "mcp__weather-service__get_temperature"
|
|
547
563
|
|
|
564
|
+
resolveOpenCodeToolName("clopen-mcp-get_temperature");
|
|
565
|
+
// Returns: "mcp__weather-service__get_temperature"
|
|
566
|
+
|
|
548
567
|
resolveOpenCodeToolName("get_temperature");
|
|
549
568
|
// Returns: "mcp__weather-service__get_temperature"
|
|
550
569
|
|
|
@@ -552,6 +571,30 @@ resolveOpenCodeToolName("unknown_tool");
|
|
|
552
571
|
// Returns: null
|
|
553
572
|
```
|
|
554
573
|
|
|
574
|
+
**Symptom of skipping this helper:** the UI renders the tool with a
|
|
575
|
+
doubled-up name like `mcp__clopen-mcp__clopen-mcp-open_new_tab` instead
|
|
576
|
+
of `mcp__browser-automation__open_new_tab`. The prefix didn't strip and
|
|
577
|
+
the registry lookup fell through.
|
|
578
|
+
|
|
579
|
+
#### `getCodexMcpConfig()` / `getCopilotMcpConfig()`
|
|
580
|
+
Sibling helpers to `getOpenCodeMcpConfig()` for the Codex and Copilot
|
|
581
|
+
engines. Each returns the **same** `/mcp` URL in the SDK-specific shape:
|
|
582
|
+
|
|
583
|
+
```typescript
|
|
584
|
+
// Codex (config object flattened to --config flags)
|
|
585
|
+
getCodexMcpConfig();
|
|
586
|
+
// { 'clopen-mcp': { url: 'http://localhost:9151/mcp', tools: { ... } } }
|
|
587
|
+
|
|
588
|
+
// Copilot (MCPHTTPServerConfig from @github/copilot-sdk)
|
|
589
|
+
getCopilotMcpConfig();
|
|
590
|
+
// { 'clopen-mcp': { type: 'http', url: 'http://localhost:9151/mcp', tools: [...] } }
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
When adding a new engine that consumes streamable-HTTP MCP, add a sibling
|
|
594
|
+
`getXxxMcpConfig()` here — do **not** introduce a new HTTP listener or a
|
|
595
|
+
new namespace key. See `backend/engine/README.md` §9.12 for the full
|
|
596
|
+
checklist (variable naming, type imports, auto-approval surface, etc.).
|
|
597
|
+
|
|
555
598
|
### Helper Functions
|
|
556
599
|
|
|
557
600
|
```typescript
|
|
@@ -1078,34 +1121,61 @@ schema: {
|
|
|
1078
1121
|
|
|
1079
1122
|
---
|
|
1080
1123
|
|
|
1081
|
-
## Open Code
|
|
1124
|
+
## Engine Integration (Open Code, Codex, Copilot, …)
|
|
1082
1125
|
|
|
1083
1126
|
### How It Works
|
|
1084
1127
|
|
|
1085
|
-
|
|
1128
|
+
Every non-Claude engine connects to the **same remote HTTP MCP server**
|
|
1129
|
+
running in the main Clopen process. Tool handlers execute directly
|
|
1130
|
+
in-process — no subprocess, no per-engine bridge. This is
|
|
1131
|
+
architecturally identical to how Claude Code uses in-process MCP
|
|
1132
|
+
servers.
|
|
1086
1133
|
|
|
1087
1134
|
1. **`remote-server.ts`** — HTTP MCP server mounted at `/mcp` on the main Elysia server. Uses `WebStandardStreamableHTTPServerTransport` from `@modelcontextprotocol/sdk` for the Streamable HTTP transport protocol.
|
|
1088
1135
|
|
|
1089
1136
|
2. **`createRemoteMcpServer()`** — Factory in `servers/helper.ts` that creates a `McpServer` instance with tools registered from the same `RawToolDef` definitions used by Claude Code. Each MCP session gets its own server instance.
|
|
1090
1137
|
|
|
1091
|
-
3. **`
|
|
1138
|
+
3. **`getXxxMcpConfig()` per engine** — `config.ts` exports one helper per
|
|
1139
|
+
engine that returns the **same** `/mcp` URL in the engine's expected
|
|
1140
|
+
shape. They all share the `'clopen-mcp'` namespace key:
|
|
1141
|
+
- `getOpenCodeMcpConfig()` → `{ type: 'remote', url, ... }`
|
|
1142
|
+
- `getCodexMcpConfig()` → `{ url, tools: { <tool>: { approval_mode: 'approve' } } }`
|
|
1143
|
+
- `getCopilotMcpConfig()` → `{ type: 'http', url, tools: [...] }`
|
|
1092
1144
|
|
|
1093
|
-
### Adding
|
|
1145
|
+
### Adding a New Tool
|
|
1094
1146
|
|
|
1095
|
-
No extra work needed! When you add a new tool via `defineServer()`, it's
|
|
1147
|
+
No extra work needed! When you add a new tool via `defineServer()`, it's
|
|
1148
|
+
automatically available to **every** engine:
|
|
1096
1149
|
|
|
1097
1150
|
- **Claude Code**: Uses the in-process `createSdkMcpServer` instance
|
|
1098
|
-
- **
|
|
1151
|
+
- **Everyone else**: `remote-server.ts` uses `createRemoteMcpServer()`
|
|
1152
|
+
with the same `allServers` and `mcpServersConfig`
|
|
1153
|
+
|
|
1154
|
+
### Adding a New Engine
|
|
1155
|
+
|
|
1156
|
+
If your engine's CLI/SDK accepts a streamable-HTTP MCP URL, follow the
|
|
1157
|
+
checklist in `backend/engine/README.md` §9.12. The summary:
|
|
1158
|
+
|
|
1159
|
+
1. Add a sibling `getXxxMcpConfig()` next to the existing helpers in
|
|
1160
|
+
`config.ts`. Reuse the `'clopen-mcp'` namespace key. Use the SDK's
|
|
1161
|
+
exported config type if it has one.
|
|
1162
|
+
2. In the adapter, name the local variable holding the helper's return
|
|
1163
|
+
value `mcpConfig` (not `mcpServers` — that shadows the registry).
|
|
1164
|
+
3. Use `resolveOpenCodeToolName()` to canonicalise incoming tool names.
|
|
1165
|
+
If the SDK introduces a new prefix separator, extend the prefix list
|
|
1166
|
+
in that helper rather than building a per-engine resolver.
|
|
1167
|
+
4. Wire the engine's auto-approval path (runtime callback or static
|
|
1168
|
+
per-tool config) so MCP tool calls don't get cancelled.
|
|
1099
1169
|
|
|
1100
1170
|
### File Locations
|
|
1101
1171
|
|
|
1102
1172
|
| File | Location | Purpose |
|
|
1103
1173
|
|------|----------|---------|
|
|
1104
1174
|
| Tool definitions | `backend/mcp/servers/` | Single source of truth |
|
|
1105
|
-
| Remote MCP server | `backend/mcp/remote-server.ts` | HTTP server for
|
|
1175
|
+
| Remote MCP server | `backend/mcp/remote-server.ts` | HTTP server for every non-Claude engine |
|
|
1106
1176
|
| Server factory | `backend/mcp/servers/helper.ts` | `createRemoteMcpServer()` |
|
|
1107
|
-
|
|
|
1108
|
-
| Tool name resolver | `backend/mcp/config.ts` | `resolveOpenCodeToolName()` |
|
|
1177
|
+
| Per-engine config | `backend/mcp/config.ts` | `getOpenCodeMcpConfig()`, `getCodexMcpConfig()`, `getCopilotMcpConfig()` |
|
|
1178
|
+
| Tool name resolver | `backend/mcp/config.ts` | `resolveOpenCodeToolName()` (handles every engine's prefix variant) |
|
|
1109
1179
|
|
|
1110
1180
|
---
|
|
1111
1181
|
|
package/backend/mcp/config.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
import { createSdkMcpServer, tool, type McpSdkServerConfigWithInstance, type McpServerConfig } from "@anthropic-ai/claude-agent-sdk";
|
|
9
9
|
import type { McpRemoteConfig } from '@opencode-ai/sdk';
|
|
10
|
+
import type { MCPHTTPServerConfig } from '@github/copilot-sdk';
|
|
11
|
+
import type { CLIMcpServerConfig as QwenMcpServerConfig } from '@qwen-code/sdk';
|
|
10
12
|
import type { ServerConfig, ParsedMcpToolName, ServerName } from './types';
|
|
11
13
|
import type { McpExecutionContext } from '../engine/types';
|
|
12
14
|
import { serverRegistry, serverFactories, serverMetadata } from './servers';
|
|
@@ -103,12 +105,20 @@ export function getEnabledMcpServers(context?: McpExecutionContext): Record<stri
|
|
|
103
105
|
if (serverConfig.enabled) {
|
|
104
106
|
if (context) {
|
|
105
107
|
// Create context-bound instance: wrap each tool handler so
|
|
106
|
-
// AsyncLocalStorage context is restored on invocation
|
|
108
|
+
// AsyncLocalStorage context is restored on invocation, then
|
|
109
|
+
// short-circuit if the owning stream's AbortSignal has fired
|
|
110
|
+
// — handler never runs, no half-completed browser ops.
|
|
107
111
|
const meta = serverMetadata[serverName as ServerName];
|
|
108
112
|
const sdkTools = (serverConfig.tools as readonly string[]).map(toolName => {
|
|
109
113
|
const def = meta.toolDefs[toolName];
|
|
110
114
|
const boundHandler = async (args: any) => {
|
|
111
|
-
return projectContextService.runWithContextAsync(context, () =>
|
|
115
|
+
return projectContextService.runWithContextAsync(context, async () => {
|
|
116
|
+
const signal = projectContextService.getCurrentSignal();
|
|
117
|
+
if (signal?.aborted) {
|
|
118
|
+
return abortedToolResult(toolName);
|
|
119
|
+
}
|
|
120
|
+
return def.handler(args);
|
|
121
|
+
});
|
|
112
122
|
};
|
|
113
123
|
return tool(toolName, def.description, def.schema, boundHandler as any);
|
|
114
124
|
});
|
|
@@ -132,6 +142,25 @@ export function getEnabledMcpServers(context?: McpExecutionContext): Record<stri
|
|
|
132
142
|
return enabledServers;
|
|
133
143
|
}
|
|
134
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Standard MCP error response when a tool call is rejected because the
|
|
147
|
+
* owning chat stream has been cancelled. Returned by both the in-process
|
|
148
|
+
* (Claude) wrapper and the remote-HTTP wrapper so engines see a consistent
|
|
149
|
+
* "tool refused" outcome no matter which transport they used.
|
|
150
|
+
*/
|
|
151
|
+
function abortedToolResult(toolName: string): { content: Array<{ type: 'text'; text: string }>; isError: true } {
|
|
152
|
+
return {
|
|
153
|
+
content: [{ type: 'text' as const, text: `Tool ${toolName} was cancelled because the chat stream was interrupted.` }],
|
|
154
|
+
isError: true,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Re-export for use by `backend/mcp/servers/helper.ts::createRemoteMcpServer`,
|
|
160
|
+
* which wraps remote-HTTP handlers with the same abort check.
|
|
161
|
+
*/
|
|
162
|
+
export { abortedToolResult };
|
|
163
|
+
|
|
135
164
|
/**
|
|
136
165
|
* Get list of all allowed MCP tool names
|
|
137
166
|
*
|
|
@@ -273,30 +302,41 @@ export function getMcpStats() {
|
|
|
273
302
|
// ============================================================================
|
|
274
303
|
|
|
275
304
|
/**
|
|
276
|
-
* Resolve
|
|
305
|
+
* Resolve a remote-MCP tool name to our mcp__server__tool format.
|
|
277
306
|
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
307
|
+
* Different SDKs prepend the MCP server config key (`clopen-mcp`) to tool
|
|
308
|
+
* names with different separators:
|
|
309
|
+
* - Open Code: "clopen-mcp_open_new_tab" (underscore)
|
|
310
|
+
* - Copilot: "clopen-mcp-open_new_tab" (hyphen)
|
|
311
|
+
* - Codex: "open_new_tab" (no prefix; mcpServerName carried separately)
|
|
312
|
+
* - Qwen Code: "mcp__clopen-mcp__open_new_tab" (already mcp-prefixed using OUR namespace key)
|
|
280
313
|
*
|
|
281
|
-
* This function strips
|
|
282
|
-
* registry — the SAME source
|
|
283
|
-
*
|
|
284
|
-
* Works for both remote HTTP MCP and legacy stdio MCP (same naming convention).
|
|
314
|
+
* This function strips whichever prefix is present and maps the bare tool
|
|
315
|
+
* name back using the `mcpServers` registry — the SAME source of truth that
|
|
316
|
+
* defines which tools exist.
|
|
285
317
|
*
|
|
286
318
|
* Returns null if the tool name is not one of our custom MCP tools.
|
|
287
319
|
*/
|
|
288
320
|
export function resolveOpenCodeToolName(toolName: string): string | null {
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
//
|
|
293
|
-
//
|
|
321
|
+
// Strip the remote-MCP server prefix if present. Both `_` and `-` are
|
|
322
|
+
// observed in the wild depending on the engine's SDK; Qwen prepends the
|
|
323
|
+
// full `mcp__<namespace>__` form so the bare tool name still needs to be
|
|
324
|
+
// re-resolved against the registry to land on the correct server alias
|
|
325
|
+
// (`mcp__browser-automation__open_new_tab`, not `mcp__clopen-mcp__...`).
|
|
294
326
|
let rawName = toolName;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
327
|
+
let strippedNamespace = false;
|
|
328
|
+
for (const prefix of ['mcp__clopen-mcp__', 'clopen-mcp_', 'clopen-mcp-']) {
|
|
329
|
+
if (rawName.startsWith(prefix)) {
|
|
330
|
+
rawName = rawName.slice(prefix.length);
|
|
331
|
+
strippedNamespace = true;
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
298
334
|
}
|
|
299
335
|
|
|
336
|
+
// Already in our canonical format and not the legacy double-prefixed
|
|
337
|
+
// `mcp__clopen-mcp__<tool>` shape — pass through unchanged.
|
|
338
|
+
if (!strippedNamespace && rawName.startsWith('mcp__')) return rawName;
|
|
339
|
+
|
|
300
340
|
// Look up which server owns this tool
|
|
301
341
|
for (const [serverName, serverConfig] of Object.entries(mcpServers)) {
|
|
302
342
|
if (!serverConfig.enabled) continue;
|
|
@@ -340,3 +380,164 @@ export function getOpenCodeMcpConfig(): Record<string, McpRemoteConfig> {
|
|
|
340
380
|
},
|
|
341
381
|
};
|
|
342
382
|
}
|
|
383
|
+
|
|
384
|
+
// ============================================================================
|
|
385
|
+
// Codex MCP Configuration
|
|
386
|
+
// ============================================================================
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Per-MCP-server config shape consumed by the Codex CLI.
|
|
390
|
+
*
|
|
391
|
+
* Mirrors the subset of `RawMcpServerConfig` we actually emit. The SDK
|
|
392
|
+
* flattens this object to `--config mcp_servers.<server>.<key>=<value>` flags
|
|
393
|
+
* on every `codex exec` invocation.
|
|
394
|
+
*/
|
|
395
|
+
type CodexMcpServerConfig = {
|
|
396
|
+
url: string;
|
|
397
|
+
tools?: Record<string, { approval_mode: 'auto' | 'prompt' | 'approve' }>;
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Get MCP configuration for Codex engine.
|
|
402
|
+
*
|
|
403
|
+
* Codex's CLI accepts an `mcp_servers.<name>.url` config that points at any
|
|
404
|
+
* Streamable HTTP MCP endpoint. We reuse the SAME `/mcp` URL Open Code
|
|
405
|
+
* already consumes — no new HTTP server, no per-engine bridge. Tool handlers
|
|
406
|
+
* execute in-process in the Clopen backend.
|
|
407
|
+
*
|
|
408
|
+
* Approval: in non-interactive `codex exec` mode there is no UI to approve
|
|
409
|
+
* MCP tool calls, so the CLI auto-cancels them with
|
|
410
|
+
* "Error: user cancelled MCP tool call". Codex has no top-level
|
|
411
|
+
* "auto-approve all MCP" switch — it only exposes per-tool
|
|
412
|
+
* `mcp_servers.<name>.tools.<tool>.approval_mode`. We enumerate every
|
|
413
|
+
* enabled tool here and set `approval_mode = "approve"` so the subprocess
|
|
414
|
+
* runs them without asking. Open Code achieves the same outcome by replying
|
|
415
|
+
* to permission events at runtime — Codex requires the decision up-front.
|
|
416
|
+
*
|
|
417
|
+
* The shape returned here is flattened by the Codex SDK into
|
|
418
|
+
* `--config mcp_servers.clopen-mcp.<dotted-key>=<value>` flags when the SDK
|
|
419
|
+
* invokes the CLI subprocess for each turn.
|
|
420
|
+
*/
|
|
421
|
+
export function getCodexMcpConfig(): Record<string, CodexMcpServerConfig> {
|
|
422
|
+
const enabledServers = getEnabledServerNames();
|
|
423
|
+
if (enabledServers.length === 0) {
|
|
424
|
+
return {};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const port = SERVER_ENV.PORT;
|
|
428
|
+
|
|
429
|
+
const tools: Record<string, { approval_mode: 'approve' }> = {};
|
|
430
|
+
for (const serverName of enabledServers) {
|
|
431
|
+
const serverConfig = mcpServers[serverName];
|
|
432
|
+
for (const toolName of serverConfig.tools as readonly string[]) {
|
|
433
|
+
tools[toolName] = { approval_mode: 'approve' };
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
debug.log('mcp', `📦 Codex MCP: remote server at http://localhost:${port}/mcp (auto-approving ${Object.keys(tools).length} tools)`);
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
'clopen-mcp': {
|
|
441
|
+
url: `http://localhost:${port}/mcp`,
|
|
442
|
+
tools,
|
|
443
|
+
},
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// ============================================================================
|
|
448
|
+
// Copilot MCP Configuration
|
|
449
|
+
// ============================================================================
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Get MCP configuration for the Copilot engine.
|
|
453
|
+
*
|
|
454
|
+
* The Copilot SDK accepts a `mcpServers` map on both `SessionConfig` and
|
|
455
|
+
* `ResumeSessionConfig` and supports the streamable-HTTP transport directly
|
|
456
|
+
* (`MCPHTTPServerConfig` with `type: 'http'`). We reuse the SAME `/mcp` URL
|
|
457
|
+
* Open Code and Codex already consume — no new HTTP server, no per-engine
|
|
458
|
+
* bridge. Tool handlers execute in-process in the Clopen backend.
|
|
459
|
+
*
|
|
460
|
+
* Approval: Copilot already auto-approves every tool call via
|
|
461
|
+
* `onPermissionRequest: approveAll` in `copilot/stream.ts`, which covers MCP
|
|
462
|
+
* tools too. No per-tool approval enumeration is required (unlike Codex).
|
|
463
|
+
*
|
|
464
|
+
* The `tools` field lists the bare tool names exposed by our remote MCP
|
|
465
|
+
* server. The SDK delivers tool calls back with `mcpServerName: 'clopen-mcp'`
|
|
466
|
+
* + the bare tool name; `mapCopilotToolName()` then routes them through
|
|
467
|
+
* `resolveOpenCodeToolName()` to recover the canonical
|
|
468
|
+
* `mcp__<server>__<tool>` form.
|
|
469
|
+
*/
|
|
470
|
+
export function getCopilotMcpConfig(): Record<string, MCPHTTPServerConfig> {
|
|
471
|
+
const enabledServers = getEnabledServerNames();
|
|
472
|
+
if (enabledServers.length === 0) {
|
|
473
|
+
return {};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const port = SERVER_ENV.PORT;
|
|
477
|
+
|
|
478
|
+
const tools: string[] = [];
|
|
479
|
+
for (const serverName of enabledServers) {
|
|
480
|
+
const serverConfig = mcpServers[serverName];
|
|
481
|
+
for (const toolName of serverConfig.tools as readonly string[]) {
|
|
482
|
+
tools.push(toolName);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
debug.log('mcp', `📦 Copilot MCP: remote server at http://localhost:${port}/mcp (${tools.length} tools)`);
|
|
487
|
+
|
|
488
|
+
return {
|
|
489
|
+
'clopen-mcp': {
|
|
490
|
+
type: 'http',
|
|
491
|
+
url: `http://localhost:${port}/mcp`,
|
|
492
|
+
tools,
|
|
493
|
+
timeout: 10_000,
|
|
494
|
+
},
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// ============================================================================
|
|
499
|
+
// Qwen Code MCP Configuration
|
|
500
|
+
// ============================================================================
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Get MCP configuration for the Qwen Code engine.
|
|
504
|
+
*
|
|
505
|
+
* The Qwen Code SDK / CLI accepts a `mcpServers` map keyed by server name with
|
|
506
|
+
* a `CLIMcpServerConfig` value that supports the Streamable-HTTP transport via
|
|
507
|
+
* `httpUrl`. We reuse the SAME `/mcp` URL Open Code, Codex and Copilot already
|
|
508
|
+
* consume — no new HTTP server, no per-engine bridge. Tool handlers run
|
|
509
|
+
* in-process in the Clopen backend (README §9.12).
|
|
510
|
+
*
|
|
511
|
+
* Approval: the Qwen adapter uses `permissionMode: 'default'` with a
|
|
512
|
+
* `canUseTool` callback that auto-allows everything. `AskUserQuestion` is
|
|
513
|
+
* blocked at the SDK level via `excludeTools` (highest permission priority),
|
|
514
|
+
* so the model never sees it. The callback covers MCP tool calls too — no
|
|
515
|
+
* per-tool enumeration needed.
|
|
516
|
+
*/
|
|
517
|
+
export function getQwenMcpConfig(): Record<string, QwenMcpServerConfig> {
|
|
518
|
+
const enabledServers = getEnabledServerNames();
|
|
519
|
+
if (enabledServers.length === 0) {
|
|
520
|
+
return {};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const port = SERVER_ENV.PORT;
|
|
524
|
+
|
|
525
|
+
const includeTools: string[] = [];
|
|
526
|
+
for (const serverName of enabledServers) {
|
|
527
|
+
const serverConfig = mcpServers[serverName];
|
|
528
|
+
for (const toolName of serverConfig.tools as readonly string[]) {
|
|
529
|
+
includeTools.push(toolName);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
debug.log('mcp', `📦 Qwen MCP: remote server at http://localhost:${port}/mcp (${includeTools.length} tools)`);
|
|
534
|
+
|
|
535
|
+
return {
|
|
536
|
+
'clopen-mcp': {
|
|
537
|
+
httpUrl: `http://localhost:${port}/mcp`,
|
|
538
|
+
includeTools,
|
|
539
|
+
timeout: 10_000,
|
|
540
|
+
trust: true,
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
}
|
package/backend/mcp/index.ts
CHANGED
|
@@ -38,6 +38,16 @@ class ProjectContextService {
|
|
|
38
38
|
// Track the most recently used projectId (as a last resort fallback)
|
|
39
39
|
private lastUsedProjectId: string | null = null;
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Per-stream AbortSignal map. The stream-manager registers each stream's
|
|
43
|
+
* AbortController.signal here when the stream starts so MCP tool handlers
|
|
44
|
+
* can fast-fail on cancellation. The remote-MCP HTTP transport (used by
|
|
45
|
+
* OpenCode/Codex/Copilot/Qwen) does not preserve the engine's
|
|
46
|
+
* AsyncLocalStorage context across the HTTP boundary — `mostRecentActiveStream`
|
|
47
|
+
* is the practical fallback for resolving "which stream is asking right now".
|
|
48
|
+
*/
|
|
49
|
+
private streamSignals = new Map<string, AbortSignal>();
|
|
50
|
+
|
|
41
51
|
/**
|
|
42
52
|
* Register a chat session with its project ID
|
|
43
53
|
* Should be called when a chat stream starts
|
|
@@ -109,12 +119,56 @@ class ProjectContextService {
|
|
|
109
119
|
debug.log('mcp', `🗑️ Unregistered session ${chatSessionId}`);
|
|
110
120
|
}
|
|
111
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Register an AbortSignal for a stream. MCP tool handlers can read this
|
|
124
|
+
* via `getCurrentSignal()` to short-circuit when the engine is cancelled
|
|
125
|
+
* — without it, in-flight browser-automation calls keep running for a
|
|
126
|
+
* second or two after the user interrupts the chat (visible as the
|
|
127
|
+
* "preview keeps moving by itself" symptom).
|
|
128
|
+
*/
|
|
129
|
+
registerStreamSignal(streamId: string, signal: AbortSignal): void {
|
|
130
|
+
if (!streamId) return;
|
|
131
|
+
this.streamSignals.set(streamId, signal);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Resolve the AbortSignal that belongs to the currently-executing MCP
|
|
136
|
+
* tool invocation. Tries (in order):
|
|
137
|
+
* 1. AsyncLocalStorage context's `streamId` (Claude in-process path).
|
|
138
|
+
* 2. AsyncLocalStorage context's `chatSessionId` mapped to its stream.
|
|
139
|
+
* 3. The most recent active stream (fallback for the remote HTTP MCP
|
|
140
|
+
* path — OpenCode/Codex/Copilot/Qwen — where context doesn't carry).
|
|
141
|
+
*
|
|
142
|
+
* Returns `undefined` when no stream is in-flight; handlers should treat
|
|
143
|
+
* absence as "not abortable" rather than as "aborted".
|
|
144
|
+
*/
|
|
145
|
+
getCurrentSignal(): AbortSignal | undefined {
|
|
146
|
+
const context = this.getCurrentContext();
|
|
147
|
+
if (context?.streamId) {
|
|
148
|
+
const signal = this.streamSignals.get(context.streamId);
|
|
149
|
+
if (signal) return signal;
|
|
150
|
+
}
|
|
151
|
+
if (context?.chatSessionId) {
|
|
152
|
+
for (const [streamId, info] of this.activeStreams) {
|
|
153
|
+
if (info.chatSessionId === context.chatSessionId) {
|
|
154
|
+
const signal = this.streamSignals.get(streamId);
|
|
155
|
+
if (signal) return signal;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (this.mostRecentActiveStream) {
|
|
160
|
+
return this.streamSignals.get(this.mostRecentActiveStream.streamId);
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
112
165
|
/**
|
|
113
166
|
* Unregister a stream (cleanup)
|
|
114
167
|
*/
|
|
115
168
|
unregisterStream(streamId: string): void {
|
|
116
169
|
this.streamProjectMap.delete(streamId);
|
|
117
170
|
this.activeStreams.delete(streamId);
|
|
171
|
+
this.streamSignals.delete(streamId);
|
|
118
172
|
|
|
119
173
|
// Clear most recent if it was this stream
|
|
120
174
|
if (this.mostRecentActiveStream?.streamId === streamId) {
|
|
@@ -158,6 +212,7 @@ class ProjectContextService {
|
|
|
158
212
|
clear(): void {
|
|
159
213
|
this.sessionProjectMap.clear();
|
|
160
214
|
this.streamProjectMap.clear();
|
|
215
|
+
this.streamSignals.clear();
|
|
161
216
|
this.lastUsedProjectId = null;
|
|
162
217
|
debug.log('mcp', '🧹 Cleared all project context mappings');
|
|
163
218
|
}
|
|
@@ -114,7 +114,12 @@ export async function actionsHandler(args: {
|
|
|
114
114
|
|
|
115
115
|
// Note: Cursor events are emitted by performAutonomousActions internally
|
|
116
116
|
// with proper delays between each action. No need to emit here.
|
|
117
|
-
|
|
117
|
+
// Pass the chat stream's AbortSignal so the action loop breaks the
|
|
118
|
+
// moment the user clicks interrupt — without this, multi-step click
|
|
119
|
+
// sequences keep firing for a few seconds after the engine subprocess
|
|
120
|
+
// dies (the "preview keeps moving by itself" symptom).
|
|
121
|
+
const abortSignal = projectContextService.getCurrentSignal();
|
|
122
|
+
const results = await previewService.performAutonomousActions(sessionId, processedActions, abortSignal);
|
|
118
123
|
|
|
119
124
|
// Format response with extracted data if any
|
|
120
125
|
const extractedData = results?.filter((r: any) => r.action === 'extract_data') || [];
|