open_web_page
Opens a specified URL in a new browser window via the MCP Communication Server, waits for WebSocket data from the page, and handles timeout settings for real-time interaction.
Instructions
在IDE中打开一个新的网页窗口显示指定的URL,并自动等待接收页面发送的WebSocket数据
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timeoutMs | No | 等待数据的超时时间(毫秒),默认10秒 | |
| url | Yes | ||
| waitForData | No | 是否等待页面发送数据,默认为true |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"timeoutMs": {
"description": "等待数据的超时时间(毫秒),默认10秒",
"type": "number"
},
"url": {
"format": "uri",
"type": "string"
},
"waitForData": {
"description": "是否等待页面发送数据,默认为true",
"type": "boolean"
}
},
"required": [
"url"
],
"type": "object"
}
Implementation Reference
- src/index.ts:70-132 (handler)The handler function opens the specified URL in the default system browser using platform-specific commands (cmd/start on Windows, open on macOS, xdg-open on Linux). It then optionally waits for data from the page via an HTTP API call to localhost:3003/api/wait-for-data, returning the received data or an error message.async ({ url, waitForData = true, timeoutMs = 10000 }: { url: string; waitForData?: boolean | undefined; timeoutMs?: number | undefined; }) => { try { let command: string; // 根据操作系统选择合适的命令 if (process.platform === 'win32') { // Windows: 使用cmd /c start来确保正确打开浏览器 command = `cmd /c start "" "${url}"`; } else if (process.platform === 'darwin') { command = `open "${url}"`; } else { command = `xdg-open "${url}"`; } await execAsync(command); if (!waitForData) { return { content: [{ type: "text", text: `成功打开网页: ${url}` }] }; } // 给页面一些时间来加载和建立WebSocket连接 console.error("⏳ 等待页面加载并建立WebSocket连接..."); await new Promise(resolve => setTimeout(resolve, 2000)); // 等待页面发送数据 try { console.error(`🔍 等待页面 ${url} 发送数据,超时时间: ${timeoutMs}ms`); const receivedData = await waitForPageDataViaAPI(timeoutMs); return { content: [{ type: "text", text: `成功打开网页并接收到数据: ${url}\n\n接收到的数据:\n${JSON.stringify(receivedData, null, 2)}` }] }; } catch (dataError) { // 如果等待数据超时,仍然返回成功打开页面的消息 return { content: [{ type: "text", text: `成功打开网页: ${url}\n\n等待数据时出现问题: ${dataError instanceof Error ? dataError.message : '未知错误'}` }] }; } } catch (error) { return { content: [{ type: "text", text: `打开网页失败: ${error instanceof Error ? error.message : '未知错误'}` }] }; } }
- src/index.ts:64-68 (schema)Zod input schema defining parameters: url (required string URL), waitForData (optional boolean, default true), timeoutMs (optional number, default 10000). Part of the tool metadata.inputSchema: { url: z.string().url("请提供有效的URL地址"), waitForData: z.boolean().optional().describe("是否等待页面发送数据,默认为true"), timeoutMs: z.number().optional().describe("等待数据的超时时间(毫秒),默认10秒") }
- src/index.ts:60-133 (registration)Registers the 'open_web_page' tool on the MCP server with title, description, input schema, and handler function.server.registerTool("open_web_page", { title: "打开网页", description: "在IDE中打开一个新的网页窗口显示指定的URL,并自动等待接收页面发送的WebSocket数据", inputSchema: { url: z.string().url("请提供有效的URL地址"), waitForData: z.boolean().optional().describe("是否等待页面发送数据,默认为true"), timeoutMs: z.number().optional().describe("等待数据的超时时间(毫秒),默认10秒") } }, async ({ url, waitForData = true, timeoutMs = 10000 }: { url: string; waitForData?: boolean | undefined; timeoutMs?: number | undefined; }) => { try { let command: string; // 根据操作系统选择合适的命令 if (process.platform === 'win32') { // Windows: 使用cmd /c start来确保正确打开浏览器 command = `cmd /c start "" "${url}"`; } else if (process.platform === 'darwin') { command = `open "${url}"`; } else { command = `xdg-open "${url}"`; } await execAsync(command); if (!waitForData) { return { content: [{ type: "text", text: `成功打开网页: ${url}` }] }; } // 给页面一些时间来加载和建立WebSocket连接 console.error("⏳ 等待页面加载并建立WebSocket连接..."); await new Promise(resolve => setTimeout(resolve, 2000)); // 等待页面发送数据 try { console.error(`🔍 等待页面 ${url} 发送数据,超时时间: ${timeoutMs}ms`); const receivedData = await waitForPageDataViaAPI(timeoutMs); return { content: [{ type: "text", text: `成功打开网页并接收到数据: ${url}\n\n接收到的数据:\n${JSON.stringify(receivedData, null, 2)}` }] }; } catch (dataError) { // 如果等待数据超时,仍然返回成功打开页面的消息 return { content: [{ type: "text", text: `成功打开网页: ${url}\n\n等待数据时出现问题: ${dataError instanceof Error ? dataError.message : '未知错误'}` }] }; } } catch (error) { return { content: [{ type: "text", text: `打开网页失败: ${error instanceof Error ? error.message : '未知错误'}` }] }; } } );
- src/index.ts:11-39 (helper)Helper function used by the handler to poll the web server at localhost:3003/api/wait-for-data for page-submitted data, with configurable timeout.async function waitForPageDataViaAPI(timeoutMs: number = 10000): Promise<any> { try { const response = await fetch('http://localhost:3003/api/wait-for-data', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ timeoutMs }) }); console.error(`HTTP响应状态: ${response.status} ${response.statusText}`); if (!response.ok) { const errorText = await response.text(); throw new Error(`HTTP请求失败 (${response.status}): ${errorText}`); } const result = await response.json(); console.error('解析的结果:', result); if (result.success) { return result.data; } else { throw new Error(result.message || '等待数据失败'); } } catch (error) { throw new Error(`HTTP API调用失败: ${error instanceof Error ? error.message : '未知错误'}`); } }