@myrialabs/clopen 0.3.22 → 0.4.0
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 +11 -1
- package/backend/chat/stream-manager.ts +14 -3
- package/backend/database/migrations/040_create_mcp_servers_table.ts +34 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/index.ts +3 -1
- package/backend/database/queries/mcp-server-queries.ts +166 -0
- package/backend/engine/README.md +2 -2
- package/backend/engine/adapters/codex/message-converter.ts +1 -1
- package/backend/engine/adapters/codex/stream.ts +1 -1
- package/backend/engine/adapters/copilot/message-converter.ts +1 -1
- package/backend/engine/adapters/copilot/stream.ts +1 -1
- package/backend/engine/adapters/qwen/message-converter.ts +1 -1
- package/backend/engine/adapters/qwen/stream.ts +1 -1
- package/backend/engine/docs/adapter-contract.md +2 -2
- package/backend/engine/docs/lessons-learned.md +3 -3
- package/backend/engine/docs/quick-reference.md +1 -1
- package/backend/index.ts +5 -1
- package/backend/mcp/README.md +67 -32
- package/backend/mcp/external/config.ts +200 -0
- package/backend/mcp/external/registry-client.ts +214 -0
- package/backend/mcp/external/types.ts +69 -0
- package/backend/mcp/index.ts +104 -19
- package/backend/mcp/{config.ts → internal/config.ts} +82 -18
- package/backend/mcp/{remote-server.test.ts → internal/remote-server.test.ts} +3 -3
- package/backend/mcp/{servers → internal/servers}/browser-automation/actions.ts +1 -1
- package/backend/mcp/{servers → internal/servers}/browser-automation/browser.ts +1 -1
- package/backend/mcp/{servers → internal/servers}/browser-automation/index.ts +2 -0
- package/backend/mcp/{servers → internal/servers}/browser-automation/inspection.ts +1 -1
- package/backend/mcp/{servers → internal/servers}/helper.ts +11 -0
- package/backend/mcp/{servers → internal/servers}/index.ts +0 -2
- package/backend/mcp/{types.ts → internal/types.ts} +3 -3
- package/backend/mcp/shared/constants.ts +66 -0
- package/backend/projects/project-cleanup.ts +1 -1
- package/backend/ws/engine/index.ts +3 -1
- package/backend/ws/engine/restart.ts +54 -0
- package/backend/ws/index.ts +5 -1
- package/backend/ws/mcp/catalog.ts +56 -0
- package/backend/ws/mcp/crud.ts +174 -0
- package/backend/ws/mcp/index.ts +18 -0
- package/dist/assets/main-B7f2Gzb3.css +1 -0
- package/dist/assets/{main-Ba19ADkk.js → main-DYt1fDrp.js} +279 -273
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/stores/settings.ts +2 -0
- package/backend/mcp/servers/weather/get-temperature.ts +0 -56
- package/backend/mcp/servers/weather/index.ts +0 -31
- package/dist/assets/main-CwCZzMVm.css +0 -1
- /package/backend/mcp/{output-validator.test.ts → internal/output-validator.test.ts} +0 -0
- /package/backend/mcp/{output-validator.ts → internal/output-validator.ts} +0 -0
- /package/backend/mcp/{project-context.test.ts → internal/project-context.test.ts} +0 -0
- /package/backend/mcp/{project-context.ts → internal/project-context.ts} +0 -0
- /package/backend/mcp/{remote-server.ts → internal/remote-server.ts} +0 -0
|
@@ -10,11 +10,12 @@ import type { McpRemoteConfig } from '@opencode-ai/sdk';
|
|
|
10
10
|
import type { MCPHTTPServerConfig } from '@github/copilot-sdk';
|
|
11
11
|
import type { CLIMcpServerConfig as QwenMcpServerConfig } from '@qwen-code/sdk';
|
|
12
12
|
import type { ServerConfig, ParsedMcpToolName, ServerName } from './types';
|
|
13
|
-
import type { McpExecutionContext } from '
|
|
13
|
+
import type { McpExecutionContext } from '../../engine/types';
|
|
14
14
|
import { serverRegistry, serverFactories, serverMetadata } from './servers';
|
|
15
15
|
import { projectContextService } from './project-context';
|
|
16
|
+
import { mcpServerQueries } from '$backend/database/queries';
|
|
16
17
|
import { debug } from '$shared/utils/logger';
|
|
17
|
-
import { SERVER_ENV } from '
|
|
18
|
+
import { SERVER_ENV } from '../../utils/env';
|
|
18
19
|
import { validateMcpOutput } from './output-validator';
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -26,12 +27,6 @@ import { validateMcpOutput } from './output-validator';
|
|
|
26
27
|
* Type-safe: Server names and tool names are validated at compile time!
|
|
27
28
|
*/
|
|
28
29
|
export const mcpServersConfig: Record<ServerName, ServerConfig> = {
|
|
29
|
-
"weather-service": {
|
|
30
|
-
enabled: true,
|
|
31
|
-
tools: [
|
|
32
|
-
"get_temperature",
|
|
33
|
-
]
|
|
34
|
-
},
|
|
35
30
|
"browser-automation": {
|
|
36
31
|
enabled: true,
|
|
37
32
|
tools: [
|
|
@@ -83,6 +78,39 @@ function createServerConfig<T extends Record<ServerName, ServerConfig>>(
|
|
|
83
78
|
*/
|
|
84
79
|
export const mcpServers: Record<string, ServerConfig & { instance: McpSdkServerConfigWithInstance }> = createServerConfig(mcpServersConfig);
|
|
85
80
|
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// Runtime enabled state (DB-backed)
|
|
83
|
+
// ============================================================================
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* In-memory mirror of each internal server's enabled toggle, keyed by server
|
|
87
|
+
* name (= the seeded `slug`). The DB row in `mcp_servers` is the source of
|
|
88
|
+
* truth; this cache avoids a query on every tool-name resolution. Refreshed at
|
|
89
|
+
* startup (after `syncInternalServers`) and whenever the user toggles a server.
|
|
90
|
+
*/
|
|
91
|
+
let internalEnabledCache: Record<string, boolean> | null = null;
|
|
92
|
+
|
|
93
|
+
/** Reload the internal enabled-state cache from the DB. */
|
|
94
|
+
export function refreshInternalEnabledCache(): void {
|
|
95
|
+
const map: Record<string, boolean> = {};
|
|
96
|
+
for (const row of mcpServerQueries.getBySource('internal')) {
|
|
97
|
+
map[row.slug] = row.is_enabled === 1;
|
|
98
|
+
}
|
|
99
|
+
internalEnabledCache = map;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether an internal server is enabled right now. Combines the static config
|
|
104
|
+
* (which tools exist) with the user's DB-backed toggle. Defaults to enabled if
|
|
105
|
+
* the row hasn't been seeded yet (sync runs at startup, before any stream).
|
|
106
|
+
*/
|
|
107
|
+
function serverEnabled(serverName: string): boolean {
|
|
108
|
+
const cfg = mcpServers[serverName];
|
|
109
|
+
if (!cfg?.enabled) return false;
|
|
110
|
+
if (internalEnabledCache === null) refreshInternalEnabledCache();
|
|
111
|
+
return internalEnabledCache?.[serverName] ?? true;
|
|
112
|
+
}
|
|
113
|
+
|
|
86
114
|
// ============================================================================
|
|
87
115
|
// Server Registry Functions
|
|
88
116
|
// ============================================================================
|
|
@@ -103,7 +131,7 @@ export function getEnabledMcpServers(context?: McpExecutionContext): Record<stri
|
|
|
103
131
|
const enabledServers: Record<string, McpServerConfig> = {};
|
|
104
132
|
|
|
105
133
|
Object.entries(mcpServers).forEach(([serverName, serverConfig]) => {
|
|
106
|
-
if (
|
|
134
|
+
if (serverEnabled(serverName)) {
|
|
107
135
|
if (context) {
|
|
108
136
|
// Create context-bound instance: wrap each tool handler so
|
|
109
137
|
// AsyncLocalStorage context is restored on invocation, then
|
|
@@ -168,13 +196,13 @@ export { abortedToolResult };
|
|
|
168
196
|
* Get list of all allowed MCP tool names
|
|
169
197
|
*
|
|
170
198
|
* Tool names follow the format: mcp__{server-name}__{tool-name}
|
|
171
|
-
* Example: "
|
|
199
|
+
* Example: "mcp__browser-automation__navigate"
|
|
172
200
|
*/
|
|
173
201
|
export function getAllowedMcpTools(): string[] {
|
|
174
202
|
const tools: string[] = [];
|
|
175
203
|
|
|
176
204
|
Object.entries(mcpServers).forEach(([serverName, serverConfig]) => {
|
|
177
|
-
if (!
|
|
205
|
+
if (!serverEnabled(serverName)) return;
|
|
178
206
|
|
|
179
207
|
serverConfig.tools.forEach((toolName) => {
|
|
180
208
|
const formattedName = `mcp__${serverName}__${toolName}`;
|
|
@@ -211,7 +239,7 @@ export function getToolConfig(serverName: string, toolName: string): boolean {
|
|
|
211
239
|
* Check if a server is enabled
|
|
212
240
|
*/
|
|
213
241
|
export function isServerEnabled(serverName: string): boolean {
|
|
214
|
-
return
|
|
242
|
+
return serverEnabled(serverName);
|
|
215
243
|
}
|
|
216
244
|
|
|
217
245
|
/**
|
|
@@ -219,7 +247,7 @@ export function isServerEnabled(serverName: string): boolean {
|
|
|
219
247
|
*/
|
|
220
248
|
export function isToolEnabled(serverName: string, toolName: string): boolean {
|
|
221
249
|
const server = mcpServers[serverName];
|
|
222
|
-
if (!server
|
|
250
|
+
if (!server || !serverEnabled(serverName)) return false;
|
|
223
251
|
|
|
224
252
|
return server.tools.includes(toolName as any);
|
|
225
253
|
}
|
|
@@ -232,7 +260,7 @@ export function isToolEnabled(serverName: string, toolName: string): boolean {
|
|
|
232
260
|
* Parse MCP tool name into components
|
|
233
261
|
*
|
|
234
262
|
* Format: mcp__{server-name}__{tool-name}
|
|
235
|
-
* Example: "
|
|
263
|
+
* Example: "mcp__browser-automation__navigate"
|
|
236
264
|
*/
|
|
237
265
|
export function parseMcpToolName(fullName: string): ParsedMcpToolName | null {
|
|
238
266
|
if (!fullName.startsWith('mcp__')) {
|
|
@@ -268,8 +296,8 @@ export function isMcpTool(toolName: string): boolean {
|
|
|
268
296
|
*/
|
|
269
297
|
export function getEnabledServerNames(): string[] {
|
|
270
298
|
return Object.entries(mcpServers)
|
|
271
|
-
.filter(([
|
|
272
|
-
.map(([name
|
|
299
|
+
.filter(([name]) => serverEnabled(name))
|
|
300
|
+
.map(([name]) => name);
|
|
273
301
|
}
|
|
274
302
|
|
|
275
303
|
/**
|
|
@@ -277,7 +305,7 @@ export function getEnabledServerNames(): string[] {
|
|
|
277
305
|
*/
|
|
278
306
|
export function getEnabledToolsForServer(serverName: string): string[] {
|
|
279
307
|
const serverConfig = mcpServers[serverName];
|
|
280
|
-
if (!serverConfig
|
|
308
|
+
if (!serverConfig || !serverEnabled(serverName)) {
|
|
281
309
|
return [];
|
|
282
310
|
}
|
|
283
311
|
|
|
@@ -300,6 +328,42 @@ export function getMcpStats() {
|
|
|
300
328
|
};
|
|
301
329
|
}
|
|
302
330
|
|
|
331
|
+
// ============================================================================
|
|
332
|
+
// Internal server ↔ DB sync
|
|
333
|
+
// ============================================================================
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Mirror the code-defined internal servers into the `mcp_servers` table so
|
|
337
|
+
* Settings → MCP can list them alongside external servers and the user can
|
|
338
|
+
* toggle them on/off. Definitions stay the source of truth in code; the DB row
|
|
339
|
+
* only carries display metadata + the enabled flag.
|
|
340
|
+
*
|
|
341
|
+
* Idempotent: refreshes title/description/version for known servers (preserving
|
|
342
|
+
* the user's enabled toggle), seeds new ones as enabled, and prunes rows whose
|
|
343
|
+
* code definition was removed. Call once at startup, after the DB is ready.
|
|
344
|
+
*/
|
|
345
|
+
export function syncInternalServers(): void {
|
|
346
|
+
const metas = Object.values(serverMetadata) as Array<{
|
|
347
|
+
name: string;
|
|
348
|
+
title: string;
|
|
349
|
+
description: string;
|
|
350
|
+
version: string;
|
|
351
|
+
}>;
|
|
352
|
+
const slugs: string[] = [];
|
|
353
|
+
for (const meta of metas) {
|
|
354
|
+
mcpServerQueries.upsertInternal({
|
|
355
|
+
slug: meta.name,
|
|
356
|
+
name: meta.title,
|
|
357
|
+
description: meta.description,
|
|
358
|
+
version: meta.version
|
|
359
|
+
});
|
|
360
|
+
slugs.push(meta.name);
|
|
361
|
+
}
|
|
362
|
+
mcpServerQueries.pruneInternalExcept(slugs);
|
|
363
|
+
refreshInternalEnabledCache();
|
|
364
|
+
debug.log('mcp', `🔄 Synced ${slugs.length} internal MCP server(s) to DB`);
|
|
365
|
+
}
|
|
366
|
+
|
|
303
367
|
// ============================================================================
|
|
304
368
|
// Open Code Tool Name Resolution (single source of truth)
|
|
305
369
|
// ============================================================================
|
|
@@ -342,7 +406,7 @@ export function resolveOpenCodeToolName(toolName: string): string | null {
|
|
|
342
406
|
|
|
343
407
|
// Look up which server owns this tool
|
|
344
408
|
for (const [serverName, serverConfig] of Object.entries(mcpServers)) {
|
|
345
|
-
if (!
|
|
409
|
+
if (!serverEnabled(serverName)) continue;
|
|
346
410
|
if ((serverConfig.tools as readonly string[]).includes(rawName)) {
|
|
347
411
|
return `mcp__${serverName}__${rawName}`;
|
|
348
412
|
}
|
|
@@ -12,9 +12,9 @@ import { describe, expect, test, beforeAll, afterAll, beforeEach } from 'bun:tes
|
|
|
12
12
|
import { randomUUID } from 'node:crypto';
|
|
13
13
|
|
|
14
14
|
import { handleMcpRequest } from './remote-server';
|
|
15
|
-
import { authQueries, settingsQueries } from '
|
|
16
|
-
import { generatePAT, generateSessionToken, hashToken } from '
|
|
17
|
-
import { initializeDatabase, closeDatabase, getDatabase } from '
|
|
15
|
+
import { authQueries, settingsQueries } from '../../database/queries';
|
|
16
|
+
import { generatePAT, generateSessionToken, hashToken } from '../../auth/tokens';
|
|
17
|
+
import { initializeDatabase, closeDatabase, getDatabase } from '../../database';
|
|
18
18
|
|
|
19
19
|
let testUserId: string;
|
|
20
20
|
let sessionToken: string;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { browserPreviewServiceManager, type BrowserPreviewService } from "$backend/preview";
|
|
6
6
|
import type { BrowserAutonomousAction } from "$backend/preview/browser/types";
|
|
7
|
-
import { projectContextService } from "$backend/mcp/project-context";
|
|
7
|
+
import { projectContextService } from "$backend/mcp/internal/project-context";
|
|
8
8
|
import { getActiveTabSession } from "./browser";
|
|
9
9
|
import { debug } from "$shared/utils/logger";
|
|
10
10
|
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
import { debug } from "$shared/utils/logger";
|
|
17
17
|
import { browserMcpControl, browserPreviewServiceManager, type BrowserPreviewService } from "$backend/preview";
|
|
18
|
-
import { projectContextService } from "$backend/mcp/project-context";
|
|
18
|
+
import { projectContextService } from "$backend/mcp/internal/project-context";
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Get BrowserPreviewService for current MCP execution context
|
|
@@ -14,6 +14,8 @@ import { getConsoleLogsHandler, clearConsoleLogsHandler, executeConsoleHandler,
|
|
|
14
14
|
|
|
15
15
|
export default defineServer({
|
|
16
16
|
name: "browser-automation",
|
|
17
|
+
title: "Browser Automation",
|
|
18
|
+
description: "Drives the built-in Preview Browser panel — navigate, click, type, and inspect pages, running multiple actions in one call for efficient interaction.",
|
|
17
19
|
version: "1.0.0",
|
|
18
20
|
tools: {
|
|
19
21
|
// ============================================================================
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { browserPreviewServiceManager, type BrowserPreviewService } from "$backend/preview";
|
|
6
|
-
import { projectContextService } from "$backend/mcp/project-context";
|
|
6
|
+
import { projectContextService } from "$backend/mcp/internal/project-context";
|
|
7
7
|
import { getActiveTabSession } from "./browser";
|
|
8
8
|
import { debug } from "$shared/utils/logger";
|
|
9
9
|
|
|
@@ -61,6 +61,12 @@ interface ServerWithMeta<
|
|
|
61
61
|
createInstance: () => ReturnType<typeof createSdkMcpServer>;
|
|
62
62
|
meta: {
|
|
63
63
|
readonly name: TName;
|
|
64
|
+
/** Human-facing title shown in Settings → MCP and the Chat tool header. */
|
|
65
|
+
readonly title: string;
|
|
66
|
+
/** Short description of what this server provides. */
|
|
67
|
+
readonly description: string;
|
|
68
|
+
/** Semantic version, e.g. `1.0.0`. */
|
|
69
|
+
readonly version: string;
|
|
64
70
|
readonly tools: TToolNames;
|
|
65
71
|
/** Raw tool definitions (schema + description) for reuse by other transports */
|
|
66
72
|
readonly toolDefs: Record<string, RawToolDef>;
|
|
@@ -73,6 +79,8 @@ interface ServerWithMeta<
|
|
|
73
79
|
export function defineServer<
|
|
74
80
|
const TConfig extends {
|
|
75
81
|
name: string;
|
|
82
|
+
title: string;
|
|
83
|
+
description: string;
|
|
76
84
|
version: string;
|
|
77
85
|
tools: Record<string, { description: string; schema?: any; handler: any }>;
|
|
78
86
|
}
|
|
@@ -123,6 +131,9 @@ export function defineServer<
|
|
|
123
131
|
createInstance,
|
|
124
132
|
meta: {
|
|
125
133
|
name: config.name,
|
|
134
|
+
title: config.title,
|
|
135
|
+
description: config.description,
|
|
136
|
+
version: config.version,
|
|
126
137
|
tools: toolNames as any,
|
|
127
138
|
toolDefs,
|
|
128
139
|
}
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
* 3. Done! Registries are auto-built and type-safe.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import weather from './weather/index';
|
|
14
13
|
import browserAutomation from './browser-automation/index';
|
|
15
14
|
import { buildServerRegistries } from './helper';
|
|
16
15
|
|
|
@@ -25,7 +24,6 @@ export { createRemoteMcpServer } from './helper';
|
|
|
25
24
|
* Metadata and registry will be automatically built.
|
|
26
25
|
*/
|
|
27
26
|
export const allServers = [
|
|
28
|
-
weather,
|
|
29
27
|
browserAutomation,
|
|
30
28
|
// Add more servers here...
|
|
31
29
|
] as const;
|
|
@@ -41,11 +41,11 @@ export type McpServerConfigWithInstance<S extends ServerName = ServerName> = Ser
|
|
|
41
41
|
* Format: mcp__server-name__tool-name
|
|
42
42
|
*/
|
|
43
43
|
export interface ParsedMcpToolName {
|
|
44
|
-
/** Server name (e.g., "
|
|
44
|
+
/** Server name (e.g., "browser-automation") */
|
|
45
45
|
server: string;
|
|
46
|
-
/** Tool name (e.g., "
|
|
46
|
+
/** Tool name (e.g., "navigate") */
|
|
47
47
|
tool: string;
|
|
48
|
-
/** Full tool name (e.g., "
|
|
48
|
+
/** Full tool name (e.g., "mcp__browser-automation__navigate") */
|
|
49
49
|
fullName: string;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared MCP namespace constants
|
|
3
|
+
*
|
|
4
|
+
* Clopen exposes MCP tools from two distinct sources. Their namespace keys are
|
|
5
|
+
* kept disjoint on purpose so the two systems never collide:
|
|
6
|
+
*
|
|
7
|
+
* - INTERNAL (custom tools defined in `backend/mcp/internal/servers/` via
|
|
8
|
+
* `defineServer()`). Every non-Claude engine consumes them through the
|
|
9
|
+
* single remote-HTTP bridge whose namespace key is
|
|
10
|
+
* `INTERNAL_BRIDGE_NAMESPACE` (`clopen-mcp`). The `clopen` prefix is what
|
|
11
|
+
* marks a namespace as internal.
|
|
12
|
+
*
|
|
13
|
+
* - EXTERNAL (servers the user installs from the official MCP registry, see
|
|
14
|
+
* `backend/mcp/external/`). Each external server uses its BARE slug as the
|
|
15
|
+
* namespace key — no prefix. Tools surface as `mcp__<slug>__<tool>`.
|
|
16
|
+
*
|
|
17
|
+
* Invariants:
|
|
18
|
+
* - The `clopen` prefix is reserved for internal namespaces; external slugs
|
|
19
|
+
* must never start with it (enforced by `slugifyRegistryName`).
|
|
20
|
+
* - External slugs are unique and only contain `[a-z0-9-]`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** Namespace key of the internal remote-HTTP MCP bridge (`/mcp`). */
|
|
24
|
+
export const INTERNAL_BRIDGE_NAMESPACE = 'clopen-mcp';
|
|
25
|
+
|
|
26
|
+
/** Prefix reserved for internal MCP namespaces (marks a key as "ours"). */
|
|
27
|
+
export const INTERNAL_PREFIX = 'clopen';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Namespace key for an external server. External servers use their bare slug
|
|
31
|
+
* directly — no prefix. Kept as a function so call sites read intent and a
|
|
32
|
+
* future scheme change has a single seam.
|
|
33
|
+
*/
|
|
34
|
+
export function externalNamespace(slug: string): string {
|
|
35
|
+
return slug;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** True when a namespace key belongs to an internal server/bridge. */
|
|
39
|
+
export function isInternalNamespace(namespace: string): boolean {
|
|
40
|
+
return namespace === INTERNAL_BRIDGE_NAMESPACE || namespace.startsWith(`${INTERNAL_PREFIX}-`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sanitize an arbitrary registry server name into a safe, unique slug usable
|
|
45
|
+
* as an MCP namespace segment. Registry names are reverse-DNS
|
|
46
|
+
* (`io.github.foo/bar`) and contain `.` / `/` / uppercase which are illegal
|
|
47
|
+
* in the `mcp__<server>__<tool>` form. We lowercase, replace every
|
|
48
|
+
* non-`[a-z0-9]` run with a single `-`, and trim leading/trailing dashes.
|
|
49
|
+
*
|
|
50
|
+
* `io.github.foo/bar-baz` → `io-github-foo-bar-baz`
|
|
51
|
+
*
|
|
52
|
+
* The `clopen` prefix is reserved for internal namespaces — if a slug would
|
|
53
|
+
* start with it, we prefix `x-` so external keys can never shadow the internal
|
|
54
|
+
* bridge or an internal server.
|
|
55
|
+
*/
|
|
56
|
+
export function slugifyRegistryName(name: string): string {
|
|
57
|
+
let slug = name
|
|
58
|
+
.toLowerCase()
|
|
59
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
60
|
+
.replace(/^-+|-+$/g, '');
|
|
61
|
+
if (!slug) slug = 'server';
|
|
62
|
+
if (slug === INTERNAL_PREFIX || slug.startsWith(`${INTERNAL_PREFIX}-`) || slug.startsWith(INTERNAL_PREFIX)) {
|
|
63
|
+
slug = `x-${slug}`;
|
|
64
|
+
}
|
|
65
|
+
return slug;
|
|
66
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Default backend cleanup handlers for fully removed projects.
|
|
3
3
|
*/
|
|
4
4
|
import { disposeProjectEngines } from '../engine';
|
|
5
|
-
import { projectContextService } from '../mcp
|
|
5
|
+
import { projectContextService } from '../mcp';
|
|
6
6
|
import { fileWatcher } from '../files/file-watcher';
|
|
7
7
|
import { clearProjectPresence } from '../project/status-manager';
|
|
8
8
|
import { registerProjectCleanup } from './project-cleanup-registry';
|
|
@@ -11,10 +11,12 @@ import { openCodeEngineRouter } from './opencode';
|
|
|
11
11
|
import { copilotEngineRouter } from './copilot';
|
|
12
12
|
import { codexEngineRouter } from './codex';
|
|
13
13
|
import { qwenEngineRouter } from './qwen';
|
|
14
|
+
import { engineRestartRouter } from './restart';
|
|
14
15
|
|
|
15
16
|
export const engineRouter = createRouter()
|
|
16
17
|
.merge(claudeCodeEngineRouter)
|
|
17
18
|
.merge(openCodeEngineRouter)
|
|
18
19
|
.merge(copilotEngineRouter)
|
|
19
20
|
.merge(codexEngineRouter)
|
|
20
|
-
.merge(qwenEngineRouter)
|
|
21
|
+
.merge(qwenEngineRouter)
|
|
22
|
+
.merge(engineRestartRouter);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Restart-all-engines handler.
|
|
3
|
+
*
|
|
4
|
+
* Disposes every cached engine instance (global + per-project) and the shared
|
|
5
|
+
* OpenCode server/client so the next chat turn re-initialises from scratch.
|
|
6
|
+
* Needed after changing MCP servers (install/uninstall/enable/disable/config)
|
|
7
|
+
* or other global engine config — the engines read that config when they
|
|
8
|
+
* (re)initialise, not mid-session.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { t } from 'elysia';
|
|
12
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
13
|
+
import { debug } from '$shared/utils/logger';
|
|
14
|
+
import { disposeAllEngines } from '../../engine';
|
|
15
|
+
import { disposeOpenCodeClient } from '../../engine/adapters/opencode/server';
|
|
16
|
+
import { streamManager } from '../../chat';
|
|
17
|
+
|
|
18
|
+
export const engineRestartRouter = createRouter()
|
|
19
|
+
.http('engine:restart-all', {
|
|
20
|
+
data: t.Object({
|
|
21
|
+
force: t.Optional(t.Boolean()),
|
|
22
|
+
}),
|
|
23
|
+
response: t.Object({
|
|
24
|
+
success: t.Boolean(),
|
|
25
|
+
activeChats: t.Optional(t.Number()),
|
|
26
|
+
needsConfirmation: t.Optional(t.Boolean()),
|
|
27
|
+
})
|
|
28
|
+
}, async ({ data }) => {
|
|
29
|
+
const activeStreams = streamManager.getActiveStreams();
|
|
30
|
+
|
|
31
|
+
// Restarting kills in-flight chats — ask first unless forced.
|
|
32
|
+
if (activeStreams.length > 0 && !data.force) {
|
|
33
|
+
return { success: false, activeChats: activeStreams.length, needsConfirmation: true };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (activeStreams.length > 0) {
|
|
37
|
+
debug.log('engine', `Force-aborting ${activeStreams.length} active stream(s) before restart-all`);
|
|
38
|
+
for (const stream of activeStreams) {
|
|
39
|
+
try {
|
|
40
|
+
await streamManager.cancelStream(stream.streamId);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
debug.warn('engine', `Failed to abort stream ${stream.streamId}:`, error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await disposeAllEngines();
|
|
48
|
+
// OpenCode keeps a separate persistent server/client beyond the engine
|
|
49
|
+
// instance — purge it too so it respawns with fresh MCP config.
|
|
50
|
+
await disposeOpenCodeClient(true);
|
|
51
|
+
|
|
52
|
+
debug.log('engine', 'All engines disposed (restart-all). Will re-initialize on next use.');
|
|
53
|
+
return { success: true };
|
|
54
|
+
});
|
package/backend/ws/index.ts
CHANGED
|
@@ -31,6 +31,7 @@ import { gitRouter } from './git';
|
|
|
31
31
|
import { engineRouter } from './engine';
|
|
32
32
|
import { systemToolsRouter } from './system-tools';
|
|
33
33
|
import { dbClientRouter } from './db-client';
|
|
34
|
+
import { mcpRouter } from './mcp';
|
|
34
35
|
|
|
35
36
|
// ============================================
|
|
36
37
|
// Main App Router - Merge All Module Routers
|
|
@@ -73,7 +74,10 @@ export const wsRouter = createRouter()
|
|
|
73
74
|
.merge(systemToolsRouter)
|
|
74
75
|
|
|
75
76
|
// DB Client (global database management)
|
|
76
|
-
.merge(dbClientRouter)
|
|
77
|
+
.merge(dbClientRouter)
|
|
78
|
+
|
|
79
|
+
// External MCP server management (install from the official registry)
|
|
80
|
+
.merge(mcpRouter);
|
|
77
81
|
|
|
78
82
|
// Export API type for frontend type-safe access
|
|
79
83
|
export type WSAPI = typeof wsRouter['$api'];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Catalog Handler
|
|
3
|
+
*
|
|
4
|
+
* Proxies the official MCP registry (registry.modelcontextprotocol.io) so the
|
|
5
|
+
* Settings → MCP "Browse" tab can search and paginate installable servers
|
|
6
|
+
* without the frontend talking to the registry directly.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { t } from 'elysia';
|
|
10
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
11
|
+
import { debug } from '$shared/utils/logger';
|
|
12
|
+
import { listRegistryServers } from '$backend/mcp';
|
|
13
|
+
|
|
14
|
+
const ENV_VAR_SCHEMA = t.Object({
|
|
15
|
+
name: t.String(),
|
|
16
|
+
description: t.Optional(t.String()),
|
|
17
|
+
isRequired: t.Boolean(),
|
|
18
|
+
isSecret: t.Boolean(),
|
|
19
|
+
default: t.Optional(t.String())
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const CATALOG_SERVER_SCHEMA = t.Object({
|
|
23
|
+
registryName: t.String(),
|
|
24
|
+
slug: t.String(),
|
|
25
|
+
title: t.String(),
|
|
26
|
+
description: t.String(),
|
|
27
|
+
version: t.String(),
|
|
28
|
+
transport: t.Union([t.Literal('stdio'), t.Literal('http'), t.Literal('sse')]),
|
|
29
|
+
command: t.Optional(t.String()),
|
|
30
|
+
args: t.Optional(t.Array(t.String())),
|
|
31
|
+
url: t.Optional(t.String()),
|
|
32
|
+
envVars: t.Array(ENV_VAR_SCHEMA),
|
|
33
|
+
headerVars: t.Array(ENV_VAR_SCHEMA),
|
|
34
|
+
packageHint: t.Optional(t.String())
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const mcpCatalogHandler = createRouter()
|
|
38
|
+
.http('mcp:catalog', {
|
|
39
|
+
data: t.Object({
|
|
40
|
+
search: t.Optional(t.String()),
|
|
41
|
+
cursor: t.Optional(t.String()),
|
|
42
|
+
limit: t.Optional(t.Number())
|
|
43
|
+
}),
|
|
44
|
+
response: t.Object({
|
|
45
|
+
servers: t.Array(CATALOG_SERVER_SCHEMA),
|
|
46
|
+
nextCursor: t.Union([t.String(), t.Null()])
|
|
47
|
+
})
|
|
48
|
+
}, async ({ data }) => {
|
|
49
|
+
debug.log('path', `mcp:catalog search="${data.search ?? ''}"`);
|
|
50
|
+
const page = await listRegistryServers({
|
|
51
|
+
search: data.search,
|
|
52
|
+
cursor: data.cursor,
|
|
53
|
+
limit: data.limit
|
|
54
|
+
});
|
|
55
|
+
return { servers: page.servers, nextCursor: page.nextCursor };
|
|
56
|
+
});
|