튜토리얼 7 / 12
API / MCP로 자동화
고급 15분
스크립트와 AI 에이전트에서 Twinworks 엔진을 실행해 보세요. 실제 JSON 요청과 응답으로 모델을 시뮬레이션하고, 병목 순위를 매기고, 버퍼를 스윕·최적화하고, 라인을 밸런싱하고, 대기행렬 공식을 검증해요.
워크스페이스의 시뮬레이션 엔진은 usefulweb API와 MCP 서버에서 도구 6개를 구동하는 TypeScript 코드와 똑같아요. 일곱 번째 도구인 자재 이동 스케줄러는 공유 차량, 운송 일정, 모델 검사에서 다뤄요. 결과는 결정적이에요. 같은 모델과 시드를 쓰면 앱에서든 서버에서든 항상 같은 숫자가 나와요.
| 도구 ID (REST) | MCP 이름 | 기능 |
|---|---|---|
twinworks-simulate | twinworks_simulate | 모델 + 실행 시간 + 시드 + 반복 횟수 → 신뢰구간이 포함된 KPI, 스테이션별 통계, 병목, 비용 |
twinworks-bottleneck-analysis | twinworks_bottleneck_analysis | 병목 순위(활성 기간 방법) + What-if 개선 효과 + 권장 사항 |
twinworks-buffer-sweep | twinworks_buffer_sweep | 버퍼 크기(또는 다른 파라미터) 스윕 → 처리량 곡선과 무릎점 |
twinworks-buffer-allocation | twinworks_buffer_allocation | 정해진 수의 버퍼 자리를 처리량이 최대가 되도록 배분(탐욕 알고리즘, 공통 난수) |
twinworks-line-balance | twinworks_line_balance | 작업 + 선행 관계 + 택트 → 스테이션 배정, 효율, 평활 지수 |
twinworks-queue-formulas | twinworks_queue_formulas | M/M/1, M/M/c, M/D/1, M/G/1, Kingman — 원하면 시뮬레이션으로 검증 |
워크스페이스에서 도움말 → API / MCP로 자동화를 열면 지금 작업 중인 모델에 맞춘 요청을 바로 복사할 수 있어요.

