Skip to content

Commit 9f13bbf

Browse files
quanruclaude
andcommitted
refactor(mcp): improve bridge mode initialization and test organization
- Improve error handling in base-tools.ts for bridge mode initialization - Add specific detection for bridge mode URL requirement errors - Provide friendly logging messages instead of generic failure messages - Clarify that deferred initialization is expected behavior - Improve comments in web-tools.ts - Remove misleading "deferred until first tool use" comment - Add clear explanations for bridge vs puppeteer mode behavior - Clarify that bridge mode requires URL while puppeteer can auto-start - Move tests from @midscene/mcp to @midscene/web-mcp - Relocate utils and puppeteer tests to correct package location - Update mcp package.json to remove obsolete test scripts - All 22 tests passing in new location 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 42bf666 commit 9f13bbf

File tree

6 files changed

+17
-6
lines changed

6 files changed

+17
-6
lines changed

packages/mcp/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"dev": "npm run build:watch",
2222
"build:watch": "rslib build --watch",
2323
"mcp-playground": "npx @modelcontextprotocol/inspector node ./dist/index.js",
24-
"test": "vitest run",
2524
"inspect": "node scripts/inspect.mjs"
2625
},
2726
"dependencies": {
@@ -37,8 +36,7 @@
3736
"@modelcontextprotocol/sdk": "1.10.2",
3837
"@rslib/core": "^0.11.2",
3938
"@types/node": "^18.0.0",
40-
"typescript": "^5.8.3",
41-
"vitest": "3.0.5"
39+
"typescript": "^5.8.3"
4240
},
4341
"license": "MIT"
4442
}

packages/shared/src/mcp/base-tools.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ export abstract class BaseMidsceneTools implements IMidsceneTools {
6363
);
6464
} catch (error) {
6565
// Layer 2: Create temporary device instance to read actionSpace
66-
debug('Failed to get action space from agent, using temporary device');
66+
// This is expected behavior for bridge mode without URL or unconnected devices
67+
const errorMessage = error instanceof Error ? error.message : String(error);
68+
if (errorMessage.includes('requires a URL') || errorMessage.includes('web_connect')) {
69+
debug('Bridge mode detected - agent will be initialized on first web_connect call');
70+
} else {
71+
debug('Agent not available yet, using temporary device for action space');
72+
}
6773
const tempDevice = this.createTemporaryDevice();
6874
actionSpace = tempDevice.actionSpace();
6975
debug(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ export class WebMidsceneTools extends BaseMidsceneTools {
6060
if (this.agent) return this.agent;
6161

6262
// Choose bridge or puppeteer mode
63-
// In bridge mode, we need a URL to connect to
64-
// If no URL provided, agent creation will be deferred until first tool use
6563
if (!this.puppeteerMode) {
64+
// Bridge mode requires a URL to connect to browser
6665
if (!openNewTabWithUrl) {
6766
throw new Error(
6867
'Bridge mode requires a URL. Use web_connect tool to connect to a page first.',
@@ -72,6 +71,7 @@ export class WebMidsceneTools extends BaseMidsceneTools {
7271
openNewTabWithUrl,
7372
)) as unknown as BaseAgent;
7473
} else {
74+
// Puppeteer mode can auto-start with default page
7575
this.agent = (await this.initPuppeteerAgent(
7676
openNewTabWithUrl,
7777
)) as unknown as BaseAgent;
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": [".", "../src/**/*", "../vitest.setup.ts"],
4+
"compilerOptions": {
5+
"types": ["vitest/globals", "node"]
6+
}
7+
}
File renamed without changes.

0 commit comments

Comments
 (0)