@dsh-cc/mcp-config 0.5.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/LICENSE +201 -0
- package/README.i18n.yaml +6 -0
- package/README.md +103 -0
- package/README.zh.md +103 -0
- package/lib/index.d.ts +146 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +230 -0
- package/lib/index.js.map +1 -0
- package/lib/invariant.d.ts +16 -0
- package/lib/invariant.d.ts.map +1 -0
- package/lib/invariant.js +23 -0
- package/lib/invariant.js.map +1 -0
- package/lib/migrate.d.ts +60 -0
- package/lib/migrate.d.ts.map +1 -0
- package/lib/migrate.js +138 -0
- package/lib/migrate.js.map +1 -0
- package/lib/paths.d.ts +79 -0
- package/lib/paths.d.ts.map +1 -0
- package/lib/paths.js +91 -0
- package/lib/paths.js.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.i18n.yaml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
|
|
2
|
+
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
|
+
# after editing either side, bring the other along and re-record with:
|
|
4
|
+
# pnpm run verify-translation-pairing --write packages/mcp/mcp-config/README.md
|
|
5
|
+
README.md: 62d41ce3b30d512ae62eb08004a417da863cc07b
|
|
6
|
+
README.zh.md: 90596f74566303c721d7c92fb490f903a44ef29a
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @dsh-cc/mcp-config
|
|
2
|
+
|
|
3
|
+
English | [中文](README.zh.md)
|
|
4
|
+
|
|
5
|
+
Claude Code-style MCP workspace-configuration loader: parses a `.mcp.json` document, validates it, expands environment substitutions, applies an enterprise allow/deny policy, and translates the accepted servers into `@dsh-cc/mcp-client` registrations ready to mount.
|
|
6
|
+
|
|
7
|
+
This package owns the file→config *reading and validation* surface only. It performs no network I/O and mounts nothing; consumers feed its output to `@dsh-cc/mcp-client` instances.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { buildRegistrations, type McpConfigPolicy } from '@dsh-cc/mcp-config'
|
|
13
|
+
import { readFileSync } from 'node:fs'
|
|
14
|
+
|
|
15
|
+
const body = JSON.parse(readFileSync('.mcp.json', 'utf8'))
|
|
16
|
+
const policy: McpConfigPolicy = {
|
|
17
|
+
deny: (name) => name === 'blocked-saas',
|
|
18
|
+
allow: (name) => name !== 'experimental',
|
|
19
|
+
}
|
|
20
|
+
// registrations: mcp-client Config objects (with serverName), in stable config order
|
|
21
|
+
const registrations = buildRegistrations(body, { env: process.env, policy })
|
|
22
|
+
|
|
23
|
+
for (const config of registrations) {
|
|
24
|
+
await ctx.plugin(mcpClient, config)
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
A typical `.mcp.json`:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"mcpServers": {
|
|
33
|
+
"github": {
|
|
34
|
+
"command": "npx",
|
|
35
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
36
|
+
"env": { "GITHUB_TOKEN": "${GITHUB_TOKEN}" }
|
|
37
|
+
},
|
|
38
|
+
"web": {
|
|
39
|
+
"type": "http",
|
|
40
|
+
"url": "https://mcp.example.com/mcp",
|
|
41
|
+
"headers": { "Authorization": "Bearer ${MCP_TOKEN}" }
|
|
42
|
+
},
|
|
43
|
+
"feed": {
|
|
44
|
+
"type": "sse",
|
|
45
|
+
"url": "https://sse.example.com/events"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Server definition types
|
|
52
|
+
|
|
53
|
+
A `.mcp.json` `mcpServers` map values are the Claude Code server definitions:
|
|
54
|
+
|
|
55
|
+
| Shape | `type` | Required | Notes |
|
|
56
|
+
|---|---|---|---|
|
|
57
|
+
| `stdio` | omitted or `"stdio"` | `command` | `args`, `env`, `cwd` optional |
|
|
58
|
+
| `http` | `"http"` (also accepts legacy `"streamable-http"`) | `url` | `headers` optional |
|
|
59
|
+
| `sse` | `"sse"` | `url` | `headers` optional |
|
|
60
|
+
|
|
61
|
+
`mcpServers` may be an object keyed by server name, or an array of such objects (the array form rejects a name that appears twice).
|
|
62
|
+
|
|
63
|
+
## Config mapping
|
|
64
|
+
|
|
65
|
+
Each accepted server translates to one `@dsh-cc/mcp-client` `Config`:
|
|
66
|
+
|
|
67
|
+
- The server **name** becomes `serverName`, the public namespace for the model-facing tool names (`mcp__<serverName>__*`).
|
|
68
|
+
- `command`-based definitions map to the `stdio` transport.
|
|
69
|
+
- `http` / `streamable-http` definitions map to the `streamable-http` transport.
|
|
70
|
+
- `sse` definitions map to the `sse` transport.
|
|
71
|
+
- Registrations default `toolCallTimeoutMs` to 60000 and `failOnStartupError` to `true` (a malformed or unreachable server fails the load loudly rather than silently activating without tools).
|
|
72
|
+
|
|
73
|
+
## Environment expansion
|
|
74
|
+
|
|
75
|
+
Strings in `command`, `args`, `cwd`, `env` values, `url`, and `headers` support:
|
|
76
|
+
|
|
77
|
+
- `${VAR}` — substituted with the environment value; **throws at load** when the variable is not set.
|
|
78
|
+
- `${VAR:-default}` — falls back to `default` when the variable is unset or empty.
|
|
79
|
+
- `$$` — a literal `$`.
|
|
80
|
+
|
|
81
|
+
Expansion reads `process.env` by default or the `env` option passed to `buildRegistrations` / `applyEnv`.
|
|
82
|
+
|
|
83
|
+
## Enterprise allow/deny policy
|
|
84
|
+
|
|
85
|
+
Pass a `McpConfigPolicy` to `buildRegistrations` to gate servers before translation:
|
|
86
|
+
|
|
87
|
+
- `deny(name, entry)` — returning `true` drops the server (runs first, wins).
|
|
88
|
+
- `allow(name, entry)` — when present, only servers where it returns `true` are kept.
|
|
89
|
+
|
|
90
|
+
Hooks receive the raw `McpServerEntry`; they must be synchronous and total. Policy happens after parsing and validation but before translation, in stable config order, and rejected servers never reach the client.
|
|
91
|
+
|
|
92
|
+
## Errors
|
|
93
|
+
|
|
94
|
+
Malformed configuration throws at load — a non-object body, a missing or non-map `mcpServers`, a duplicated name, an unknown transport type, a missing required `command`/`url`, or an unset environment variable. Fail loud beats silently skipping a missing referent.
|
|
95
|
+
|
|
96
|
+
## API
|
|
97
|
+
|
|
98
|
+
- `parseMcpServers(body)` — validate the `mcpServers` map and return it; throws on malformed input.
|
|
99
|
+
- `dedupServers(map)` — normalize a parsed map into a stable `McpServerSpec[]`; throws on a duplicated name.
|
|
100
|
+
- `normalizeServerEntry(entry)` — one raw entry → an mcp-client `Config` (no env expansion), validating required fields.
|
|
101
|
+
- `applyEnv(config, env?)` — expand `${...}` substitutions in one normalized config.
|
|
102
|
+
- `expandEnv(value, env)` — expand one string's substitutions.
|
|
103
|
+
- `buildRegistrations(body, { env?, policy? })` — full pipeline to mcp-client registrations.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @dsh-cc/mcp-config
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 中文
|
|
4
|
+
|
|
5
|
+
Claude Code 风格的 MCP 工作区配置加载器:解析 `.mcp.json` 文档,进行校验、环境变量展开、企业级 allow/deny 策略过滤,并把通过的服务转换为可直接挂载的 `@dsh-cc/mcp-client` 注册项。
|
|
6
|
+
|
|
7
|
+
本包只负责文件→配置的**读取与校验**。它不做任何网络 I/O,也不自行挂载;下游把输出喂给 `@dsh-cc/mcp-client` 实例。
|
|
8
|
+
|
|
9
|
+
## 用法
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { buildRegistrations, type McpConfigPolicy } from '@dsh-cc/mcp-config'
|
|
13
|
+
import { readFileSync } from 'node:fs'
|
|
14
|
+
|
|
15
|
+
const body = JSON.parse(readFileSync('.mcp.json', 'utf8'))
|
|
16
|
+
const policy: McpConfigPolicy = {
|
|
17
|
+
deny: (name) => name === 'blocked-saas',
|
|
18
|
+
allow: (name) => name !== 'experimental',
|
|
19
|
+
}
|
|
20
|
+
// registrations: mcp-client Config objects (with serverName), in stable config order
|
|
21
|
+
const registrations = buildRegistrations(body, { env: process.env, policy })
|
|
22
|
+
|
|
23
|
+
for (const config of registrations) {
|
|
24
|
+
await ctx.plugin(mcpClient, config)
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
典型的 `.mcp.json`:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"mcpServers": {
|
|
33
|
+
"github": {
|
|
34
|
+
"command": "npx",
|
|
35
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
36
|
+
"env": { "GITHUB_TOKEN": "${GITHUB_TOKEN}" }
|
|
37
|
+
},
|
|
38
|
+
"web": {
|
|
39
|
+
"type": "http",
|
|
40
|
+
"url": "https://mcp.example.com/mcp",
|
|
41
|
+
"headers": { "Authorization": "Bearer ${MCP_TOKEN}" }
|
|
42
|
+
},
|
|
43
|
+
"feed": {
|
|
44
|
+
"type": "sse",
|
|
45
|
+
"url": "https://sse.example.com/events"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 服务定义类型
|
|
52
|
+
|
|
53
|
+
`.mcp.json` 的 `mcpServers` 值为 Claude Code 服务定义:
|
|
54
|
+
|
|
55
|
+
| 形态 | `type` | 必填 | 说明 |
|
|
56
|
+
|---|---|---|---|
|
|
57
|
+
| `stdio` | 省略或 `"stdio"` | `command` | `args`、`env`、`cwd` 可选 |
|
|
58
|
+
| `http` | `"http"`(也接受旧式 `"streamable-http"`) | `url` | `headers` 可选 |
|
|
59
|
+
| `sse` | `"sse"` | `url` | `headers` 可选 |
|
|
60
|
+
|
|
61
|
+
`mcpServers` 可以是按服务名索引的对象,也可以是若干此类对象的数组(数组形式会拒绝重复出现的服务名)。
|
|
62
|
+
|
|
63
|
+
## 配置映射
|
|
64
|
+
|
|
65
|
+
每个通过的服务转换为一个 `@dsh-cc/mcp-client` `Config`:
|
|
66
|
+
|
|
67
|
+
- 服务**名**成为 `serverName`,即模型可见工具名的公共命名空间(`mcp__<serverName>__*`)。
|
|
68
|
+
- 基于 `command` 的定义映射为 `stdio` 传输。
|
|
69
|
+
- `http` / `streamable-http` 定义映射为 `streamable-http` 传输。
|
|
70
|
+
- `sse` 定义映射为 `sse` 传输。
|
|
71
|
+
- 注册项默认 `toolCallTimeoutMs` 为 60000、`failOnStartupError` 为 `true`(坏配置或不可达服服务在加载时响亮失败,而非静默地无工具激活)。
|
|
72
|
+
|
|
73
|
+
## 环境变量展开
|
|
74
|
+
|
|
75
|
+
`command`、`args`、`cwd`、`env` 值、`url`、`headers` 中的字符串支持:
|
|
76
|
+
|
|
77
|
+
- `${VAR}` —— 用环境变量值替换;**变量未设置时在加载期抛错**。
|
|
78
|
+
- `${VAR:-default}` —— 变量未设置或为空时回退到 `default`。
|
|
79
|
+
- `$$` —— 一个字面量 `$`。
|
|
80
|
+
|
|
81
|
+
展开默认读取 `process.env`,也可传入 `buildRegistrations`/`applyEnv` 的 `env` 选项。
|
|
82
|
+
|
|
83
|
+
## 企业级 allow/deny 策略
|
|
84
|
+
|
|
85
|
+
向 `buildRegistrations` 传入 `McpConfigPolicy` 以在转换前过滤服务:
|
|
86
|
+
|
|
87
|
+
- `deny(name, entry)` —— 返回 `true` 时丢弃该服务(先执行,优先级更高)。
|
|
88
|
+
- `allow(name, entry)` —— 存在时,只有返回 `true` 的服务被保留。
|
|
89
|
+
|
|
90
|
+
钩子接收原始 `McpServerEntry`,必须同步且收敛;抛出的钩子会中止整个加载。策略发生在解析与校验之后、转换之前,按稳定配置顺序执行;被拒绝的服务永远不会进入客户端。
|
|
91
|
+
|
|
92
|
+
## 错误
|
|
93
|
+
|
|
94
|
+
格式错误的配置在加载期抛错——非对象 body、缺失或非映射的 `mcpServers`、重复的服务名、未知传输类型、缺少必填的 `command`/`url`、或未设置的环境变量。响亮失败胜过静默跳过缺失引用。
|
|
95
|
+
|
|
96
|
+
## API
|
|
97
|
+
|
|
98
|
+
- `parseMcpServers(body)` —— 校验 `mcpServers` 映射并返回;畸形输入抛错。
|
|
99
|
+
- `dedupServers(map)` —— 将解析后的映射规范化为稳定的 `McpServerSpec[]`;重复服务名抛错。
|
|
100
|
+
- `normalizeServerEntry(entry)` —— 单个原始条目 → mcp-client `Config`(不展开环境变量),校验必填字段。
|
|
101
|
+
- `applyEnv(config, env?)` —— 在一个规范化配置中展开 `${...}` 替换。
|
|
102
|
+
- `expandEnv(value, env)` —— 展开单个字符串的替换。
|
|
103
|
+
- `buildRegistrations(body, { env?, policy? })` —— 到 mcp-client 注册项的完整流水线。
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP workspace-configuration loader (`@dsh-cc/mcp-config`).
|
|
3
|
+
*
|
|
4
|
+
* Parses a Claude Code-style `.mcp.json` document — the `mcpServers` map of
|
|
5
|
+
* stdio / HTTP(S) / SSE server definitions — validates it (malformed input
|
|
6
|
+
* throws at load), expands `${VAR}` and `${VAR:-default}` environment
|
|
7
|
+
* substitutions, dedupes server names, applies an enterprise allow/deny policy,
|
|
8
|
+
* and translates the accepted servers into `@dsh-cc/mcp-client`
|
|
9
|
+
* registrations ready for mount.
|
|
10
|
+
*
|
|
11
|
+
* The package owns the file→config *reading and validation* surface only; it
|
|
12
|
+
* performs no network I/O and mounts nothing itself. Consumers translate the
|
|
13
|
+
* returned registrations into mcp-client plugin instances.
|
|
14
|
+
*
|
|
15
|
+
* Two side modules extend that surface: `src/paths.ts` adds side-effect-free
|
|
16
|
+
* default-path classification and per-file server-name reads, and
|
|
17
|
+
* `src/migrate.ts` is the package's only file-WRITING surface — the atomic,
|
|
18
|
+
* user-invoked `/mcp migrate` import into `$DSH_HOME/.mcp.json`.
|
|
19
|
+
*
|
|
20
|
+
* @module @dsh-cc/mcp-config
|
|
21
|
+
*/
|
|
22
|
+
import type { Config } from '@dsh-cc/mcp-client';
|
|
23
|
+
/**
|
|
24
|
+
* A `.mcp.json` server definition before transport normalization.
|
|
25
|
+
* `stdio` servers omit `type` (the Claude Code default) or set it explicitly;
|
|
26
|
+
* network servers set `http` / `sse` (or Claude Code's legacy `streamable-http`).
|
|
27
|
+
*/
|
|
28
|
+
export interface McpServerEntry {
|
|
29
|
+
/** `stdio` (default when absent), `http`, or `sse`. */
|
|
30
|
+
type?: 'stdio' | 'http' | 'sse' | 'streamable-http';
|
|
31
|
+
/** Stdio executable (required for the stdio transport). */
|
|
32
|
+
command?: string;
|
|
33
|
+
/** Arguments passed directly, without shell interpolation. */
|
|
34
|
+
args?: string[];
|
|
35
|
+
/** Extra env vars merged on top of scrubbed ambient env. */
|
|
36
|
+
env?: Record<string, string>;
|
|
37
|
+
/** Working directory for the child process. */
|
|
38
|
+
cwd?: string;
|
|
39
|
+
/** MCP endpoint URL (required for http/sse transports). */
|
|
40
|
+
url?: string;
|
|
41
|
+
/** Additional headers attached to MCP requests. */
|
|
42
|
+
headers?: Record<string, string>;
|
|
43
|
+
}
|
|
44
|
+
/** A `.mcp.json` document body: a map (or deduping array) of server definitions. */
|
|
45
|
+
export type McpConfigFile = {
|
|
46
|
+
mcpServers: Record<string, McpServerEntry>;
|
|
47
|
+
} | {
|
|
48
|
+
mcpServers: Array<Record<string, McpServerEntry>>;
|
|
49
|
+
};
|
|
50
|
+
/** A normalized mcp-client transport config, before the `serverName` is set. */
|
|
51
|
+
export type McpTransport = {
|
|
52
|
+
transport: 'stdio';
|
|
53
|
+
command: string;
|
|
54
|
+
args: string[];
|
|
55
|
+
env: Record<string, string>;
|
|
56
|
+
cwd: string;
|
|
57
|
+
toolCallTimeoutMs: number;
|
|
58
|
+
failOnStartupError: boolean;
|
|
59
|
+
} | {
|
|
60
|
+
transport: 'streamable-http' | 'sse';
|
|
61
|
+
url: string;
|
|
62
|
+
headers: Record<string, string>;
|
|
63
|
+
toolCallTimeoutMs: number;
|
|
64
|
+
failOnStartupError: boolean;
|
|
65
|
+
};
|
|
66
|
+
/** Named, validated, normalized server definition. */
|
|
67
|
+
export interface McpServerSpec {
|
|
68
|
+
/** Server name from the configuration key; becomes the mcp-client `serverName`. */
|
|
69
|
+
name: string;
|
|
70
|
+
/** Normalized client transport config (environment still unexpanded). */
|
|
71
|
+
entry: McpTransport;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Enterprise gate over the configured servers. `deny` runs first and wins; a
|
|
75
|
+
* name allowed by neither is dropped. Order is stable config order. Hooks must
|
|
76
|
+
* be synchronous and total; a throwing hook aborts the whole load.
|
|
77
|
+
*/
|
|
78
|
+
export interface McpConfigPolicy {
|
|
79
|
+
/** Deny a server by name/entry; returning `true` drops it. */
|
|
80
|
+
deny?: (name: string, entry: McpServerEntry) => boolean;
|
|
81
|
+
/** Allow a server by name/entry; returning `true` keeps it (after `deny`). */
|
|
82
|
+
allow?: (name: string, entry: McpServerEntry) => boolean;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolve one environment substitution in `value`: `${VAR}` requires the
|
|
86
|
+
* variable to be set (throwing otherwise) and `${VAR:-default}` falls back to
|
|
87
|
+
* `default` when the variable is unset or empty. Doubled `$$` escapes a literal
|
|
88
|
+
* `$`. Values with neither form are returned unchanged.
|
|
89
|
+
*
|
|
90
|
+
* @param value - raw string possibly containing `${VAR}` / `${VAR:-default}`.
|
|
91
|
+
* @param env - environment lookup; an unset key is `undefined`.
|
|
92
|
+
* @returns the fully expanded string.
|
|
93
|
+
*/
|
|
94
|
+
export declare function expandEnv(value: string, env: Record<string, string | undefined>): string;
|
|
95
|
+
/**
|
|
96
|
+
* Normalize a raw server entry into an mcp-client transport config, validating
|
|
97
|
+
* transport-specific required fields. Malformed entries throw at load.
|
|
98
|
+
*
|
|
99
|
+
* @param entry - raw `.mcp.json` server definition.
|
|
100
|
+
* @returns a normalized transport config (environment not yet expanded).
|
|
101
|
+
*/
|
|
102
|
+
export declare function normalizeServerEntry(entry: McpServerEntry): McpTransport;
|
|
103
|
+
/**
|
|
104
|
+
* Expand environment substitutions in a normalized server entry.
|
|
105
|
+
* @param entry - a normalized transport config before expansion.
|
|
106
|
+
* @param env - environment lookup; defaults to `process.env`.
|
|
107
|
+
* @returns a new config with every `${...}` substitution resolved.
|
|
108
|
+
*/
|
|
109
|
+
export declare function applyEnv(entry: McpTransport, env?: Record<string, string | undefined>): McpTransport;
|
|
110
|
+
/**
|
|
111
|
+
* Read and validate the `mcpServers` map from a `.mcp.json` body.
|
|
112
|
+
* @param body - parsed JSON document (or its `mcpServers` value).
|
|
113
|
+
* @returns the raw name→entry map, validated.
|
|
114
|
+
*/
|
|
115
|
+
export declare function parseMcpServers(body: unknown): Record<string, McpServerEntry>;
|
|
116
|
+
/**
|
|
117
|
+
* Normalize a parsed server map into a stable ordered list. The map form cannot
|
|
118
|
+
* contain duplicate keys, so this is primarily the revalidation/normalization
|
|
119
|
+
* pass that also feeds environment expansion.
|
|
120
|
+
* @param servers - validated name→entry map.
|
|
121
|
+
* @returns `[name, normalized spec]` pairs in stable order.
|
|
122
|
+
*/
|
|
123
|
+
export declare function dedupServers(servers: Record<string, McpServerEntry>): McpServerSpec[];
|
|
124
|
+
/**
|
|
125
|
+
* Collapse a Claude Code server name into the mcp-client `[A-Za-z0-9_-]{1,32}` tool-prefix contract.
|
|
126
|
+
*/
|
|
127
|
+
export declare function normalizeServerName(name: string): string;
|
|
128
|
+
/**
|
|
129
|
+
* Full pipeline: parse, validate, expand environment, dedupe, apply policy,
|
|
130
|
+
* and translate to mcp-client registrations. Malformed or unknown input throws
|
|
131
|
+
* at load; policy rejects are dropped silently in stable config order.
|
|
132
|
+
* @param body - parsed `.mcp.json` body.
|
|
133
|
+
* @param options - env lookups and enterprise policy.
|
|
134
|
+
* @returns accepted servers as mcp-client `Config` objects (with `serverName`).
|
|
135
|
+
*/
|
|
136
|
+
export declare function buildRegistrations(body: McpConfigFile, options?: {
|
|
137
|
+
env?: Record<string, string | undefined>;
|
|
138
|
+
policy?: McpConfigPolicy;
|
|
139
|
+
deferStartupConnect?: boolean;
|
|
140
|
+
}): Config[];
|
|
141
|
+
export type { Config };
|
|
142
|
+
export { claudeOnlyServers, readMcpServerNames, resolveDefaultMcpPaths, } from './paths.ts';
|
|
143
|
+
export type { ClaudeOnlySource, McpPathInputs, McpServerNames, ResolvedMcpPaths } from './paths.ts';
|
|
144
|
+
export { migrateMcpServers } from './migrate.ts';
|
|
145
|
+
export type { McpMigrationResult, McpMigrationSourceReport } from './migrate.ts';
|
|
146
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAEhD;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,uDAAuD;IACvD,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,iBAAiB,CAAA;IACnD,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,8DAA8D;IAC9D,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf,4DAA4D;IAC5D,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,+CAA+C;IAC/C,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,2DAA2D;IAC3D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AE,wBAAwB,EAAE,MAAM,cAAc,CAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP workspace-configuration loader (`@dsh-cc/mcp-config`).
|
|
3
|
+
*
|
|
4
|
+
* Parses a Claude Code-style `.mcp.json` document — the `mcpServers` map of
|
|
5
|
+
* stdio / HTTP(S) / SSE server definitions — validates it (malformed input
|
|
6
|
+
* throws at load), expands `${VAR}` and `${VAR:-default}` environment
|
|
7
|
+
* substitutions, dedupes server names, applies an enterprise allow/deny policy,
|
|
8
|
+
* and translates the accepted servers into `@dsh-cc/mcp-client`
|
|
9
|
+
* registrations ready for mount.
|
|
10
|
+
*
|
|
11
|
+
* The package owns the file→config *reading and validation* surface only; it
|
|
12
|
+
* performs no network I/O and mounts nothing itself. Consumers translate the
|
|
13
|
+
* returned registrations into mcp-client plugin instances.
|
|
14
|
+
*
|
|
15
|
+
* Two side modules extend that surface: `src/paths.ts` adds side-effect-free
|
|
16
|
+
* default-path classification and per-file server-name reads, and
|
|
17
|
+
* `src/migrate.ts` is the package's only file-WRITING surface — the atomic,
|
|
18
|
+
* user-invoked `/mcp migrate` import into `$DSH_HOME/.mcp.json`.
|
|
19
|
+
*
|
|
20
|
+
* @module @dsh-cc/mcp-config
|
|
21
|
+
*/
|
|
22
|
+
/** Whether a env lookup value counts as "set" (present and not the empty string). */
|
|
23
|
+
function isSet(value) {
|
|
24
|
+
return value !== undefined && value !== '';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolve one environment substitution in `value`: `${VAR}` requires the
|
|
28
|
+
* variable to be set (throwing otherwise) and `${VAR:-default}` falls back to
|
|
29
|
+
* `default` when the variable is unset or empty. Doubled `$$` escapes a literal
|
|
30
|
+
* `$`. Values with neither form are returned unchanged.
|
|
31
|
+
*
|
|
32
|
+
* @param value - raw string possibly containing `${VAR}` / `${VAR:-default}`.
|
|
33
|
+
* @param env - environment lookup; an unset key is `undefined`.
|
|
34
|
+
* @returns the fully expanded string.
|
|
35
|
+
*/
|
|
36
|
+
export function expandEnv(value, env) {
|
|
37
|
+
return value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}|\$\$/g, (match, name, fallback) => {
|
|
38
|
+
if (match === '$$')
|
|
39
|
+
return '$';
|
|
40
|
+
const key = name;
|
|
41
|
+
const raw = env[key];
|
|
42
|
+
if (isSet(raw))
|
|
43
|
+
return raw;
|
|
44
|
+
if (fallback !== undefined)
|
|
45
|
+
return fallback;
|
|
46
|
+
// A present-but-empty value with no fallback expands to the empty string
|
|
47
|
+
// rather than throwing; only a truly unset variable is an error.
|
|
48
|
+
if (raw === '')
|
|
49
|
+
return '';
|
|
50
|
+
throw new Error(`dsh-mcp-config: environment variable "${key}" is not set`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Normalize a raw server entry into an mcp-client transport config, validating
|
|
55
|
+
* transport-specific required fields. Malformed entries throw at load.
|
|
56
|
+
*
|
|
57
|
+
* @param entry - raw `.mcp.json` server definition.
|
|
58
|
+
* @returns a normalized transport config (environment not yet expanded).
|
|
59
|
+
*/
|
|
60
|
+
export function normalizeServerEntry(entry) {
|
|
61
|
+
// Widen to `string` so an at-runtime unknown transport (from a buggy or
|
|
62
|
+
// future `.mcp.json`) still reaches the throwing `default` below rather than
|
|
63
|
+
// being narrowed away by the closed union.
|
|
64
|
+
const type = entry.type;
|
|
65
|
+
switch (type) {
|
|
66
|
+
case undefined:
|
|
67
|
+
case 'stdio': {
|
|
68
|
+
if (typeof entry.command !== 'string' || entry.command.length === 0) {
|
|
69
|
+
throw new Error('dsh-mcp-config: stdio server requires a "command"');
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
transport: 'stdio',
|
|
73
|
+
command: entry.command,
|
|
74
|
+
args: entry.args ?? [],
|
|
75
|
+
env: entry.env ?? {},
|
|
76
|
+
cwd: entry.cwd ?? '',
|
|
77
|
+
toolCallTimeoutMs: 60_000,
|
|
78
|
+
failOnStartupError: true,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
case 'http':
|
|
82
|
+
case 'streamable-http':
|
|
83
|
+
return networkConfig('streamable-http', entry);
|
|
84
|
+
case 'sse':
|
|
85
|
+
return networkConfig('sse', entry);
|
|
86
|
+
default:
|
|
87
|
+
throw new Error(`dsh-mcp-config: unsupported MCP transport "${type}"`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Build a network transport config, requiring a valid `url`. */
|
|
91
|
+
function networkConfig(transport, entry) {
|
|
92
|
+
if (typeof entry.url !== 'string' || entry.url.length === 0) {
|
|
93
|
+
throw new Error(`dsh-mcp-config: ${transport} server requires a "url"`);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
transport,
|
|
97
|
+
url: entry.url,
|
|
98
|
+
headers: entry.headers ?? {},
|
|
99
|
+
toolCallTimeoutMs: 60_000,
|
|
100
|
+
failOnStartupError: true,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Expand every env substitution inside one normalized transport config. */
|
|
104
|
+
function expandConfig(config, env) {
|
|
105
|
+
if (config.transport === 'stdio') {
|
|
106
|
+
return {
|
|
107
|
+
...config,
|
|
108
|
+
command: expandEnv(config.command, env),
|
|
109
|
+
args: config.args.map(a => expandEnv(a, env)),
|
|
110
|
+
env: Object.fromEntries(Object.entries(config.env).map(([k, v]) => [k, expandEnv(v, env)])),
|
|
111
|
+
cwd: expandEnv(config.cwd, env),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
...config,
|
|
116
|
+
url: expandEnv(config.url, env),
|
|
117
|
+
headers: Object.fromEntries(Object.entries(config.headers).map(([k, v]) => [k, expandEnv(v, env)])),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Expand environment substitutions in a normalized server entry.
|
|
122
|
+
* @param entry - a normalized transport config before expansion.
|
|
123
|
+
* @param env - environment lookup; defaults to `process.env`.
|
|
124
|
+
* @returns a new config with every `${...}` substitution resolved.
|
|
125
|
+
*/
|
|
126
|
+
export function applyEnv(entry, env = process.env) {
|
|
127
|
+
return expandConfig(entry, env);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Read and validate the `mcpServers` map from a `.mcp.json` body.
|
|
131
|
+
* @param body - parsed JSON document (or its `mcpServers` value).
|
|
132
|
+
* @returns the raw name→entry map, validated.
|
|
133
|
+
*/
|
|
134
|
+
export function parseMcpServers(body) {
|
|
135
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) {
|
|
136
|
+
throw new Error('dsh-mcp-config: config body must be an object');
|
|
137
|
+
}
|
|
138
|
+
const servers = body.mcpServers;
|
|
139
|
+
if (servers === undefined)
|
|
140
|
+
throw new Error('dsh-mcp-config: config must declare "mcpServers"');
|
|
141
|
+
const map = {};
|
|
142
|
+
if (Array.isArray(servers)) {
|
|
143
|
+
for (const group of servers) {
|
|
144
|
+
if (group === null || typeof group !== 'object' || Array.isArray(group)) {
|
|
145
|
+
throw new Error('dsh-mcp-config: each mcpServers array entry must be a name→definition object');
|
|
146
|
+
}
|
|
147
|
+
for (const [name, entry] of Object.entries(group)) {
|
|
148
|
+
if (Object.hasOwn(map, name))
|
|
149
|
+
throw new Error(`dsh-mcp-config: duplicate server name "${name}"`);
|
|
150
|
+
map[name] = entry;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return map;
|
|
154
|
+
}
|
|
155
|
+
if (typeof servers !== 'object' || servers === null) {
|
|
156
|
+
throw new Error('dsh-mcp-config: "mcpServers" must be an object or array of objects');
|
|
157
|
+
}
|
|
158
|
+
Object.assign(map, servers);
|
|
159
|
+
return map;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Normalize a parsed server map into a stable ordered list. The map form cannot
|
|
163
|
+
* contain duplicate keys, so this is primarily the revalidation/normalization
|
|
164
|
+
* pass that also feeds environment expansion.
|
|
165
|
+
* @param servers - validated name→entry map.
|
|
166
|
+
* @returns `[name, normalized spec]` pairs in stable order.
|
|
167
|
+
*/
|
|
168
|
+
export function dedupServers(servers) {
|
|
169
|
+
const specs = [];
|
|
170
|
+
for (const [name, entry] of Object.entries(servers)) {
|
|
171
|
+
specs.push({ name, entry: normalizeServerEntry(entry) });
|
|
172
|
+
}
|
|
173
|
+
return specs;
|
|
174
|
+
}
|
|
175
|
+
/** Recover the original raw entry for policy hooks. */
|
|
176
|
+
function findOriginal(name, body) {
|
|
177
|
+
const servers = body.mcpServers;
|
|
178
|
+
if (Array.isArray(servers)) {
|
|
179
|
+
for (const group of servers)
|
|
180
|
+
if (Object.hasOwn(group, name))
|
|
181
|
+
return group[name];
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
return servers[name];
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Collapse a Claude Code server name into the mcp-client `[A-Za-z0-9_-]{1,32}` tool-prefix contract.
|
|
188
|
+
*/
|
|
189
|
+
export function normalizeServerName(name) {
|
|
190
|
+
const normalized = name.replace(/[^A-Za-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 32);
|
|
191
|
+
// An all-invalid-char name would collapse to '' — fall back to a dash so the pattern still matches.
|
|
192
|
+
return normalized === '' ? '-' : normalized;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Full pipeline: parse, validate, expand environment, dedupe, apply policy,
|
|
196
|
+
* and translate to mcp-client registrations. Malformed or unknown input throws
|
|
197
|
+
* at load; policy rejects are dropped silently in stable config order.
|
|
198
|
+
* @param body - parsed `.mcp.json` body.
|
|
199
|
+
* @param options - env lookups and enterprise policy.
|
|
200
|
+
* @returns accepted servers as mcp-client `Config` objects (with `serverName`).
|
|
201
|
+
*/
|
|
202
|
+
export function buildRegistrations(body, options = {}) {
|
|
203
|
+
const env = options.env ?? process.env;
|
|
204
|
+
const configs = [];
|
|
205
|
+
for (const { name, entry: raw } of dedupServers(parseMcpServers(body))) {
|
|
206
|
+
const original = findOriginal(name, body) ?? {};
|
|
207
|
+
if (options.policy?.deny?.(name, original) === true)
|
|
208
|
+
continue;
|
|
209
|
+
if (options.policy?.allow !== undefined && !options.policy.allow(name, original))
|
|
210
|
+
continue;
|
|
211
|
+
const expanded = expandConfig(raw, env);
|
|
212
|
+
// Policy hooks and findOriginal above see the ORIGINAL name; only the
|
|
213
|
+
// emitted mcp-client `serverName` (the tool-name prefix) is normalized.
|
|
214
|
+
// `deferStartupConnect` opts the caller into non-blocking mounts; because
|
|
215
|
+
// fail-fast startup forces fully blocking behavior in mcp-client, the
|
|
216
|
+
// deferral also downgrades the normalized `failOnStartupError: true` so
|
|
217
|
+
// the two flags never contradict each other.
|
|
218
|
+
configs.push({
|
|
219
|
+
...expanded,
|
|
220
|
+
serverName: normalizeServerName(name),
|
|
221
|
+
...(options.deferStartupConnect === true
|
|
222
|
+
? { deferStartupConnect: true, failOnStartupError: false }
|
|
223
|
+
: {}),
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return configs;
|
|
227
|
+
}
|
|
228
|
+
export { claudeOnlyServers, readMcpServerNames, resolveDefaultMcpPaths, } from "./paths.js";
|
|
229
|
+
export { migrateMcpServers } from "./migrate.js";
|
|
230
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAsEH,qFAAqF;AACrF,SAAS,KAAK,CAAC,KAAyB;IACtC,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,CAAA;AAC5C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa,EAAE,GAAuC;IAC9E,OAAO,KAAK,CAAC,OAAO,CAAC,oDAAoD,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,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AAC;YACX,GAAG,QAAQ;YACX,UAAU,EAAE,mBAAmB,CAAC,IAAI,CAAC;YACrC,GAAG,CAAC,OAAO,CAAC,mBAAmB,KAAK,IAAI;gBACtC,CAAC,CAAC,EAAE,mBAAmB,EAAE,IAAI,EAAE,kBAAkB,EAAE,KAAK,EAAE;gBAC1D,CAAC,CAAC,EAAE,CAAC;SACR,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC;AAID,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package-owned invariant companion for `@dsh-cc/mcp-config`.
|
|
3
|
+
* @module @dsh-cc/mcp-config/invariant
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
6
|
+
/** Cordis companion plugin name. */
|
|
7
|
+
export declare const name = "mcp-config-invariant";
|
|
8
|
+
/** Service required before the companion can reserve package ownership. */
|
|
9
|
+
export declare const inject: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Register this package's invariant companion.
|
|
12
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
13
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
14
|
+
*/
|
|
15
|
+
export declare const apply: (ctx: Context) => Promise<() => void>;
|
|
16
|
+
//# sourceMappingURL=invariant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariant.d.ts","sourceRoot":"","sources":["../src/invariant.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAKlD,oCAAoC;AACpC,eAAO,MAAM,IAAI,yBAAyB,CAAA;AAC1C,2EAA2E;AAC3E,eAAO,MAAM,MAAM,UAAiB,CAAA;AASpC;;;;GAIG;AACH,eAAO,MAAM,KAAK,GAAI,KAAK,OAAO,KAAG,OAAO,CAAC,MAAM,IAAI,CACU,CAAA"}
|
package/lib/invariant.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package-owned invariant companion for `@dsh-cc/mcp-config`.
|
|
3
|
+
* @module @dsh-cc/mcp-config/invariant
|
|
4
|
+
*/
|
|
5
|
+
const PACKAGE_NAME = '@dsh-cc/mcp-config';
|
|
6
|
+
/** Cordis companion plugin name. */
|
|
7
|
+
export const name = 'mcp-config-invariant';
|
|
8
|
+
/** Service required before the companion can reserve package ownership. */
|
|
9
|
+
export const inject = ['invariants'];
|
|
10
|
+
/**
|
|
11
|
+
* No runtime invariant: aside from `migrate.ts`'s atomic, user-invoked file
|
|
12
|
+
* write, mcp-config produces registrations and introduces no live registries
|
|
13
|
+
* of its own to reconcile.
|
|
14
|
+
*/
|
|
15
|
+
const install = () => { };
|
|
16
|
+
/**
|
|
17
|
+
* Register this package's invariant companion.
|
|
18
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
19
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
20
|
+
*/
|
|
21
|
+
export const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
22
|
+
/* jscpd:ignore-end */
|
|
23
|
+
//# sourceMappingURL=invariant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariant.js","sourceRoot":"","sources":["../src/invariant.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,MAAM,YAAY,GAAG,oBAAoB,CAAA;AAEzC,oCAAoC;AACpC,MAAM,CAAC,MAAM,IAAI,GAAG,sBAAsB,CAAA;AAC1C,2EAA2E;AAC3E,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,YAAY,CAAC,CAAA;AAEpC;;;;GAIG;AACH,MAAM,OAAO,GAAuB,GAAG,EAAE,GAAE,CAAC,CAAA;AAE5C;;;;GAIG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,GAAY,EAAuB,EAAE,CACzD,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAA;AACjE,sBAAsB"}
|
package/lib/migrate.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's ONLY file-WRITING surface: `/mcp migrate` importing Claude
|
|
3
|
+
* Code MCP servers into `$DSH_HOME/.mcp.json`.
|
|
4
|
+
*
|
|
5
|
+
* `migrateMcpServers` copies raw `mcpServers` entries verbatim — no env
|
|
6
|
+
* expansion, no name normalization, no transport reshaping (those belong to
|
|
7
|
+
* load time; round-tripping them here would corrupt the config). The Claude
|
|
8
|
+
* Code source files are never modified. Existing target names always win, the
|
|
9
|
+
* write is atomic (temp file + same-dir rename), and a `.bak` backup is made
|
|
10
|
+
* only when an existing target is overwritten. Note the backup duplicates any
|
|
11
|
+
* secrets inside `env` / `headers` entries.
|
|
12
|
+
*
|
|
13
|
+
* @module @dsh-cc/mcp-config/migrate
|
|
14
|
+
*/
|
|
15
|
+
/** Per-source migration outcome, in argument order. */
|
|
16
|
+
export interface McpMigrationSourceReport {
|
|
17
|
+
/** The source file path. */
|
|
18
|
+
path: string;
|
|
19
|
+
/** Server names this source contributed (empty when it failed). */
|
|
20
|
+
servers: string[];
|
|
21
|
+
/** Present when the source was unreadable/unparseable; its error message. */
|
|
22
|
+
error?: string;
|
|
23
|
+
}
|
|
24
|
+
/** Full result of one `migrateMcpServers` run. */
|
|
25
|
+
export interface McpMigrationResult {
|
|
26
|
+
/** The migration target file. */
|
|
27
|
+
target: string;
|
|
28
|
+
/** Server names written by this run. */
|
|
29
|
+
added: string[];
|
|
30
|
+
/** Names already present in the target (never overwritten). */
|
|
31
|
+
kept: string[];
|
|
32
|
+
/** Cross-source collisions: which source kept vs. skipped each name. */
|
|
33
|
+
sourceConflicts: {
|
|
34
|
+
name: string;
|
|
35
|
+
kept: string;
|
|
36
|
+
skipped: string;
|
|
37
|
+
}[];
|
|
38
|
+
/** Per-source reports, in argument order. */
|
|
39
|
+
sources: McpMigrationSourceReport[];
|
|
40
|
+
/** `false` when nothing needed adding: no write, no backup (idempotent). */
|
|
41
|
+
wrote: boolean;
|
|
42
|
+
/** Backup path, set only when an existing target was overwritten. */
|
|
43
|
+
backup?: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Import servers from source config files into a target `.mcp.json`.
|
|
47
|
+
*
|
|
48
|
+
* @param options - `sources` (Claude Code config files, read independently)
|
|
49
|
+
* and `target` (typically `$DSH_HOME/.mcp.json`).
|
|
50
|
+
* @returns the full migration report. When nothing needs adding the target is
|
|
51
|
+
* untouched and `wrote` is `false`.
|
|
52
|
+
* @throws when the existing target is unparseable or its `mcpServers` fails
|
|
53
|
+
* validation, or when the atomic write itself fails (the temp file is removed
|
|
54
|
+
* and the error names the target path).
|
|
55
|
+
*/
|
|
56
|
+
export declare function migrateMcpServers(options: {
|
|
57
|
+
sources: string[];
|
|
58
|
+
target: string;
|
|
59
|
+
}): McpMigrationResult;
|
|
60
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAMH,uDAAuD;AACvD,MAAM,WAAW,wBAAwB;IACvC,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,mEAAmE;IACnE,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,kDAAkD;AAClD,MAAM,WAAW,kBAAkB;IACjC,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,wCAAwC;IACxC,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,+DAA+D;IAC/D,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,wEAAwE;IACxE,eAAe,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;IAClE,6CAA6C;IAC7C,OAAO,EAAE,wBAAwB,EAAE,CAAA;IACnC,4EAA4E;IAC5E,KAAK,EAAE,OAAO,CAAA;IACd,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE;IAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,kBAAkB,CAkFpG"}
|
package/lib/migrate.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's ONLY file-WRITING surface: `/mcp migrate` importing Claude
|
|
3
|
+
* Code MCP servers into `$DSH_HOME/.mcp.json`.
|
|
4
|
+
*
|
|
5
|
+
* `migrateMcpServers` copies raw `mcpServers` entries verbatim — no env
|
|
6
|
+
* expansion, no name normalization, no transport reshaping (those belong to
|
|
7
|
+
* load time; round-tripping them here would corrupt the config). The Claude
|
|
8
|
+
* Code source files are never modified. Existing target names always win, the
|
|
9
|
+
* write is atomic (temp file + same-dir rename), and a `.bak` backup is made
|
|
10
|
+
* only when an existing target is overwritten. Note the backup duplicates any
|
|
11
|
+
* secrets inside `env` / `headers` entries.
|
|
12
|
+
*
|
|
13
|
+
* @module @dsh-cc/mcp-config/migrate
|
|
14
|
+
*/
|
|
15
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { dirname } from 'node:path';
|
|
17
|
+
import { parseMcpServers } from "./index.js";
|
|
18
|
+
/**
|
|
19
|
+
* Import servers from source config files into a target `.mcp.json`.
|
|
20
|
+
*
|
|
21
|
+
* @param options - `sources` (Claude Code config files, read independently)
|
|
22
|
+
* and `target` (typically `$DSH_HOME/.mcp.json`).
|
|
23
|
+
* @returns the full migration report. When nothing needs adding the target is
|
|
24
|
+
* untouched and `wrote` is `false`.
|
|
25
|
+
* @throws when the existing target is unparseable or its `mcpServers` fails
|
|
26
|
+
* validation, or when the atomic write itself fails (the temp file is removed
|
|
27
|
+
* and the error names the target path).
|
|
28
|
+
*/
|
|
29
|
+
export function migrateMcpServers(options) {
|
|
30
|
+
const { sources, target } = options;
|
|
31
|
+
// Target first: its existing names always win, and a broken target aborts
|
|
32
|
+
// with nothing written.
|
|
33
|
+
const targetExisted = existsSync(target);
|
|
34
|
+
const targetRoot = readTargetRoot(target);
|
|
35
|
+
const isMap = !Array.isArray(targetRoot.mcpServers);
|
|
36
|
+
const existing = targetRoot.mcpServers === undefined ? {} : parseMcpServers(targetRoot);
|
|
37
|
+
const written = new Map(Object.entries(existing));
|
|
38
|
+
const result = {
|
|
39
|
+
target,
|
|
40
|
+
added: [],
|
|
41
|
+
kept: [],
|
|
42
|
+
sourceConflicts: [],
|
|
43
|
+
sources: [],
|
|
44
|
+
wrote: false,
|
|
45
|
+
};
|
|
46
|
+
const addedEntries = [];
|
|
47
|
+
for (const path of sources) {
|
|
48
|
+
let entries;
|
|
49
|
+
try {
|
|
50
|
+
entries = parseMcpServers(JSON.parse(readFileSync(path, 'utf8')));
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
result.sources.push({ path, servers: [], error: error.message });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const names = [];
|
|
57
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
58
|
+
if (targetExisted && existing[name] !== undefined) {
|
|
59
|
+
if (!result.kept.includes(name))
|
|
60
|
+
result.kept.push(name);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (written.has(name)) {
|
|
64
|
+
// First source wins across sources; later declarations are skipped.
|
|
65
|
+
const keptBy = result.sources.find(report => report.servers.includes(name)).path;
|
|
66
|
+
result.sourceConflicts.push({ name, kept: keptBy, skipped: path });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
written.set(name, entry);
|
|
70
|
+
names.push(name);
|
|
71
|
+
addedEntries.push([name, entry]);
|
|
72
|
+
}
|
|
73
|
+
result.sources.push({ path, servers: names });
|
|
74
|
+
}
|
|
75
|
+
result.added = addedEntries.map(([name]) => name);
|
|
76
|
+
if (addedEntries.length === 0)
|
|
77
|
+
return result;
|
|
78
|
+
// Preserve the target's other top-level keys and its map-vs-array shape.
|
|
79
|
+
const output = { ...targetRoot };
|
|
80
|
+
if (isMap) {
|
|
81
|
+
output.mcpServers = { ...existing, ...Object.fromEntries(addedEntries) };
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Array form gets exactly ONE appended group object.
|
|
85
|
+
output.mcpServers = [...targetRoot.mcpServers, Object.fromEntries(addedEntries)];
|
|
86
|
+
}
|
|
87
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
88
|
+
let backup;
|
|
89
|
+
if (targetExisted) {
|
|
90
|
+
// The backup duplicates any secrets inside env/headers entries.
|
|
91
|
+
backup = `${target}.bak`;
|
|
92
|
+
copyFileSync(target, backup);
|
|
93
|
+
}
|
|
94
|
+
const temp = `${target}.tmp-${process.pid}`;
|
|
95
|
+
try {
|
|
96
|
+
writeFileSync(temp, JSON.stringify(output, null, 2) + '\n');
|
|
97
|
+
renameSync(temp, target);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
try {
|
|
101
|
+
unlinkSync(temp);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// The temp file may never have been created; nothing to clean up.
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`dsh-mcp-config: failed to write migrated config to "${target}": ${error.message}`);
|
|
107
|
+
}
|
|
108
|
+
result.wrote = true;
|
|
109
|
+
if (backup !== undefined)
|
|
110
|
+
result.backup = backup;
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Read and parse the target document, defaulting to `{ mcpServers: {} }` when
|
|
115
|
+
* absent. A present-but-unparseable target, or one whose `mcpServers` fails
|
|
116
|
+
* validation, throws with an actionable message before anything is written.
|
|
117
|
+
*/
|
|
118
|
+
function readTargetRoot(target) {
|
|
119
|
+
if (!existsSync(target))
|
|
120
|
+
return { mcpServers: {} };
|
|
121
|
+
let root;
|
|
122
|
+
try {
|
|
123
|
+
root = JSON.parse(readFileSync(target, 'utf8'));
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
throw new Error(`dsh-mcp-config: migration target "${target}" is not valid JSON: ${error.message}`);
|
|
127
|
+
}
|
|
128
|
+
if (root === null || typeof root !== 'object' || Array.isArray(root)) {
|
|
129
|
+
throw new Error(`dsh-mcp-config: migration target "${target}" must be a JSON object`);
|
|
130
|
+
}
|
|
131
|
+
const doc = root;
|
|
132
|
+
if (doc.mcpServers !== undefined) {
|
|
133
|
+
// Validate an existing mcpServers key (map or array form) before touching anything.
|
|
134
|
+
parseMcpServers(doc);
|
|
135
|
+
}
|
|
136
|
+
return root;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=migrate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAClH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,eAAe,EAAuB,MAAM,YAAY,CAAA;AA8BjE;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA8C;IAC9E,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAA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,EAAE,CAAA;IAClD,IAAI,IAAa,CAAA;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACjD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,qCAAqC,MAAM,wBAAyB,KAAe,CAAC,OAAO,EAAE,CAAC,CAAA;IAChH,CAAC;IACD,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACrE,MAAM,IAAI,KAAK,CAAC,qCAAqC,MAAM,yBAAyB,CAAC,CAAA;IACvF,CAAC;IACD,MAAM,GAAG,GAAG,IAAgC,CAAA;IAC5C,IAAI,GAAG,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QACjC,oFAAoF;QACpF,eAAe,CAAC,GAAG,CAAC,CAAA;IACtB,CAAC;IACD,OAAO,IAAgC,CAAA;AACzC,CAAC"}
|
package/lib/paths.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect-free path resolution and file-reading for MCP config discovery.
|
|
3
|
+
*
|
|
4
|
+
* `resolveDefaultMcpPaths` classifies the default `.mcp.json` locations into
|
|
5
|
+
* dsh-native, Claude Code, and migration-target buckets as a pure function of
|
|
6
|
+
* injectable `{ env, cwd, home }`. `readMcpServerNames` reads a single config
|
|
7
|
+
* file without ever throwing, and `claudeOnlyServers` reports the Claude Code
|
|
8
|
+
* server names not shadowed by a dsh-native file. Nothing in this module
|
|
9
|
+
* writes to disk; the package's only file-WRITING surface is `migrate.ts`.
|
|
10
|
+
*
|
|
11
|
+
* @module @dsh-cc/mcp-config/paths
|
|
12
|
+
*/
|
|
13
|
+
/** The default MCP config locations, classified by origin. */
|
|
14
|
+
export interface ResolvedMcpPaths {
|
|
15
|
+
/** dsh-native config files in discovery order (`<cwd>/.mcp.json`, `$DSH_HOME/.mcp.json`). */
|
|
16
|
+
dsh: string[];
|
|
17
|
+
/** Claude Code config files in discovery order (`<claudeDir>/.mcp.json`, `<home>/.claude.json`). */
|
|
18
|
+
claude: string[];
|
|
19
|
+
/** The migration target: `$DSH_HOME/.mcp.json` (default `~/.dsh/.mcp.json`). */
|
|
20
|
+
target: string;
|
|
21
|
+
}
|
|
22
|
+
/** Injectable process inputs for {@link resolveDefaultMcpPaths}; defaults read `process`/`os`. */
|
|
23
|
+
export interface McpPathInputs {
|
|
24
|
+
/** Environment lookup; defaults to `process.env`. */
|
|
25
|
+
env?: Record<string, string | undefined>;
|
|
26
|
+
/** Working directory anchoring the project `.mcp.json`; defaults to `process.cwd()`. */
|
|
27
|
+
cwd?: string;
|
|
28
|
+
/** User home directory; defaults to `os.homedir()`. */
|
|
29
|
+
home?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Classify the default MCP config paths. `claudeDir` honors
|
|
33
|
+
* `CLAUDE_CONFIG_DIR` exactly as Claude Code does, while the `~/.claude.json`
|
|
34
|
+
* state file stays anchored at the user home regardless of the config dir.
|
|
35
|
+
*
|
|
36
|
+
* @param inputs - injectable `{ env, cwd, home }`; pure when all are provided.
|
|
37
|
+
* @returns the dsh-native, Claude Code, and migration-target paths.
|
|
38
|
+
*/
|
|
39
|
+
export declare function resolveDefaultMcpPaths(inputs?: McpPathInputs): ResolvedMcpPaths;
|
|
40
|
+
/**
|
|
41
|
+
* The declared server names of one config file, without ever throwing.
|
|
42
|
+
* Names are the raw `mcpServers` keys in file order — never env-expanded or
|
|
43
|
+
* tool-name normalized.
|
|
44
|
+
*/
|
|
45
|
+
export type McpServerNames = {
|
|
46
|
+
kind: 'absent';
|
|
47
|
+
} | {
|
|
48
|
+
kind: 'invalid';
|
|
49
|
+
error: string;
|
|
50
|
+
} | {
|
|
51
|
+
kind: 'ok';
|
|
52
|
+
names: string[];
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Read the server names declared by one `.mcp.json` file.
|
|
56
|
+
*
|
|
57
|
+
* @param path - config file to read.
|
|
58
|
+
* @returns `absent` when the file does not exist, `invalid` with the error
|
|
59
|
+
* message when it is unreadable/unparseable/fails `mcpServers` validation,
|
|
60
|
+
* else `ok` with the raw declared names in order.
|
|
61
|
+
*/
|
|
62
|
+
export declare function readMcpServerNames(path: string): McpServerNames;
|
|
63
|
+
/** One Claude Code file's server names that no readable dsh file declares. */
|
|
64
|
+
export interface ClaudeOnlySource {
|
|
65
|
+
/** The Claude Code config file the names were read from. */
|
|
66
|
+
path: string;
|
|
67
|
+
/** Declared names not shadowed by any dsh-native file. */
|
|
68
|
+
names: string[];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Report, per Claude Code file, the server names that no dsh-native file
|
|
72
|
+
* declares — the servers a gating discovery would silently skip. Invalid or
|
|
73
|
+
* absent files on either side are skipped.
|
|
74
|
+
*
|
|
75
|
+
* @param paths - the dsh-native and Claude Code file paths.
|
|
76
|
+
* @returns one entry per Claude Code file that still declares unshadowed names.
|
|
77
|
+
*/
|
|
78
|
+
export declare function claudeOnlyServers(paths: Pick<ResolvedMcpPaths, 'dsh' | 'claude'>): ClaudeOnlySource[];
|
|
79
|
+
//# sourceMappingURL=paths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,8DAA8D;AAC9D,MAAM,WAAW,gBAAgB;IAC/B,6FAA6F;IAC7F,GAAG,EAAE,MAAM,EAAE,CAAA;IACb,oGAAoG;IACpG,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAA;CACf;AAED,kGAAkG;AAClG,MAAM,WAAW,aAAa;IAC5B,qDAAqD;IACrD,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;IACxC,wFAAwF;IACxF,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,GAAE,aAAkB,GAAG,gBAAgB,CAanF;AAED;;;;GAIG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAA;AAEnC;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAa/D;AAED,8EAA8E;AAC9E,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAA;IACZ,0DAA0D;IAC1D,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,GAAG,QAAQ,CAAC,GAAG,gBAAgB,EAAE,CAcrG"}
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect-free path resolution and file-reading for MCP config discovery.
|
|
3
|
+
*
|
|
4
|
+
* `resolveDefaultMcpPaths` classifies the default `.mcp.json` locations into
|
|
5
|
+
* dsh-native, Claude Code, and migration-target buckets as a pure function of
|
|
6
|
+
* injectable `{ env, cwd, home }`. `readMcpServerNames` reads a single config
|
|
7
|
+
* file without ever throwing, and `claudeOnlyServers` reports the Claude Code
|
|
8
|
+
* server names not shadowed by a dsh-native file. Nothing in this module
|
|
9
|
+
* writes to disk; the package's only file-WRITING surface is `migrate.ts`.
|
|
10
|
+
*
|
|
11
|
+
* @module @dsh-cc/mcp-config/paths
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from 'node:fs';
|
|
14
|
+
import { homedir } from 'node:os';
|
|
15
|
+
import { join, resolve } from 'node:path';
|
|
16
|
+
import { parseMcpServers } from "./index.js";
|
|
17
|
+
/**
|
|
18
|
+
* Classify the default MCP config paths. `claudeDir` honors
|
|
19
|
+
* `CLAUDE_CONFIG_DIR` exactly as Claude Code does, while the `~/.claude.json`
|
|
20
|
+
* state file stays anchored at the user home regardless of the config dir.
|
|
21
|
+
*
|
|
22
|
+
* @param inputs - injectable `{ env, cwd, home }`; pure when all are provided.
|
|
23
|
+
* @returns the dsh-native, Claude Code, and migration-target paths.
|
|
24
|
+
*/
|
|
25
|
+
export function resolveDefaultMcpPaths(inputs = {}) {
|
|
26
|
+
const env = inputs.env ?? process.env;
|
|
27
|
+
const cwd = inputs.cwd ?? process.cwd();
|
|
28
|
+
const home = inputs.home ?? homedir();
|
|
29
|
+
const dshHome = env.DSH_HOME ? resolve(env.DSH_HOME) : join(home, '.dsh');
|
|
30
|
+
const claudeDir = env.CLAUDE_CONFIG_DIR ? resolve(env.CLAUDE_CONFIG_DIR) : join(home, '.claude');
|
|
31
|
+
const projectFile = join(resolve(cwd), '.mcp.json');
|
|
32
|
+
const dshFile = join(dshHome, '.mcp.json');
|
|
33
|
+
return {
|
|
34
|
+
dsh: [projectFile, dshFile],
|
|
35
|
+
claude: [join(claudeDir, '.mcp.json'), join(home, '.claude.json')],
|
|
36
|
+
target: dshFile,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Read the server names declared by one `.mcp.json` file.
|
|
41
|
+
*
|
|
42
|
+
* @param path - config file to read.
|
|
43
|
+
* @returns `absent` when the file does not exist, `invalid` with the error
|
|
44
|
+
* message when it is unreadable/unparseable/fails `mcpServers` validation,
|
|
45
|
+
* else `ok` with the raw declared names in order.
|
|
46
|
+
*/
|
|
47
|
+
export function readMcpServerNames(path) {
|
|
48
|
+
let raw;
|
|
49
|
+
try {
|
|
50
|
+
raw = readFileSync(path, 'utf8');
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.code === 'ENOENT')
|
|
54
|
+
return { kind: 'absent' };
|
|
55
|
+
return { kind: 'invalid', error: error.message };
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return { kind: 'ok', names: Object.keys(parseMcpServers(JSON.parse(raw))) };
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return { kind: 'invalid', error: error.message };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Report, per Claude Code file, the server names that no dsh-native file
|
|
66
|
+
* declares — the servers a gating discovery would silently skip. Invalid or
|
|
67
|
+
* absent files on either side are skipped.
|
|
68
|
+
*
|
|
69
|
+
* @param paths - the dsh-native and Claude Code file paths.
|
|
70
|
+
* @returns one entry per Claude Code file that still declares unshadowed names.
|
|
71
|
+
*/
|
|
72
|
+
export function claudeOnlyServers(paths) {
|
|
73
|
+
const dshNames = new Set();
|
|
74
|
+
for (const file of paths.dsh) {
|
|
75
|
+
const names = readMcpServerNames(file);
|
|
76
|
+
if (names.kind === 'ok')
|
|
77
|
+
for (const name of names.names)
|
|
78
|
+
dshNames.add(name);
|
|
79
|
+
}
|
|
80
|
+
const result = [];
|
|
81
|
+
for (const file of paths.claude) {
|
|
82
|
+
const names = readMcpServerNames(file);
|
|
83
|
+
if (names.kind !== 'ok')
|
|
84
|
+
continue;
|
|
85
|
+
const only = names.names.filter(name => !dshNames.has(name));
|
|
86
|
+
if (only.length > 0)
|
|
87
|
+
result.push({ path: file, names: only });
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=paths.js.map
|
package/lib/paths.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.js","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AI,CAAC,CAAA;QACtC,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI;YAAE,SAAQ;QACjC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;QAC5D,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IAC/D,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dsh-cc/mcp-config",
|
|
3
|
+
"description": "MCP workspace-configuration loader: parses .mcp.json into mcp-client registrations",
|
|
4
|
+
"version": "0.5.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/dsh-cc/dsh-cc.git",
|
|
8
|
+
"directory": "packages/mcp/mcp-config"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./invariant": {
|
|
17
|
+
"types": "./lib/invariant.d.ts",
|
|
18
|
+
"default": "./lib/invariant.js"
|
|
19
|
+
},
|
|
20
|
+
"./src/*": "./src/*",
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"lib"
|
|
25
|
+
],
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@deepseek-ai/dsh-invariants": ">=0.1.1-rc.2",
|
|
29
|
+
"@deepseek-ai/cordis": ">=0.1.1-rc.2",
|
|
30
|
+
"@deepseek-ai/schemastery": "^3.18.1"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@dsh-cc/mcp-client": "^0.5.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@deepseek-ai/dsh-invariants": "link:../../../../deepseek-harness/packages/runtime-diagnostics/invariants",
|
|
37
|
+
"@deepseek-ai/cordis": "link:../../../../deepseek-harness/vendor/cordis",
|
|
38
|
+
"@deepseek-ai/schemastery": "link:../../../../deepseek-harness/vendor/schemastery",
|
|
39
|
+
"@dsh-cc/mcp-client": "^0.5.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
44
|
+
}
|