网站抓取服务器

一个简单的MCP服务器,提供网站抓取工具,支持完整内容获取和流式传输。

  • by EzzoHamdan
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 ezzohamdan/mcp-web-content-server

使用说明

MCP服务器

一个简单的MCP服务器,它公开了网站获取工具。

用法

使用stdio(默认)或SSE传输启动服务器:

# Using stdio transport (default)
uv run mcp-simple-tool

# Using SSE transport on custom port
uv run mcp-simple-tool --transport sse --port 8000

服务器公开了两个工具:

  • fetch:获取网站并将其全部内容作为单个响应返回。
  • fetch_streamed:获取一个网站,并以500个字符的块返回其内容,以供流媒体或令牌感知使用。

这两个工具都接受一个必需的参数:

  • url:要获取的网站的URL

示例

使用MCP客户端,您可以使用以下工具(STDIO传输):

import asyncio
from mcp.client.session import ClientSession
from mcp.client.stdio import StdioServerParameters, stdio_client

async def main():
    async with stdio_client(
        StdioServerParameters(command="uv", args=["run", "mcp-simple-tool"])
    ) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            # List available tools
            tools = await session.list_tools()
            print(tools)

            # Call the fetch tool (returns full content)
            result = await session.call_tool("fetch", {"url": "https://example.com"})
            print("Fetch result:", result)

            # Call the fetch_streamed tool (returns content in chunks)
            streamed_result = await session.call_tool("fetch_streamed", {"url": "https://example.com"})
            print("Fetch streamed result:")
            for chunk_content in streamed_result:
                print(chunk_content)

asyncio.run(main())

输出为 fetch_streamed

这 fetch_streamed 工具返回以下列表 TextContent 对象,每个对象包含网站内容的一大块。要重建完整内容,请连接 .text 领域:

full_content = "".join(chunk.text for chunk in streamed_result)

有关更多详细信息,请参阅中的代码 mcp_simple_tool/server.py