@midscene/shared 1.0.1-beta-20251208112226.0 → 1.0.1-beta-20251209112631.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/dist/es/mcp/base-server.mjs +250 -0
- package/dist/es/mcp/base-tools.mjs +84 -0
- package/dist/es/mcp/index.mjs +5 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
- package/dist/es/mcp/tool-generator.mjs +207 -0
- package/dist/es/mcp/types.mjs +3 -0
- package/dist/es/node/fs.mjs +1 -1
- package/dist/es/zod-schema-utils.mjs +54 -0
- package/dist/lib/baseDB.js +2 -2
- package/dist/lib/build/copy-static.js +2 -2
- package/dist/lib/build/rspack-config.js +2 -2
- package/dist/lib/common.js +2 -2
- package/dist/lib/constants/example-code.js +2 -2
- package/dist/lib/constants/index.js +2 -2
- package/dist/lib/env/basic.js +2 -2
- package/dist/lib/env/constants.js +2 -2
- package/dist/lib/env/global-config-manager.js +2 -2
- package/dist/lib/env/helper.js +2 -2
- package/dist/lib/env/index.js +6 -6
- package/dist/lib/env/init-debug.js +2 -2
- package/dist/lib/env/model-config-manager.js +2 -2
- package/dist/lib/env/parse-model-config.js +2 -2
- package/dist/lib/env/types.js +2 -2
- package/dist/lib/env/utils.js +2 -2
- package/dist/lib/extractor/constants.js +2 -2
- package/dist/lib/extractor/debug.js +1 -1
- package/dist/lib/extractor/dom-util.js +2 -2
- package/dist/lib/extractor/index.js +2 -2
- package/dist/lib/extractor/locator.js +2 -2
- package/dist/lib/extractor/tree.js +2 -2
- package/dist/lib/extractor/util.js +2 -2
- package/dist/lib/extractor/web-extractor.js +2 -2
- package/dist/lib/img/box-select.js +2 -2
- package/dist/lib/img/draw-box.js +2 -2
- package/dist/lib/img/get-jimp.js +2 -2
- package/dist/lib/img/get-photon.js +2 -2
- package/dist/lib/img/get-sharp.js +2 -2
- package/dist/lib/img/index.js +2 -2
- package/dist/lib/img/info.js +2 -2
- package/dist/lib/img/transform.js +2 -2
- package/dist/lib/index.js +2 -2
- package/dist/lib/logger.js +2 -2
- package/dist/lib/mcp/base-server.js +300 -0
- package/dist/lib/mcp/base-tools.js +118 -0
- package/dist/lib/mcp/index.js +86 -0
- package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
- package/dist/lib/mcp/tool-generator.js +244 -0
- package/dist/lib/mcp/types.js +40 -0
- package/dist/lib/node/fs.js +3 -3
- package/dist/lib/node/index.js +2 -2
- package/dist/lib/polyfills/async-hooks.js +2 -2
- package/dist/lib/polyfills/index.js +2 -2
- package/dist/lib/types/index.js +2 -2
- package/dist/lib/us-keyboard-layout.js +2 -2
- package/dist/lib/utils.js +2 -2
- package/dist/lib/zod-schema-utils.js +97 -0
- package/dist/types/mcp/base-server.d.ts +77 -0
- package/dist/types/mcp/base-tools.d.ts +55 -0
- package/dist/types/mcp/index.d.ts +5 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
- package/dist/types/mcp/tool-generator.d.ts +11 -0
- package/dist/types/mcp/types.d.ts +100 -0
- package/dist/types/zod-schema-utils.d.ts +23 -0
- package/package.json +17 -3
- package/src/mcp/base-server.ts +435 -0
- package/src/mcp/base-tools.ts +196 -0
- package/src/mcp/index.ts +5 -0
- package/src/mcp/inject-report-html-plugin.ts +119 -0
- package/src/mcp/tool-generator.ts +330 -0
- package/src/mcp/types.ts +108 -0
- package/src/zod-schema-utils.ts +133 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import type { BaseAgent, BaseDevice, IMidsceneTools, ToolDefinition } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for platform-specific MCP tools
|
|
5
|
+
* Generic type TAgent allows subclasses to use their specific agent types
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class BaseMidsceneTools<TAgent extends BaseAgent = BaseAgent> implements IMidsceneTools {
|
|
8
|
+
protected mcpServer?: McpServer;
|
|
9
|
+
protected agent?: TAgent;
|
|
10
|
+
protected toolDefinitions: ToolDefinition[];
|
|
11
|
+
/**
|
|
12
|
+
* Ensure agent is initialized and ready for use.
|
|
13
|
+
* Must be implemented by subclasses to create platform-specific agent.
|
|
14
|
+
* @param initParam Optional initialization parameter (platform-specific, e.g., URL, device ID)
|
|
15
|
+
* @returns Promise resolving to initialized agent instance
|
|
16
|
+
* @throws Error if agent initialization fails
|
|
17
|
+
*/
|
|
18
|
+
protected abstract ensureAgent(initParam?: string): Promise<TAgent>;
|
|
19
|
+
/**
|
|
20
|
+
* Optional: prepare platform-specific tools (e.g., device connection)
|
|
21
|
+
*/
|
|
22
|
+
protected preparePlatformTools(): ToolDefinition[];
|
|
23
|
+
/**
|
|
24
|
+
* Must be implemented by subclasses to create a temporary device instance
|
|
25
|
+
* This allows getting real actionSpace without connecting to device
|
|
26
|
+
*/
|
|
27
|
+
protected abstract createTemporaryDevice(): BaseDevice;
|
|
28
|
+
/**
|
|
29
|
+
* Initialize all tools by querying actionSpace
|
|
30
|
+
* Uses two-layer fallback strategy:
|
|
31
|
+
* 1. Try to get actionSpace from connected agent (if available)
|
|
32
|
+
* 2. Create temporary device instance to read actionSpace (always succeeds)
|
|
33
|
+
*/
|
|
34
|
+
initTools(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Attach to MCP server and register all tools
|
|
37
|
+
*/
|
|
38
|
+
attachToServer(server: McpServer): void;
|
|
39
|
+
/**
|
|
40
|
+
* Wrapper for auto-destroy behavior
|
|
41
|
+
*/
|
|
42
|
+
private toolWithAutoDestroy;
|
|
43
|
+
/**
|
|
44
|
+
* Cleanup method - destroy agent and release resources
|
|
45
|
+
*/
|
|
46
|
+
closeBrowser(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Helper: Convert base64 screenshot to image content array
|
|
49
|
+
*/
|
|
50
|
+
protected buildScreenshotContent(screenshot: string): {
|
|
51
|
+
type: "image";
|
|
52
|
+
data: string;
|
|
53
|
+
mimeType: string;
|
|
54
|
+
}[];
|
|
55
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface RslibPluginApi {
|
|
2
|
+
onAfterBuild: (callback: () => void) => void;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Rslib plugin to inject report HTML from @midscene/core dist into MCP bundle.
|
|
6
|
+
* This runs after build and reads the already-injected HTML from core.
|
|
7
|
+
*
|
|
8
|
+
* Prerequisites:
|
|
9
|
+
* - @midscene/report must be in devDependencies to ensure correct build order
|
|
10
|
+
* - @midscene/core dist must exist with injected HTML
|
|
11
|
+
*
|
|
12
|
+
* @param packageDir - The directory of the MCP package (use __dirname)
|
|
13
|
+
*/
|
|
14
|
+
export declare function injectReportHtmlFromCore(packageDir: string): {
|
|
15
|
+
name: string;
|
|
16
|
+
setup(api: RslibPluginApi): void;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ActionSpaceItem, BaseAgent, ToolDefinition } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Converts DeviceAction from actionSpace into MCP ToolDefinition
|
|
4
|
+
* This is the core logic that removes need for hardcoded tool definitions
|
|
5
|
+
*/
|
|
6
|
+
export declare function generateToolsFromActionSpace(actionSpace: ActionSpaceItem[], getAgent: () => Promise<BaseAgent>): ToolDefinition[];
|
|
7
|
+
/**
|
|
8
|
+
* Generate common tools (screenshot, waitFor)
|
|
9
|
+
* SIMPLIFIED: Only keep essential helper tools, removed assert
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateCommonTools(getAgent: () => Promise<BaseAgent>): ToolDefinition[];
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Default timeout constants for app loading verification
|
|
5
|
+
*/
|
|
6
|
+
export declare const defaultAppLoadingTimeoutMs = 10000;
|
|
7
|
+
export declare const defaultAppLoadingCheckIntervalMs = 2000;
|
|
8
|
+
/**
|
|
9
|
+
* Content item types for tool results (MCP compatible)
|
|
10
|
+
*/
|
|
11
|
+
export type ToolResultContent = {
|
|
12
|
+
type: 'text';
|
|
13
|
+
text: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'image';
|
|
16
|
+
data: string;
|
|
17
|
+
mimeType: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'audio';
|
|
20
|
+
data: string;
|
|
21
|
+
mimeType: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'resource';
|
|
24
|
+
resource: {
|
|
25
|
+
text: string;
|
|
26
|
+
uri: string;
|
|
27
|
+
mimeType?: string;
|
|
28
|
+
} | {
|
|
29
|
+
uri: string;
|
|
30
|
+
blob: string;
|
|
31
|
+
mimeType?: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Result type for tool execution (MCP compatible)
|
|
36
|
+
*/
|
|
37
|
+
export interface ToolResult {
|
|
38
|
+
[x: string]: unknown;
|
|
39
|
+
content: ToolResultContent[];
|
|
40
|
+
isError?: boolean;
|
|
41
|
+
_meta?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Tool handler function type
|
|
45
|
+
* Takes parsed arguments and returns a tool result
|
|
46
|
+
*/
|
|
47
|
+
export type ToolHandler<T = Record<string, unknown>> = (args: T) => Promise<ToolResult>;
|
|
48
|
+
/**
|
|
49
|
+
* Tool schema type using Zod
|
|
50
|
+
*/
|
|
51
|
+
export type ToolSchema = Record<string, z.ZodTypeAny>;
|
|
52
|
+
/**
|
|
53
|
+
* Tool definition for MCP server
|
|
54
|
+
*/
|
|
55
|
+
export interface ToolDefinition<T = Record<string, unknown>> {
|
|
56
|
+
name: string;
|
|
57
|
+
description: string;
|
|
58
|
+
schema: ToolSchema;
|
|
59
|
+
handler: ToolHandler<T>;
|
|
60
|
+
autoDestroy?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Action space item definition
|
|
64
|
+
* Note: Intentionally no index signature to maintain compatibility with DeviceAction
|
|
65
|
+
*/
|
|
66
|
+
export interface ActionSpaceItem {
|
|
67
|
+
name: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
args?: Record<string, unknown>;
|
|
70
|
+
paramSchema?: z.ZodTypeAny;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Base agent interface
|
|
74
|
+
* Represents a platform-specific agent (Android, iOS, Web)
|
|
75
|
+
* Note: Return types use `unknown` for compatibility with platform-specific implementations
|
|
76
|
+
*/
|
|
77
|
+
export interface BaseAgent {
|
|
78
|
+
getActionSpace(): Promise<ActionSpaceItem[]>;
|
|
79
|
+
destroy?(): Promise<void>;
|
|
80
|
+
page?: {
|
|
81
|
+
screenshotBase64(): Promise<string>;
|
|
82
|
+
};
|
|
83
|
+
aiAction?: (description: string, params?: Record<string, unknown>) => Promise<unknown>;
|
|
84
|
+
aiWaitFor?: (assertion: string, options: Record<string, unknown>) => Promise<unknown>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Base device interface for temporary device instances
|
|
88
|
+
*/
|
|
89
|
+
export interface BaseDevice {
|
|
90
|
+
actionSpace(): ActionSpaceItem[];
|
|
91
|
+
destroy?(): Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Interface for platform-specific MCP tools manager
|
|
95
|
+
*/
|
|
96
|
+
export interface IMidsceneTools {
|
|
97
|
+
attachToServer(server: McpServer): void;
|
|
98
|
+
initTools(): Promise<void>;
|
|
99
|
+
closeBrowser?(): Promise<void>;
|
|
100
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Recursively unwrap optional, nullable, default, and effects wrapper types
|
|
4
|
+
* to get the actual inner Zod type
|
|
5
|
+
*/
|
|
6
|
+
export declare function unwrapZodField(field: unknown): unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Check if a field is a Midscene locator field
|
|
9
|
+
* Checks for either:
|
|
10
|
+
* 1. midscene_location_field_flag in shape (result schema)
|
|
11
|
+
* 2. prompt field in shape (input schema)
|
|
12
|
+
*/
|
|
13
|
+
export declare function isMidsceneLocatorField(field: unknown): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Get type name string from a Zod schema field
|
|
16
|
+
* @param field - Zod schema field
|
|
17
|
+
* @param locatorTypeDescription - Optional description for MidsceneLocation fields (used by core)
|
|
18
|
+
*/
|
|
19
|
+
export declare function getZodTypeName(field: unknown, locatorTypeDescription?: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Get description from a Zod schema field
|
|
22
|
+
*/
|
|
23
|
+
export declare function getZodDescription(field: z.ZodTypeAny): string | null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midscene/shared",
|
|
3
|
-
"version": "1.0.1-beta-
|
|
3
|
+
"version": "1.0.1-beta-20251209112631.0",
|
|
4
4
|
"repository": "https://github.com/web-infra-dev/midscene",
|
|
5
5
|
"homepage": "https://midscenejs.com/",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -57,6 +57,16 @@
|
|
|
57
57
|
"import": "./dist/es/common.mjs",
|
|
58
58
|
"require": "./dist/lib/common.js"
|
|
59
59
|
},
|
|
60
|
+
"./mcp": {
|
|
61
|
+
"types": "./dist/types/mcp/index.d.ts",
|
|
62
|
+
"import": "./dist/es/mcp/index.mjs",
|
|
63
|
+
"require": "./dist/lib/mcp/index.js"
|
|
64
|
+
},
|
|
65
|
+
"./logger": {
|
|
66
|
+
"types": "./dist/types/logger.d.ts",
|
|
67
|
+
"import": "./dist/es/logger.mjs",
|
|
68
|
+
"require": "./dist/lib/logger.js"
|
|
69
|
+
},
|
|
60
70
|
"./*": {
|
|
61
71
|
"types": "./dist/types/*.d.ts",
|
|
62
72
|
"import": "./dist/es/*.mjs",
|
|
@@ -72,21 +82,25 @@
|
|
|
72
82
|
"@silvia-odwyer/photon": "0.3.3",
|
|
73
83
|
"@silvia-odwyer/photon-node": "0.3.3",
|
|
74
84
|
"debug": "4.4.0",
|
|
85
|
+
"express": "^4.21.2",
|
|
75
86
|
"jimp": "0.22.12",
|
|
76
87
|
"js-sha256": "0.11.0",
|
|
77
88
|
"sharp": "^0.34.3",
|
|
78
89
|
"uuid": "11.1.0"
|
|
79
90
|
},
|
|
80
91
|
"devDependencies": {
|
|
81
|
-
"@rslib/core": "^0.18.
|
|
92
|
+
"@rslib/core": "^0.18.3",
|
|
93
|
+
"@modelcontextprotocol/sdk": "1.10.2",
|
|
82
94
|
"@types/debug": "4.1.12",
|
|
95
|
+
"@types/express": "^4.17.21",
|
|
83
96
|
"@types/node": "^18.0.0",
|
|
84
97
|
"@ui-tars/shared": "1.2.0",
|
|
85
98
|
"dotenv": "^16.4.5",
|
|
86
99
|
"openai": "6.3.0",
|
|
87
100
|
"rimraf": "~3.0.2",
|
|
88
101
|
"typescript": "^5.8.3",
|
|
89
|
-
"vitest": "3.0.5"
|
|
102
|
+
"vitest": "3.0.5",
|
|
103
|
+
"zod": "3.24.3"
|
|
90
104
|
},
|
|
91
105
|
"sideEffects": [],
|
|
92
106
|
"publishConfig": {
|