Skip to content

Commit 1c23d4e

Browse files
quanruclaude
andcommitted
refactor(mcp): centralize app loading timeout constants in shared package
Extract Constants: - Move defaultAppLoadingTimeoutMs (10000) to @midscene/shared/mcp/types - Move defaultAppLoadingCheckIntervalMs (2000) to @midscene/shared/mcp/types - Remove duplicate constant definitions from android-mcp and ios-mcp Type Safety: - Add BaseAgent import to android-tools.ts - Use double type assertion (as unknown as) for agent type conversion - Fix spelling: temp-for-actionspace → temp-for-action-space This follows DRY principle by maintaining constants in a single location and improves type safety with proper imports and assertions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ee9866f commit 1c23d4e

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

packages/android-mcp/src/android-tools.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { type AndroidAgent, agentFromAdbDevice } from '@midscene/android';
22
import { z } from '@midscene/core';
33
import { getDebug } from '@midscene/shared/logger';
4-
import { BaseMidsceneTools, type ToolDefinition } from '@midscene/shared/mcp';
4+
import {
5+
type BaseAgent,
6+
BaseMidsceneTools,
7+
type ToolDefinition,
8+
defaultAppLoadingCheckIntervalMs,
9+
defaultAppLoadingTimeoutMs,
10+
} from '@midscene/shared/mcp';
511

612
const debug = getDebug('mcp:android-tools');
713

8-
// Default timeout for app loading verification
9-
const defaultAppLoadingTimeoutMs = 10000;
10-
const defaultAppLoadingCheckIntervalMs = 2000;
11-
1214
/**
1315
* Android-specific tools manager
1416
* Extends BaseMidsceneTools to provide Android ADB device connection tools
@@ -19,28 +21,29 @@ export class AndroidMidsceneTools extends BaseMidsceneTools {
1921
const { AndroidDevice } = require('@midscene/android');
2022
// Create minimal temporary instance without connecting to device
2123
// The constructor doesn't establish ADB connection
22-
return new AndroidDevice('temp-for-actionspace', {});
24+
return new AndroidDevice('temp-for-action-space', {});
2325
}
2426

2527
protected async ensureAgent(deviceId?: string): Promise<AndroidAgent> {
2628
if (this.agent && deviceId) {
2729
// If a specific deviceId is requested and we have an agent,
2830
// destroy it to create a new one with the new device
2931
try {
30-
await this.agent.destroy();
32+
await this.agent.destroy?.();
3133
} catch (error) {
3234
debug('Failed to destroy agent during cleanup:', error);
3335
}
3436
this.agent = undefined;
3537
}
3638

3739
if (this.agent) {
38-
return this.agent;
40+
return this.agent as unknown as AndroidAgent;
3941
}
4042

4143
debug('Creating Android agent with deviceId:', deviceId || 'auto-detect');
42-
this.agent = await agentFromAdbDevice(deviceId);
43-
return this.agent;
44+
const agent = await agentFromAdbDevice(deviceId);
45+
this.agent = agent as unknown as BaseAgent;
46+
return agent;
4447
}
4548

4649
/**

packages/ios-mcp/src/ios-tools.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { z } from '@midscene/core';
22
import { type IOSAgent, agentFromWebDriverAgent } from '@midscene/ios';
33
import { getDebug } from '@midscene/shared/logger';
4-
import { BaseMidsceneTools, type ToolDefinition } from '@midscene/shared/mcp';
4+
import {
5+
BaseMidsceneTools,
6+
type ToolDefinition,
7+
defaultAppLoadingCheckIntervalMs,
8+
defaultAppLoadingTimeoutMs,
9+
} from '@midscene/shared/mcp';
510

611
const debug = getDebug('mcp:ios-tools');
712

8-
// Default timeout for app loading verification
9-
const defaultAppLoadingTimeoutMs = 10000;
10-
const defaultAppLoadingCheckIntervalMs = 2000;
11-
1213
/**
1314
* iOS-specific tools manager
1415
* Extends BaseMidsceneTools to provide iOS WebDriverAgent connection tools

packages/shared/src/mcp/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import type { z } from 'zod';
44
// Avoid circular dependency: don't import from @midscene/core
55
// Instead, use generic types that will be provided by implementation
66

7+
/**
8+
* Default timeout constants for app loading verification
9+
*/
10+
export const defaultAppLoadingTimeoutMs = 10000;
11+
export const defaultAppLoadingCheckIntervalMs = 2000;
12+
713
/**
814
* Content item types for tool results (MCP compatible)
915
*/

0 commit comments

Comments
 (0)