@myrialabs/clopen 0.3.22 → 0.4.1
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 +12 -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/git/git-parser.ts +58 -29
- package/backend/git/git-service.ts +79 -10
- package/backend/git/git-spawn-validation.ts +3 -3
- 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/git/branch.ts +3 -2
- package/backend/ws/git/commit.ts +14 -0
- package/backend/ws/git/remote.ts +85 -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/backend/ws/settings/crud.ts +31 -0
- package/dist/assets/main-DKLZPDUN.css +1 -0
- package/dist/assets/main-qT_RedHf.js +377 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/git.ts +4 -0
- 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-Ba19ADkk.js +0 -377
- 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
|
@@ -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/git/branch.ts
CHANGED
|
@@ -20,7 +20,8 @@ const BranchSchema = t.Object({
|
|
|
20
20
|
export const branchHandler = createRouter()
|
|
21
21
|
.http('git:branches', {
|
|
22
22
|
data: t.Object({
|
|
23
|
-
projectId: t.String()
|
|
23
|
+
projectId: t.String(),
|
|
24
|
+
selectedRemote: t.Optional(t.String())
|
|
24
25
|
}),
|
|
25
26
|
response: t.Object({
|
|
26
27
|
current: t.String(),
|
|
@@ -31,7 +32,7 @@ export const branchHandler = createRouter()
|
|
|
31
32
|
})
|
|
32
33
|
}, async ({ data, conn }) => {
|
|
33
34
|
const project = requireProjectAccess(conn, data.projectId);
|
|
34
|
-
return await gitService.getBranches(project.path);
|
|
35
|
+
return await gitService.getBranches(project.path, data.selectedRemote);
|
|
35
36
|
})
|
|
36
37
|
|
|
37
38
|
.http('git:create-branch', {
|
package/backend/ws/git/commit.ts
CHANGED
|
@@ -62,6 +62,20 @@ export const commitHandler = createRouter()
|
|
|
62
62
|
return await gitService.revertCommit(project.path, data.ref);
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
+
.http('git:cherry-pick', {
|
|
66
|
+
data: t.Object({
|
|
67
|
+
projectId: t.String(),
|
|
68
|
+
hashes: t.Array(t.String(), { minItems: 1 })
|
|
69
|
+
}),
|
|
70
|
+
response: t.Object({
|
|
71
|
+
success: t.Boolean(),
|
|
72
|
+
message: t.String()
|
|
73
|
+
})
|
|
74
|
+
}, async ({ data, conn }) => {
|
|
75
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
76
|
+
return await gitService.cherryPick(project.path, data.hashes);
|
|
77
|
+
})
|
|
78
|
+
|
|
65
79
|
.http('git:clean', {
|
|
66
80
|
data: t.Object({
|
|
67
81
|
projectId: t.String()
|
package/backend/ws/git/remote.ts
CHANGED
|
@@ -115,6 +115,49 @@ export const remoteHandler = createRouter()
|
|
|
115
115
|
return { ok: true };
|
|
116
116
|
})
|
|
117
117
|
|
|
118
|
+
.http('git:set-remote-url', {
|
|
119
|
+
data: t.Object({
|
|
120
|
+
projectId: t.String(),
|
|
121
|
+
name: t.String(),
|
|
122
|
+
url: t.String()
|
|
123
|
+
}),
|
|
124
|
+
response: t.Object({ ok: t.Boolean() })
|
|
125
|
+
}, async ({ data, conn }) => {
|
|
126
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
127
|
+
await gitService.setRemoteUrl(project.path, data.name, data.url);
|
|
128
|
+
return { ok: true };
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
.http('git:rename-remote', {
|
|
132
|
+
data: t.Object({
|
|
133
|
+
projectId: t.String(),
|
|
134
|
+
oldName: t.String(),
|
|
135
|
+
newName: t.String()
|
|
136
|
+
}),
|
|
137
|
+
response: t.Object({ ok: t.Boolean() })
|
|
138
|
+
}, async ({ data, conn }) => {
|
|
139
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
140
|
+
await gitService.renameRemote(project.path, data.oldName, data.newName);
|
|
141
|
+
return { ok: true };
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
.http('git:edit-remote', {
|
|
145
|
+
data: t.Object({
|
|
146
|
+
projectId: t.String(),
|
|
147
|
+
oldName: t.String(),
|
|
148
|
+
newName: t.String(),
|
|
149
|
+
newUrl: t.String()
|
|
150
|
+
}),
|
|
151
|
+
response: t.Object({ ok: t.Boolean() })
|
|
152
|
+
}, async ({ data, conn }) => {
|
|
153
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
154
|
+
if (data.oldName !== data.newName) {
|
|
155
|
+
await gitService.renameRemote(project.path, data.oldName, data.newName);
|
|
156
|
+
}
|
|
157
|
+
await gitService.setRemoteUrl(project.path, data.newName, data.newUrl);
|
|
158
|
+
return { ok: true };
|
|
159
|
+
})
|
|
160
|
+
|
|
118
161
|
.http('git:remove-remote', {
|
|
119
162
|
data: t.Object({
|
|
120
163
|
projectId: t.String(),
|
|
@@ -127,6 +170,19 @@ export const remoteHandler = createRouter()
|
|
|
127
170
|
return { ok: true };
|
|
128
171
|
})
|
|
129
172
|
|
|
173
|
+
.http('git:delete-remote-branch', {
|
|
174
|
+
data: t.Object({
|
|
175
|
+
projectId: t.String(),
|
|
176
|
+
remote: t.String(),
|
|
177
|
+
branch: t.String()
|
|
178
|
+
}),
|
|
179
|
+
response: t.Object({ ok: t.Boolean() })
|
|
180
|
+
}, async ({ data, conn }) => {
|
|
181
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
182
|
+
await gitService.deleteRemoteBranch(project.path, data.remote, data.branch);
|
|
183
|
+
return { ok: true };
|
|
184
|
+
})
|
|
185
|
+
|
|
130
186
|
.http('git:stash-list', {
|
|
131
187
|
data: t.Object({
|
|
132
188
|
projectId: t.String()
|
|
@@ -180,6 +236,35 @@ export const remoteHandler = createRouter()
|
|
|
180
236
|
return { ok: true };
|
|
181
237
|
})
|
|
182
238
|
|
|
239
|
+
.http('git:stash-diff', {
|
|
240
|
+
data: t.Object({
|
|
241
|
+
projectId: t.String(),
|
|
242
|
+
index: t.Optional(t.Number())
|
|
243
|
+
}),
|
|
244
|
+
response: t.Array(t.Object({
|
|
245
|
+
oldPath: t.String(),
|
|
246
|
+
newPath: t.String(),
|
|
247
|
+
status: t.String(),
|
|
248
|
+
hunks: t.Array(t.Object({
|
|
249
|
+
oldStart: t.Number(),
|
|
250
|
+
oldLines: t.Number(),
|
|
251
|
+
newStart: t.Number(),
|
|
252
|
+
newLines: t.Number(),
|
|
253
|
+
header: t.String(),
|
|
254
|
+
lines: t.Array(t.Object({
|
|
255
|
+
type: t.Union([t.Literal('add'), t.Literal('delete'), t.Literal('context'), t.Literal('header')]),
|
|
256
|
+
content: t.String(),
|
|
257
|
+
oldLineNumber: t.Optional(t.Number()),
|
|
258
|
+
newLineNumber: t.Optional(t.Number())
|
|
259
|
+
}))
|
|
260
|
+
})),
|
|
261
|
+
isBinary: t.Boolean()
|
|
262
|
+
}))
|
|
263
|
+
}, async ({ data, conn }) => {
|
|
264
|
+
const project = requireProjectAccess(conn, data.projectId);
|
|
265
|
+
return await gitService.stashDiff(project.path, data.index);
|
|
266
|
+
})
|
|
267
|
+
|
|
183
268
|
.http('git:tags', {
|
|
184
269
|
data: t.Object({
|
|
185
270
|
projectId: t.String()
|
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
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Server CRUD Handlers
|
|
3
|
+
*
|
|
4
|
+
* Manage external (user-installed) MCP servers stored in `mcp_servers`:
|
|
5
|
+
* - mcp:list — installed servers (env values redacted to key names)
|
|
6
|
+
* - mcp:install — persist a server from the catalog (or a custom entry)
|
|
7
|
+
* - mcp:toggle — enable / disable
|
|
8
|
+
* - mcp:uninstall — remove
|
|
9
|
+
*
|
|
10
|
+
* Mutations are admin-gated in `backend/auth/permissions.ts`. Changes take
|
|
11
|
+
* effect on the next chat stream (engines read MCP config when a stream starts).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { t } from 'elysia';
|
|
15
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
16
|
+
import { debug } from '$shared/utils/logger';
|
|
17
|
+
import { mcpServerQueries, type McpServerRow, type McpTransport } from '$backend/database/queries';
|
|
18
|
+
import { slugifyRegistryName, externalNamespace, refreshInternalEnabledCache } from '$backend/mcp';
|
|
19
|
+
|
|
20
|
+
const TRANSPORT_SCHEMA = t.Union([t.Literal('stdio'), t.Literal('http'), t.Literal('sse')]);
|
|
21
|
+
|
|
22
|
+
const INSTALLED_SERVER_SCHEMA = t.Object({
|
|
23
|
+
id: t.Number(),
|
|
24
|
+
slug: t.String(),
|
|
25
|
+
namespace: t.String(),
|
|
26
|
+
name: t.String(),
|
|
27
|
+
description: t.Union([t.String(), t.Null()]),
|
|
28
|
+
registryName: t.Union([t.String(), t.Null()]),
|
|
29
|
+
version: t.Union([t.String(), t.Null()]),
|
|
30
|
+
transport: TRANSPORT_SCHEMA,
|
|
31
|
+
command: t.Union([t.String(), t.Null()]),
|
|
32
|
+
args: t.Array(t.String()),
|
|
33
|
+
envKeys: t.Array(t.String()),
|
|
34
|
+
headerKeys: t.Array(t.String()),
|
|
35
|
+
url: t.Union([t.String(), t.Null()]),
|
|
36
|
+
source: t.String(),
|
|
37
|
+
enabled: t.Boolean(),
|
|
38
|
+
createdAt: t.String()
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function toDTO(row: McpServerRow) {
|
|
42
|
+
let args: string[] = [];
|
|
43
|
+
let envKeys: string[] = [];
|
|
44
|
+
let headerKeys: string[] = [];
|
|
45
|
+
try { args = JSON.parse(row.args); } catch { /* ignore */ }
|
|
46
|
+
try { envKeys = Object.keys(JSON.parse(row.env)); } catch { /* ignore */ }
|
|
47
|
+
try { headerKeys = Object.keys(JSON.parse(row.headers)); } catch { /* ignore */ }
|
|
48
|
+
return {
|
|
49
|
+
id: row.id,
|
|
50
|
+
slug: row.slug,
|
|
51
|
+
namespace: externalNamespace(row.slug),
|
|
52
|
+
name: row.name,
|
|
53
|
+
description: row.description,
|
|
54
|
+
registryName: row.registry_name,
|
|
55
|
+
version: row.version,
|
|
56
|
+
transport: row.transport,
|
|
57
|
+
command: row.command,
|
|
58
|
+
args,
|
|
59
|
+
envKeys,
|
|
60
|
+
headerKeys,
|
|
61
|
+
url: row.url,
|
|
62
|
+
source: row.source,
|
|
63
|
+
enabled: row.is_enabled === 1,
|
|
64
|
+
createdAt: row.created_at
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Derive a slug that doesn't collide with an already-installed server. */
|
|
69
|
+
function uniqueSlug(base: string): string {
|
|
70
|
+
const root = slugifyRegistryName(base);
|
|
71
|
+
if (!mcpServerQueries.getBySlug(root)) return root;
|
|
72
|
+
for (let i = 2; i < 1000; i++) {
|
|
73
|
+
const candidate = `${root}-${i}`;
|
|
74
|
+
if (!mcpServerQueries.getBySlug(candidate)) return candidate;
|
|
75
|
+
}
|
|
76
|
+
return `${root}-${Date.now()}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const mcpCrudHandler = createRouter()
|
|
80
|
+
.http('mcp:list', {
|
|
81
|
+
data: t.Object({}),
|
|
82
|
+
response: t.Object({ servers: t.Array(INSTALLED_SERVER_SCHEMA) })
|
|
83
|
+
}, async () => {
|
|
84
|
+
debug.log('path', 'mcp:list');
|
|
85
|
+
return { servers: mcpServerQueries.getAll().map(toDTO) };
|
|
86
|
+
})
|
|
87
|
+
.http('mcp:install', {
|
|
88
|
+
data: t.Object({
|
|
89
|
+
slug: t.String(),
|
|
90
|
+
name: t.String(),
|
|
91
|
+
description: t.Optional(t.String()),
|
|
92
|
+
registryName: t.Optional(t.String()),
|
|
93
|
+
version: t.Optional(t.String()),
|
|
94
|
+
transport: TRANSPORT_SCHEMA,
|
|
95
|
+
command: t.Optional(t.String()),
|
|
96
|
+
args: t.Optional(t.Array(t.String())),
|
|
97
|
+
url: t.Optional(t.String()),
|
|
98
|
+
env: t.Optional(t.Record(t.String(), t.String())),
|
|
99
|
+
headers: t.Optional(t.Record(t.String(), t.String())),
|
|
100
|
+
source: t.Optional(t.Union([t.Literal('registry'), t.Literal('custom')]))
|
|
101
|
+
}),
|
|
102
|
+
response: t.Object({ server: INSTALLED_SERVER_SCHEMA })
|
|
103
|
+
}, async ({ data }) => {
|
|
104
|
+
debug.log('path', `mcp:install ${data.slug} (${data.transport})`);
|
|
105
|
+
|
|
106
|
+
if (data.transport === 'stdio' && !data.command) {
|
|
107
|
+
throw new Error('A stdio MCP server requires a command');
|
|
108
|
+
}
|
|
109
|
+
if ((data.transport === 'http' || data.transport === 'sse') && !data.url) {
|
|
110
|
+
throw new Error('A remote MCP server requires a URL');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const slug = uniqueSlug(data.slug || data.name);
|
|
114
|
+
const row = mcpServerQueries.insert({
|
|
115
|
+
slug,
|
|
116
|
+
name: data.name,
|
|
117
|
+
description: data.description ?? null,
|
|
118
|
+
registryName: data.registryName ?? null,
|
|
119
|
+
version: data.version ?? null,
|
|
120
|
+
transport: data.transport as McpTransport,
|
|
121
|
+
command: data.command ?? null,
|
|
122
|
+
args: data.args ?? [],
|
|
123
|
+
env: data.env ?? {},
|
|
124
|
+
url: data.url ?? null,
|
|
125
|
+
headers: data.headers ?? {},
|
|
126
|
+
source: data.source ?? 'registry'
|
|
127
|
+
});
|
|
128
|
+
debug.log('mcp', `📥 Installed external MCP server: ${slug}`);
|
|
129
|
+
return { server: toDTO(row) };
|
|
130
|
+
})
|
|
131
|
+
.http('mcp:toggle', {
|
|
132
|
+
data: t.Object({ id: t.Number(), enabled: t.Boolean() }),
|
|
133
|
+
response: t.Object({ server: INSTALLED_SERVER_SCHEMA })
|
|
134
|
+
}, async ({ data }) => {
|
|
135
|
+
debug.log('path', `mcp:toggle ${data.id} → ${data.enabled}`);
|
|
136
|
+
const existing = mcpServerQueries.getById(data.id);
|
|
137
|
+
if (!existing) throw new Error('MCP server not found');
|
|
138
|
+
mcpServerQueries.setEnabled(data.id, data.enabled);
|
|
139
|
+
// Internal servers are gated through an in-memory cache — refresh it so
|
|
140
|
+
// the toggle takes effect on the next chat stream.
|
|
141
|
+
if (existing.source === 'internal') refreshInternalEnabledCache();
|
|
142
|
+
return { server: toDTO(mcpServerQueries.getById(data.id)!) };
|
|
143
|
+
})
|
|
144
|
+
.http('mcp:update-config', {
|
|
145
|
+
data: t.Object({
|
|
146
|
+
id: t.Number(),
|
|
147
|
+
env: t.Optional(t.Record(t.String(), t.String())),
|
|
148
|
+
headers: t.Optional(t.Record(t.String(), t.String()))
|
|
149
|
+
}),
|
|
150
|
+
response: t.Object({ server: INSTALLED_SERVER_SCHEMA })
|
|
151
|
+
}, async ({ data }) => {
|
|
152
|
+
debug.log('path', `mcp:update-config ${data.id}`);
|
|
153
|
+
const existing = mcpServerQueries.getById(data.id);
|
|
154
|
+
if (!existing) throw new Error('MCP server not found');
|
|
155
|
+
if (existing.source === 'internal') throw new Error('Built-in MCP servers cannot be configured');
|
|
156
|
+
// Drop blank values so we never store empty env/header entries.
|
|
157
|
+
const clean = (obj?: Record<string, string>) =>
|
|
158
|
+
Object.fromEntries(Object.entries(obj ?? {}).filter(([, v]) => v.trim() !== ''));
|
|
159
|
+
mcpServerQueries.updateConfig(data.id, clean(data.env), clean(data.headers));
|
|
160
|
+
debug.log('mcp', `🔧 Updated config for external MCP server: ${existing.slug}`);
|
|
161
|
+
return { server: toDTO(mcpServerQueries.getById(data.id)!) };
|
|
162
|
+
})
|
|
163
|
+
.http('mcp:uninstall', {
|
|
164
|
+
data: t.Object({ id: t.Number() }),
|
|
165
|
+
response: t.Object({ success: t.Boolean() })
|
|
166
|
+
}, async ({ data }) => {
|
|
167
|
+
debug.log('path', `mcp:uninstall ${data.id}`);
|
|
168
|
+
const existing = mcpServerQueries.getById(data.id);
|
|
169
|
+
if (!existing) throw new Error('MCP server not found');
|
|
170
|
+
if (existing.source === 'internal') throw new Error('Built-in MCP servers cannot be uninstalled');
|
|
171
|
+
mcpServerQueries.remove(data.id);
|
|
172
|
+
debug.log('mcp', `🗑️ Uninstalled external MCP server: ${existing.slug}`);
|
|
173
|
+
return { success: true };
|
|
174
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Router
|
|
3
|
+
*
|
|
4
|
+
* Entry point for external (user-installed) MCP server management:
|
|
5
|
+
* - mcp:catalog — browse the official MCP registry
|
|
6
|
+
* - mcp:list / install / toggle / uninstall — manage installed servers
|
|
7
|
+
*
|
|
8
|
+
* Internal custom-tool servers (backend/mcp/internal) are NOT managed here —
|
|
9
|
+
* they are defined in code.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
13
|
+
import { mcpCatalogHandler } from './catalog';
|
|
14
|
+
import { mcpCrudHandler } from './crud';
|
|
15
|
+
|
|
16
|
+
export const mcpRouter = createRouter()
|
|
17
|
+
.merge(mcpCatalogHandler)
|
|
18
|
+
.merge(mcpCrudHandler);
|
|
@@ -54,6 +54,37 @@ export const crudHandler = createRouter()
|
|
|
54
54
|
return setting;
|
|
55
55
|
})
|
|
56
56
|
|
|
57
|
+
// Merge-update system settings (admin only).
|
|
58
|
+
// Reads the current `system:settings` blob, merges the provided patch, and
|
|
59
|
+
// writes it back atomically. Sending only the changed keys prevents a partial
|
|
60
|
+
// update from clobbering sibling fields (e.g. onboardingComplete) when a
|
|
61
|
+
// client's in-memory copy of the settings is stale.
|
|
62
|
+
.http('settings:update-system', {
|
|
63
|
+
data: t.Object({
|
|
64
|
+
patch: t.Record(t.String(), t.Any())
|
|
65
|
+
}),
|
|
66
|
+
response: t.Any()
|
|
67
|
+
}, async ({ data }) => {
|
|
68
|
+
const KEY = 'system:settings';
|
|
69
|
+
const existing = settingsQueries.get(KEY);
|
|
70
|
+
|
|
71
|
+
let current: Record<string, unknown> = {};
|
|
72
|
+
if (existing?.value) {
|
|
73
|
+
try {
|
|
74
|
+
current = typeof existing.value === 'string'
|
|
75
|
+
? JSON.parse(existing.value)
|
|
76
|
+
: (existing.value as Record<string, unknown>);
|
|
77
|
+
} catch {
|
|
78
|
+
current = {};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const merged = { ...current, ...data.patch };
|
|
83
|
+
settingsQueries.set(KEY, JSON.stringify(merged));
|
|
84
|
+
|
|
85
|
+
return settingsQueries.get(KEY);
|
|
86
|
+
})
|
|
87
|
+
|
|
57
88
|
// Batch update multiple settings
|
|
58
89
|
.http('settings:update-batch', {
|
|
59
90
|
data: t.Object({
|