@namzu/sdk 69.1.0 → 69.3.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/CHANGELOG.md +21 -0
- package/dist/constants/plugin/index.d.ts +8 -0
- package/dist/constants/plugin/index.d.ts.map +1 -1
- package/dist/constants/plugin/index.js +8 -0
- package/dist/constants/plugin/index.js.map +1 -1
- package/dist/persona/assembler.d.ts.map +1 -1
- package/dist/persona/assembler.js +7 -1
- package/dist/persona/assembler.js.map +1 -1
- package/dist/plugin/common-layout/index.d.ts +5 -0
- package/dist/plugin/common-layout/index.d.ts.map +1 -0
- package/dist/plugin/common-layout/index.js +3 -0
- package/dist/plugin/common-layout/index.js.map +1 -0
- package/dist/plugin/common-layout/mcp.d.ts +21 -0
- package/dist/plugin/common-layout/mcp.d.ts.map +1 -0
- package/dist/plugin/common-layout/mcp.js +305 -0
- package/dist/plugin/common-layout/mcp.js.map +1 -0
- package/dist/plugin/common-layout/paths.d.ts +27 -0
- package/dist/plugin/common-layout/paths.d.ts.map +1 -0
- package/dist/plugin/common-layout/paths.js +48 -0
- package/dist/plugin/common-layout/paths.js.map +1 -0
- package/dist/plugin/common-layout/read.d.ts +25 -0
- package/dist/plugin/common-layout/read.d.ts.map +1 -0
- package/dist/plugin/common-layout/read.js +1155 -0
- package/dist/plugin/common-layout/read.js.map +1 -0
- package/dist/plugin/common-layout/types.d.ts +66 -0
- package/dist/plugin/common-layout/types.d.ts.map +1 -0
- package/dist/plugin/common-layout/types.js +2 -0
- package/dist/plugin/common-layout/types.js.map +1 -0
- package/dist/plugin/common-layout-names.d.ts +65 -0
- package/dist/plugin/common-layout-names.d.ts.map +1 -0
- package/dist/plugin/common-layout-names.js +94 -0
- package/dist/plugin/common-layout-names.js.map +1 -0
- package/dist/plugin/hook-module/capabilities.d.ts +41 -0
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -0
- package/dist/plugin/hook-module/capabilities.js +438 -0
- package/dist/plugin/hook-module/capabilities.js.map +1 -0
- package/dist/plugin/hook-module/consent.d.ts +39 -0
- package/dist/plugin/hook-module/consent.d.ts.map +1 -0
- package/dist/plugin/hook-module/consent.js +59 -0
- package/dist/plugin/hook-module/consent.js.map +1 -0
- package/dist/plugin/hook-module/events.d.ts +33 -0
- package/dist/plugin/hook-module/events.d.ts.map +1 -0
- package/dist/plugin/hook-module/events.js +149 -0
- package/dist/plugin/hook-module/events.js.map +1 -0
- package/dist/plugin/hook-module/host.d.ts +79 -0
- package/dist/plugin/hook-module/host.d.ts.map +1 -0
- package/dist/plugin/hook-module/host.js +546 -0
- package/dist/plugin/hook-module/host.js.map +1 -0
- package/dist/plugin/hook-module/index.d.ts +11 -0
- package/dist/plugin/hook-module/index.d.ts.map +1 -0
- package/dist/plugin/hook-module/index.js +6 -0
- package/dist/plugin/hook-module/index.js.map +1 -0
- package/dist/plugin/hook-module/scan.d.ts +99 -0
- package/dist/plugin/hook-module/scan.d.ts.map +1 -0
- package/dist/plugin/hook-module/scan.js +789 -0
- package/dist/plugin/hook-module/scan.js.map +1 -0
- package/dist/plugin/hook-module/worker-source.d.ts +31 -0
- package/dist/plugin/hook-module/worker-source.d.ts.map +1 -0
- package/dist/plugin/hook-module/worker-source.js +498 -0
- package/dist/plugin/hook-module/worker-source.js.map +1 -0
- package/dist/plugin/index.d.ts +5 -2
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +4 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +57 -0
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +92 -11
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/loader.d.ts +30 -0
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +76 -19
- package/dist/plugin/loader.js.map +1 -1
- package/dist/plugin/shell-hook-env.d.ts +7 -0
- package/dist/plugin/shell-hook-env.d.ts.map +1 -1
- package/dist/plugin/shell-hook-env.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +10 -1
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +81 -70
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/public-runtime.d.ts +3 -2
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +2 -2
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +2 -0
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +4 -0
- package/dist/public-tools.js.map +1 -1
- package/dist/skills/loader.d.ts +8 -2
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +22 -11
- package/dist/skills/loader.js.map +1 -1
- package/dist/skills/registry.d.ts.map +1 -1
- package/dist/skills/registry.js +9 -1
- package/dist/skills/registry.js.map +1 -1
- package/dist/tools/builtins/atomic-write-file.d.ts +19 -3
- package/dist/tools/builtins/atomic-write-file.d.ts.map +1 -1
- package/dist/tools/builtins/atomic-write-file.js +56 -3
- package/dist/tools/builtins/atomic-write-file.js.map +1 -1
- package/dist/types/plugin/index.d.ts +73 -0
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +28 -6
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/skills/index.d.ts +13 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/dist/types/skills/index.js.map +1 -1
- package/package.json +2 -1
- package/src/constants/plugin/index.ts +9 -0
- package/src/persona/assembler.ts +7 -1
- package/src/plugin/common-layout/index.ts +19 -0
- package/src/plugin/common-layout/mcp.ts +362 -0
- package/src/plugin/common-layout/paths.ts +70 -0
- package/src/plugin/common-layout/read.ts +1282 -0
- package/src/plugin/common-layout/types.ts +77 -0
- package/src/plugin/common-layout-names.ts +108 -0
- package/src/plugin/hook-module/capabilities.ts +500 -0
- package/src/plugin/hook-module/consent.ts +96 -0
- package/src/plugin/hook-module/events.ts +177 -0
- package/src/plugin/hook-module/host.ts +649 -0
- package/src/plugin/hook-module/index.ts +52 -0
- package/src/plugin/hook-module/scan.ts +1004 -0
- package/src/plugin/hook-module/worker-source.ts +500 -0
- package/src/plugin/index.ts +13 -1
- package/src/plugin/lifecycle.ts +179 -17
- package/src/plugin/loader.ts +97 -18
- package/src/plugin/shell-hook-env.ts +7 -0
- package/src/plugin/shell-hook.ts +91 -70
- package/src/public-runtime.ts +35 -0
- package/src/public-tools.ts +6 -0
- package/src/skills/loader.ts +26 -9
- package/src/skills/registry.ts +9 -1
- package/src/tools/builtins/atomic-write-file.ts +73 -5
- package/src/types/plugin/index.ts +81 -24
- package/src/types/skills/index.ts +15 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path'
|
|
2
|
+
import type {
|
|
3
|
+
PluginMCPServerConfig,
|
|
4
|
+
PluginRemoteMCPServerConfig,
|
|
5
|
+
} from '../../types/plugin/index.js'
|
|
6
|
+
import { COMMON_USER_CONFIG_PREFIX } from '../common-layout-names.js'
|
|
7
|
+
import { type PluginPathValues, expandPluginPaths } from './paths.js'
|
|
8
|
+
import type { LayoutFinding, LayoutSetupServer } from './types.js'
|
|
9
|
+
|
|
10
|
+
/** One servers map and the file (or manifest) it was read from. */
|
|
11
|
+
export interface McpSource {
|
|
12
|
+
readonly origin: string
|
|
13
|
+
readonly map: Readonly<Record<string, unknown>>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface McpReadResult {
|
|
17
|
+
readonly local: readonly PluginMCPServerConfig[]
|
|
18
|
+
readonly remote: readonly PluginRemoteMCPServerConfig[]
|
|
19
|
+
readonly findings: readonly LayoutFinding[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const SERVER_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/
|
|
23
|
+
const KNOWN_KEYS = new Set(['command', 'args', 'env', 'type', 'url', 'headers', 'connectTimeoutMs'])
|
|
24
|
+
const REMOTE_TYPES = new Set(['http', 'sse', 'streamable-http'])
|
|
25
|
+
|
|
26
|
+
function isMapping(value: unknown): value is Record<string, unknown> {
|
|
27
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The servers of one parsed file or inline value: `{ "mcpServers": { name: spec } }`
|
|
32
|
+
* or the flat `{ name: spec }`.
|
|
33
|
+
*/
|
|
34
|
+
export function serversOf(value: unknown): Record<string, unknown> | undefined {
|
|
35
|
+
if (!isMapping(value)) return undefined
|
|
36
|
+
if (isMapping(value.mcpServers)) return value.mcpServers
|
|
37
|
+
return value
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Expanded {
|
|
41
|
+
readonly text: string
|
|
42
|
+
readonly needs: readonly string[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class Reader {
|
|
46
|
+
readonly findings: LayoutFinding[] = []
|
|
47
|
+
readonly local: PluginMCPServerConfig[] = []
|
|
48
|
+
readonly remote: PluginRemoteMCPServerConfig[] = []
|
|
49
|
+
private readonly seen = new Set<string>()
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
private readonly values: PluginPathValues,
|
|
53
|
+
private readonly environment: Readonly<Record<string, string | undefined>>,
|
|
54
|
+
) {}
|
|
55
|
+
|
|
56
|
+
add(finding: LayoutFinding): void {
|
|
57
|
+
this.findings.push(finding)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Path names, then (only in an `env` value) bare environment references; the rest is said. */
|
|
61
|
+
private expand(
|
|
62
|
+
text: string,
|
|
63
|
+
field: string,
|
|
64
|
+
name: string,
|
|
65
|
+
mode: 'env' | 'keep' | 'none',
|
|
66
|
+
): Expanded {
|
|
67
|
+
const needs: string[] = []
|
|
68
|
+
let expanded: string
|
|
69
|
+
try {
|
|
70
|
+
const result = expandPluginPaths(text, this.values)
|
|
71
|
+
expanded = result.text
|
|
72
|
+
for (const unresolved of result.unresolved) {
|
|
73
|
+
needs.push(`\${${unresolved}} has no folder here (${field})`)
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
needs.push(error instanceof Error ? error.message : String(error))
|
|
77
|
+
expanded = text
|
|
78
|
+
}
|
|
79
|
+
expanded = expanded.replace(/\$\{([^}]*)\}/g, (whole: string, inner: string) => {
|
|
80
|
+
if (inner.startsWith(COMMON_USER_CONFIG_PREFIX)) {
|
|
81
|
+
needs.push(`${whole} is a saved option, which is not supported yet (${field})`)
|
|
82
|
+
return whole
|
|
83
|
+
}
|
|
84
|
+
if ((mode === 'env' || mode === 'keep') && /^[A-Za-z_][A-Za-z0-9_]*$/.test(inner)) {
|
|
85
|
+
const value = this.environment[inner]
|
|
86
|
+
if (value === undefined || value === '') {
|
|
87
|
+
needs.push(`${inner} is not set`)
|
|
88
|
+
return whole
|
|
89
|
+
}
|
|
90
|
+
// A header keeps its reference as written: the value is read when it connects.
|
|
91
|
+
return mode === 'keep' ? whole : value
|
|
92
|
+
}
|
|
93
|
+
needs.push(`${whole} is not expanded in ${field}`)
|
|
94
|
+
return whole
|
|
95
|
+
})
|
|
96
|
+
const bare = expanded.replace(/\$\{[^}]*\}/g, '')
|
|
97
|
+
if (/\$[A-Za-z_]|`|%[A-Za-z_][A-Za-z0-9_]*%/.test(bare)) {
|
|
98
|
+
this.add({
|
|
99
|
+
level: 'ignored',
|
|
100
|
+
code: 'mcp-text-left-as-is',
|
|
101
|
+
field: `mcpServers.${name}.${field}`,
|
|
102
|
+
message: `${name}: ${field} holds a $-name, backtick or %NAME% that is left as written; no shell reads it.`,
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
return { text: expanded, needs }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private refuse(name: string, code: string, message: string): void {
|
|
109
|
+
this.add({ level: 'refused', code, field: `mcpServers.${name}`, message })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private needsSetup(
|
|
113
|
+
name: string,
|
|
114
|
+
needs: readonly string[],
|
|
115
|
+
server?: Omit<LayoutSetupServer, 'name'>,
|
|
116
|
+
): void {
|
|
117
|
+
this.add({
|
|
118
|
+
...(server !== undefined ? { server: { name, ...server } } : {}),
|
|
119
|
+
level: 'notUsed',
|
|
120
|
+
code: 'mcp-needs-setup',
|
|
121
|
+
field: `mcpServers.${name}`,
|
|
122
|
+
message: `${name} needs setup: ${needs.join('; ')}. It is not started.`,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
readSource(source: McpSource): void {
|
|
127
|
+
for (const [name, spec] of Object.entries(source.map)) {
|
|
128
|
+
if (this.seen.has(name)) {
|
|
129
|
+
this.add({
|
|
130
|
+
level: 'ignored',
|
|
131
|
+
code: 'mcp-server-duplicate',
|
|
132
|
+
field: `mcpServers.${name}`,
|
|
133
|
+
message: `${name} is named twice; the first one in ${source.origin} order is kept.`,
|
|
134
|
+
})
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
this.seen.add(name)
|
|
138
|
+
this.readOne(name, spec)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private readOne(name: string, spec: unknown): void {
|
|
143
|
+
if (!SERVER_NAME.test(name)) {
|
|
144
|
+
this.refuse(name, 'mcp-name-invalid', `"${name}" is not a usable server name.`)
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
if (!isMapping(spec)) {
|
|
148
|
+
this.add({
|
|
149
|
+
level: 'ignored',
|
|
150
|
+
code: 'mcp-server-empty',
|
|
151
|
+
field: `mcpServers.${name}`,
|
|
152
|
+
message: `${name} has no settings.`,
|
|
153
|
+
})
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
for (const key of Object.keys(spec)) {
|
|
157
|
+
if (!KNOWN_KEYS.has(key)) {
|
|
158
|
+
this.add({
|
|
159
|
+
level: 'ignored',
|
|
160
|
+
code: 'mcp-key-ignored',
|
|
161
|
+
field: `mcpServers.${name}.${key}`,
|
|
162
|
+
message: `${name}: "${key}" is not read.`,
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const hasCommand = typeof spec.command === 'string' && spec.command.length > 0
|
|
167
|
+
const hasUrl = typeof spec.url === 'string' && spec.url.length > 0
|
|
168
|
+
const type = typeof spec.type === 'string' ? spec.type : undefined
|
|
169
|
+
if (hasCommand && hasUrl) {
|
|
170
|
+
this.refuse(name, 'mcp-server-both', `${name} names both a command and an address; use one.`)
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
if (!hasCommand && !hasUrl) {
|
|
174
|
+
this.add({
|
|
175
|
+
level: 'ignored',
|
|
176
|
+
code: 'mcp-server-empty',
|
|
177
|
+
field: `mcpServers.${name}`,
|
|
178
|
+
message: `${name} names neither a command nor an address.`,
|
|
179
|
+
})
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
if (hasUrl) {
|
|
183
|
+
if (type !== undefined && !REMOTE_TYPES.has(type)) {
|
|
184
|
+
this.refuse(name, 'mcp-type-unsupported', `${name}: the type "${type}" is not supported.`)
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
this.readRemote(name, spec, type)
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
if (type !== undefined && type !== 'stdio') {
|
|
191
|
+
this.refuse(name, 'mcp-type-unsupported', `${name}: the type "${type}" is not supported.`)
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
this.readLocal(name, spec)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private readLocal(name: string, spec: Record<string, unknown>): void {
|
|
198
|
+
const needs: string[] = []
|
|
199
|
+
const command = this.expand(spec.command as string, 'command', name, 'none')
|
|
200
|
+
needs.push(...command.needs)
|
|
201
|
+
const looksRelative =
|
|
202
|
+
/[\\/]/.test(command.text) && !isAbsolute(command.text) && !/^[A-Za-z]:/.test(command.text)
|
|
203
|
+
if (looksRelative && command.text.split(/[\\/]/).includes('..')) {
|
|
204
|
+
this.refuse(name, 'mcp-command-escapes', `${name}: the command leaves the plugin with "..".`)
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
let args: string[] | undefined
|
|
208
|
+
if (spec.args !== undefined) {
|
|
209
|
+
if (!Array.isArray(spec.args) || !spec.args.every((arg) => typeof arg === 'string')) {
|
|
210
|
+
this.refuse(name, 'mcp-args-invalid', `${name}: "args" must be a list of text.`)
|
|
211
|
+
return
|
|
212
|
+
}
|
|
213
|
+
args = (spec.args as string[]).map((arg) => {
|
|
214
|
+
const expanded = this.expand(arg, 'args', name, 'none')
|
|
215
|
+
needs.push(...expanded.needs)
|
|
216
|
+
return expanded.text
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
let env: Record<string, string> | undefined
|
|
220
|
+
if (spec.env !== undefined) {
|
|
221
|
+
if (
|
|
222
|
+
!isMapping(spec.env) ||
|
|
223
|
+
!Object.values(spec.env).every((value) => typeof value === 'string')
|
|
224
|
+
) {
|
|
225
|
+
this.refuse(name, 'mcp-env-invalid', `${name}: "env" must map names to text.`)
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
env = {}
|
|
229
|
+
for (const [key, value] of Object.entries(spec.env as Record<string, string>)) {
|
|
230
|
+
const expanded = this.expand(value, `env.${key}`, name, 'env')
|
|
231
|
+
needs.push(...expanded.needs)
|
|
232
|
+
env[key] = expanded.text
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
let connectTimeoutMs: number | undefined
|
|
236
|
+
if (typeof spec.connectTimeoutMs === 'number') {
|
|
237
|
+
if (
|
|
238
|
+
!Number.isInteger(spec.connectTimeoutMs) ||
|
|
239
|
+
spec.connectTimeoutMs <= 0 ||
|
|
240
|
+
spec.connectTimeoutMs > 3_600_000
|
|
241
|
+
) {
|
|
242
|
+
this.refuse(name, 'mcp-timeout-invalid', `${name}: "connectTimeoutMs" is out of range.`)
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
connectTimeoutMs = spec.connectTimeoutMs
|
|
246
|
+
}
|
|
247
|
+
if (needs.length > 0) {
|
|
248
|
+
this.needsSetup(name, [...new Set(needs)], {
|
|
249
|
+
kind: 'local',
|
|
250
|
+
target: spec.command as string,
|
|
251
|
+
args: Array.isArray(spec.args) ? (spec.args as string[]) : [],
|
|
252
|
+
values: isMapping(spec.env) ? (spec.env as Record<string, string>) : {},
|
|
253
|
+
})
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
this.local.push({
|
|
257
|
+
name,
|
|
258
|
+
command: command.text,
|
|
259
|
+
...(args !== undefined ? { args } : {}),
|
|
260
|
+
...(env !== undefined ? { env } : {}),
|
|
261
|
+
...(connectTimeoutMs !== undefined ? { connectTimeoutMs } : {}),
|
|
262
|
+
})
|
|
263
|
+
this.add({
|
|
264
|
+
level: 'used',
|
|
265
|
+
code: 'mcp-server-local',
|
|
266
|
+
field: `mcpServers.${name}`,
|
|
267
|
+
message: `${name} starts as a local program while the plugin is on.`,
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private readRemote(name: string, spec: Record<string, unknown>, type: string | undefined): void {
|
|
272
|
+
const url = this.expand(spec.url as string, 'url', name, 'none')
|
|
273
|
+
const asWritten = (): Omit<LayoutSetupServer, 'name'> => ({
|
|
274
|
+
kind: 'remote',
|
|
275
|
+
target: spec.url as string,
|
|
276
|
+
transport: type === 'sse' ? 'sse' : 'http',
|
|
277
|
+
values:
|
|
278
|
+
isMapping(spec.headers) &&
|
|
279
|
+
Object.values(spec.headers).every((value) => typeof value === 'string')
|
|
280
|
+
? (spec.headers as Record<string, string>)
|
|
281
|
+
: {},
|
|
282
|
+
})
|
|
283
|
+
if (url.needs.length > 0) {
|
|
284
|
+
this.needsSetup(name, url.needs, asWritten())
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
let parsed: URL
|
|
288
|
+
try {
|
|
289
|
+
parsed = new URL(url.text)
|
|
290
|
+
} catch {
|
|
291
|
+
this.refuse(name, 'mcp-url-invalid', `${name}: the address is not a valid URL.`)
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
295
|
+
this.refuse(
|
|
296
|
+
name,
|
|
297
|
+
'mcp-url-invalid',
|
|
298
|
+
`${name}: the address must start with https:// or http://.`,
|
|
299
|
+
)
|
|
300
|
+
return
|
|
301
|
+
}
|
|
302
|
+
if (parsed.username !== '' || parsed.password !== '') {
|
|
303
|
+
this.refuse(
|
|
304
|
+
name,
|
|
305
|
+
'mcp-url-invalid',
|
|
306
|
+
`${name}: the address must not hold a user name or password.`,
|
|
307
|
+
)
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
let headers: Record<string, string> | undefined
|
|
311
|
+
if (spec.headers !== undefined) {
|
|
312
|
+
if (
|
|
313
|
+
!isMapping(spec.headers) ||
|
|
314
|
+
!Object.values(spec.headers).every((value) => typeof value === 'string')
|
|
315
|
+
) {
|
|
316
|
+
this.refuse(name, 'mcp-headers-invalid', `${name}: "headers" must map names to text.`)
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
headers = {}
|
|
320
|
+
const needs: string[] = []
|
|
321
|
+
for (const [key, value] of Object.entries(spec.headers as Record<string, string>)) {
|
|
322
|
+
// A `${VAR}` stays as written: the host that connects reads the variable
|
|
323
|
+
// then, and the plugin file never holds the value.
|
|
324
|
+
const expanded = this.expand(value, `headers.${key}`, name, 'keep')
|
|
325
|
+
needs.push(...expanded.needs)
|
|
326
|
+
headers[key] = expanded.text
|
|
327
|
+
}
|
|
328
|
+
if (needs.length > 0) {
|
|
329
|
+
this.needsSetup(name, [...new Set(needs)], asWritten())
|
|
330
|
+
return
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const transport = type === 'sse' ? 'sse' : 'http'
|
|
334
|
+
this.remote.push({
|
|
335
|
+
name,
|
|
336
|
+
url: url.text,
|
|
337
|
+
transport,
|
|
338
|
+
...(headers !== undefined ? { headers } : {}),
|
|
339
|
+
})
|
|
340
|
+
this.add({
|
|
341
|
+
level: 'notUsed',
|
|
342
|
+
code: 'mcp-server-remote',
|
|
343
|
+
field: `mcpServers.${name}`,
|
|
344
|
+
message: `${name} is a remote server at ${parsed.origin}${parsed.pathname === '/' ? '' : parsed.pathname}. It connects only after you approve it.`,
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Read every server of every source, applying the path and environment rules. */
|
|
350
|
+
export function readMcpSources(
|
|
351
|
+
sources: readonly McpSource[],
|
|
352
|
+
values: PluginPathValues,
|
|
353
|
+
environment: Readonly<Record<string, string | undefined>>,
|
|
354
|
+
): McpReadResult {
|
|
355
|
+
const reader = new Reader(values, environment)
|
|
356
|
+
for (const source of sources) reader.readSource(source)
|
|
357
|
+
return {
|
|
358
|
+
local: reader.local,
|
|
359
|
+
remote: reader.remote,
|
|
360
|
+
findings: reader.findings,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COMMON_DATA_VARIABLE,
|
|
3
|
+
COMMON_PROJECT_VARIABLE,
|
|
4
|
+
COMMON_ROOT_VARIABLE,
|
|
5
|
+
NAMZU_DATA_VARIABLE,
|
|
6
|
+
NAMZU_PROJECT_VARIABLE,
|
|
7
|
+
NAMZU_ROOT_VARIABLE,
|
|
8
|
+
} from '../common-layout-names.js'
|
|
9
|
+
|
|
10
|
+
/** The folders the six path names stand for. A name with no folder here is left as written. */
|
|
11
|
+
export interface PluginPathValues {
|
|
12
|
+
/** The plugin's own folder. */
|
|
13
|
+
readonly root: string
|
|
14
|
+
/** The folder kept for the plugin's data. */
|
|
15
|
+
readonly data?: string | undefined
|
|
16
|
+
/** The folder the session was started for. */
|
|
17
|
+
readonly project?: string | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The six names {@link expandPluginPaths} replaces, and nothing else. */
|
|
21
|
+
export const PLUGIN_PATH_VARIABLES: readonly string[] = [
|
|
22
|
+
COMMON_ROOT_VARIABLE,
|
|
23
|
+
NAMZU_ROOT_VARIABLE,
|
|
24
|
+
COMMON_DATA_VARIABLE,
|
|
25
|
+
NAMZU_DATA_VARIABLE,
|
|
26
|
+
COMMON_PROJECT_VARIABLE,
|
|
27
|
+
NAMZU_PROJECT_VARIABLE,
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
export interface ExpandedPaths {
|
|
31
|
+
readonly text: string
|
|
32
|
+
/** Path names that had no folder to stand for, in order of appearance. */
|
|
33
|
+
readonly unresolved: readonly string[]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function folderFor(name: string, values: PluginPathValues): string | undefined {
|
|
37
|
+
if (name === COMMON_ROOT_VARIABLE || name === NAMZU_ROOT_VARIABLE) return values.root
|
|
38
|
+
if (name === COMMON_DATA_VARIABLE || name === NAMZU_DATA_VARIABLE) return values.data
|
|
39
|
+
if (name === COMMON_PROJECT_VARIABLE || name === NAMZU_PROJECT_VARIABLE) return values.project
|
|
40
|
+
return undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `${NAME}` for exactly the six path names becomes the folder. Nothing runs a
|
|
45
|
+
* shell and nothing else is touched: `$NAME`, `${OTHER}` and `${NAME:-x}` are
|
|
46
|
+
* left as written. A folder that is not absolute, or holds a NUL, is refused
|
|
47
|
+
* with a thrown reason, so a substituted value never turns into a relative
|
|
48
|
+
* or truncated path.
|
|
49
|
+
*/
|
|
50
|
+
export function expandPluginPaths(text: string, values: PluginPathValues): ExpandedPaths {
|
|
51
|
+
const unresolved: string[] = []
|
|
52
|
+
const out = text.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (whole: string, name: string) => {
|
|
53
|
+
if (!PLUGIN_PATH_VARIABLES.includes(name)) return whole
|
|
54
|
+
const folder = folderFor(name, values)
|
|
55
|
+
if (folder === undefined) {
|
|
56
|
+
unresolved.push(name)
|
|
57
|
+
return whole
|
|
58
|
+
}
|
|
59
|
+
if (folder.includes('\0') || !/^([A-Za-z]:[\\/]|[\\/])/.test(folder)) {
|
|
60
|
+
throw new Error(`The folder for ${name} must be an absolute path without NUL characters.`)
|
|
61
|
+
}
|
|
62
|
+
return folder
|
|
63
|
+
})
|
|
64
|
+
return { text: out, unresolved }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Only the plugin's own folder: the form skill, command and agent text gets. */
|
|
68
|
+
export function expandPluginRootText(text: string, root: string): string {
|
|
69
|
+
return expandPluginPaths(text, { root }).text
|
|
70
|
+
}
|