1. 템플릿 시뮬레이션
모델을 다루는 도구는 모두 template ID나 model을 받아요. 모델은 파일 → 내보내기 → 모델 JSON (활성 시나리오)에서 내보낸 JSON을 문자열로 넘기면 돼요.
curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-simulate/calculate \
-H 'content-type: application/json' \
-d '{"template":"three-station-line","horizonHours":8,"replications":5,"seed":42}'
응답(일부 생략):
{
"summary": "3-station line: 64.1 ± 3.4 good parts/h (95 % CI, 5 replications), mean lead time 304 s, WIP 5.53; bottleneck: Mill.",
"result": {
"replications": 5,
"seeds": [42, 3029642774, 2605890936, 2032093786, 1137000544],
"throughputPerHour": { "mean": 64.1333, "lo": 60.7384, "hi": 67.5282, "halfWidth": 3.3949, "n": 5 },
"little": { "L": 6.8785, "lambdaPerHour": 66.6667, "W": 369.285, "lambdaW": 6.8386, "relError": 0.00581, "ok": true },
"stations": [
{ "id": "M2", "name": "Mill", "utilization": 0.9126,
"states": { "busy": 0.9126, "setup": 0, "waitop": 0, "blocked": 0, "idle": 0.0874, "down": 0, "maint": 0, "off": 0 },
"availability": 1, "performance": 0.903, "quality": 1, "oee": 0.903 }
],
"bottleneck": { "primary": "M2", "ranking": [ { "id": "M2", "name": "Mill", "sole": 0.8252, "shifting": 0.0606, "total": 0.8858 } ] },
"openInAppUrl": "https://twinworks.usefulweb.site/app#project=…"
}
}
seeds에는 반복마다 쓴 시드가 모두 들어 있어서 어떤 실행이든 똑같이 재현할 수 있어요. little은 측정한 WIP를 첫 번째 반복의 처리량 × 리드 타임과 비교해요(리틀의 법칙). 모델이 맞는지 빠르게 확인하는 방법이에요. openInAppUrl을 열면 워크스페이스에서 모델이 열려요.
2. 직접 만든 모델을 손으로 검산하기
결정적 2기계 라인을 예로 들어 볼게요. 공급은 무제한, M1 = 30 s, 3자리 버퍼, M2 = 40 s예요. 이 모델을 two-machines.json으로 저장하세요.
{
"name": "Two machines",
"settings": { "horizon": 7200, "warmup": 600 },
"nodes": [
{ "id": "S", "type": "source", "arrival": "saturated" },
{ "id": "M1", "type": "machine", "cycle": { "type": "constant", "value": 30 } },
{ "id": "B", "type": "buffer", "capacity": 3 },
{ "id": "M2", "type": "machine", "cycle": { "type": "constant", "value": 40 } },
{ "id": "OUT", "type": "sink" }
],
"links": [ { "from": "S", "to": "M1" }, { "from": "M1", "to": "B" }, { "from": "B", "to": "M2" }, { "from": "M2", "to": "OUT" } ]
}
model 필드는 텍스트(문자열로 된 JSON)라서 파일을 jq로 끼워 넣어요.
jq -n --rawfile m two-machines.json '{model: $m, replications: 1}' |
curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-simulate/calculate \
-H 'content-type: application/json' --data @-
결과: "summary": "Two machines: 90 good parts/h (single run), mean lead time 240 s, WIP 6; bottleneck: Machine M2." (기계 2대: 양품 90개/h(단일 실행), 평균 리드 타임 240 s, WIP 6, 병목: 기계 M2)
결정적 라인에서 이론으로 계산한 값과 정확히 같아요. 처리량 = 1 / 병목 사이클 타임 = 3600 / 40 = 90 parts/h예요. M1은 30/40 = 75 % 동안 가동되고 나머지 25 %는 막혀 있고, M2는 100 % 가동돼요. 리틀의 법칙도 정확히 맞아요. L = λ·W = (90 / 3600 s) × 240 s = 시스템 안에 6 parts가 있어요.
분포 유형: constant {value}, uniform {min,max}, triangular {min,mode,max}, normal {mean,sd}(0에서 잘림), exponential {mean}, lognormal {mean,sd}. 전체 형식은 /project.schema.json에서 볼 수 있어요.
3. 병목 순위 매기기와 개선안 검증
curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-bottleneck-analysis/calculate \
-H 'content-type: application/json' \
-d '{"template":"bottleneck-buffer-sizing","replications":3}'
{
"summary": "\"OP30 Hone\" is the main bottleneck (58.3% of the time; best tested fix: Add a parallel server at \"OP30 Hone\" (1 → 2), +7.85 parts/h).",
"result": {
"primary": "M3",
"ranking": [
{ "rank": 1, "id": "M3", "name": "OP30 Hone", "sole": 0.4043, "shifting": 0.1788, "total": 0.5831, "utilization": 0.8477 },
{ "rank": 2, "id": "M2", "name": "OP20 Bore", "sole": 0.2613, "shifting": 0.1628, "total": 0.4241, "utilization": 0.7929 }
],
"recommendations": [ "\"OP30 Hone\" is the bottleneck 58.3% of the time (40.4% sole, 17.9% shifting): …" ]
}
}
4. 버퍼 스윕
{ "template": "bottleneck-buffer-sizing", "target": "B2", "parameter": "capacity", "from": 1, "to": 8, "step": 1, "replications": 4 }
이 요청을 /api/v1/tools/twinworks-buffer-sweep/calculate로 POST하세요.
{
"summary": "Throughput ranges from 99.6 to 105.8 parts/h over buffer capacity 1…8 at B2; knee point at 3.",
"result": {
"points": [
{ "value": 1, "throughput": { "mean": 99.5682, "lo": 95.1451, "hi": 103.9913 }, "wip": { "mean": 7.9107 }, "bottleneck": "M3" },
{ "value": 2, "throughput": { "mean": 102.4318, "lo": 97.7379, "hi": 107.1257 }, "wip": { "mean": 8.6199 }, "bottleneck": "M3" },
{ "value": 3, "throughput": { "mean": 103.7273, "lo": 99.2019, "hi": 108.2527 }, "wip": { "mean": 9.285 }, "bottleneck": "M3" }
]
}
}
모든 스윕 지점에 같은 반복 시드를 쓰기 때문에 곡선이 매끄럽고, 지점 사이의 차이도 의미가 있어요.
5. 버퍼 배분 최적화
{ "template": "bottleneck-buffer-sizing", "total": 8, "replications": 3, "horizonHours": 6 }
이 요청을 /api/v1/tools/twinworks-buffer-allocation/calculate로 POST하세요.
{
"summary": "8 buffer places: B1=1, B2=4, B3=2, B4=1 gives 97.13 parts/h (current allocation 95.2, significant gain); 6 well-placed places already reach the current throughput.",
"result": {
"best": { "capacities": { "B1": 1, "B2": 4, "B3": 2, "B4": 1 } },
"gain": { "mean": 1.9333, "lo": 1.6465, "hi": 2.2202 },
"placesForCurrentThroughput": 6,
"evaluations": 18
}
}
result.model은 용량을 최적화한 모델이라 twinworks-simulate에 그대로 넣을 수 있어요. total을 빼면 지금 있는 자리 수를 그대로 다시 배분해요. buffers(예: "B2,B3")를 지정하면 일부 버퍼만 최적화해요.

