{
  "openapi": "3.1.1",
  "info": {
    "title": "Twinworks Bottleneck Analysis API",
    "version": "1.0.0",
    "summary": "Find the constraint of a production line and test fixes: ranked bottlenecks with what-if gains.",
    "description": "Every endpoint performs the same calculation as the human page and returns inputs, result, units, assumptions, sources, warnings and computedAt. MCP endpoint: /mcp."
  },
  "servers": [
    {
      "url": "https://twinworksbottleneckanalysis.usefulweb.site"
    }
  ],
  "paths": {
    "/api/v1/calculate": {
      "post": {
        "operationId": "calculate",
        "summary": "Find the constraint of a production line and test fixes: ranked bottlenecks with what-if gains.",
        "description": "Ranks the stations of a Twinworks factory model by how often they constrain the line (active-period method: sole and shifting bottleneck shares), explains the dominant loss (working, breakdowns, setups, waiting for operators) and simulates typical improvements of the top bottleneck — an extra server, a 10 % faster cycle, faster repairs, shorter setups, more operators or a bigger buffer — with paired confidence intervals.\n\nMethodology:\n- Each replication records, per station, every active period (uninterrupted time working, in setup, under repair/maintenance or waiting for an operator; starved and blocked times end a period).\n- At every instant the station with the longest current active period is the bottleneck. Where the periods of consecutive bottlenecks overlap, both are shifting bottlenecks; otherwise the station is the sole bottleneck (Roser, Nakano & Tanaka 2001).\n- Stations are ranked by sole + shifting share (averaged over the window of replication 1, votes = how often a station was the top bottleneck across replications); ties are broken by active percentage.\n- What-if options for the top station are simulated with the same seeds as the baseline (common random numbers); the gain interval is a paired Student-t interval of the throughput differences.\n\nImprovement options are illustrative single changes; real fixes have costs and side effects that the model may not capture.",
        "tags": [
          "apps"
        ],
        "x-mcp-tool": "twinworks_bottleneck_analysis",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TwinworksBottleneckAnalysisInput"
              },
              "examples": {
                "example1": {
                  "summary": "Bottleneck demo (5 stations, tiny buffers)",
                  "value": {
                    "template": "bottleneck-buffer-sizing",
                    "replications": 5,
                    "horizonHours": 8
                  }
                },
                "example2": {
                  "summary": "Job shop with setups on the mill",
                  "value": {
                    "template": "job-shop-routings",
                    "replications": 4,
                    "whatIf": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Calculation result envelope.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TwinworksBottleneckAnalysisResult"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON body.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Invalid input (see errors[]).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded (see Retry-After).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/tool": {
      "get": {
        "operationId": "getToolDefinition",
        "summary": "Inputs, output schema and examples for this tool",
        "responses": {
          "200": {
            "description": "Tool definition.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Problem": {
        "type": "object",
        "description": "RFC 9457 problem details. 422 responses include per-field `errors`.",
        "required": [
          "type",
          "title",
          "status",
          "detail"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "detail": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "field",
                "message"
              ],
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "TwinworksBottleneckAnalysisInput": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "model": {
            "title": "Model or project JSON",
            "description": "A Twinworks project (.twinworks.json, format \"twinworks-project\") or a bare model with \"nodes\" and \"links\". Times in seconds, lengths in metres. Leave empty and pick a template instead.",
            "type": "string",
            "maxLength": 400000
          },
          "template": {
            "title": "Or start from a template",
            "type": "string",
            "enum": [
              "",
              "three-station-line",
              "line-with-failures",
              "assembly-two-feeders",
              "job-shop-routings",
              "bottleneck-buffer-sizing",
              "conveyor-packaging",
              "mm1-validation"
            ],
            "default": ""
          },
          "replications": {
            "title": "Replications",
            "type": "integer",
            "minimum": 2,
            "maximum": 20,
            "default": 5
          },
          "horizonHours": {
            "title": "Run length per replication",
            "description": "Overrides the model setting.",
            "type": "number",
            "minimum": 0.1,
            "maximum": 2400,
            "x-unit": "h"
          },
          "whatIf": {
            "title": "Simulate improvement options for the top bottleneck",
            "type": "boolean",
            "default": true
          }
        },
        "required": []
      },
      "TwinworksBottleneckAnalysisResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "tool",
          "computedAt",
          "inputs",
          "summary",
          "result",
          "units",
          "assumptions",
          "sources",
          "warnings",
          "presentation",
          "links"
        ],
        "properties": {
          "schemaVersion": {
            "type": "string",
            "const": "1.0"
          },
          "tool": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "name",
              "version"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "version": {
                "type": "string"
              }
            }
          },
          "computedAt": {
            "type": "string",
            "format": "date-time"
          },
          "inputs": {
            "type": "object",
            "description": "Normalised inputs actually used, including applied defaults."
          },
          "summary": {
            "type": "string"
          },
          "result": {
            "type": "object",
            "required": [
              "primary",
              "ranking",
              "recommendations",
              "baselineThroughput",
              "whatIf"
            ],
            "properties": {
              "primary": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "primaryName": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "ranking": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "rank",
                    "id",
                    "name",
                    "sole",
                    "shifting",
                    "total",
                    "activePct",
                    "utilization",
                    "dominantState",
                    "votes"
                  ],
                  "properties": {
                    "rank": {
                      "type": "integer"
                    },
                    "id": {
                      "type": "string"
                    },
                    "name": {
                      "type": "string"
                    },
                    "sole": {
                      "type": "number"
                    },
                    "shifting": {
                      "type": "number"
                    },
                    "total": {
                      "type": "number"
                    },
                    "activePct": {
                      "type": "number"
                    },
                    "utilization": {
                      "type": "number"
                    },
                    "dominantState": {
                      "type": "string"
                    },
                    "votes": {
                      "type": "integer"
                    }
                  }
                }
              },
              "recommendations": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "baselineThroughput": {
                "type": "object",
                "required": [
                  "mean",
                  "lo",
                  "hi",
                  "halfWidth",
                  "n"
                ],
                "properties": {
                  "mean": {
                    "type": "number"
                  },
                  "lo": {
                    "type": "number"
                  },
                  "hi": {
                    "type": "number"
                  },
                  "halfWidth": {
                    "type": "number"
                  },
                  "n": {
                    "type": "integer"
                  }
                }
              },
              "whatIf": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "id",
                    "label",
                    "throughput",
                    "gain",
                    "gainPct"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "label": {
                      "type": "string"
                    },
                    "throughput": {
                      "type": "object",
                      "required": [
                        "mean",
                        "lo",
                        "hi",
                        "halfWidth",
                        "n"
                      ],
                      "properties": {
                        "mean": {
                          "type": "number"
                        },
                        "lo": {
                          "type": "number"
                        },
                        "hi": {
                          "type": "number"
                        },
                        "halfWidth": {
                          "type": "number"
                        },
                        "n": {
                          "type": "integer"
                        }
                      }
                    },
                    "gain": {
                      "type": "object",
                      "required": [
                        "mean",
                        "lo",
                        "hi",
                        "halfWidth",
                        "n"
                      ],
                      "properties": {
                        "mean": {
                          "type": "number"
                        },
                        "lo": {
                          "type": "number"
                        },
                        "hi": {
                          "type": "number"
                        },
                        "halfWidth": {
                          "type": "number"
                        },
                        "n": {
                          "type": "integer"
                        }
                      }
                    },
                    "gainPct": {
                      "type": "number"
                    }
                  }
                }
              }
            }
          },
          "units": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "assumptions": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "title"
              ],
              "properties": {
                "title": {
                  "type": "string"
                },
                "url": {
                  "type": "string"
                },
                "publisher": {
                  "type": "string"
                },
                "retrievedAt": {
                  "type": "string"
                },
                "note": {
                  "type": "string"
                }
              }
            }
          },
          "warnings": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "presentation": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Display blocks used by the human page (same data)."
          },
          "links": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "human",
              "api",
              "openapi",
              "methodology"
            ],
            "properties": {
              "human": {
                "type": "string",
                "format": "uri"
              },
              "api": {
                "type": "string",
                "format": "uri"
              },
              "openapi": {
                "type": "string",
                "format": "uri"
              },
              "methodology": {
                "type": "string",
                "format": "uri"
              }
            }
          }
        }
      }
    }
  }
}