Commits

Repository Review Before Building the Agents MCP

A review before touching anything

A review before touching anything

Before designing the Agents MCP, I went through the entire repository to understand what was there and avoid breaking anything that already worked. Everything that follows was verified with real commands, not assumptions.

The result is a document in the repository (docs/revision-completa-para-el-mcp.md) and, along with it, one of the decisions that most constrains the rest of the work.

What was already there

The project has three independent components: a FastAPI API using SQLAlchemy and PostgreSQL, a Vue 3 frontend with TypeScript, and a PHP connector installed inside each WordPress site that is to be monitored. There are 215 backend tests passing, 76 connector test cases, and 64 endpoints.

Half of the endpoints are read-only; the other half modify the state of the websites. This means that an MCP exposing all endpoints as though they were harmless would be a revolving door: 31% of the reads and 52% of the writes access client websites.

What the connector does that the API does not

One of the things that took a while to spot was that the security review does not live in the API. The API is an intermediary that talks to the connector; the actual logic —17 checks, 9 hardening measures, and 4 security headers— is written in the connector's PHP code.

This has a practical consequence: if a site's connector is outdated, the data that arrives is incomplete, and the MCP would not know. For this reason, the MCP must always return the connector version alongside the data.

And this is where a useful number came up. The connector history shows that the audit checks went from 9 to 11 and then to 16, with a significant jump in October. Sites running the old connector do not check five or seven things that are checked today. That is the difference between "we have a plugin" and "we have a review," and it can be explained to a client in a single sentence.

Four things we did not know that changed the plan

There is no continuous integration. No workflow files. With 215 tests and a new working branch, it should have been in place before starting to change anything.

The tests depend on an artifact that is not in the repository. One of the connector connection tests failed with a 503 because the connector package had not been built. With the package built, all 215 tests pass. It was the only failure, and it was precisely on the path we intended to expose. Without continuous integration, nobody had noticed.

There is no per-site history of the connector version. The version is a column that is overwritten on every check, as is the last-visit time. There is an action log that preserves the versions and dates of every update made from the panel, but for sites that have never been updated, all we know is how they were at the last visit. To say "this site has not been reviewed for seven months," we would need to store the first time it was seen.

Authentication relies only on a session cookie. There is no API key. An MCP has to decide whether it signs in with a cookie —no cost, but with all the permissions of an administrator— or whether we add a key with separate permissions, which would itself require API development.

The plan: three phases, the first of which does not touch the product

The first phase is to build the safety net: continuous integration and a bootstrap that builds the connector package before testing anything. It does not change any behavior and is what makes it possible to do the rest with confidence.

The second is a read-only MCP with three tools. The first is new and costs nothing to build: a coverage report explaining what each check examines and what it does not. The second already exists in the API and calculates whether a site's connector is outdated. The third consists of the reads, classified by their real effect rather than the HTTP verb, because it has been confirmed that one read also writes to the database.

The third phase consists of actions that modify websites, and here an idea that seemed good was rejected: automatic restoration in case of failure. Looking at the restorer's code reveals that it is asymmetric: the database returns exactly to the point of the backup, but the files are merely overwritten, meaning that anything created after the backup survives. An unsupervised DROP TABLE can be worse than the original failure. Therefore: a backup is always taken, and if an action fails, there is no automatic restoration; the backup path is reported so that the decision belongs to the person responsible.

What has not been touched yet

The website remains on the production server with no changes, the main branch is intact, and the return branch is exactly where everything began. These first two iterations are documentation and verification: no MCP code has been written yet.

The gate that makes a new endpoint start closed

After the review, two things needed to be addressed: putting the project's first continuous integration in place and figuring out how access would work.

The continuous integration problem was that the connector package is not in the repository —it is deliberately excluded because it is a build artifact— and the API looks for it in the build directory. On a fresh checkout, a test failed with a missing-package error. Without CI, nobody had noticed: it was the only failure in the entire suite and it affected the path we intended to expose to agents. Now the suite builds the package if it cannot find it, and CI runs seven jobs: the API on three Python versions, the connector, the package, and the frontend.

The real issue, however, was the gate. My first proposal was that the MCP would authenticate with an administrator cookie and delegate permission enforcement to the MCP itself. It was the zero-cost option, and that is why I defended it: it was wrong. If the cookie leaks, an attacker can do everything that was previously possible. The server must enforce permissions.

There is now a service credential: an independent token for each agent, stored as a hash, revocable, and time-limited. The token can access only an explicit list of reviewed read operations, and anything not on that list is rejected: a new endpoint starts closed. The list is also the limit of what can be granted, so not even by mistake is a mutation granted. Token usage and rejections are logged without secrets, and if the token fails, the system does not fall back to the panel session.

Two security gaps were found that are worth recounting. The first: the catalog contained a path with a trailing slash, while the server registered it without one, so the request passed the check and it was the redirect that rejected it. This was invisible because most tests used routes without a trailing slash. There is now a test that compares the catalog with the server's actual routes.

The second: the role of a token's user could be changed, and an administrator would no longer be restricted to a filtered set of websites. The token is now valid only while its user is an active agent, and the role is checked again when the principal is resolved, because there was a race condition between the gate and the handler. This has been tested by promoting the role to administrator while the request is pending.

The first two MCP tools

With the gate firmly in place, the first tools can now be exposed. There are two, and both are read-only operations over existing endpoints: the site inventory and the connector status. No new endpoints, no mutations, no deployment.

The MCP does not implement permissions: it talks to the API with the service credential, and the server does the filtering. The token is not written anywhere else—not in the logs, not on disk, and not in the tool arguments—and it never appears in any error.

The tricky part of these two tools is not transport; it is the honesty of the data. The API caches the inventory for each site, and when the cache is cold it returns 0 and false for data that has not been measured. A 0 for pending connectors is read as "there is nothing pending," which is a lie. That is why each site reports how old its inventory is, and why, when there is no observation, pending updates and health are returned as null instead of zero, accompanied by a warning that explains why. The connector status comes from the last connection to the site, and it says when that connection occurred: a connector that no one has checked for six months is not "the latest"; it is "the latest one seen six months ago."

The end-to-end test was performed against a real staging installation, not against mocks: the MCP starts, connects with the credential, and calls the tools using the protocol. Twelve checks pass. One detail was difficult to get right: if the tool lets the exception bubble up, the client receives a generic "Error executing tool" and does not know what went wrong. By using the protocol's own exception, the agent can read that the credential is invalid.

There is also a change in the SDK that saved me a headache: in version 2, FastMCP is no longer called that. The MCP package also comes with its own environment, because the API's environment is pinned and must not be changed.