@godscene/shared 1.7.11
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/README.md +9 -0
- package/dist/es/baseDB.mjs +109 -0
- package/dist/es/cli/cli-args.mjs +95 -0
- package/dist/es/cli/cli-error.mjs +24 -0
- package/dist/es/cli/cli-runner.mjs +122 -0
- package/dist/es/cli/index.mjs +4 -0
- package/dist/es/common.mjs +37 -0
- package/dist/es/constants/example-code.mjs +227 -0
- package/dist/es/constants/index.mjs +124 -0
- package/dist/es/env/basic.mjs +6 -0
- package/dist/es/env/constants.mjs +110 -0
- package/dist/es/env/global-config-manager.mjs +94 -0
- package/dist/es/env/helper.mjs +43 -0
- package/dist/es/env/index.mjs +5 -0
- package/dist/es/env/init-debug.mjs +18 -0
- package/dist/es/env/model-config-manager.mjs +79 -0
- package/dist/es/env/parse-model-config.mjs +165 -0
- package/dist/es/env/types.mjs +232 -0
- package/dist/es/env/utils.mjs +18 -0
- package/dist/es/extractor/constants.mjs +2 -0
- package/dist/es/extractor/cs_postmessage.mjs +61 -0
- package/dist/es/extractor/customLocator.mjs +641 -0
- package/dist/es/extractor/debug.mjs +6 -0
- package/dist/es/extractor/dom-util.mjs +96 -0
- package/dist/es/extractor/index.mjs +5 -0
- package/dist/es/extractor/locator.mjs +250 -0
- package/dist/es/extractor/tree.mjs +78 -0
- package/dist/es/extractor/util.mjs +245 -0
- package/dist/es/extractor/web-extractor.mjs +393 -0
- package/dist/es/img/box-select.mjs +824 -0
- package/dist/es/img/canvas-fallback.mjs +238 -0
- package/dist/es/img/get-photon.mjs +45 -0
- package/dist/es/img/get-sharp.mjs +11 -0
- package/dist/es/img/index.mjs +4 -0
- package/dist/es/img/info.mjs +35 -0
- package/dist/es/img/transform.mjs +275 -0
- package/dist/es/index.mjs +2 -0
- package/dist/es/key-alias-utils.mjs +19 -0
- package/dist/es/logger.mjs +64 -0
- package/dist/es/mcp/base-server.mjs +282 -0
- package/dist/es/mcp/base-tools.mjs +159 -0
- package/dist/es/mcp/chrome-path.mjs +35 -0
- package/dist/es/mcp/cli-report-session.mjs +78 -0
- package/dist/es/mcp/error-formatter.mjs +19 -0
- package/dist/es/mcp/index.mjs +9 -0
- package/dist/es/mcp/init-arg-utils.mjs +38 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
- package/dist/es/mcp/launcher-helper.mjs +52 -0
- package/dist/es/mcp/tool-generator.mjs +419 -0
- package/dist/es/mcp/types.mjs +3 -0
- package/dist/es/node/fs.mjs +44 -0
- package/dist/es/node/index.mjs +2 -0
- package/dist/es/node/port.mjs +24 -0
- package/dist/es/polyfills/async-hooks.mjs +2 -0
- package/dist/es/polyfills/index.mjs +1 -0
- package/dist/es/types/index.mjs +3 -0
- package/dist/es/us-keyboard-layout.mjs +1414 -0
- package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
- package/dist/es/utils.mjs +72 -0
- package/dist/es/zod-schema-utils.mjs +54 -0
- package/dist/lib/baseDB.js +149 -0
- package/dist/lib/cli/cli-args.js +138 -0
- package/dist/lib/cli/cli-error.js +61 -0
- package/dist/lib/cli/cli-runner.js +181 -0
- package/dist/lib/cli/index.js +53 -0
- package/dist/lib/common.js +93 -0
- package/dist/lib/constants/example-code.js +264 -0
- package/dist/lib/constants/index.js +221 -0
- package/dist/lib/env/basic.js +40 -0
- package/dist/lib/env/constants.js +153 -0
- package/dist/lib/env/global-config-manager.js +128 -0
- package/dist/lib/env/helper.js +80 -0
- package/dist/lib/env/index.js +90 -0
- package/dist/lib/env/init-debug.js +52 -0
- package/dist/lib/env/model-config-manager.js +113 -0
- package/dist/lib/env/parse-model-config.js +211 -0
- package/dist/lib/env/types.js +572 -0
- package/dist/lib/env/utils.js +61 -0
- package/dist/lib/extractor/constants.js +42 -0
- package/dist/lib/extractor/cs_postmessage.js +98 -0
- package/dist/lib/extractor/customLocator.js +693 -0
- package/dist/lib/extractor/debug.js +12 -0
- package/dist/lib/extractor/dom-util.js +157 -0
- package/dist/lib/extractor/index.js +87 -0
- package/dist/lib/extractor/locator.js +296 -0
- package/dist/lib/extractor/tree.js +124 -0
- package/dist/lib/extractor/util.js +336 -0
- package/dist/lib/extractor/web-extractor.js +442 -0
- package/dist/lib/img/box-select.js +875 -0
- package/dist/lib/img/canvas-fallback.js +305 -0
- package/dist/lib/img/get-photon.js +82 -0
- package/dist/lib/img/get-sharp.js +45 -0
- package/dist/lib/img/index.js +95 -0
- package/dist/lib/img/info.js +92 -0
- package/dist/lib/img/transform.js +364 -0
- package/dist/lib/index.js +36 -0
- package/dist/lib/key-alias-utils.js +62 -0
- package/dist/lib/logger.js +114 -0
- package/dist/lib/mcp/base-server.js +332 -0
- package/dist/lib/mcp/base-tools.js +193 -0
- package/dist/lib/mcp/chrome-path.js +72 -0
- package/dist/lib/mcp/cli-report-session.js +121 -0
- package/dist/lib/mcp/error-formatter.js +53 -0
- package/dist/lib/mcp/index.js +114 -0
- package/dist/lib/mcp/init-arg-utils.js +78 -0
- package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
- package/dist/lib/mcp/launcher-helper.js +86 -0
- package/dist/lib/mcp/tool-generator.js +456 -0
- package/dist/lib/mcp/types.js +40 -0
- package/dist/lib/node/fs.js +97 -0
- package/dist/lib/node/index.js +65 -0
- package/dist/lib/node/port.js +61 -0
- package/dist/lib/polyfills/async-hooks.js +36 -0
- package/dist/lib/polyfills/index.js +58 -0
- package/dist/lib/types/index.js +37 -0
- package/dist/lib/us-keyboard-layout.js +1457 -0
- package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
- package/dist/lib/utils.js +148 -0
- package/dist/lib/zod-schema-utils.js +97 -0
- package/dist/types/baseDB.d.ts +25 -0
- package/dist/types/cli/cli-args.d.ts +8 -0
- package/dist/types/cli/cli-error.d.ts +5 -0
- package/dist/types/cli/cli-runner.d.ts +19 -0
- package/dist/types/cli/index.d.ts +4 -0
- package/dist/types/common.d.ts +12 -0
- package/dist/types/constants/example-code.d.ts +2 -0
- package/dist/types/constants/index.d.ts +61 -0
- package/dist/types/env/basic.d.ts +6 -0
- package/dist/types/env/constants.d.ts +50 -0
- package/dist/types/env/global-config-manager.d.ts +32 -0
- package/dist/types/env/helper.d.ts +4 -0
- package/dist/types/env/index.d.ts +4 -0
- package/dist/types/env/init-debug.d.ts +1 -0
- package/dist/types/env/model-config-manager.d.ts +25 -0
- package/dist/types/env/parse-model-config.d.ts +31 -0
- package/dist/types/env/types.d.ts +339 -0
- package/dist/types/env/utils.d.ts +7 -0
- package/dist/types/extractor/constants.d.ts +1 -0
- package/dist/types/extractor/cs_postmessage.d.ts +2 -0
- package/dist/types/extractor/customLocator.d.ts +69 -0
- package/dist/types/extractor/debug.d.ts +1 -0
- package/dist/types/extractor/dom-util.d.ts +57 -0
- package/dist/types/extractor/index.d.ts +33 -0
- package/dist/types/extractor/locator.d.ts +9 -0
- package/dist/types/extractor/tree.d.ts +6 -0
- package/dist/types/extractor/util.d.ts +47 -0
- package/dist/types/extractor/web-extractor.d.ts +24 -0
- package/dist/types/img/box-select.d.ts +26 -0
- package/dist/types/img/canvas-fallback.d.ts +105 -0
- package/dist/types/img/get-photon.d.ts +19 -0
- package/dist/types/img/get-sharp.d.ts +3 -0
- package/dist/types/img/index.d.ts +3 -0
- package/dist/types/img/info.d.ts +34 -0
- package/dist/types/img/transform.d.ts +98 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/key-alias-utils.d.ts +9 -0
- package/dist/types/logger.d.ts +5 -0
- package/dist/types/mcp/base-server.d.ts +93 -0
- package/dist/types/mcp/base-tools.d.ts +148 -0
- package/dist/types/mcp/chrome-path.d.ts +2 -0
- package/dist/types/mcp/cli-report-session.d.ts +12 -0
- package/dist/types/mcp/error-formatter.d.ts +12 -0
- package/dist/types/mcp/index.d.ts +9 -0
- package/dist/types/mcp/init-arg-utils.d.ts +13 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
- package/dist/types/mcp/launcher-helper.d.ts +94 -0
- package/dist/types/mcp/tool-generator.d.ts +10 -0
- package/dist/types/mcp/types.d.ts +113 -0
- package/dist/types/node/fs.d.ts +15 -0
- package/dist/types/node/index.d.ts +2 -0
- package/dist/types/node/port.d.ts +8 -0
- package/dist/types/polyfills/async-hooks.d.ts +6 -0
- package/dist/types/polyfills/index.d.ts +4 -0
- package/dist/types/types/index.d.ts +36 -0
- package/dist/types/us-keyboard-layout.d.ts +32 -0
- package/dist/types/utils.d.ts +34 -0
- package/dist/types/zod-schema-utils.d.ts +23 -0
- package/package.json +125 -0
- package/src/baseDB.ts +158 -0
- package/src/cli/cli-args.ts +173 -0
- package/src/cli/cli-error.ts +24 -0
- package/src/cli/cli-runner.ts +230 -0
- package/src/cli/index.ts +4 -0
- package/src/common.ts +67 -0
- package/src/constants/example-code.ts +227 -0
- package/src/constants/index.ts +139 -0
- package/src/env/basic.ts +12 -0
- package/src/env/constants.ts +303 -0
- package/src/env/global-config-manager.ts +191 -0
- package/src/env/helper.ts +58 -0
- package/src/env/index.ts +4 -0
- package/src/env/init-debug.ts +34 -0
- package/src/env/model-config-manager.ts +149 -0
- package/src/env/parse-model-config.ts +357 -0
- package/src/env/types.ts +583 -0
- package/src/env/utils.ts +39 -0
- package/src/extractor/constants.ts +5 -0
- package/src/extractor/cs_postmessage.ts +136 -0
- package/src/extractor/customLocator.ts +1245 -0
- package/src/extractor/debug.ts +10 -0
- package/src/extractor/dom-util.ts +231 -0
- package/src/extractor/index.ts +50 -0
- package/src/extractor/locator.ts +469 -0
- package/src/extractor/tree.ts +179 -0
- package/src/extractor/util.ts +482 -0
- package/src/extractor/web-extractor.ts +617 -0
- package/src/img/box-select.ts +588 -0
- package/src/img/canvas-fallback.ts +393 -0
- package/src/img/get-photon.ts +108 -0
- package/src/img/get-sharp.ts +18 -0
- package/src/img/index.ts +27 -0
- package/src/img/info.ts +102 -0
- package/src/img/transform.ts +553 -0
- package/src/index.ts +1 -0
- package/src/key-alias-utils.ts +23 -0
- package/src/logger.ts +96 -0
- package/src/mcp/base-server.ts +500 -0
- package/src/mcp/base-tools.ts +391 -0
- package/src/mcp/chrome-path.ts +48 -0
- package/src/mcp/cli-report-session.ts +130 -0
- package/src/mcp/error-formatter.ts +52 -0
- package/src/mcp/index.ts +9 -0
- package/src/mcp/init-arg-utils.ts +105 -0
- package/src/mcp/inject-report-html-plugin.ts +119 -0
- package/src/mcp/launcher-helper.ts +200 -0
- package/src/mcp/tool-generator.ts +658 -0
- package/src/mcp/types.ts +131 -0
- package/src/node/fs.ts +84 -0
- package/src/node/index.ts +2 -0
- package/src/node/port.ts +37 -0
- package/src/polyfills/async-hooks.ts +6 -0
- package/src/polyfills/index.ts +4 -0
- package/src/types/index.ts +54 -0
- package/src/us-keyboard-layout.ts +723 -0
- package/src/utils.ts +149 -0
- package/src/zod-schema-utils.ts +133 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import { getKeyAliases, isRecord } from '../key-alias-utils';
|
|
3
|
+
import type { ToolSchema } from './types';
|
|
4
|
+
|
|
5
|
+
function readAliasedValue(
|
|
6
|
+
args: Record<string, unknown>,
|
|
7
|
+
key: string,
|
|
8
|
+
): unknown | undefined {
|
|
9
|
+
for (const alias of getKeyAliases(key)) {
|
|
10
|
+
if (alias in args) {
|
|
11
|
+
return args[alias];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readNamespacedArg(
|
|
19
|
+
args: Record<string, unknown>,
|
|
20
|
+
namespace: string,
|
|
21
|
+
key: string,
|
|
22
|
+
): unknown | undefined {
|
|
23
|
+
// Lookup order: namespace object first, then flat dotted form, then bare key
|
|
24
|
+
// fallback. Namespace-aware inputs win so multi-platform callers cannot be
|
|
25
|
+
// cross-contaminated by a top-level bare `deviceId` leaking into the wrong
|
|
26
|
+
// platform.
|
|
27
|
+
const namespacedArgs = readAliasedValue(args, namespace);
|
|
28
|
+
if (isRecord(namespacedArgs)) {
|
|
29
|
+
const nestedValue = readAliasedValue(namespacedArgs, key);
|
|
30
|
+
if (nestedValue !== undefined) {
|
|
31
|
+
return nestedValue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const dottedValue = readAliasedValue(args, `${namespace}.${key}`);
|
|
36
|
+
if (dottedValue !== undefined) {
|
|
37
|
+
return dottedValue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const directValue = readAliasedValue(args, key);
|
|
41
|
+
if (directValue !== undefined) {
|
|
42
|
+
return directValue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function extractNamespacedArgs<
|
|
49
|
+
TFieldName extends string,
|
|
50
|
+
TArgs extends Record<string, unknown> = Record<string, unknown>,
|
|
51
|
+
>(
|
|
52
|
+
args: Record<string, unknown>,
|
|
53
|
+
namespace: string,
|
|
54
|
+
keys: readonly TFieldName[],
|
|
55
|
+
): TArgs | undefined {
|
|
56
|
+
const extracted: Record<string, unknown> = {};
|
|
57
|
+
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
const value = readNamespacedArg(args, namespace, key);
|
|
60
|
+
if (value !== undefined) {
|
|
61
|
+
extracted[key] = value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return Object.keys(extracted).length > 0 ? (extracted as TArgs) : undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function sanitizeNamespacedArgs(
|
|
69
|
+
args: Record<string, unknown>,
|
|
70
|
+
namespace: string,
|
|
71
|
+
keys: readonly string[],
|
|
72
|
+
): Record<string, unknown> {
|
|
73
|
+
const excludedKeys = new Set<string>(getKeyAliases(namespace));
|
|
74
|
+
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
for (const alias of getKeyAliases(key)) {
|
|
77
|
+
excludedKeys.add(alias);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const alias of getKeyAliases(`${namespace}.${key}`)) {
|
|
81
|
+
excludedKeys.add(alias);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return Object.fromEntries(
|
|
86
|
+
Object.entries(args).filter(([key]) => !excludedKeys.has(key)),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Build a flat MCP tool schema whose keys are dotted `"<namespace>.<field>"`.
|
|
92
|
+
*
|
|
93
|
+
* We intentionally stay flat (rather than `{ namespace: z.object({...}) }`) so
|
|
94
|
+
* that CLI (`--android.device-id`), MCP clients, and `--help` output all share
|
|
95
|
+
* the same spelling. `readNamespacedArg` understands all three input shapes:
|
|
96
|
+
* nested namespace object, dotted flat key, and bare key fallback.
|
|
97
|
+
*/
|
|
98
|
+
export function createNamespacedInitArgSchema(
|
|
99
|
+
namespace: string,
|
|
100
|
+
shape: Record<string, z.ZodTypeAny>,
|
|
101
|
+
): ToolSchema {
|
|
102
|
+
return Object.fromEntries(
|
|
103
|
+
Object.entries(shape).map(([key, value]) => [`${namespace}.${key}`, value]),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const MAGIC_STRING = 'REPLACE_ME_WITH_REPORT_HTML';
|
|
5
|
+
const REPLACED_MARK = '/*REPORT_HTML_REPLACED*/';
|
|
6
|
+
const REG_EXP_FOR_REPLACE = /\/\*REPORT_HTML_REPLACED\*\/.*/;
|
|
7
|
+
|
|
8
|
+
interface RslibPluginApi {
|
|
9
|
+
onAfterBuild: (callback: () => void) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Rslib plugin to inject report HTML from @godscene/core dist into MCP bundle.
|
|
14
|
+
* This runs after build and reads the already-injected HTML from core.
|
|
15
|
+
*
|
|
16
|
+
* Prerequisites:
|
|
17
|
+
* - @godscene/report must be in devDependencies to ensure correct build order
|
|
18
|
+
* - @godscene/core dist must exist with injected HTML
|
|
19
|
+
*
|
|
20
|
+
* @param packageDir - The directory of the MCP package (use __dirname)
|
|
21
|
+
*/
|
|
22
|
+
export function injectReportHtmlFromCore(packageDir: string) {
|
|
23
|
+
return {
|
|
24
|
+
name: 'inject-report-html-from-core',
|
|
25
|
+
setup(api: RslibPluginApi) {
|
|
26
|
+
api.onAfterBuild(() => {
|
|
27
|
+
const coreUtilsPath = path.resolve(
|
|
28
|
+
packageDir,
|
|
29
|
+
'..',
|
|
30
|
+
'core',
|
|
31
|
+
'dist',
|
|
32
|
+
'lib',
|
|
33
|
+
'utils.js',
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
if (!fs.existsSync(coreUtilsPath)) {
|
|
37
|
+
console.warn(
|
|
38
|
+
'[inject-report-html] @godscene/core dist not found, skipping',
|
|
39
|
+
);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const coreContent = fs.readFileSync(coreUtilsPath, 'utf-8');
|
|
44
|
+
if (!coreContent.includes(REPLACED_MARK)) {
|
|
45
|
+
console.warn(
|
|
46
|
+
'[inject-report-html] HTML not found in core dist. Ensure report builds first.',
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Extract the JSON string after the marker
|
|
52
|
+
// JSON strings can contain escaped quotes, so we need to properly parse it
|
|
53
|
+
const markerIndex = coreContent.indexOf(REPLACED_MARK);
|
|
54
|
+
const jsonStart = markerIndex + REPLACED_MARK.length;
|
|
55
|
+
|
|
56
|
+
// Find the end of the JSON string by tracking quote escaping
|
|
57
|
+
let jsonEnd = jsonStart;
|
|
58
|
+
if (coreContent[jsonStart] === '"') {
|
|
59
|
+
jsonEnd = jsonStart + 1;
|
|
60
|
+
while (jsonEnd < coreContent.length) {
|
|
61
|
+
if (coreContent[jsonEnd] === '\\') {
|
|
62
|
+
jsonEnd += 2; // Skip escaped character
|
|
63
|
+
} else if (coreContent[jsonEnd] === '"') {
|
|
64
|
+
jsonEnd += 1; // Include closing quote
|
|
65
|
+
break;
|
|
66
|
+
} else {
|
|
67
|
+
jsonEnd += 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const jsonString = coreContent.slice(jsonStart, jsonEnd);
|
|
73
|
+
if (!jsonString || jsonString.length < 10) {
|
|
74
|
+
console.warn('[inject-report-html] Failed to extract HTML from core');
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const finalContent = `${REPLACED_MARK}${jsonString}`;
|
|
79
|
+
const distDir = path.join(packageDir, 'dist');
|
|
80
|
+
|
|
81
|
+
if (!fs.existsSync(distDir)) return;
|
|
82
|
+
|
|
83
|
+
const jsFiles = fs
|
|
84
|
+
.readdirSync(distDir)
|
|
85
|
+
.filter((f) => f.endsWith('.js'));
|
|
86
|
+
let injectedCount = 0;
|
|
87
|
+
|
|
88
|
+
for (const file of jsFiles) {
|
|
89
|
+
const filePath = path.join(distDir, file);
|
|
90
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
91
|
+
|
|
92
|
+
if (content.includes(REPLACED_MARK)) {
|
|
93
|
+
if (REG_EXP_FOR_REPLACE.test(content)) {
|
|
94
|
+
fs.writeFileSync(
|
|
95
|
+
filePath,
|
|
96
|
+
content.replace(REG_EXP_FOR_REPLACE, () => finalContent),
|
|
97
|
+
);
|
|
98
|
+
console.log(`[inject-report-html] Updated: ${file}`);
|
|
99
|
+
injectedCount++;
|
|
100
|
+
}
|
|
101
|
+
} else if (content.includes(`'${MAGIC_STRING}'`)) {
|
|
102
|
+
fs.writeFileSync(
|
|
103
|
+
filePath,
|
|
104
|
+
content.replace(`'${MAGIC_STRING}'`, () => finalContent),
|
|
105
|
+
);
|
|
106
|
+
console.log(`[inject-report-html] Injected: ${file}`);
|
|
107
|
+
injectedCount++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (injectedCount > 0) {
|
|
112
|
+
console.log(
|
|
113
|
+
`[inject-report-html] Completed: ${injectedCount} file(s)`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { BaseMCPServer } from './base-server';
|
|
2
|
+
import type { HttpLaunchOptions, LaunchMCPServerResult } from './base-server';
|
|
3
|
+
import type { IMidsceneTools } from './types';
|
|
4
|
+
|
|
5
|
+
export interface LaunchMCPServerOptions extends HttpLaunchOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to show server logs
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
verbose?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Generic agent type (avoid importing from @godscene/core to prevent circular deps)
|
|
15
|
+
*/
|
|
16
|
+
export interface GenericAgent<TDevice = any> {
|
|
17
|
+
interface: TDevice;
|
|
18
|
+
constructor: { name: string };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Additional information for logging server startup
|
|
23
|
+
*/
|
|
24
|
+
export interface StartupInfo {
|
|
25
|
+
port?: number;
|
|
26
|
+
host?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MCPServerLauncherConfig<
|
|
30
|
+
AgentType extends GenericAgent = GenericAgent,
|
|
31
|
+
ToolsManagerType extends IMidsceneTools = IMidsceneTools,
|
|
32
|
+
> {
|
|
33
|
+
agent: AgentType;
|
|
34
|
+
platformName: string;
|
|
35
|
+
ToolsManagerClass: new (...args: any[]) => ToolsManagerType;
|
|
36
|
+
MCPServerClass: new (toolsManager?: ToolsManagerType) => BaseMCPServer;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a generic MCP server launcher for a given agent, tools manager, and MCP server.
|
|
41
|
+
*
|
|
42
|
+
* This helper centralizes the common wiring logic used by platform-specific launchers:
|
|
43
|
+
* it constructs a tools manager, attaches the provided `agent` to it, then instantiates
|
|
44
|
+
* the `MCPServerClass` and exposes convenience methods to start the server over stdio
|
|
45
|
+
* (`launch`) or HTTP (`launchHttp`).
|
|
46
|
+
*
|
|
47
|
+
* Use this helper when adding a new platform-specific launcher or when you want to
|
|
48
|
+
* avoid duplicating boilerplate code for starting an MCP server. Typically, callers
|
|
49
|
+
* provide:
|
|
50
|
+
* - an `agent` instance that contains the underlying device on its `interface` property
|
|
51
|
+
* - a `ToolsManagerClass` that knows how to expose tools for that agent
|
|
52
|
+
* - an `MCPServerClass` that implements the MCP protocol and supports `launch` and
|
|
53
|
+
* `launchHttp` methods.
|
|
54
|
+
*
|
|
55
|
+
* The returned object has two methods:
|
|
56
|
+
* - `launch(options?)` to start the server using stdio transport
|
|
57
|
+
* - `launchHttp(options)` to start the server using HTTP transport
|
|
58
|
+
* Both methods accept a `verbose` flag to control console logging.
|
|
59
|
+
*
|
|
60
|
+
* @param config Configuration describing the agent, platform name (for logging),
|
|
61
|
+
* tools manager implementation, and MCP server implementation.
|
|
62
|
+
*
|
|
63
|
+
* @returns An object with `launch` and `launchHttp` methods to start the MCP server.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import { createMCPServerLauncher } from '@godscene/shared/mcp';
|
|
68
|
+
* import { Agent } from '@godscene/core/agent';
|
|
69
|
+
* import { WebMidsceneTools } from './web-tools';
|
|
70
|
+
* import { WebMCPServer } from './server';
|
|
71
|
+
*
|
|
72
|
+
* const agent = new Agent();
|
|
73
|
+
* const launcher = createMCPServerLauncher({
|
|
74
|
+
* agent,
|
|
75
|
+
* platformName: 'Web',
|
|
76
|
+
* ToolsManagerClass: WebMidsceneTools,
|
|
77
|
+
* MCPServerClass: WebMCPServer,
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* // Start with stdio
|
|
81
|
+
* await launcher.launch({ verbose: true });
|
|
82
|
+
*
|
|
83
|
+
* // Or start with HTTP
|
|
84
|
+
* await launcher.launchHttp({ port: 3000, host: 'localhost' });
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
export function createMCPServerLauncher<
|
|
90
|
+
AgentType extends GenericAgent,
|
|
91
|
+
ToolsManagerType extends IMidsceneTools,
|
|
92
|
+
>(config: MCPServerLauncherConfig<AgentType, ToolsManagerType>) {
|
|
93
|
+
const { agent, platformName, ToolsManagerClass, MCPServerClass } = config;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Validate that the agent has the required interface property
|
|
97
|
+
* @throws {Error} If agent.interface is missing
|
|
98
|
+
*/
|
|
99
|
+
function validateAgent(): void {
|
|
100
|
+
const device = agent.interface;
|
|
101
|
+
if (!device) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Agent must have an 'interface' property that references the underlying device.
|
|
104
|
+
Please ensure your agent instance is properly initialized with a device interface.
|
|
105
|
+
Expected: agent.interface to be defined, but got: ${typeof device}
|
|
106
|
+
Solution: Check that your agent constructor properly sets the interface property.`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Create and configure a tools manager with the agent
|
|
113
|
+
* @returns Configured tools manager instance
|
|
114
|
+
*/
|
|
115
|
+
function createToolsManager(): ToolsManagerType {
|
|
116
|
+
const toolsManager = new ToolsManagerClass();
|
|
117
|
+
// Type-safe agent injection: define explicit interface for tools manager with agent
|
|
118
|
+
interface ToolsManagerWithAgent extends IMidsceneTools {
|
|
119
|
+
agent: AgentType;
|
|
120
|
+
}
|
|
121
|
+
(toolsManager as unknown as ToolsManagerWithAgent).agent = agent;
|
|
122
|
+
return toolsManager;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Log server startup information
|
|
127
|
+
* @param mode - Transport mode ('stdio' or 'HTTP')
|
|
128
|
+
* @param additionalInfo - Additional info to log (e.g., port, host)
|
|
129
|
+
*/
|
|
130
|
+
function logStartupInfo(
|
|
131
|
+
mode: 'stdio' | 'HTTP',
|
|
132
|
+
additionalInfo?: StartupInfo,
|
|
133
|
+
): void {
|
|
134
|
+
const device = agent.interface;
|
|
135
|
+
console.log(`Starting Midscene ${platformName} MCP Server (${mode})...`);
|
|
136
|
+
console.log(`Agent: ${agent.constructor.name}`);
|
|
137
|
+
console.log(`Device: ${device.constructor.name}`);
|
|
138
|
+
|
|
139
|
+
if (additionalInfo?.port !== undefined) {
|
|
140
|
+
console.log(`Port: ${additionalInfo.port}`);
|
|
141
|
+
}
|
|
142
|
+
if (additionalInfo?.host) {
|
|
143
|
+
console.log(`Host: ${additionalInfo.host}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
/**
|
|
149
|
+
* Launch the MCP server with stdio transport
|
|
150
|
+
*/
|
|
151
|
+
async launch(
|
|
152
|
+
options: { verbose?: boolean } = {},
|
|
153
|
+
): Promise<LaunchMCPServerResult> {
|
|
154
|
+
const { verbose = true } = options;
|
|
155
|
+
|
|
156
|
+
validateAgent();
|
|
157
|
+
|
|
158
|
+
if (verbose) {
|
|
159
|
+
logStartupInfo('stdio');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const toolsManager = createToolsManager();
|
|
163
|
+
const server = new MCPServerClass(toolsManager);
|
|
164
|
+
const result = await server.launch();
|
|
165
|
+
|
|
166
|
+
if (verbose) {
|
|
167
|
+
console.log(`${platformName} MCP Server started (stdio mode)`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return result;
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Launch the MCP server with HTTP transport
|
|
175
|
+
*/
|
|
176
|
+
async launchHttp(
|
|
177
|
+
options: LaunchMCPServerOptions,
|
|
178
|
+
): Promise<LaunchMCPServerResult> {
|
|
179
|
+
const { port, host = 'localhost', verbose = true } = options;
|
|
180
|
+
|
|
181
|
+
validateAgent();
|
|
182
|
+
|
|
183
|
+
if (verbose) {
|
|
184
|
+
logStartupInfo('HTTP', { port, host });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const toolsManager = createToolsManager();
|
|
188
|
+
const server = new MCPServerClass(toolsManager);
|
|
189
|
+
const result = await server.launchHttp({ port, host });
|
|
190
|
+
|
|
191
|
+
if (verbose) {
|
|
192
|
+
console.log(
|
|
193
|
+
`${platformName} MCP Server started on http://${result.host}:${result.port}/mcp`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return result;
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|