# Graph topological sort

`graph-topological-sort` · version 1.0.0 · Graphs & scheduling · free, no key needed

Return a Kahn topological order of a directed acyclic graph, breaking ready-set ties by declared node index.

**Use when you need to: graph topological sort · kahn topological order · linearize dag dependencies.**

## Supported

- graph topological sort
- kahn topological order
- linearize dag dependencies

## Not supported

- partial topological order of a subgraph
- weighted edges
- lexicographic-by-id ready-set ties
- undirected topological sort

## Behavior

- Input is a directed simple graph: unique non-empty node id strings and unique {from, to} edges whose endpoints both appear in nodes.
- Edge from→to is a prerequisite: from must complete before to, so from appears before to in the order.
- The order is Kahn topological sort: repeatedly emit the remaining node of indegree 0 with the smallest declared index in the nodes array.
- Ready-set ties use declared node index only; node ids are not ordered lexicographically.
- If a cycle exists, including a self-loop, run throws invalid_input with a message containing "cycle".
- The output order lists every node id exactly once. The empty graph returns { order: [] }.

## Input

- `nodes` (array of string, required): max items 2000; each min length 1; each max length 256
- `edges` (array of object, required): max items 10000

## Output

- `order` (array of string, required)

## Limits

- max nodes: 2000
- max edges: 10000
- max node id bytes: 256

## Example

Request input:

```json
{
  "nodes": [
    "deploy",
    "build",
    "lint",
    "test"
  ],
  "edges": [
    {
      "from": "build",
      "to": "test"
    },
    {
      "from": "lint",
      "to": "test"
    },
    {
      "from": "test",
      "to": "deploy"
    }
  ]
}
```

Response:

```json
{
  "result": {
    "order": [
      "build",
      "lint",
      "test",
      "deploy"
    ]
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "graph-topological-sort",
  "version": "1.0.0",
  "input": {
    "nodes": [
      "deploy",
      "build",
      "lint",
      "test"
    ],
    "edges": [
      {
        "from": "build",
        "to": "test"
      },
      {
        "from": "lint",
        "to": "test"
      },
      {
        "from": "test",
        "to": "deploy"
      }
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/graph-topological-sort/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"nodes":["deploy","build","lint","test"],"edges":[{"from":"build","to":"test"},{"from":"lint","to":"test"},{"from":"test","to":"deploy"}]}'
```

The machine-readable contract is at [/v1/tools/graph-topological-sort/versions/1.0.0](/v1/tools/graph-topological-sort/versions/1.0.0).

### CLI

```sh
node cli.mjs run graph-topological-sort 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Graph DAG longest path](/tools/graph-dag-longest-path): Return a longest node-weighted path in a DAG, where path weight is the sum of node durations.
- [Graph dependency impact](/tools/graph-dependency-impact): List nodes downstream of a removed set: not themselves removed, reachable by a directed path of length at least 1.
- [Graph transitive reduction](/tools/graph-transitive-reduction): Compute the transitive reduction of a DAG: keep an edge u→v iff the original graph has no directed path from u to v of length at least 2.
- [Schedule ASAP layers](/tools/schedule-asap-layers): Partition a DAG into ASAP topological generations: each layer is the nodes that become ready together.
- [Graph ancestors](/tools/graph-ancestors): List every ancestor of given targets, including the targets, in reverse-graph BFS discovery order.
- [Graph connected components](/tools/graph-connected-components): Partition a directed simple graph into undirected connected components, ignoring edge direction.
