DEV Community

Minuth Prom
Minuth Prom

Posted on

Letting AI Agents Run E2E API Contract Tests Safely via MCP

AI coding assistants are pretty good at reading an OpenAPI spec and writing integration tests. The awkward part starts when you actually want them to run those tests against a live server.

Most people end up giving the agent shell access with curl and an .env file full of Bearer tokens or API keys. That works for a quick experiment, but it falls apart fast if you care about security or reliability:

  1. Your secrets end up in the prompt context. API keys, client secrets, and internal staging URLs get passed around in tool outputs and conversation logs.
  2. There are no real guardrails. If an agent hallucinates a path or decides to test an admin endpoint, nothing stops it from hitting DELETE endpoints.
  3. A 200 OK isn't a contract test. curl tells you the server responded, not whether the JSON payload actually matches your OpenAPI response schema.

I ran into this while building Open API Portal, so I added a scoped Model Context Protocol (MCP) test runner directly into the portal.
MCP Token

Keeping credentials out of the agent's hands

The main idea is simple: the AI agent should know the API contract, not the credentials or even the raw target URL.

In Open API Portal, you set up a Test Profile and an MCP Test Suite from the UI:

  • Auth stays encrypted on the server. Bearer tokens, API keys, and Basic Auth credentials are encrypted at rest (AES-256-GCM) and injected by the backend when a test runs. The agent never sees the token or the baseUrl.
  • Only allowlisted endpoints are visible. You pick which METHOD + /path operations a suite is allowed to touch. Everything else is off-limits.
  • Scoped MCP tokens. Your IDE or AI client connects to /mcp using a revocable bearer token instead of your main account session.

MCP Scopes

What the agent actually sees

When an agent connects to /mcp, it gets five tools:

  • list_test_profiles to see available environments and attached specs (without URLs or secrets)
  • list_mcp_suites to find test suites configured for a profile
  • get_suite_endpoints to read parameters, request bodies, and response schemas for the allowlisted endpoints
  • run_test_scenarios to execute multi-step tests on the server and get back pass/fail diagnostics
  • get_uncovered_endpoints to check which endpoints in the suite still need test coverage

Chaining steps and checking schemas

Real API flows usually take more than one call. The server injects the profile's encrypted auth automatically on every step, and the agent can use skipAuth: true to verify negative 401 cases or capture values (using JSONPath or headers) to pass into later steps with {{var}}, plus built-in helpers like {{$uuid}} and {{$isoNow}}:

{
  "name": "Create and Verify Order",
  "steps": [
    {
      "name": "Reject Unauthenticated Request",
      "method": "GET",
      "path": "/api/v1/orders",
      "skipAuth": true,
      "expectedStatus": 401
    },
    {
      "name": "Create Order",
      "method": "POST",
      "path": "/api/v1/orders",
      "body": { "reference": "ord_{{$uuid}}", "amount": 49.99 },
      "expectedStatus": 201,
      "capture": { "orderId": "$.id" },
      "assertions": [
        { "source": "body", "path": "$.status", "op": "eq", "value": "CREATED" },
        { "source": "latency", "op": "lt", "value": 1000 }
      ]
    },
    {
      "name": "Verify Order Contract",
      "method": "GET",
      "path": "/api/v1/orders/{id}",
      "pathParams": { "id": "{{orderId}}" },
      "expectedStatus": 200,
      "validateSchema": true
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Whenever validateSchema is enabled, the runner validates the live JSON response against the OpenAPI schema using Ajv. If a field is missing or comes back with the wrong type, the agent gets the exact schema error and can fix the API code right away.

Every run is also saved as a Test Report inside the portal, so you can open the UI and see the exact status codes, latencies, assertions, and schema checks yourself.

Test reports

Try it out

If you want to test it locally, the repo includes a built-in mock server so you don't need an external API to play with it:

git clone https://github.com/minuth/open-api-portal.git
cd open-api-portal
cp example.env .env
npm install && npm run dev
Enter fullscreen mode Exit fullscreen mode

Source code and setup docs are on GitHub: github.com/minuth/open-api-portal.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The line that frames the whole design is "the agent should know the API contract, not the credentials or even the raw target URL" — that's the right boundary, and the allowlisted METHOD + /path list is what actually enforces it. Most MCP test setups hand the agent curl and a .env file and hope it behaves, which is exactly how a hallucinated path ends up hitting a DELETE on an admin endpoint.

One question on the allowlist: when a new endpoint lands in the spec, does the suite fail closed — the endpoint stays invisible until someone explicitly allowlists it? And for chained flows, when a step captures a value with {{var}} to pass into the next call, does the agent ever see the captured value, or does that substitution happen server-side too?