Traffic Analysis MCP Server

Extend DevTools with AI-powered traffic analysis for code modernization. Infer types from real API responses, generate test fixtures from captured traffic, and validate changes haven't broken API contracts.

circle-info

Need real-time debugging instead? See the DevTools MCP Server for live traffic inspection.

What you'll build:

You: "Capture 2 minutes of traffic while I run my test suite"

AI: [Calling store_traffic tool...]
"Captured 156 requests across 12 unique routes. Ready for analysis."

You: "What endpoints are we hitting?"

AI: [Calling get_routes tool...]
"Found 12 routes:
- /v1/charges (23 requests, POST, all 200s)
- /v1/customers/{id} (18 requests, GET, 200/404)
- /v1/balance (12 requests, GET, all 200s)
..."

You: "Generate types for the charges endpoint"

AI: [Calling generate_types tool...]
"Generated ChargesResponse with 45 typed fields including nested
BillingDetails, PaymentMethodDetails, and Card objects."

You: "Create test fixtures from this traffic"

AI: [Calling generate_test_data tool...]
"Created pytest fixtures with 5 test cases covering 200, 400, and 402 responses."

Available Tools

Tool
Use Case
Best For

store_traffic

Capture live traffic to SQLite cache

Building a corpus before analysis

clear_traffic

Remove old traffic from cache

Cleaning up after sessions

get_traffic_stats

Cache statistics

Checking what's available

get_routes

Discover routes grouped by pattern

Finding endpoints to analyze

get_route_examples

Representative request/response pairs

Understanding payload shapes

infer_schema

JSON Schema from observed traffic

Understanding data contracts

generate_types

Type definitions in multiple formats

Adding types to code

generate_test_data

Test fixtures from real traffic

Creating test cases

compare_traffic

Schema drift detection

Validating changes

All tools work with cached traffic. Capture first with store_traffic, then analyze.

circle-exclamation

Limits: Capture is capped at 5 minutes and 1,000 HTTP transactions per call. Schema inference uses only successful (2xx) responses.


Use Cases

Adding Types to Legacy Code

Capture traffic from your running application, then generate type definitions:

Generating Test Fixtures

Create realistic test data from actual API responses:

Validating API Contracts

Before and after dependency upgrades, compare traffic to detect breaking changes:

Dependency Modernization

Migrating between library versions (e.g., Pydantic v1 to v2):


Prerequisites

  • Linux host with Qtap installed and DevTools enabled

  • Python 3.10+

  • MCP and httpx packages


Create the MCP Server

Step 1: Install Dependencies

Step 2: Create the Server

Save this as traffic_analysis_mcp.py:

Make it executable:


Connect to AI Assistants

Option 1: Codex CLI

Codex CLIarrow-up-right is OpenAI's terminal-based coding assistant that supports MCP servers natively.

Add the MCP server to your config:

Or edit ~/.codex/config.toml directly:

circle-info

Using a virtual environment? Replace python3 with your venv Python path:

Use it:


Option 2: ChatGPT Developer Mode

ChatGPT can connect to MCP servers via Developer Mode connectors. Since ChatGPT requires HTTPS, you'll need to expose your local server.

1. Start the MCP server with HTTP transport:

Create a separate entrypoint file traffic_analysis_http.py to avoid modifying the main script (which would break Codex/Claude stdio mode):

Then run it:

2. Expose your local server via HTTPS tunnel:

3. In ChatGPT:

  • Go to Settings > Connectors > Advanced Settings

  • Enable Developer Mode

  • Add a new connector with your tunnel URL


Option 3: Claude Code

Claude Code supports MCP servers natively via the CLI.

Add the MCP server:

circle-info

Using a virtual environment? Replace python3 with your venv Python path:

Verify it's connected:

Use it in Claude Code:


Example Workflow

Step 1: Start Traffic Generation

Run your application, test suite, or any code that makes HTTP requests:

Step 2: Capture Traffic

While your application is running, ask your AI assistant to capture:

The assistant will call store_traffic(seconds=120) and report what was captured.

circle-info

Note: Traffic capture blocks for the specified duration. During a 2-minute capture, the assistant waits until capture completes before responding.

Step 3: Discover Routes

The assistant calls get_routes() and shows you all endpoints grouped by pattern.

Step 4: Examine Examples

The assistant calls get_route_examples("/v1/charges") to show request/response pairs.

Step 5: Generate Types

The assistant calls generate_types("/v1/charges", format="pydantic_v2") to create type definitions.

Step 6: Create Test Fixtures

The assistant calls generate_test_data("/v1/charges", format="pytest") to generate test code.


Example Prompts

Route Discovery:

  • "What endpoints are we hitting?"

  • "Show me all routes for api.stripe.com"

  • "Which routes have error responses?"

Type Generation:

  • "Generate types for the /users endpoint"

  • "Create Pydantic v1 models for backwards compatibility"

  • "Generate TypedDicts instead of Pydantic models"

  • "What does the response schema look like for /v1/charges?"

Test Generation:

  • "Create test fixtures for the payment API"

  • "Generate JSON test data I can use in my tests"

  • "Build pytest fixtures with examples of error responses"

Schema Validation:

  • "Has the user API schema changed since yesterday?"

  • "Compare recent traffic against the baseline"

  • "Are there any breaking changes in the response format?"

Cache Management:

  • "How much traffic have we captured?"

  • "Clear traffic older than 1 hour"

  • "What's in the cache?"


Example Output

get_routes()

infer_schema()

generate_types() - Pydantic v2


Path Normalization

Routes are automatically normalized to group similar paths:

Raw Path
Normalized Pattern

/users/123

/users/{id}

/users/456

/users/{id}

/orders/abc-def-123-456-789

/orders/{uuid}

/events/2024-01-15

/events/{date}

/sessions/abc123def456789012345678

/sessions/{object_id}

/tokens/sk_test_abc123...

/tokens/{token}

This grouping ensures you get meaningful aggregates even with high-volume traffic containing many unique IDs.


Supported Type Formats

Format
Description
Use Case

json_schema

Raw JSON Schema (RFC draft-07)

Language-agnostic, OpenAPI

pydantic_v2

Pydantic v2 BaseModel

Modern Python

pydantic_v1

Pydantic v1 compatible

Legacy Python

typeddict

typing.TypedDict

Lightweight typing

dataclass

@dataclass

Simple data containers


Troubleshooting

"Cannot connect to DevTools API"

Ensure Qtap is running with DevTools enabled:

Verify it's accessible:

"No traffic captured"

Traffic capture is live and on-demand, not persistent background collection.

  • Ensure traffic is flowing while store_traffic() runs

  • Run your app/tests in one terminal, capture in another

  • Try longer capture times: store_traffic(seconds=120)

"No valid JSON bodies to analyze"

The endpoint may return non-JSON responses (HTML, XML, binary).

  • Check get_route_examples() to see actual response bodies

  • Schema inference only works with JSON content

"MCP server not found"

Check your configuration path:

Ensure the Python path is absolute and the script exists.


Next Steps

Last updated