@cyanmycelium/mcp-broker 0.4.0 → 1.2.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/.mcp-broker.example/CONFIGURATION-EN.md +861 -0
- package/.mcp-broker.example/CONFIGURATION-FR.md +871 -0
- package/.mcp-broker.example/README.md +8 -3
- package/.mcp-broker.example/config.json +86 -0
- package/README.md +122 -3
- package/dist/bin.d.ts +0 -1
- package/dist/bin.js +180 -208
- package/dist/bin.js.map +1 -1
- package/dist/chunk-FTDKH2C4.js +3670 -0
- package/dist/chunk-FTDKH2C4.js.map +1 -0
- package/dist/{broker/grammars → grammars}/claude/en.json +1 -1
- package/dist/{broker/grammars → grammars}/claude/fr.json +1 -1
- package/dist/index.d.ts +1790 -18
- package/dist/index.js +2 -14
- package/dist/index.js.map +1 -1
- package/package.json +14 -8
- package/scripts/copy-assets.mjs +16 -10
- package/scripts/gen-cert.mjs +5 -5
- package/scripts/pack-mcpb.mjs +5 -5
- package/scripts/sign-bundle.mjs +6 -6
- package/src/auth/auth.config.ts +98 -0
- package/src/auth/auth.types.ts +120 -0
- package/src/auth/http.auth.ts +128 -0
- package/src/auth/index.ts +34 -0
- package/src/auth/jwt.validator.ts +63 -0
- package/src/auth/provider.auth.ts +114 -0
- package/src/auth/resource.metadata.ts +34 -0
- package/src/authorization/audit.ts +35 -0
- package/src/authorization/capability.classifier.ts +114 -0
- package/src/authorization/index.ts +37 -0
- package/src/authorization/policy.engine.ts +212 -0
- package/src/authorization/policy.types.ts +105 -0
- package/src/authorization/resource.path.ts +126 -0
- package/src/authorization/runtime.ts +56 -0
- package/src/authorization/slot.resource.ts +50 -0
- package/src/authorization/subject.mapper.ts +81 -0
- package/src/bin.ts +90 -7
- package/src/broker/adapters/broker.adapter.info.ts +3 -3
- package/src/broker/adapters/broker.adapter.providers.ts +3 -3
- package/src/broker/aggregate/aggregate.catalog.ts +49 -21
- package/src/broker/aggregate/aggregate.server.ts +149 -19
- package/src/broker/aggregate/provider.client.session.ts +22 -22
- package/src/broker/behaviors/broker.behavior.info.ts +4 -4
- package/src/broker/behaviors/broker.behavior.providers.ts +6 -6
- package/src/broker/broker.context.ts +10 -4
- package/src/broker/broker.grammars.ts +16 -13
- package/src/broker/broker.server.ts +12 -9
- package/src/broker/grammars/claude/en.json +1 -1
- package/src/broker/grammars/claude/fr.json +1 -1
- package/src/broker/index.ts +9 -9
- package/src/config.ts +81 -8
- package/src/index.ts +121 -20
- package/src/{mcpb.loader.ts → mcpb/mcpb.loader.ts} +24 -21
- package/src/{mcpb.unzip.ts → mcpb/mcpb.unzip.ts} +2 -2
- package/src/remote.transports.ts +11 -8
- package/src/remote.upstream.ts +11 -8
- package/src/stdio.upstream.ts +9 -6
- package/src/upstream.ts +6 -3
- package/src/ws/ws.interfaces.ts +357 -0
- package/src/{ws.tunnel.builder.ts → ws/ws.tunnel.builder.ts} +112 -9
- package/src/{ws.tunnel.ts → ws/ws.tunnel.ts} +646 -457
- package/web/README.md +94 -0
- package/web/assets/logo.png +0 -0
- package/web/broker-self-mcp.html +338 -0
- package/web/css/styles.css +580 -0
- package/web/demos/DemoPlaceholder.html +258 -0
- package/web/demos/broker-explorer/css/app.css +393 -0
- package/web/demos/broker-explorer/index.html +94 -0
- package/web/demos/broker-explorer/js/app.js +271 -0
- package/web/demos/broker-explorer/js/mcp-ws-client.js +132 -0
- package/web/demos/oauth-lab/README.md +94 -0
- package/web/demos/oauth-lab/config.json +117 -0
- package/web/demos/oauth-lab/css/app.css +1097 -0
- package/web/demos/oauth-lab/index.html +323 -0
- package/web/demos/oauth-lab/js/app.js +654 -0
- package/web/demos/oauth-lab/server/auth-server.mjs +426 -0
- package/web/demos/oauth-lab/server/factory-provider.mjs +269 -0
- package/web/demos/oauth-lab/server/smoke-test.mjs +308 -0
- package/web/demos/oauth-lab/server/start.mjs +106 -0
- package/web/demos/provider-tunnel/css/app.css +384 -0
- package/web/demos/provider-tunnel/index.html +99 -0
- package/web/demos/provider-tunnel/js/app.js +226 -0
- package/web/demos/provider-tunnel/js/toolbox-server.js +186 -0
- package/web/index.html +558 -0
- package/web/js/lib/broker-tunnel.js +173 -0
- package/dist/broker/adapters/broker.adapter.info.d.ts +0 -16
- package/dist/broker/adapters/broker.adapter.info.js +0 -43
- package/dist/broker/adapters/broker.adapter.info.js.map +0 -1
- package/dist/broker/adapters/broker.adapter.providers.d.ts +0 -18
- package/dist/broker/adapters/broker.adapter.providers.js +0 -61
- package/dist/broker/adapters/broker.adapter.providers.js.map +0 -1
- package/dist/broker/aggregate/aggregate.catalog.d.ts +0 -54
- package/dist/broker/aggregate/aggregate.catalog.js +0 -105
- package/dist/broker/aggregate/aggregate.catalog.js.map +0 -1
- package/dist/broker/aggregate/aggregate.server.d.ts +0 -47
- package/dist/broker/aggregate/aggregate.server.js +0 -151
- package/dist/broker/aggregate/aggregate.server.js.map +0 -1
- package/dist/broker/aggregate/provider.client.session.d.ts +0 -52
- package/dist/broker/aggregate/provider.client.session.js +0 -140
- package/dist/broker/aggregate/provider.client.session.js.map +0 -1
- package/dist/broker/behaviors/broker.behavior.info.d.ts +0 -15
- package/dist/broker/behaviors/broker.behavior.info.js +0 -41
- package/dist/broker/behaviors/broker.behavior.info.js.map +0 -1
- package/dist/broker/behaviors/broker.behavior.providers.d.ts +0 -19
- package/dist/broker/behaviors/broker.behavior.providers.js +0 -69
- package/dist/broker/behaviors/broker.behavior.providers.js.map +0 -1
- package/dist/broker/broker.context.d.ts +0 -59
- package/dist/broker/broker.context.js +0 -2
- package/dist/broker/broker.context.js.map +0 -1
- package/dist/broker/broker.grammars.d.ts +0 -130
- package/dist/broker/broker.grammars.js +0 -229
- package/dist/broker/broker.grammars.js.map +0 -1
- package/dist/broker/broker.server.d.ts +0 -66
- package/dist/broker/broker.server.js +0 -73
- package/dist/broker/broker.server.js.map +0 -1
- package/dist/broker/index.d.ts +0 -9
- package/dist/broker/index.js +0 -7
- package/dist/broker/index.js.map +0 -1
- package/dist/config.d.ts +0 -136
- package/dist/config.js +0 -61
- package/dist/config.js.map +0 -1
- package/dist/mcpb.loader.d.ts +0 -24
- package/dist/mcpb.loader.js +0 -161
- package/dist/mcpb.loader.js.map +0 -1
- package/dist/mcpb.unzip.d.ts +0 -6
- package/dist/mcpb.unzip.js +0 -95
- package/dist/mcpb.unzip.js.map +0 -1
- package/dist/remote.transports.d.ts +0 -16
- package/dist/remote.transports.js +0 -297
- package/dist/remote.transports.js.map +0 -1
- package/dist/remote.upstream.d.ts +0 -36
- package/dist/remote.upstream.js +0 -52
- package/dist/remote.upstream.js.map +0 -1
- package/dist/stdio.upstream.d.ts +0 -45
- package/dist/stdio.upstream.js +0 -85
- package/dist/stdio.upstream.js.map +0 -1
- package/dist/upstream.d.ts +0 -33
- package/dist/upstream.js +0 -2
- package/dist/upstream.js.map +0 -1
- package/dist/version.d.ts +0 -2
- package/dist/version.js +0 -9
- package/dist/version.js.map +0 -1
- package/dist/ws.tunnel.builder.d.ts +0 -139
- package/dist/ws.tunnel.builder.js +0 -205
- package/dist/ws.tunnel.builder.js.map +0 -1
- package/dist/ws.tunnel.d.ts +0 -373
- package/dist/ws.tunnel.js +0 -1090
- package/dist/ws.tunnel.js.map +0 -1
- /package/dist/{broker/grammars → grammars}/default/en.json +0 -0
- /package/dist/{broker/grammars → grammars}/default/fr.json +0 -0
- /package/dist/{broker/grammars → grammars}/default/zh.json +0 -0
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { McpGrammar } from "@cyanmycelium/mcp-core";
|
|
2
|
-
/**
|
|
3
|
-
* Locale identifier used to look up a grammar JSON file under
|
|
4
|
-
* `<userAgent>/<locale>.json`. Open string: a host application can use any
|
|
5
|
-
* value its grammar resources support.
|
|
6
|
-
*
|
|
7
|
-
* The broker registers each `(userAgent, locale)` pair found on disk as a
|
|
8
|
-
* separate `McpGrammar` keyed by {@link brokerGrammarKey}. The actual
|
|
9
|
-
* resolution of "which key to use for this session" is delegated to
|
|
10
|
-
* `@cyanmycelium/mcp-core@0.3.0`'s `grammarResolverFromOptions`, which
|
|
11
|
-
* handles BCP-47 narrowing (`fr-CA` → `fr` → `en`), agent-family fallback,
|
|
12
|
-
* and the optional version dimension natively.
|
|
13
|
-
*/
|
|
14
|
-
export type BrokerLocale = string;
|
|
15
|
-
/**
|
|
16
|
-
* User-agent family identifier used to look up a grammar JSON file under
|
|
17
|
-
* `<userAgent>/<locale>.json`. Open string. Conventional values follow
|
|
18
|
-
* the defaults emitted by `grammarResolverFromOptions`: `claude`, `gpt`,
|
|
19
|
-
* `mistral`, `copilot`, plus the universal `default`. Custom families
|
|
20
|
-
* are supported by passing a custom `agents` map in
|
|
21
|
-
* `StartBrokerServerOptions.grammarResolverOptions`.
|
|
22
|
-
*/
|
|
23
|
-
export type BrokerUserAgent = string;
|
|
24
|
-
/**
|
|
25
|
-
* Builds the canonical grammar key for the `(userAgent, locale, version?)`
|
|
26
|
-
* matrix the broker registers on disk.
|
|
27
|
-
*
|
|
28
|
-
* Pattern:
|
|
29
|
-
* - `"<userAgent>:<locale>"` (no version) — e.g. `"claude:fr"`, `"default:en"`
|
|
30
|
-
* - `"<userAgent>:<locale>@<version>"` (versioned) — e.g. `"claude:fr@v2"`
|
|
31
|
-
*
|
|
32
|
-
* The colon separator is reserved for the `<ua>:<locale>` composition; the
|
|
33
|
-
* `@` separator is reserved for the optional version suffix. Neither
|
|
34
|
-
* character is allowed inside the identifier segments. This matches the
|
|
35
|
-
* default `composeKey` of `grammarResolverFromOptions` exactly, so a
|
|
36
|
-
* broker-loaded grammar at `claude/fr@v2.json` is automatically picked up
|
|
37
|
-
* when a Claude session resolves to the `claude:fr@v2` candidate.
|
|
38
|
-
*/
|
|
39
|
-
export declare function brokerGrammarKey(userAgent: BrokerUserAgent, locale: BrokerLocale, version?: string): string;
|
|
40
|
-
/**
|
|
41
|
-
* Parses a grammar JSON filename of the form `<locale>.json` or
|
|
42
|
-
* `<locale>@<version>.json` (without the `.json` suffix) into its
|
|
43
|
-
* components. The first `@` (if any) separates locale from version; any
|
|
44
|
-
* additional `@` is folded into the version string.
|
|
45
|
-
*
|
|
46
|
-
* Returns `null` when the input cannot be split into a usable locale.
|
|
47
|
-
*/
|
|
48
|
-
export declare function parseBrokerGrammarStem(stem: string): {
|
|
49
|
-
locale: BrokerLocale;
|
|
50
|
-
version?: string;
|
|
51
|
-
} | null;
|
|
52
|
-
/**
|
|
53
|
-
* Loads and caches the grammar for a given `(userAgent, locale, version?)`
|
|
54
|
-
* combination. Returns `undefined` (instead of throwing) when the resource
|
|
55
|
-
* file is missing, so the caller can implement a fallback chain.
|
|
56
|
-
*
|
|
57
|
-
* Filename convention on disk:
|
|
58
|
-
* - `<userAgent>/<locale>.json` (no version)
|
|
59
|
-
* - `<userAgent>/<locale>@<version>.json` (versioned)
|
|
60
|
-
*/
|
|
61
|
-
export declare function loadBrokerGrammar(userAgent: BrokerUserAgent, locale: BrokerLocale, version?: string): McpGrammar | undefined;
|
|
62
|
-
/**
|
|
63
|
-
* Walks a grammars directory and yields every `(userAgent, locale)` pair
|
|
64
|
-
* found on disk. The directory must follow the layout
|
|
65
|
-
* `<dir>/<userAgent>/<locale>.json`.
|
|
66
|
-
*
|
|
67
|
-
* Used by the broker server at startup to bulk-register both the packaged
|
|
68
|
-
* grammars and any local overrides. No hard-coded list of supported
|
|
69
|
-
* user-agents or locales — adding a new grammar is dropping a JSON file.
|
|
70
|
-
*/
|
|
71
|
-
export interface BrokerGrammarEntry {
|
|
72
|
-
userAgent: BrokerUserAgent;
|
|
73
|
-
locale: BrokerLocale;
|
|
74
|
-
/** Set only for filenames carrying an `@<version>` suffix. */
|
|
75
|
-
version?: string;
|
|
76
|
-
/** Composed via {@link brokerGrammarKey} from the three segments above. */
|
|
77
|
-
key: string;
|
|
78
|
-
grammar: McpGrammar;
|
|
79
|
-
}
|
|
80
|
-
export declare function iterBrokerGrammarsFrom(grammarsDir: string): Generator<BrokerGrammarEntry>;
|
|
81
|
-
/**
|
|
82
|
-
* Walks the **packaged** grammars directory (the one shipped with the
|
|
83
|
-
* mcp-broker package). Equivalent to `iterBrokerGrammarsFrom(<packaged-dir>)`.
|
|
84
|
-
*
|
|
85
|
-
* For local user overrides, see {@link iterBrokerGrammarsFrom} with a custom
|
|
86
|
-
* directory — typically `.mcp-broker/grammars/` next to the config file.
|
|
87
|
-
*/
|
|
88
|
-
export declare function iterAvailableBrokerGrammars(): Generator<BrokerGrammarEntry>;
|
|
89
|
-
/**
|
|
90
|
-
* Returns the baseline grammar used by the broker behaviors as their
|
|
91
|
-
* source-of-truth for inline tool / property descriptions.
|
|
92
|
-
*
|
|
93
|
-
* Conventionally this is `default:en`. Session-specific grammars selected by
|
|
94
|
-
* the resolver override individual entries on top of this baseline.
|
|
95
|
-
*
|
|
96
|
-
* Throws if the JSON resource is missing — the broker behaviors cannot be
|
|
97
|
-
* built without baseline descriptions.
|
|
98
|
-
*/
|
|
99
|
-
export declare function brokerBaselineGrammar(): McpGrammar;
|
|
100
|
-
/**
|
|
101
|
-
* Convenience accessor for a baseline tool description. Throws when the
|
|
102
|
-
* tool is not listed in the baseline grammar — i.e. the JSON file is missing
|
|
103
|
-
* an entry for a tool the code knows about.
|
|
104
|
-
*/
|
|
105
|
-
export declare function brokerBaselineToolDescription(toolName: string): string;
|
|
106
|
-
/**
|
|
107
|
-
* Convenience accessor for a baseline property description. Throws when the
|
|
108
|
-
* property is not listed under the tool in the baseline grammar.
|
|
109
|
-
*/
|
|
110
|
-
export declare function brokerBaselinePropertyDescription(toolName: string, propertyName: string): string;
|
|
111
|
-
/**
|
|
112
|
-
* Convenience accessor for a baseline resource name. Throws when the resource
|
|
113
|
-
* URI has no entry in the baseline grammar.
|
|
114
|
-
*/
|
|
115
|
-
export declare function brokerBaselineResourceName(uri: string): string;
|
|
116
|
-
/**
|
|
117
|
-
* Convenience accessor for a baseline resource description. Throws when the
|
|
118
|
-
* resource URI has no entry in the baseline grammar.
|
|
119
|
-
*/
|
|
120
|
-
export declare function brokerBaselineResourceDescription(uri: string): string;
|
|
121
|
-
/**
|
|
122
|
-
* Convenience accessor for a baseline resource template name. Throws when the
|
|
123
|
-
* template URI has no entry in the baseline grammar.
|
|
124
|
-
*/
|
|
125
|
-
export declare function brokerBaselineResourceTemplateName(uriTemplate: string): string;
|
|
126
|
-
/**
|
|
127
|
-
* Convenience accessor for a baseline resource template description. Throws
|
|
128
|
-
* when the template URI has no entry in the baseline grammar.
|
|
129
|
-
*/
|
|
130
|
-
export declare function brokerBaselineResourceTemplateDescription(uriTemplate: string): string;
|
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { McpGrammar } from "@cyanmycelium/mcp-core";
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
// Canonical grammar key
|
|
7
|
-
// ---------------------------------------------------------------------------
|
|
8
|
-
/**
|
|
9
|
-
* Builds the canonical grammar key for the `(userAgent, locale, version?)`
|
|
10
|
-
* matrix the broker registers on disk.
|
|
11
|
-
*
|
|
12
|
-
* Pattern:
|
|
13
|
-
* - `"<userAgent>:<locale>"` (no version) — e.g. `"claude:fr"`, `"default:en"`
|
|
14
|
-
* - `"<userAgent>:<locale>@<version>"` (versioned) — e.g. `"claude:fr@v2"`
|
|
15
|
-
*
|
|
16
|
-
* The colon separator is reserved for the `<ua>:<locale>` composition; the
|
|
17
|
-
* `@` separator is reserved for the optional version suffix. Neither
|
|
18
|
-
* character is allowed inside the identifier segments. This matches the
|
|
19
|
-
* default `composeKey` of `grammarResolverFromOptions` exactly, so a
|
|
20
|
-
* broker-loaded grammar at `claude/fr@v2.json` is automatically picked up
|
|
21
|
-
* when a Claude session resolves to the `claude:fr@v2` candidate.
|
|
22
|
-
*/
|
|
23
|
-
export function brokerGrammarKey(userAgent, locale, version) {
|
|
24
|
-
const base = `${userAgent}:${locale}`;
|
|
25
|
-
return version ? `${base}@${version}` : base;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Parses a grammar JSON filename of the form `<locale>.json` or
|
|
29
|
-
* `<locale>@<version>.json` (without the `.json` suffix) into its
|
|
30
|
-
* components. The first `@` (if any) separates locale from version; any
|
|
31
|
-
* additional `@` is folded into the version string.
|
|
32
|
-
*
|
|
33
|
-
* Returns `null` when the input cannot be split into a usable locale.
|
|
34
|
-
*/
|
|
35
|
-
export function parseBrokerGrammarStem(stem) {
|
|
36
|
-
const at = stem.indexOf("@");
|
|
37
|
-
if (at < 0)
|
|
38
|
-
return stem.length > 0 ? { locale: stem } : null;
|
|
39
|
-
const locale = stem.slice(0, at);
|
|
40
|
-
const version = stem.slice(at + 1);
|
|
41
|
-
if (locale.length === 0 || version.length === 0)
|
|
42
|
-
return null;
|
|
43
|
-
return { locale, version };
|
|
44
|
-
}
|
|
45
|
-
// ---------------------------------------------------------------------------
|
|
46
|
-
// JSON resource loading
|
|
47
|
-
// ---------------------------------------------------------------------------
|
|
48
|
-
/**
|
|
49
|
-
* Absolute path of the directory holding the grammar JSON resources.
|
|
50
|
-
*
|
|
51
|
-
* Layout (one folder per user-agent family, one JSON file per locale):
|
|
52
|
-
* ```
|
|
53
|
-
* <GRAMMARS_DIR>/
|
|
54
|
-
* ├── default/
|
|
55
|
-
* │ ├── en.json
|
|
56
|
-
* │ ├── fr.json
|
|
57
|
-
* │ └── zh.json
|
|
58
|
-
* └── claude/
|
|
59
|
-
* ├── en.json
|
|
60
|
-
* └── fr.json
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* JSON files live alongside this module in `src/broker/grammars/` during
|
|
64
|
-
* development and are mirrored under `dist/broker/grammars/` at build time
|
|
65
|
-
* by `scripts/copy-assets.mjs`. Adding a new `(userAgent, locale)` pair is
|
|
66
|
-
* just a matter of dropping a new JSON file — no code change required.
|
|
67
|
-
*/
|
|
68
|
-
const GRAMMARS_DIR = join(dirname(fileURLToPath(import.meta.url)), "grammars");
|
|
69
|
-
const _cache = new Map();
|
|
70
|
-
/**
|
|
71
|
-
* Loads and caches the grammar for a given `(userAgent, locale, version?)`
|
|
72
|
-
* combination. Returns `undefined` (instead of throwing) when the resource
|
|
73
|
-
* file is missing, so the caller can implement a fallback chain.
|
|
74
|
-
*
|
|
75
|
-
* Filename convention on disk:
|
|
76
|
-
* - `<userAgent>/<locale>.json` (no version)
|
|
77
|
-
* - `<userAgent>/<locale>@<version>.json` (versioned)
|
|
78
|
-
*/
|
|
79
|
-
export function loadBrokerGrammar(userAgent, locale, version) {
|
|
80
|
-
const key = brokerGrammarKey(userAgent, locale, version);
|
|
81
|
-
const cached = _cache.get(key);
|
|
82
|
-
if (cached)
|
|
83
|
-
return cached;
|
|
84
|
-
const filename = version ? `${locale}@${version}.json` : `${locale}.json`;
|
|
85
|
-
const path = join(GRAMMARS_DIR, userAgent, filename);
|
|
86
|
-
if (!existsSync(path))
|
|
87
|
-
return undefined;
|
|
88
|
-
const raw = readFileSync(path, "utf-8");
|
|
89
|
-
const data = JSON.parse(raw);
|
|
90
|
-
const grammar = McpGrammar.fromJSON(data);
|
|
91
|
-
_cache.set(key, grammar);
|
|
92
|
-
return grammar;
|
|
93
|
-
}
|
|
94
|
-
export function* iterBrokerGrammarsFrom(grammarsDir) {
|
|
95
|
-
if (!existsSync(grammarsDir))
|
|
96
|
-
return;
|
|
97
|
-
const userAgents = readdirSync(grammarsDir).sort();
|
|
98
|
-
for (const userAgent of userAgents) {
|
|
99
|
-
const uaDir = join(grammarsDir, userAgent);
|
|
100
|
-
if (!statSync(uaDir).isDirectory())
|
|
101
|
-
continue;
|
|
102
|
-
const files = readdirSync(uaDir).sort();
|
|
103
|
-
for (const file of files) {
|
|
104
|
-
if (!file.endsWith(".json"))
|
|
105
|
-
continue;
|
|
106
|
-
const stem = file.slice(0, -".json".length);
|
|
107
|
-
const parsed = parseBrokerGrammarStem(stem);
|
|
108
|
-
if (!parsed) {
|
|
109
|
-
process.stderr.write(`[mcp-broker] Skipping unparseable grammar filename ${file} in ${uaDir}\n`);
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
const { locale, version } = parsed;
|
|
113
|
-
const path = join(uaDir, file);
|
|
114
|
-
try {
|
|
115
|
-
const raw = readFileSync(path, "utf-8");
|
|
116
|
-
const data = JSON.parse(raw);
|
|
117
|
-
const grammar = McpGrammar.fromJSON(data);
|
|
118
|
-
yield {
|
|
119
|
-
userAgent,
|
|
120
|
-
locale,
|
|
121
|
-
version,
|
|
122
|
-
key: brokerGrammarKey(userAgent, locale, version),
|
|
123
|
-
grammar,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
catch (err) {
|
|
127
|
-
process.stderr.write(`[mcp-broker] Failed to load grammar ${path}: ${err.message}\n`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Walks the **packaged** grammars directory (the one shipped with the
|
|
134
|
-
* mcp-broker package). Equivalent to `iterBrokerGrammarsFrom(<packaged-dir>)`.
|
|
135
|
-
*
|
|
136
|
-
* For local user overrides, see {@link iterBrokerGrammarsFrom} with a custom
|
|
137
|
-
* directory — typically `.mcp-broker/grammars/` next to the config file.
|
|
138
|
-
*/
|
|
139
|
-
export function* iterAvailableBrokerGrammars() {
|
|
140
|
-
yield* iterBrokerGrammarsFrom(GRAMMARS_DIR);
|
|
141
|
-
}
|
|
142
|
-
// ---------------------------------------------------------------------------
|
|
143
|
-
// Baseline helpers (used by behaviors to source their inline descriptions)
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
/**
|
|
146
|
-
* Returns the baseline grammar used by the broker behaviors as their
|
|
147
|
-
* source-of-truth for inline tool / property descriptions.
|
|
148
|
-
*
|
|
149
|
-
* Conventionally this is `default:en`. Session-specific grammars selected by
|
|
150
|
-
* the resolver override individual entries on top of this baseline.
|
|
151
|
-
*
|
|
152
|
-
* Throws if the JSON resource is missing — the broker behaviors cannot be
|
|
153
|
-
* built without baseline descriptions.
|
|
154
|
-
*/
|
|
155
|
-
export function brokerBaselineGrammar() {
|
|
156
|
-
const g = loadBrokerGrammar("default", "en");
|
|
157
|
-
if (!g) {
|
|
158
|
-
throw new Error(`Required baseline broker grammar "default:en" is missing — expected at ${join(GRAMMARS_DIR, "default", "en.json")}.`);
|
|
159
|
-
}
|
|
160
|
-
return g;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Convenience accessor for a baseline tool description. Throws when the
|
|
164
|
-
* tool is not listed in the baseline grammar — i.e. the JSON file is missing
|
|
165
|
-
* an entry for a tool the code knows about.
|
|
166
|
-
*/
|
|
167
|
-
export function brokerBaselineToolDescription(toolName) {
|
|
168
|
-
const desc = brokerBaselineGrammar().getToolDescription(toolName);
|
|
169
|
-
if (!desc) {
|
|
170
|
-
throw new Error(`Missing baseline description for tool "${toolName}" in default/en.json.`);
|
|
171
|
-
}
|
|
172
|
-
return desc;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Convenience accessor for a baseline property description. Throws when the
|
|
176
|
-
* property is not listed under the tool in the baseline grammar.
|
|
177
|
-
*/
|
|
178
|
-
export function brokerBaselinePropertyDescription(toolName, propertyName) {
|
|
179
|
-
const desc = brokerBaselineGrammar().getPropertyDescription(toolName, propertyName);
|
|
180
|
-
if (!desc) {
|
|
181
|
-
throw new Error(`Missing baseline description for property "${propertyName}" of tool "${toolName}" in default/en.json.`);
|
|
182
|
-
}
|
|
183
|
-
return desc;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Convenience accessor for a baseline resource name. Throws when the resource
|
|
187
|
-
* URI has no entry in the baseline grammar.
|
|
188
|
-
*/
|
|
189
|
-
export function brokerBaselineResourceName(uri) {
|
|
190
|
-
const name = brokerBaselineGrammar().getResourceName(uri);
|
|
191
|
-
if (!name) {
|
|
192
|
-
throw new Error(`Missing baseline name for resource "${uri}" in default/en.json.`);
|
|
193
|
-
}
|
|
194
|
-
return name;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Convenience accessor for a baseline resource description. Throws when the
|
|
198
|
-
* resource URI has no entry in the baseline grammar.
|
|
199
|
-
*/
|
|
200
|
-
export function brokerBaselineResourceDescription(uri) {
|
|
201
|
-
const desc = brokerBaselineGrammar().getResourceDescription(uri);
|
|
202
|
-
if (!desc) {
|
|
203
|
-
throw new Error(`Missing baseline description for resource "${uri}" in default/en.json.`);
|
|
204
|
-
}
|
|
205
|
-
return desc;
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Convenience accessor for a baseline resource template name. Throws when the
|
|
209
|
-
* template URI has no entry in the baseline grammar.
|
|
210
|
-
*/
|
|
211
|
-
export function brokerBaselineResourceTemplateName(uriTemplate) {
|
|
212
|
-
const name = brokerBaselineGrammar().getResourceTemplateName(uriTemplate);
|
|
213
|
-
if (!name) {
|
|
214
|
-
throw new Error(`Missing baseline name for resource template "${uriTemplate}" in default/en.json.`);
|
|
215
|
-
}
|
|
216
|
-
return name;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Convenience accessor for a baseline resource template description. Throws
|
|
220
|
-
* when the template URI has no entry in the baseline grammar.
|
|
221
|
-
*/
|
|
222
|
-
export function brokerBaselineResourceTemplateDescription(uriTemplate) {
|
|
223
|
-
const desc = brokerBaselineGrammar().getResourceTemplateDescription(uriTemplate);
|
|
224
|
-
if (!desc) {
|
|
225
|
-
throw new Error(`Missing baseline description for resource template "${uriTemplate}" in default/en.json.`);
|
|
226
|
-
}
|
|
227
|
-
return desc;
|
|
228
|
-
}
|
|
229
|
-
//# sourceMappingURL=broker.grammars.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"broker.grammars.js","sourceRoot":"","sources":["../../src/broker/broker.grammars.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EA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M,IAAI,KAAK,CAAC,uCAAuC,GAAG,uBAAuB,CAAC,CAAC;IACvF,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iCAAiC,CAAC,GAAW;IACzD,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;IACjE,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,8CAA8C,GAAG,uBAAuB,CAAC,CAAC;IAC9F,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kCAAkC,CAAC,WAAmB;IAClE,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC,uBAAuB,CAAC,WAAW,CAAC,CAAC;IAC1E,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,gDAAgD,WAAW,uBAAuB,CAAC,CAAC;IACxG,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,yCAAyC,CAAC,WAAmB;IACzE,MAAM,IAAI,GAAG,qBAAqB,EAAE,CAAC,8BAA8B,CAAC,WAAW,CAAC,CAAC;IACjF,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,uDAAuD,WAAW,uBAAuB,CAAC,CAAC;IAC/G,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { GrammarResolverOptions, IMcpServer, IMessageTransport } from "@cyanmycelium/mcp-core";
|
|
2
|
-
import type { BrokerContext } from "./broker.context.js";
|
|
3
|
-
/**
|
|
4
|
-
* Reserved provider slot name under which the broker exposes itself as an MCP
|
|
5
|
-
* server. Clients reach it via `<host>/_broker/mcp` (or any other client transport).
|
|
6
|
-
*
|
|
7
|
-
* Prefixed with `_` to make it unambiguously a system slot, and to reduce the
|
|
8
|
-
* chance of collision with user-supplied provider names.
|
|
9
|
-
*/
|
|
10
|
-
export declare const BROKER_PROVIDER_NAME = "_broker";
|
|
11
|
-
/**
|
|
12
|
-
* Optional knobs passed to {@link startBrokerServer}. Lets the embedder
|
|
13
|
-
* replace either resolver with custom logic without touching mcp-broker
|
|
14
|
-
* internals.
|
|
15
|
-
*/
|
|
16
|
-
export interface StartBrokerServerOptions {
|
|
17
|
-
/**
|
|
18
|
-
* Overrides for the built-in grammar resolver from `@cyanmycelium/mcp-core`.
|
|
19
|
-
*
|
|
20
|
-
* The broker installs sensible defaults: `localeSource` reads
|
|
21
|
-
* `process.env.MCP_BROKER_LOCALE`, the `agents` map uses the mcp-core
|
|
22
|
-
* defaults (`claude`, `gpt`, `mistral`, `copilot`, `default`), the
|
|
23
|
-
* narrowing chain is BCP-47-style, and `fallbackKey` is `default:en`
|
|
24
|
-
* so the baseline grammar always matches as last resort.
|
|
25
|
-
*
|
|
26
|
-
* Pass partial overrides here to inject a custom `localeSource` (e.g.
|
|
27
|
-
* pull from an HTTP header proxied by your transport), enable the
|
|
28
|
-
* `versionFrom` dimension, or extend the `agents` map with additional
|
|
29
|
-
* LLM families. Anything you omit keeps the broker default.
|
|
30
|
-
*/
|
|
31
|
-
grammarResolverOptions?: Partial<GrammarResolverOptions>;
|
|
32
|
-
/**
|
|
33
|
-
* Path to a user-supplied grammars directory whose `<userAgent>/<locale>.json`
|
|
34
|
-
* files are registered **in addition to** the packaged grammars.
|
|
35
|
-
*
|
|
36
|
-
* Both packaged and local entries are registered raw against the server
|
|
37
|
-
* via `withGrammar(brokerGrammarKey(ua, locale), grammar)`. The
|
|
38
|
-
* candidate-chain resolution implemented by `McpServer.initialize` in
|
|
39
|
-
* mcp-core@0.3.0 then walks the chain and merges the four layers
|
|
40
|
-
* (behavior, adapter, static, store) for the first matching key —
|
|
41
|
-
* the old hand-rolled pre-merge cascade is no longer needed.
|
|
42
|
-
*
|
|
43
|
-
* When `undefined` (default), only the packaged grammars are loaded.
|
|
44
|
-
*/
|
|
45
|
-
localGrammarsDir?: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Constructs the broker's own MCP server (the Tier-1 introspection behaviors)
|
|
49
|
-
* and returns the running server plus the loopback transport that must be
|
|
50
|
-
* registered against the {@link WsTunnel} as the `_broker` provider slot.
|
|
51
|
-
*
|
|
52
|
-
* Usage from {@link WsTunnel.start}:
|
|
53
|
-
* ```ts
|
|
54
|
-
* const { server, clientTransport } = await startBrokerServer(this, { ... });
|
|
55
|
-
* this._registerLoopbackProvider(BROKER_PROVIDER_NAME, clientTransport);
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @param context Read-only view of the broker's state.
|
|
59
|
-
* @param options Optional resolver overrides.
|
|
60
|
-
* @returns The running {@link IMcpServer} (call `.stop()` on shutdown) and the
|
|
61
|
-
* loopback transport to attach to the tunnel.
|
|
62
|
-
*/
|
|
63
|
-
export declare function startBrokerServer(context: BrokerContext, options?: StartBrokerServerOptions): Promise<{
|
|
64
|
-
server: IMcpServer;
|
|
65
|
-
clientTransport: IMessageTransport;
|
|
66
|
-
}>;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { McpServerBuilder, LoopbackTransport } from "@cyanmycelium/mcp-core";
|
|
2
|
-
import { BrokerInfoBehavior } from "./behaviors/broker.behavior.info.js";
|
|
3
|
-
import { BrokerProvidersBehavior } from "./behaviors/broker.behavior.providers.js";
|
|
4
|
-
import { iterAvailableBrokerGrammars, iterBrokerGrammarsFrom } from "./broker.grammars.js";
|
|
5
|
-
/**
|
|
6
|
-
* Reserved provider slot name under which the broker exposes itself as an MCP
|
|
7
|
-
* server. Clients reach it via `<host>/_broker/mcp` (or any other client transport).
|
|
8
|
-
*
|
|
9
|
-
* Prefixed with `_` to make it unambiguously a system slot, and to reduce the
|
|
10
|
-
* chance of collision with user-supplied provider names.
|
|
11
|
-
*/
|
|
12
|
-
export const BROKER_PROVIDER_NAME = "_broker";
|
|
13
|
-
/**
|
|
14
|
-
* Constructs the broker's own MCP server (the Tier-1 introspection behaviors)
|
|
15
|
-
* and returns the running server plus the loopback transport that must be
|
|
16
|
-
* registered against the {@link WsTunnel} as the `_broker` provider slot.
|
|
17
|
-
*
|
|
18
|
-
* Usage from {@link WsTunnel.start}:
|
|
19
|
-
* ```ts
|
|
20
|
-
* const { server, clientTransport } = await startBrokerServer(this, { ... });
|
|
21
|
-
* this._registerLoopbackProvider(BROKER_PROVIDER_NAME, clientTransport);
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @param context Read-only view of the broker's state.
|
|
25
|
-
* @param options Optional resolver overrides.
|
|
26
|
-
* @returns The running {@link IMcpServer} (call `.stop()` on shutdown) and the
|
|
27
|
-
* loopback transport to attach to the tunnel.
|
|
28
|
-
*/
|
|
29
|
-
export async function startBrokerServer(context, options = {}) {
|
|
30
|
-
const [serverEnd, clientEnd] = LoopbackTransport.createPair();
|
|
31
|
-
// Without an initializer, McpServerBuilder reports `version: "0.0.0"` in the
|
|
32
|
-
// `initialize` handshake. Supply the real package version from the context.
|
|
33
|
-
const builder = new McpServerBuilder()
|
|
34
|
-
.withName(BROKER_PROVIDER_NAME)
|
|
35
|
-
.withTransport(serverEnd)
|
|
36
|
-
.withInitializer({
|
|
37
|
-
initialize: () => ({
|
|
38
|
-
protocolVersion: "2024-11-05",
|
|
39
|
-
serverInfo: { name: BROKER_PROVIDER_NAME, version: context.version },
|
|
40
|
-
}),
|
|
41
|
-
})
|
|
42
|
-
.register(new BrokerInfoBehavior(context), new BrokerProvidersBehavior(context));
|
|
43
|
-
// Register every `(userAgent, locale)` JSON found on disk as a raw
|
|
44
|
-
// grammar layer. The candidate-chain resolution in
|
|
45
|
-
// McpServer.initialize (mcp-core@0.3.0) walks the resolver's chain
|
|
46
|
-
// and merges all matching layers — so partial user-agent files no
|
|
47
|
-
// longer need to be pre-merged with the default-locale baseline at
|
|
48
|
-
// boot. Local overrides come after packaged entries; identical keys
|
|
49
|
-
// get overlaid via the registry's last-write-wins.
|
|
50
|
-
for (const entry of iterAvailableBrokerGrammars()) {
|
|
51
|
-
builder.withGrammar(entry.key, entry.grammar);
|
|
52
|
-
}
|
|
53
|
-
if (options.localGrammarsDir) {
|
|
54
|
-
for (const entry of iterBrokerGrammarsFrom(options.localGrammarsDir)) {
|
|
55
|
-
builder.withGrammar(entry.key, entry.grammar);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// Wire the resolver via mcp-core's declarative helper. The broker
|
|
59
|
-
// ships sensible defaults (locale from MCP_BROKER_LOCALE env, agents
|
|
60
|
-
// from mcp-core's catalogue, BCP-47 narrowing, default:en fallback);
|
|
61
|
-
// anything the host application sets via `grammarResolverOptions`
|
|
62
|
-
// takes precedence.
|
|
63
|
-
builder.withGrammarResolver({
|
|
64
|
-
localeSource: () => process.env["MCP_BROKER_LOCALE"],
|
|
65
|
-
...(options.grammarResolverOptions ?? {}),
|
|
66
|
-
});
|
|
67
|
-
// No reconnect policy → the McpServer does not attempt to reopen the loopback
|
|
68
|
-
// when WsTunnel.stop() closes it. Clean shutdown.
|
|
69
|
-
const server = builder.build();
|
|
70
|
-
await server.start();
|
|
71
|
-
return { server, clientTransport: clientEnd };
|
|
72
|
-
}
|
|
73
|
-
//# sourceMappingURL=broker.server.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"broker.server.js","sourceRoot":"","sources":["../../src/broker/broker.server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAG3F;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,SAAS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AE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;QACpD,GAAG,CAAC,OAAO,CAAC,sBAAsB,IAAI,EAAE,CAAC;KAC5C,CAAC,CAAC;IAEH,8EAA8E;IAC9E,kDAAkD;IAClD,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IAE/B,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IAErB,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;AAClD,CAAC"}
|
package/dist/broker/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { BrokerInfoBehavior } from "./behaviors/broker.behavior.info.js";
|
|
2
|
-
export { BrokerProvidersBehavior } from "./behaviors/broker.behavior.providers.js";
|
|
3
|
-
export { BrokerInfoAdapter, BROKER_INFO_URI } from "./adapters/broker.adapter.info.js";
|
|
4
|
-
export { BrokerProvidersAdapter, PROVIDERS_URI, PROVIDER_URI_TEMPLATE } from "./adapters/broker.adapter.providers.js";
|
|
5
|
-
export { startBrokerServer, BROKER_PROVIDER_NAME } from "./broker.server.js";
|
|
6
|
-
export type { StartBrokerServerOptions } from "./broker.server.js";
|
|
7
|
-
export { brokerBaselineGrammar, brokerBaselinePropertyDescription, brokerBaselineResourceDescription, brokerBaselineResourceName, brokerBaselineResourceTemplateDescription, brokerBaselineResourceTemplateName, brokerBaselineToolDescription, brokerGrammarKey, iterAvailableBrokerGrammars, iterBrokerGrammarsFrom, loadBrokerGrammar, parseBrokerGrammarStem, } from "./broker.grammars.js";
|
|
8
|
-
export type { BrokerGrammarEntry, BrokerLocale, BrokerUserAgent } from "./broker.grammars.js";
|
|
9
|
-
export type { BrokerContext, BrokerProviderInfo, BrokerProviderTransport } from "./broker.context.js";
|
package/dist/broker/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { BrokerInfoBehavior } from "./behaviors/broker.behavior.info.js";
|
|
2
|
-
export { BrokerProvidersBehavior } from "./behaviors/broker.behavior.providers.js";
|
|
3
|
-
export { BrokerInfoAdapter, BROKER_INFO_URI } from "./adapters/broker.adapter.info.js";
|
|
4
|
-
export { BrokerProvidersAdapter, PROVIDERS_URI, PROVIDER_URI_TEMPLATE } from "./adapters/broker.adapter.providers.js";
|
|
5
|
-
export { startBrokerServer, BROKER_PROVIDER_NAME } from "./broker.server.js";
|
|
6
|
-
export { brokerBaselineGrammar, brokerBaselinePropertyDescription, brokerBaselineResourceDescription, brokerBaselineResourceName, brokerBaselineResourceTemplateDescription, brokerBaselineResourceTemplateName, brokerBaselineToolDescription, brokerGrammarKey, iterAvailableBrokerGrammars, iterBrokerGrammarsFrom, loadBrokerGrammar, parseBrokerGrammarStem, } from "./broker.grammars.js";
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
package/dist/broker/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/broker/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACvF,OAAO,EAAE,sBAAsB,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AACtH,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE7E,OAAO,EACH,qBAAqB,EACrB,iCAAiC,EACjC,iCAAiC,EACjC,0BAA0B,EAC1B,yCAAyC,EACzC,kCAAkC,EAClC,6BAA6B,EAC7B,gBAAgB,EAChB,2BAA2B,EAC3B,sBAAsB,EACtB,iBAAiB,EACjB,sBAAsB,GACzB,MAAM,sBAAsB,CAAC"}
|