网页摘录
MCP服务器,用于获取网页并提取可读的Markdown、表格和元数据。
特性
- 从URL获取原始HTML
- 将HTML转换为干净易读的Markdown
- 将结构化表数据提取为JSON
- 提取元数据,包括Open Graph、JSON-LD等
- 无需无头浏览器(轻量级HTML解析)
安装
npm install
npm run build用法
HTTP传输(默认)
# Start with default HTTP transport on port 8080
npm start
# or
node dist/index.js
# Start with custom port
node dist/index.js --port 3000STDIO交通(用于当地发展)
npm run dev:stdio
# or
node dist/index.js --stdio工具
1. fetch_url
从具有可选自定义标头和超时的URL获取原始HTML。
输入:
{
"url": "https://example.com",
"headers": { "Accept-Language": "en-US" },
"timeout_ms": 10000
}输出:
{
"ok": true,
"data": {
"html": "...",
"status_code": 200,
"content_type": "text/html; charset=utf-8",
"final_url": "https://example.com/"
},
"meta": {
"source": "https://example.com",
"retrieved_at": "2024-01-15T12:00:00.000Z",
"pagination": { "next_cursor": null },
"warnings": []
}
}2. extract_readable_markdown
将HTML转换为可读的Markdown,删除样板、导航、广告和侧边栏。
输入:
{
"html_or_url": "https://example.com/article"
}或者使用原始HTML:
{
"html_or_url": "
Title
Content
"
}输出:
{
"ok": true,
"data": {
"markdown": "# Title\n\nContent",
"headings": [
{ "level": 1, "text": "Title" }
],
"word_count": 2
},
"meta": {
"source": "https://example.com/article",
"retrieved_at": "2024-01-15T12:00:00.000Z",
"pagination": { "next_cursor": null },
"warnings": []
}
}3. extract_tables
将HTML中的所有数据表提取为结构化JSON。
输入:
{
"html_or_url": "https://example.com/data"
}输出:
{
"ok": true,
"data": {
"tables": [
{
"headers": ["Name", "Age", "City"],
"rows": [
["Alice", "30", "New York"],
["Bob", "25", "Los Angeles"]
],
"caption": "User Data"
}
],
"count": 1
},
"meta": {
"retrieved_at": "2024-01-15T12:00:00.000Z",
"pagination": { "next_cursor": null },
"warnings": []
}
}4. extract_metadata
提取元数据,包括规范URL、标题、描述、Open Graph标签、JSON-LD、作者和发布日期。
输入:
{
"html_or_url": "https://example.com/article"
}输出:
{
"ok": true,
"data": {
"title": "Article Title",
"description": "Article description",
"canonical_url": "https://example.com/article",
"author": "John Doe",
"publish_date": "2024-01-15T12:00:00Z",
"open_graph": {
"title": "OG Title",
"description": "OG Description",
"image": "https://example.com/image.jpg",
"type": "article"
},
"json_ld": [
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Article Title"
}
],
"meta_tags": {
"author": "John Doe",
"description": "Article description"
}
},
"meta": {
"retrieved_at": "2024-01-15T12:00:00.000Z",
"pagination": { "next_cursor": null },
"warnings": []
}
}响应信封
所有工具均以标准信封格式返回响应:
成功响应
{
"ok": true,
"data": {},
"meta": {
"source": "optional URL if fetched",
"retrieved_at": "ISO-8601 timestamp",
"pagination": { "next_cursor": null },
"warnings": []
}
}错误响应
{
"ok": false,
"error": {
"code": "ERROR_CODE",
"message": "Human readable message",
"details": {}
},
"meta": {
"retrieved_at": "ISO-8601 timestamp"
}
}错误代码:
INVALID_INPUT-输入参数无效或缺失UPSTREAM_ERROR-目标服务器出错(HTTP错误、连接失败)RATE_LIMITED-请求受到费率限制TIMEOUT-请求超时PARSE_ERROR-解析HTML失败INTERNAL_ERROR-意外内部错误
局限性
- 无JavaScript渲染 -此服务器仅处理静态HTML。JavaScript呈现的内容将不会被捕获。对于JavaScript密集型网站,可以考虑使用无头浏览器解决方案。
- 内容检测 -可读标记提取使用启发式方法来识别主要内容。复杂或不寻常的页面布局可能无法得到最佳处理。
- 表检测 -布局表(用于页面结构而非数据的表)使用启发式方法过滤掉。一些边缘案例可能被错误地分类。
- JSON-LD解析 -JSON-LD提取是最好的努力。格式错误的JSON-LD块会被自动跳过。
- 无需认证 -此服务器不处理身份验证。对需要登录的页面的请求将返回登录页面或错误。
发展
运行测试
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch项目结构
webpage-extract/
├── src/
│ ├── index.ts # Main entry point
│ ├── cli.ts # Command-line argument parsing
│ ├── config.ts # Configuration management
│ ├── server.ts # MCP server instance
│ ├── types.ts # TypeScript type definitions
│ ├── tools/
│ │ ├── index.ts # Tool exports
│ │ ├── fetch.ts # fetch_url tool
│ │ ├── markdown.ts # extract_readable_markdown tool
│ │ ├── tables.ts # extract_tables tool
│ │ └── metadata.ts # extract_metadata tool
│ └── transport/
│ ├── index.ts # Transport exports
│ ├── http.ts # HTTP transport
│ └── stdio.ts # STDIO transport
├── tests/
│ ├── unit/
│ │ └── tools.test.ts # Unit tests
│ └── e2e/
│ └── server.test.ts # E2E tests
├── package.json
├── tsconfig.json
├── .env.example
└── README.md配置
可以通过环境变量或命令行参数设置配置。
| 变量 | CLI标志 | 默认值 | 描述 |
|---|---|---|---|
TRANSPORT | --transport, -t | http | 传输类型:“http”或“stdio” |
PORT | --port, -p | 8080 | HTTP服务器端口 |
DEFAULT_TIMEOUT_MS | - | 30000 | 默认请求超时(毫秒) |
USER_AGENT | - | webpage-extract/1.0.0 | HTTP请求的用户代理 |
许可证
麻省理工学院