6. 라인 밸런싱
{
"tasks": [
{ "id": "A", "time": 60, "predecessors": "" }, { "id": "B", "time": 40, "predecessors": "A" },
{ "id": "C", "time": 55, "predecessors": "B" }, { "id": "D", "time": 50, "predecessors": "A" },
{ "id": "E", "time": 30, "predecessors": "D" }, { "id": "F", "time": 45, "predecessors": "D" },
{ "id": "G", "time": 20, "predecessors": "F" }, { "id": "H", "time": 35, "predecessors": "C,E,G" }
],
"availableMinutes": 450, "demand": 270, "method": "best"
}
→ "4 stations for a takt time of 100 s (theoretical minimum 4), efficiency 83.8%, smoothness index 47.7 s."(택트 타임 100 s에 스테이션 4개(이론적 최소 4), 효율 83.8%, 평활 지수 47.7 s)가 나오고, 스테이션 구성은 [A,B], [D,F], [C,E], [G,H]예요. 결과에는 밸런싱한 라인을 바로 시뮬레이션할 수 있는 model도 들어 있어요.
7. 대기행렬 공식 검증
{ "queue": "mm1", "arrivalsPerHour": 60, "serviceTime": 48, "verify": true, "replications": 10 }
→ "M/M/1: utilization 80%, mean wait 192 s, mean queue 3.2; simulation confirms the result."(M/M/1: 가동률 80%, 평균 대기 시간 192 s, 평균 대기열 길이 3.2, 시뮬레이션으로 결과 확인됨) verification 블록에는 시뮬레이션한 평균 대기 시간 193.4 s와 95 % 신뢰구간 [177.2, 209.6]이 들어 있어요. 이 구간에 정확한 값인 192 s가 포함돼요.
8. AI 에이전트에서 사용하기 (MCP)
MCP 클라이언트(Claude Desktop, Cursor 등)에 MCP 엔드포인트 https://twinworks.usefulweb.site/mcp를 추가하세요. 도구 이름에는 밑줄을 써요.
{
"jsonrpc": "2.0", "id": 2, "method": "tools/call",
"params": {
"name": "twinworks_bottleneck_analysis",
"arguments": { "template": "bottleneck-buffer-sizing", "replications": 3 }
}
}
응답에는 요약이 텍스트로 들어 있고, 전체 JSON은 structuredContent에 있어요. 이런 프롬프트가 잘 통해요. "내 라인을 Twinworks JSON으로 줄게. 병목을 찾고, 그 앞에 있는 버퍼를 1부터 8까지 스윕해서 개선 효과의 95 %를 얻는 가장 작은 크기를 알려 줘."
제한
반복 한 번에 쓸 수 있는 이벤트는 최대 600만 개예요. API는 twinworks-simulate에서 반복을 최대 30회까지, 분석 도구와 스윕 도구에서는 지점당 최대 20회까지 받고, 실행 시간은 최대 2,400시간이에요. 더 큰 분석은 워크스페이스(Web Worker에서 실행돼요)를 쓰거나 작업을 여러 번의 호출로 나누세요.
사용해 볼 준비가 되셨나요?