Skip to main content
Glama
FuzzyCZX

MCP Communication Server

by FuzzyCZX

打开网页

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

TableJSON Schema
NameRequiredDescriptionDefault
timeoutMsNo等待数据的超时时间(毫秒),默认10秒
urlYes
waitForDataNo是否等待页面发送数据,默认为true

Implementation Reference

  • 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 : '未知错误'}` 
          }]
        };
      }
    }
  • 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 : '未知错误'}` 
            }]
          };
        }
      }
    );
  • 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 : '未知错误'}`);
      }
    }

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/FuzzyCZX/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server