Scrapy, a fast high-level web crawling & scraping framework for Python.
Agents docs page. (#8120)
Andrey Rakhmatullin3fd2a407cc32
diff --git a/docs/faq.rst b/docs/faq.rstindex 73a72a2df..232dbe144 100644--- a/docs/faq.rst+++ b/docs/faq.rst@@ -242,6 +242,10 @@ often, more useful than plain old ``pdb.set_trace()``.For more info see :ref:`topics-shell-inspect-response`.+You can also :ref:`connect to live crawls <connect-live-crawl>`, which is the+best way to debug running spiders without adding breakpoints to their code in+advance.+Simplest way to dump all my scraped items into a JSON/CSV/XML file?-------------------------------------------------------------------diff --git a/docs/index.rst b/docs/index.rstindex 34eb003e6..ad23c3a79 100644--- a/docs/index.rst+++ b/docs/index.rst@@ -156,6 +156,7 @@ Solving specific problems:hidden:faq+ topics/agentstopics/debugtopics/contractstopics/practices@@ -175,6 +176,9 @@ Solving specific problems:doc:`faq`Get answers to most frequently asked questions.+:doc:`topics/agents`+ Learn how to use Scrapy with coding agents efficiently.+:doc:`topics/debug`Learn how to debug common problems of your Scrapy spider.diff --git a/docs/intro/overview.rst b/docs/intro/overview.rstindex 5937c5860..4ed0f9c6d 100644--- a/docs/intro/overview.rst+++ b/docs/intro/overview.rst@@ -134,9 +134,10 @@ scraping easy and efficient, such as:- crawl depth restriction- and more-* A :ref:`Telnet console <topics-telnetconsole>` for hooking into a Python- console running inside your Scrapy process, to introspect and debug your- crawler+* Several ways to :ref:`execute code inside a Scrapy process+ <connect-live-crawl>`, to introspect and debug it.++* An official :ref:`plugin for coding agents <agents>`.* Plus other goodies like reusable spiders to crawl sites from `Sitemaps`_ andXML/CSV feeds, a media pipeline for :ref:`automatically downloading imagesdiff --git a/docs/intro/tutorial.rst b/docs/intro/tutorial.rstindex efade47e6..e1d86736d 100644--- a/docs/intro/tutorial.rst+++ b/docs/intro/tutorial.rst@@ -837,4 +837,6 @@ command-line tool, spiders, selectors and other things the tutorial hasn't covermodeling the scraped data. If you'd prefer to play with an example project, checkthe :ref:`intro-examples` section.+If you are using a coding agent, see :ref:`agents`.+.. _JSON: https://en.wikipedia.org/wiki/JSONdiff --git a/docs/topics/agents.rst b/docs/topics/agents.rstnew file mode 100644index 000000000..594ff4281--- /dev/null+++ b/docs/topics/agents.rst@@ -0,0 +1,49 @@+.. _agents:++===============================+Using Scrapy with coding agents+===============================++If you use coding agents to work with Scrapy projects, we recommend installing+the official `agent plugin`_. To install it, point your agent to the plugin+repository URL (read the plugin documentation for more information)::++ https://github.com/scrapy/scrapy-agent-plugin++This plugin adds a skill that helps agents build and troubleshoot Scrapy+projects, and configures the official `Scrapy MCP server`_, which allows agents+to connect to Scrapy crawls.++.. _using-mcp-server:++Using the MCP server+====================++.. versionadded:: VERSION++The `Scrapy MCP server`_ can connect to running local crawls that have the+:class:`~.RemoteControl` extension enabled. Here are some tasks that are+possible thanks to it:++- Checking on the progress of a crawl, by looking at the current values of+ :ref:`stats <topics-stats>` and, when that's not enough, by inspecting core+ objects such as the :ref:`scheduler <topics-scheduler>` directly.++- Investigating why a crawl is slow, doesn't make requests, or makes requests+ without producing items, by looking at its runtime state, settings, and+ source code. It's also easy to see how the runtime state (e.g. the stats or+ the queue state) changes over a set amount of time, by reading it twice in+ the same request with an :func:`asyncio.sleep` call in between.+ See :ref:`optimize-bottleneck` for more ideas about investigating slow+ crawls.++- Studying memory usage and debugging memory leaks, as described in+ :ref:`topics-leaks`.++- Modifying the runtime state, configuration, and even code of a crawl. This is+ fragile and not always possible, but it may save a broken crawl that is+ expensive or impossible to restart without losing data and that cannot be+ fixed in any other way.++.. _agent plugin: https://github.com/scrapy/scrapy-agent-plugin+.. _Scrapy MCP server: https://github.com/scrapy/scrapy-mcp-officialdiff --git a/docs/topics/debug.rst b/docs/topics/debug.rstindex 8ce714514..659bc1cec 100644--- a/docs/topics/debug.rst+++ b/docs/topics/debug.rst@@ -126,6 +126,14 @@ Fortunately, the :command:`shell` is your bread and butter in this case (seeSee also: :ref:`topics-shell-inspect-response`.+Scrapy MCP server+=================++You can use the :ref:`Scrapy MCP server <using-mcp-server>` to connect a coding+agent to a running crawl, so that it can check the progress of the crawl and+investigate or even modify its runtime state.++Open in browser===============diff --git a/docs/topics/leaks.rst b/docs/topics/leaks.rstindex 0a8b146d2..a055ee715 100644--- a/docs/topics/leaks.rst+++ b/docs/topics/leaks.rst@@ -19,8 +19,8 @@ To help debugging memory leaks, Scrapy provides a built-in mechanism fortracking objects references called :ref:`trackref <topics-leaks-trackrefs>`,and you can also use a third-party library called :ref:`muppy<topics-leaks-muppy>` for more advanced memory debugging (see below for more-info). Both mechanisms must be used from the :ref:`Telnet Console-<topics-telnetconsole>`.+info). Both mechanisms must be used after :ref:`connecting to a live crawl+<connect-live-crawl>`.Common causes of memory leaks=============================diff --git a/docs/topics/optimize.rst b/docs/topics/optimize.rstindex caa06c306..ed981bcd4 100644--- a/docs/topics/optimize.rst+++ b/docs/topics/optimize.rst@@ -44,6 +44,9 @@ what every part of the engine is doing at a given moment:engine.scraper.slot.active_size : 0engine.scraper.slot.needs_backout() : False+A coding agent using the :ref:`Scrapy MCP server <using-mcp-server>` can also+retrieve these values directly.+Take a few readings at different points of the crawl:- ``len(engine.downloader.active)`` stays at :setting:`CONCURRENT_REQUESTS`:diff --git a/docs/topics/practices.rst b/docs/topics/practices.rstindex ecc1fb107..42a791dee 100644--- a/docs/topics/practices.rst+++ b/docs/topics/practices.rst@@ -580,6 +580,20 @@ Static analysisConsider using :doc:`scrapy-lint <scrapy-lint:index>`, a linter for Scrapyprojects that detects common mistakes and anti-patterns.+.. _connect-live-crawl:++Connecting to live crawls+=========================++It's useful to be able to connect to live long-running crawls, either to check+their progress in detail or to investigate problems with them. Scrapy provides+the following tools for this:++- :ref:`Telnet console <topics-telnetconsole>`: connect to a crawl process with+ a telnet client and execute Python code inside it.+- :ref:`Scrapy MCP server <using-mcp-server>`: point a coding agent to a crawl+ process so that it can execute Python code inside it.+.. _Tor project: https://www.torproject.org/.. _commercial support: https://www.scrapy.org/companies.. _ProxyMesh: https://proxymesh.com/diff --git a/docs/topics/security.rst b/docs/topics/security.rstindex 444800bec..583e446e4 100644--- a/docs/topics/security.rst+++ b/docs/topics/security.rst@@ -231,7 +231,8 @@ Remote control serverScrapy enables the remote control HTTP server(:class:`scrapy.extensions.remote_control.RemoteControl`) by default(:setting:`REMOTE_CONTROL_ENABLED`). Its purpose is to run arbitrary code-inside the Scrapy process, so anyone who can connect to it can do that.+inside the Scrapy process, so anyone who can connect to it can do that, e.g.+via the :ref:`Scrapy MCP server <using-mcp-server>`.The server listens on a random localhost port and requires a token forauthentication. This token is stored in a job file (seediff --git a/docs/topics/telnetconsole.rst b/docs/topics/telnetconsole.rstindex 6448121ab..ffba2346a 100644--- a/docs/topics/telnetconsole.rst+++ b/docs/topics/telnetconsole.rst@@ -29,7 +29,7 @@ disable it if you want. For more information about the extension itself see.. note::This feature is not supported when :setting:`TWISTED_REACTOR_ENABLED` is ``False``.-.. seealso:: :ref:`security-telnet`+.. seealso:: :ref:`security-telnet`, :ref:`using-mcp-server`.. highlight:: nonediff --git a/scrapy/extensions/remote_control.py b/scrapy/extensions/remote_control.pyindex 78116ffcd..22fe82230 100644--- a/scrapy/extensions/remote_control.py+++ b/scrapy/extensions/remote_control.py@@ -94,6 +94,9 @@ class RemoteControl:truncated (omitted if ``false``).- ``traceback_truncated`` (boolean, optional): whether ``traceback``was truncated (omitted if ``false``).++ This extension can be used with any HTTP client, but it is intended to be+ used by the Scrapy MCP server (see :ref:`using-mcp-server`)."""def __init__(self, crawler: Crawler):