@webda/mcp 0.0.0-stage → 4.0.0-beta.4
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 +166 -0
- package/README.md +168 -2
- package/lib/auth.d.ts +27 -0
- package/lib/auth.d.ts.map +1 -0
- package/lib/auth.js +14 -0
- package/lib/auth.js.map +1 -0
- package/lib/bridge.d.ts +26 -0
- package/lib/bridge.d.ts.map +1 -0
- package/lib/bridge.js +72 -0
- package/lib/bridge.js.map +1 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -0
- package/lib/invoke.d.ts +108 -0
- package/lib/invoke.d.ts.map +1 -0
- package/lib/invoke.js +180 -0
- package/lib/invoke.js.map +1 -0
- package/lib/mcpservice.service.d.ts +194 -0
- package/lib/mcpservice.service.d.ts.map +1 -0
- package/lib/mcpservice.service.js +463 -0
- package/lib/mcpservice.service.js.map +1 -0
- package/lib/resources.d.ts +86 -0
- package/lib/resources.d.ts.map +1 -0
- package/lib/resources.js +125 -0
- package/lib/resources.js.map +1 -0
- package/lib/schema.d.ts +23 -0
- package/lib/schema.d.ts.map +1 -0
- package/lib/schema.js +69 -0
- package/lib/schema.js.map +1 -0
- package/lib/server.d.ts +54 -0
- package/lib/server.d.ts.map +1 -0
- package/lib/server.js +167 -0
- package/lib/server.js.map +1 -0
- package/lib/sessions.d.ts +78 -0
- package/lib/sessions.d.ts.map +1 -0
- package/lib/sessions.js +99 -0
- package/lib/sessions.js.map +1 -0
- package/lib/tools.d.ts +62 -0
- package/lib/tools.d.ts.map +1 -0
- package/lib/tools.js +126 -0
- package/lib/tools.js.map +1 -0
- package/package.json +47 -3
- package/webda.module.json +211 -0
package/lib/invoke.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { callOperation, SimpleOperationContext, WebdaError } from "@webda/core";
|
|
2
|
+
import { JSONUtils } from "@webda/utils";
|
|
3
|
+
import { useLog } from "@webda/workout";
|
|
4
|
+
/**
|
|
5
|
+
* Raised when an MCP client cancels a running operation
|
|
6
|
+
*/
|
|
7
|
+
export class CancelledError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Build the cancellation error
|
|
10
|
+
*/
|
|
11
|
+
constructor() {
|
|
12
|
+
super("Cancelled");
|
|
13
|
+
this.name = "CancelledError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Operation context used for MCP calls: collects AsyncGenerator chunks
|
|
18
|
+
* instead of concatenating them into the output, and stops on cancellation.
|
|
19
|
+
*/
|
|
20
|
+
export class McpOperationContext extends SimpleOperationContext {
|
|
21
|
+
/**
|
|
22
|
+
* Chunks yielded by a streaming operation
|
|
23
|
+
*/
|
|
24
|
+
chunks = [];
|
|
25
|
+
/**
|
|
26
|
+
* Abort signal of the MCP request
|
|
27
|
+
*/
|
|
28
|
+
signal;
|
|
29
|
+
/**
|
|
30
|
+
* Called for every streamed chunk with its 1-based index
|
|
31
|
+
*/
|
|
32
|
+
onChunk;
|
|
33
|
+
/**
|
|
34
|
+
* @param output - chunk or result
|
|
35
|
+
* @param encoding - unused for chunks
|
|
36
|
+
* @param cb - unused for chunks
|
|
37
|
+
* @returns true
|
|
38
|
+
*/
|
|
39
|
+
write(output, encoding, cb) {
|
|
40
|
+
if (!this.getExtension("operationStreaming")) {
|
|
41
|
+
return super.write(output, encoding, cb);
|
|
42
|
+
}
|
|
43
|
+
if (this.signal?.aborted) {
|
|
44
|
+
// Throwing ends callOperation's for-await loop, which calls the generator's return()
|
|
45
|
+
throw new CancelledError();
|
|
46
|
+
}
|
|
47
|
+
// Same public-audience filtering as OperationContext.write: `__` keys never leave the server
|
|
48
|
+
const chunk = output === undefined ? output : JSON.parse(JSONUtils.stringify(output, undefined, 0, true));
|
|
49
|
+
this.chunks.push(chunk);
|
|
50
|
+
try {
|
|
51
|
+
this.onChunk?.(chunk, this.chunks.length);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
useLog("WARN", "MCP progress notification failed", err);
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Build a context holding a session, for permission checks
|
|
61
|
+
* @param session - caller session
|
|
62
|
+
* @returns a context with that session
|
|
63
|
+
*/
|
|
64
|
+
export function sessionContext(session) {
|
|
65
|
+
const ctx = new SimpleOperationContext();
|
|
66
|
+
ctx.setSession(session);
|
|
67
|
+
return ctx;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Run an operation as the caller through core `callOperation`
|
|
71
|
+
* (permission, validation, events, audit and dispatch all apply)
|
|
72
|
+
* @param operationId - operation id
|
|
73
|
+
* @param options - session, input, cancellation and streaming hooks
|
|
74
|
+
* @returns the parsed result, or the chunks when the operation streamed
|
|
75
|
+
*/
|
|
76
|
+
export async function runOperation(operationId, options) {
|
|
77
|
+
if (options.signal?.aborted) {
|
|
78
|
+
throw new CancelledError();
|
|
79
|
+
}
|
|
80
|
+
const ctx = new McpOperationContext();
|
|
81
|
+
await ctx.init();
|
|
82
|
+
ctx.setSession(options.session);
|
|
83
|
+
ctx.setInput(Buffer.from(JSON.stringify(options.input ?? {})));
|
|
84
|
+
ctx.signal = options.signal;
|
|
85
|
+
ctx.onChunk = options.onChunk;
|
|
86
|
+
await callOperation(ctx, operationId);
|
|
87
|
+
if (ctx.getExtension("operationStreaming")) {
|
|
88
|
+
return { value: ctx.chunks, streamed: true };
|
|
89
|
+
}
|
|
90
|
+
const output = ctx.getOutput();
|
|
91
|
+
if (output === undefined || output === null || output === "") {
|
|
92
|
+
return { value: undefined, streamed: false };
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
return { value: JSON.parse(output), streamed: false };
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return { value: output, streamed: false };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Truncate a string to at most `maxBytes` UTF-8 bytes without cutting a character
|
|
103
|
+
* @param text - the text
|
|
104
|
+
* @param maxBytes - maximum number of bytes kept
|
|
105
|
+
* @returns the (possibly) truncated text, whether it was truncated and the original byte length
|
|
106
|
+
*/
|
|
107
|
+
export function truncateUtf8(text, maxBytes) {
|
|
108
|
+
const bytes = Buffer.from(text);
|
|
109
|
+
if (bytes.length <= maxBytes) {
|
|
110
|
+
return { text, truncated: false, bytes: bytes.length };
|
|
111
|
+
}
|
|
112
|
+
// Back off to a valid UTF-8 boundary so a cut multibyte character is dropped, not replaced
|
|
113
|
+
let end = Math.max(0, maxBytes);
|
|
114
|
+
while (end > 0 && (bytes[end] & 0xc0) === 0x80) {
|
|
115
|
+
end--;
|
|
116
|
+
}
|
|
117
|
+
return { text: bytes.subarray(0, end).toString(), truncated: true, bytes: bytes.length };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Convert an operation result into an MCP tool result.
|
|
121
|
+
*
|
|
122
|
+
* Tools declare no outputSchema (serialized models rarely match their schema exactly),
|
|
123
|
+
* so structuredContent is informative only: the object itself, `{ value }` for scalars
|
|
124
|
+
* and arrays, `{ items }` for streamed chunks, and nothing for void.
|
|
125
|
+
* @param _entry - the tool
|
|
126
|
+
* @param value - operation result
|
|
127
|
+
* @param streamed - whether value is the list of streamed chunks
|
|
128
|
+
* @param maxOutputBytes - truncation threshold for the text content
|
|
129
|
+
* @returns the tool result
|
|
130
|
+
*/
|
|
131
|
+
export function toToolResult(_entry, value, streamed, maxOutputBytes) {
|
|
132
|
+
let structured;
|
|
133
|
+
if (streamed) {
|
|
134
|
+
structured = { items: value };
|
|
135
|
+
}
|
|
136
|
+
else if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
137
|
+
structured = value;
|
|
138
|
+
}
|
|
139
|
+
else if (value !== undefined) {
|
|
140
|
+
structured = { value };
|
|
141
|
+
}
|
|
142
|
+
if (structured === undefined) {
|
|
143
|
+
return { content: [{ type: "text", text: "" }] };
|
|
144
|
+
}
|
|
145
|
+
const json = JSON.stringify(structured);
|
|
146
|
+
const cut = truncateUtf8(json, maxOutputBytes);
|
|
147
|
+
if (cut.truncated) {
|
|
148
|
+
return {
|
|
149
|
+
isError: true,
|
|
150
|
+
content: [
|
|
151
|
+
{
|
|
152
|
+
type: "text",
|
|
153
|
+
text: `${cut.text}\n[output truncated: ${cut.bytes} bytes exceeds maxOutputBytes ${maxOutputBytes}]`
|
|
154
|
+
}
|
|
155
|
+
]
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return { content: [{ type: "text", text: json }], structuredContent: structured };
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Convert an error into an MCP tool error result.
|
|
162
|
+
* Client errors (4xx) are returned so the agent can correct its input;
|
|
163
|
+
* anything else is logged and masked.
|
|
164
|
+
* @param err - the error
|
|
165
|
+
* @returns the tool error result
|
|
166
|
+
*/
|
|
167
|
+
export function errorToToolResult(err) {
|
|
168
|
+
if (err instanceof CancelledError) {
|
|
169
|
+
return { isError: true, content: [{ type: "text", text: "Cancelled" }] };
|
|
170
|
+
}
|
|
171
|
+
if (err instanceof WebdaError.CodeError) {
|
|
172
|
+
const code = err.getResponseCode();
|
|
173
|
+
if (code >= 400 && code < 500) {
|
|
174
|
+
return { isError: true, content: [{ type: "text", text: `${err.getCode()}: ${err.message}` }] };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
useLog("ERROR", "MCP tool call failed", err);
|
|
178
|
+
return { isError: true, content: [{ type: "text", text: "Internal error" }] };
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=invoke.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke.js","sourceRoot":"","sources":["../src/invoke.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAW,sBAAsB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzF,OAAO,EAAE,SAAS,EAAE,MAAM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,GAAG,GAAG,CAAC,eAAe,EAAE,CAAC;QACnC,IAAI,IAAI,IAAI,GAAG,IAAI,IAAI,GAAG,GAAG,EAAE,CAAC;YAC9B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,OAAO,EAAE,KAAK,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC;QAClG,CAAC;IACH,CAAC;IACD,MAAM,CAAC,OAAO,EAAE,sBAAsB,EAAE,GAAG,CAAC,CAAC;IAC7C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,EAAE,CAAC,EAAE,CAAC;AAChF,CAAC"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { OperationsTransport, OperationsTransportParameters, OperationDefinition, Session, WebContext } from "@webda/core";
|
|
2
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
3
|
+
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
4
|
+
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
|
|
5
|
+
import { Writable } from "node:stream";
|
|
6
|
+
import { McpAuthenticator } from "./auth.js";
|
|
7
|
+
import { ResourceRegistry } from "./resources.js";
|
|
8
|
+
import { McpSessionEntry, McpSessionManager } from "./sessions.js";
|
|
9
|
+
import { ToolRegistry } from "./tools.js";
|
|
10
|
+
/**
|
|
11
|
+
* Parameters for the MCP transport
|
|
12
|
+
*/
|
|
13
|
+
export declare class McpServiceParameters extends OperationsTransportParameters {
|
|
14
|
+
/**
|
|
15
|
+
* HTTP endpoint; false disables HTTP (stdio only)
|
|
16
|
+
* @default "/mcp"
|
|
17
|
+
*/
|
|
18
|
+
url?: string | false;
|
|
19
|
+
/**
|
|
20
|
+
* Models exposed as resources (short ids or "*"); false disables resources
|
|
21
|
+
* @default {"models":["*"]}
|
|
22
|
+
*/
|
|
23
|
+
resources?: false | {
|
|
24
|
+
models?: string[];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Seconds of inactivity before an MCP session is evicted
|
|
28
|
+
* @default 1800
|
|
29
|
+
*/
|
|
30
|
+
sessionTimeout?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Extra Origin values accepted besides the request's own origin
|
|
33
|
+
* @default []
|
|
34
|
+
*/
|
|
35
|
+
allowedOrigins?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* Hostnames (any port) accepted in the request Host, against DNS rebinding;
|
|
38
|
+
* "*" disables the check
|
|
39
|
+
* @default ["localhost","127.0.0.1","::1"]
|
|
40
|
+
*/
|
|
41
|
+
allowedHosts?: string[];
|
|
42
|
+
/**
|
|
43
|
+
* Maximum number of concurrent MCP HTTP sessions; at the limit the least
|
|
44
|
+
* recently used session is closed to make room for a new one
|
|
45
|
+
* @default 1000
|
|
46
|
+
*/
|
|
47
|
+
maxSessions?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Larger tool/resource outputs are truncated
|
|
50
|
+
* @default 1048576
|
|
51
|
+
*/
|
|
52
|
+
maxOutputBytes?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Server name announced to clients (defaults to the application package name)
|
|
55
|
+
*/
|
|
56
|
+
serverName?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Server version announced to clients (defaults to the application package version)
|
|
59
|
+
*/
|
|
60
|
+
serverVersion?: string;
|
|
61
|
+
/**
|
|
62
|
+
* stdio defaults
|
|
63
|
+
*/
|
|
64
|
+
stdio?: {
|
|
65
|
+
/**
|
|
66
|
+
* Default user for `webda mcp`
|
|
67
|
+
*/
|
|
68
|
+
user?: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Name of a service implementing McpAuthenticator; default uses the Webda session
|
|
72
|
+
*/
|
|
73
|
+
authenticator?: string;
|
|
74
|
+
/**
|
|
75
|
+
* @param params - raw parameters
|
|
76
|
+
* @returns this with defaults applied
|
|
77
|
+
*/
|
|
78
|
+
load(params?: any): this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Reserve stdout for the MCP protocol: every other write to `stdout` (log lines,
|
|
82
|
+
* whatever `--log-stream` says, unpatched console calls, third-party output) is
|
|
83
|
+
* sent to `stderr`, and the returned stream writes to the real stdout.
|
|
84
|
+
* @param stdout - stream carrying protocol frames
|
|
85
|
+
* @param stderr - stream receiving everything else
|
|
86
|
+
* @returns the protocol stream and a function restoring stdout
|
|
87
|
+
*/
|
|
88
|
+
export declare function reserveStdout(stdout?: NodeJS.WritableStream, stderr?: NodeJS.WritableStream): {
|
|
89
|
+
protocol: Writable;
|
|
90
|
+
restore: () => void;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* MCP transport: exposes operations as Model Context Protocol tools and
|
|
94
|
+
* models as resources, over Streamable HTTP (a Webda route) and stdio (`webda mcp`).
|
|
95
|
+
*
|
|
96
|
+
* @WebdaModda
|
|
97
|
+
*/
|
|
98
|
+
export declare class McpService<T extends McpServiceParameters = McpServiceParameters> extends OperationsTransport<T> {
|
|
99
|
+
protected tools: ToolRegistry;
|
|
100
|
+
protected resourceRegistry?: ResourceRegistry;
|
|
101
|
+
protected sessions: McpSessionManager;
|
|
102
|
+
protected fingerprint: string;
|
|
103
|
+
protected authenticator: McpAuthenticator;
|
|
104
|
+
protected evictTimer?: NodeJS.Timeout;
|
|
105
|
+
protected stdioServers: Server[];
|
|
106
|
+
/**
|
|
107
|
+
* Build typed parameters with defaults. Application replaces this with the
|
|
108
|
+
* webda.module.json `Configuration` class (the same McpServiceParameters);
|
|
109
|
+
* it is defined here so the service also works when registered by hand.
|
|
110
|
+
* @param params - raw parameters
|
|
111
|
+
* @returns typed parameters
|
|
112
|
+
*/
|
|
113
|
+
static createConfiguration(params?: any): McpServiceParameters;
|
|
114
|
+
/**
|
|
115
|
+
* Tools are built in bulk by refreshRegistries
|
|
116
|
+
* @param _operationId - unused
|
|
117
|
+
* @param _definition - unused
|
|
118
|
+
*/
|
|
119
|
+
exposeOperation(_operationId: string, _definition: OperationDefinition): void;
|
|
120
|
+
/**
|
|
121
|
+
* @returns this once the authenticator is resolved
|
|
122
|
+
*/
|
|
123
|
+
resolve(): this;
|
|
124
|
+
/**
|
|
125
|
+
* Build registries, mount the HTTP route and start session eviction
|
|
126
|
+
* @returns this
|
|
127
|
+
*/
|
|
128
|
+
init(): Promise<this>;
|
|
129
|
+
/**
|
|
130
|
+
* Rebuild tool and resource registries when operations changed
|
|
131
|
+
* @returns true when they were rebuilt
|
|
132
|
+
*/
|
|
133
|
+
refreshRegistries(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Detect runtime operation registration and notify open sessions
|
|
136
|
+
*/
|
|
137
|
+
checkForChanges(): void;
|
|
138
|
+
/**
|
|
139
|
+
* @param getSession - caller session resolution
|
|
140
|
+
* @returns a new SDK server over this service's registries
|
|
141
|
+
*/
|
|
142
|
+
createServer(getSession: (extra: {
|
|
143
|
+
authInfo?: AuthInfo;
|
|
144
|
+
}) => Session): Server;
|
|
145
|
+
/**
|
|
146
|
+
* Origin (protocol + host[:port]) of a request, from the `x-forwarded-host` header
|
|
147
|
+
* when present (HttpServer rejects forwarded headers from untrusted sources) or
|
|
148
|
+
* the `host` header. Parsed here rather than via HttpContext.getAbsoluteUrl,
|
|
149
|
+
* which cannot represent IPv6 literals like `[::1]:18080`.
|
|
150
|
+
* @param ctx - the request context
|
|
151
|
+
* @returns the parsed origin, or undefined when the host is missing or invalid
|
|
152
|
+
*/
|
|
153
|
+
protected requestOrigin(ctx: WebContext): URL | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* DNS-rebinding guard: the request hostname must be in allowedHosts
|
|
156
|
+
* @param ctx - the request context
|
|
157
|
+
* @returns true when the host is accepted
|
|
158
|
+
*/
|
|
159
|
+
protected isHostAllowed(ctx: WebContext): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Handle GET/POST/DELETE on the MCP endpoint
|
|
162
|
+
* @param ctx - the request context
|
|
163
|
+
*/
|
|
164
|
+
handleHttp(ctx: WebContext): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* Create an SDK server + transport pair for a new MCP session
|
|
167
|
+
* @param ctx - the initializing request
|
|
168
|
+
* @param userId - caller user id
|
|
169
|
+
* @param requestOrigin - origin of the request (see requestOrigin)
|
|
170
|
+
* @returns the (not yet registered) session entry; it registers on initialize
|
|
171
|
+
*/
|
|
172
|
+
protected createHttpSession(ctx: WebContext, userId: string | undefined, requestOrigin?: URL | undefined): Promise<McpSessionEntry>;
|
|
173
|
+
/**
|
|
174
|
+
* Serve the MCP tools over stdio until stdin closes
|
|
175
|
+
*
|
|
176
|
+
* Anyone able to start this command acts with `user`'s permissions
|
|
177
|
+
* (or anonymous permissions when no user is given).
|
|
178
|
+
*
|
|
179
|
+
* @param user - user id whose permissions apply
|
|
180
|
+
*/
|
|
181
|
+
mcp(user?: string): Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Connect a new MCP server to a transport, acting as `user`
|
|
184
|
+
* @param transport - stdio (or any) transport
|
|
185
|
+
* @param user - user id whose permissions apply; anonymous when empty
|
|
186
|
+
* @returns the connected server
|
|
187
|
+
*/
|
|
188
|
+
serveStdio(transport: Transport, user?: string): Promise<Server>;
|
|
189
|
+
/**
|
|
190
|
+
* Stop eviction and close every MCP session
|
|
191
|
+
*/
|
|
192
|
+
stop(): Promise<void>;
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=mcpservice.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcpservice.service.d.ts","sourceRoot":"","sources":["../src/mcpservice.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,mBAAmB,EACnB,6BAA6B,EAC7B,mBAAmB,EACnB,OAAO,EAGP,UAAU,EACX,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAA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