feat(mcp): add playwright example tool to help generate playwright test code (#660)

* fix(mcp): resolve server close logic

* feat(mcp): add playwright example tools
This commit is contained in:
2025-04-28 13:33:21 +08:00 committed by GitHub
parent 33d2733c77
commit 762b602940
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 8 deletions

View File

@ -9,9 +9,7 @@
"require": "./dist/index.cjs"
}
},
"bin": {
"midscene-mcp": "dist/index.cjs"
},
"bin": "dist/index.cjs",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",

View File

@ -17,6 +17,23 @@ const server = new McpServer({
'Midscene MCP Server: Control the browser using natural language commands for navigation, clicking, input, hovering, and achieving goals. Also supports screenshots and JavaScript execution.',
});
server.tool(
'midscene_playwright_example',
'Provides Playwright code examples for Midscene. If users need to generate Midscene test cases, they can call this method to get sample Midscene Playwright test cases for generating end-user test cases. Each step must first be verified using the mcp method, and then the final test case is generated based on the playwright example according to the steps executed by mcp',
{},
async () => {
return {
content: [
{
type: 'text',
text: PROMPTS.PLAYWRIGHT_CODE_EXAMPLE,
},
],
isError: false,
};
},
);
server.resource(
'playwright-example',
'file:///playwright-example.mdx',
@ -56,6 +73,6 @@ runServer().catch(console.error);
process.stdin.on('close', () => {
console.error('Midscene MCP Server closing, cleaning up browser...');
midsceneManager.closeBrowser().catch(console.error);
server.close();
midsceneManager.closeBrowser().catch(console.error);
});

View File

@ -105,10 +105,7 @@ export class MidsceneManager {
const agent = await this.initAgent(true);
await agent.connectNewTabWithUrl(url);
return {
content: [
{ type: 'text', text: `Navigated to ${url}` },
{ type: 'text', text: `report file: ${agent.reportFile}` },
],
content: [{ type: 'text', text: `Navigated to ${url}` }],
isError: false,
};
},