返回市场
文件预览

文件预览

作者:seanivore22 星标更新:2024-12-27

项目介绍

MCP 文件预览服务器

这是一个提供HTML文件预览和分析功能的Model Context Protocol (MCP)服务器。该服务器能够捕获本地HTML文件的全页截图,并分析其结构。

功能

  • 文件预览:捕获带有适当CSS样式的HTML文件的全页截图
  • 内容分析:分析HTML结构(标题、段落、图片、链接)
  • 本地文件支持:处理本地文件路径和资源
  • 截图管理:将截图保存到专用目录

安装

  1. 克隆仓库:
git clone https://github.com/your-username/mcp-file-preview.git
cd mcp-file-preview
  1. 安装依赖:
npm install
  1. 构建项目:
npm run build

配置

在你的Claude或Cline MCP设置中添加服务器:

Claude桌面应用

~/Library/Application Support/Claude/claude_desktop_config.json中添加:

{
  "mcpServers": {
    "file-preview": {
      "command": "node",
      "args": ["/path/to/mcp-file-preview/build/index.js"]
    }
  }
}

Cline VSCode扩展

在VSCode的MCP设置中添加:

{
  "mcpServers": {
    "file-preview": {
      "command": "node",
      "args": ["/path/to/mcp-file-preview/build/index.js"]
    }
  }
}

使用

该服务器提供了两个主要工具:

preview_file

捕获截图并返回HTML内容:

<use_mcp_tool>
<server_name>file-preview</server_name>
<tool_name>preview_file</tool_name>
<arguments>
{
  "filePath": "/path/to/file.html",
  "width": 1024,  // 可选
  "height": 768   // 可选
}
</arguments>
</use_mcp_tool>

截图保存在项目文件夹中的screenshots/目录下。

analyze_content

分析HTML结构:

<use_mcp_tool>
<server_name>file-preview</server_name>
<tool_name>analyze_content</tool_name>
<arguments>
{
  "filePath": "/path/to/file.html"
}
</arguments>
</use_mcp_tool>

返回以下计数:

  • 标题
  • 段落
  • 图片
  • 链接

开发

  1. 安装依赖:
npm install @modelcontextprotocol/sdk puppeteer typescript @types/node @types/puppeteer
  1. src/中进行更改
  2. 构建:
npm run build
  1. 本地测试:
npm run dev

实现细节

服务器使用MCP SDK的Server类进行适当的初始化:

this.server = new Server(
  // 元数据对象
  {
    name: 'file-preview-server',
    version: '0.1.0'
  },
  // 带有功能选项的对象
  {
    capabilities: {
      tools: {
        preview_file: {
          description: '预览本地HTML文件并捕获截图',
          inputSchema: {
            // ...模式定义
          }
        }
      }
    }
  }
);

关键点:

  • Server构造函数接受单独的元数据和选项对象
  • 工具声明在capabilities.tools中
  • 每个工具需要一个描述和inputSchema
  • 截图保存在本地的screenshots/目录下

调试

  1. 使用MCP Inspector:
npx @modelcontextprotocol/inspector
  1. 连接方式:

    • 传输类型:STDIO
    • 命令:node
    • 参数:/path/to/build/index.js
  2. 如果工具没有出现在下拉菜单中,请检查Claude OS日志

贡献

请阅读CONTRIBUTING.md,了解我们的行为准则以及提交拉取请求的过程。

许可证

本项目采用MIT许可证 - 详情见LICENSE文件。