---
name: azion-generate-graphql-queries-with-the-mcp-server
description: >-
  Ask a coding agent connected to the Azion observe MCP server for a GraphQL query that answers a data goal, then run the query with the GraphQL API.
---

# Generate GraphQL queries with the MCP server

You can ask a coding agent connected to the observe server of the [Azion MCP servers](/en/documentation/devtools/mcp/) for a [GraphQL API](/en/documentation/devtools/graphql/) query that answers a goal, such as the traffic of your workloads by location or the cache status of your images. You then run the query with the GraphQL API and read the rows it returns. To write and run a query without an agent, refer to [GraphQL API quickstart](/en/documentation/devtools/graphql/first-steps/).

---

## Prerequisites

- A coding agent connected to the observe server, `https://observe-mcp.azion.com/mcp`. To connect one, refer to [MCP server quickstart](/en/documentation/devtools/mcp/quickstart/).
- A personal token, to run the query with the GraphQL API. To create one, refer to [Personal tokens](/en/documentation/guides/platform/account-and-billing/personal-tokens/).

---

## Generate a query with your agent

The observe server writes queries with its `create_graphql_query` tool. The tool takes two inputs: `query`, the goal in plain words, and `dataSource`, the data the query reads. A language model writes the query, and the tool runs it against the GraphQL API of your account, with your token, to validate it. These validation queries only read data. To generate a query:

1. **Ask your agent for a query**

   Describe the data you want, the time window, and how to group the rows. Name the data source the goal reads: `real-time-metrics`, `real-time-events`, `accounting`, or `consumption`. The agent adjusts the date ranges and filters to your requirements.

2. **Review the answer of the tool**

   The tool returns a query that runs on your account, or this text when no attempt produces one:

   ```text
   It was not possible to retrieve the requested information with GraphQL.
   ```

   The text then points to the GraphQL API documentation. A language model writes each query, so two calls with the same goal can return different queries. When the tool returns this text, refer to [Troubleshoot the MCP server](/en/documentation/devtools/mcp/troubleshooting/).

3. **Run the query**

   Send the query to the GraphQL API endpoint of its dataset with your personal token, or paste it into GraphiQL. For both routes, refer to [GraphQL API quickstart](/en/documentation/devtools/graphql/first-steps/) and [GraphiQL Playground](/en/documentation/devtools/graphql/graphql-playground/).

4. **Read the response**

   The `data` key of the response holds one array per dataset, with one object per row. For what each field carries, refer to [Real-Time Metrics fields](/en/documentation/devtools/graphql/gql-real-time-metrics-fields/) and [Real-Time Events fields](/en/documentation/devtools/graphql/gql-real-time-events-fields/).

The response carries the rows that answer your goal. The two sections that follow each give a goal, the request to the agent, and a query that answers it, so you can check a proposal or run the query when the tool returns no query.

---

## Query the traffic of your workloads by location

This goal asks for the requests of the last seven days, grouped by the Azion location that received them. Ask your agent:

```text
Create a GraphQL query to show my application's traffic from the last 7 days, grouped by Azion location.
```

A query that answers this goal reads `workloadMetrics`, a metrics dataset, so it runs on the metrics endpoint, `https://api.azion.com/v4/metrics/graphql`. It adds up `requests` per `sourceLocPop`, the location of the Azion server that received the request, and returns up to 10 locations, the ones with the most requests first. Before you run it, replace the two `tsRange` dates with the last seven days:

```graphql
query TrafficByLocation {
  workloadMetrics(
    limit: 10
    filter: {
      tsRange: {begin: "2026-09-28T00:00:00", end: "2026-10-05T00:00:00"}
    }
    aggregate: { sum: requests }
    groupBy: [sourceLocPop]
    orderBy: [sum_DESC]
  ) {
    sourceLocPop
    sum
  }
}
```

The endpoint returns HTTP `200` and one row per location, with the request total in `sum`. The response below is cut after the third row:

```json
{
  "data": {
    "workloadMetrics": [
      {
        "sourceLocPop": "sdu-eqn",
        "sum": 2778
      },
      {
        "sourceLocPop": "cgh-eqn",
        "sum": 2184
      },
      {
        "sourceLocPop": "cgh-act",
        "sum": 1039
      },
      …
    ]
  }
}
```

---

## Query the cache status of your images

This goal asks how the cache answered requests for image files. Ask your agent:

```text
Generate a query to analyze cache hit rates for my images.
```

A query that answers this goal reads `workloadEvents`, an events dataset, so it runs on the events endpoint, `https://api.azion.com/v4/events/graphql`. It keeps the requests whose URI matches `%.jpg`, `%.png`, or `%.webp` in `requestUriLike`, and counts them per `upstreamCacheStatus`. Events datasets keep records for about seven days, so replace the `tsRange` dates with a window inside the last week:

```graphql
query CachePerformance {
  workloadEvents(
    limit: 10
    filter: {
      tsRange: {begin: "2026-09-28T00:00:00", end: "2026-10-05T00:00:00"}
      or: [
        { requestUriLike: "%.jpg" }
        { requestUriLike: "%.png" }
        { requestUriLike: "%.webp" }
      ]
    }
    aggregate: { count: rows }
    groupBy: [upstreamCacheStatus]
    orderBy: [count_DESC]
  ) {
    upstreamCacheStatus
    count
  }
}
```

The endpoint returns HTTP `200` and one row per cache status, with the request count in `count`. The response holds:

```json
{
  "data": {
    "workloadEvents": [
      {
        "upstreamCacheStatus": "MISS",
        "count": 19
      },
      {
        "upstreamCacheStatus": "REVALIDATED",
        "count": 19
      }
    ]
  }
}
```

Compare the count of each status with the total of all rows. For every status value of `upstreamCacheStatus`, such as `HIT` and `MISS`, refer to [Real-Time Events fields](/en/documentation/devtools/graphql/gql-real-time-events-fields/).

---

## Next steps

- [GraphQL API quickstart](/en/documentation/devtools/graphql/first-steps.md): Create a personal token, run a first query, and read the response.
- [Real-Time Metrics fields](/en/documentation/devtools/graphql/gql-real-time-metrics-fields.md): Every field of the metrics datasets, to group and filter the rows of a query.
- [Real-Time Events fields](/en/documentation/devtools/graphql/gql-real-time-events-fields.md): Every field of the events datasets, such as the cache status of each request.
- [Tools and resources](/en/documentation/devtools/mcp/tools.md): The inputs and the answer of every tool the MCP server exposes.
