Karim shoair commited on
Commit ·
4a670f6
1
Parent(s): 9626eaa
feat: Make mcp able to use http transport
Browse files- scrapling/cli.py +18 -2
- scrapling/core/ai.py +3 -11
scrapling/cli.py
CHANGED
|
@@ -136,10 +136,26 @@ def install(force): # pragma: no cover
|
|
| 136 |
|
| 137 |
|
| 138 |
@command(help="Run Scrapling's MCP server (Check the docs for more info).")
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
from scrapling.core.ai import ScraplingMCPServer
|
| 141 |
|
| 142 |
-
ScraplingMCPServer()
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
@command(help="Interactive scraping console")
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
@command(help="Run Scrapling's MCP server (Check the docs for more info).")
|
| 139 |
+
@option(
|
| 140 |
+
"--http",
|
| 141 |
+
type=bool,
|
| 142 |
+
default=False,
|
| 143 |
+
help="Whether to run the MCP server in streamable-http transport or leave it as stdio (Default: False)",
|
| 144 |
+
)
|
| 145 |
+
@option(
|
| 146 |
+
"--host",
|
| 147 |
+
type=str,
|
| 148 |
+
default="0.0.0.0",
|
| 149 |
+
help="The host to use if streamable-http transport is enabled (Default: '0.0.0.0')",
|
| 150 |
+
)
|
| 151 |
+
@option(
|
| 152 |
+
"--port", type=int, default=8000, help="The port to use if streamable-http transport is enabled (Default: 8000)"
|
| 153 |
+
)
|
| 154 |
+
def mcp(http, host, port):
|
| 155 |
from scrapling.core.ai import ScraplingMCPServer
|
| 156 |
|
| 157 |
+
server = ScraplingMCPServer(host, port)
|
| 158 |
+
server.run(transport="stdio" if not http else "streamable-http")
|
| 159 |
|
| 160 |
|
| 161 |
@command(help="Interactive scraping console")
|
scrapling/core/ai.py
CHANGED
|
@@ -41,10 +41,9 @@ def _ContentTranslator(content: Generator[str, None, None], page: _ScraplingResp
|
|
| 41 |
return ResponseModel(status=page.status, content=[result for result in content], url=page.url)
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
-
_server = FastMCP(name="Scrapling")
|
| 46 |
|
| 47 |
-
@staticmethod
|
| 48 |
@_server.tool()
|
| 49 |
def get(
|
| 50 |
url: str,
|
|
@@ -123,7 +122,6 @@ class ScraplingMCPServer:
|
|
| 123 |
page,
|
| 124 |
)
|
| 125 |
|
| 126 |
-
@staticmethod
|
| 127 |
@_server.tool()
|
| 128 |
async def bulk_get(
|
| 129 |
urls: Tuple[str, ...],
|
|
@@ -210,7 +208,6 @@ class ScraplingMCPServer:
|
|
| 210 |
for page in responses
|
| 211 |
]
|
| 212 |
|
| 213 |
-
@staticmethod
|
| 214 |
@_server.tool()
|
| 215 |
async def fetch(
|
| 216 |
url: str,
|
|
@@ -299,7 +296,6 @@ class ScraplingMCPServer:
|
|
| 299 |
page,
|
| 300 |
)
|
| 301 |
|
| 302 |
-
@staticmethod
|
| 303 |
@_server.tool()
|
| 304 |
async def bulk_fetch(
|
| 305 |
urls: Tuple[str, ...],
|
|
@@ -393,7 +389,6 @@ class ScraplingMCPServer:
|
|
| 393 |
for page in responses
|
| 394 |
]
|
| 395 |
|
| 396 |
-
@staticmethod
|
| 397 |
@_server.tool()
|
| 398 |
async def stealthy_fetch(
|
| 399 |
url: str,
|
|
@@ -493,7 +488,6 @@ class ScraplingMCPServer:
|
|
| 493 |
page,
|
| 494 |
)
|
| 495 |
|
| 496 |
-
@staticmethod
|
| 497 |
@_server.tool()
|
| 498 |
async def bulk_stealthy_fetch(
|
| 499 |
urls: Tuple[str, ...],
|
|
@@ -598,6 +592,4 @@ class ScraplingMCPServer:
|
|
| 598 |
for page in responses
|
| 599 |
]
|
| 600 |
|
| 601 |
-
|
| 602 |
-
"""Serve the MCP server."""
|
| 603 |
-
self._server.run(transport="stdio")
|
|
|
|
| 41 |
return ResponseModel(status=page.status, content=[result for result in content], url=page.url)
|
| 42 |
|
| 43 |
|
| 44 |
+
def ScraplingMCPServer(host: str, port: int) -> FastMCP:
|
| 45 |
+
_server = FastMCP(name="Scrapling", host=host, port=port)
|
| 46 |
|
|
|
|
| 47 |
@_server.tool()
|
| 48 |
def get(
|
| 49 |
url: str,
|
|
|
|
| 122 |
page,
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
@_server.tool()
|
| 126 |
async def bulk_get(
|
| 127 |
urls: Tuple[str, ...],
|
|
|
|
| 208 |
for page in responses
|
| 209 |
]
|
| 210 |
|
|
|
|
| 211 |
@_server.tool()
|
| 212 |
async def fetch(
|
| 213 |
url: str,
|
|
|
|
| 296 |
page,
|
| 297 |
)
|
| 298 |
|
|
|
|
| 299 |
@_server.tool()
|
| 300 |
async def bulk_fetch(
|
| 301 |
urls: Tuple[str, ...],
|
|
|
|
| 389 |
for page in responses
|
| 390 |
]
|
| 391 |
|
|
|
|
| 392 |
@_server.tool()
|
| 393 |
async def stealthy_fetch(
|
| 394 |
url: str,
|
|
|
|
| 488 |
page,
|
| 489 |
)
|
| 490 |
|
|
|
|
| 491 |
@_server.tool()
|
| 492 |
async def bulk_stealthy_fetch(
|
| 493 |
urls: Tuple[str, ...],
|
|
|
|
| 592 |
for page in responses
|
| 593 |
]
|
| 594 |
|
| 595 |
+
return _server
|
|
|
|
|
|