Skip to content

Commit 06a74ac

Browse files
docs(guides): add adoption guides for cli, rest, and mcp
1 parent 7dbcacd commit 06a74ac

5 files changed

Lines changed: 152 additions & 0 deletions

File tree

.cleo/tasks.db

4 KB
Binary file not shown.

docs/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
* [LLM Agent Guide](guides/llm-agent-guide.md)
3838
* [Schema Extension Guide](guides/schema-extension.md)
3939
* [Compliance Pipeline Guide](guides/compliance-pipeline.md)
40+
* [LAFS for CLI Tools](guides/lafs-for-cli.md)
41+
* [LAFS for REST APIs](guides/lafs-for-rest.md)
42+
* [LAFS for MCP Tool Servers](guides/lafs-for-mcp.md)
4043

4144
## Implementation Guides
4245

docs/guides/lafs-for-cli.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# LAFS for CLI Tools
2+
3+
Use LAFS envelopes to make CLI output deterministic for scripts and agents.
4+
5+
## Producer pattern
6+
7+
```typescript
8+
import { createEnvelope, enforceCompliance } from "@cleocode/lafs-protocol";
9+
10+
export function buildCliEnvelope(requestId: string) {
11+
const envelope = createEnvelope({
12+
success: true,
13+
result: { items: ["a", "b"] },
14+
meta: {
15+
operation: "cli.items.list",
16+
requestId,
17+
transport: "cli",
18+
strict: true,
19+
mvi: "standard",
20+
contextVersion: 0,
21+
},
22+
});
23+
24+
const gate = enforceCompliance(envelope, {
25+
checkConformance: true,
26+
requireJsonOutput: true,
27+
flags: { jsonFlag: true },
28+
});
29+
30+
if (!gate.ok) {
31+
throw new Error(JSON.stringify(gate.issues));
32+
}
33+
34+
return envelope;
35+
}
36+
```
37+
38+
## Consumer pattern
39+
40+
```typescript
41+
import { LafsError, parseLafsResponse } from "@cleocode/lafs-protocol";
42+
43+
try {
44+
const result = parseLafsResponse<{ items: string[] }>(envelopeJson);
45+
console.log(result.items.length);
46+
} catch (error) {
47+
if (error instanceof LafsError) {
48+
console.error(error.code, error.message);
49+
}
50+
}
51+
```
52+
53+
## Diagnostic checks
54+
55+
```bash
56+
lafs-conformance --envelope ./envelope.json
57+
lafs-conformance --flags ./flags.json
58+
```

docs/guides/lafs-for-mcp.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# LAFS for MCP Tool Servers
2+
3+
For MCP servers, normalize `CallToolResult` into LAFS envelopes.
4+
5+
## Wrapper pattern
6+
7+
```typescript
8+
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
9+
import { enforceCompliance, wrapMCPResult } from "@cleocode/lafs-protocol";
10+
11+
export function wrapToolResult(result: CallToolResult, toolName: string) {
12+
const envelope = wrapMCPResult(result, `tools/${toolName}`);
13+
14+
const gate = enforceCompliance(envelope, { checkConformance: true });
15+
if (!gate.ok) {
16+
throw new Error(JSON.stringify(gate.issues));
17+
}
18+
19+
return envelope;
20+
}
21+
```
22+
23+
## Parse downstream
24+
25+
```typescript
26+
import { parseLafsResponse } from "@cleocode/lafs-protocol";
27+
28+
const payload = parseLafsResponse(envelope);
29+
```
30+
31+
## Notes
32+
33+
- `wrapMCPResult` converts MCP content/error signals into the LAFS success/error model.
34+
- Keep transport details in `_extensions`; keep core semantics in envelope fields.

docs/guides/lafs-for-rest.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# LAFS for REST APIs
2+
3+
Adopt LAFS at HTTP boundaries so every endpoint returns one parseable shape.
4+
5+
## Express example
6+
7+
```typescript
8+
import crypto from "node:crypto";
9+
import express from "express";
10+
import { createEnvelope, enforceCompliance } from "@cleocode/lafs-protocol";
11+
12+
const app = express();
13+
14+
app.get("/api/users", (_req, res) => {
15+
const envelope = createEnvelope({
16+
success: true,
17+
result: { users: [] },
18+
meta: {
19+
operation: "users.list",
20+
requestId: crypto.randomUUID(),
21+
transport: "http",
22+
strict: true,
23+
mvi: "standard",
24+
contextVersion: 0,
25+
},
26+
});
27+
28+
const gate = enforceCompliance(envelope, { checkConformance: true });
29+
if (!gate.ok) {
30+
res.status(500).json({ issues: gate.issues });
31+
return;
32+
}
33+
34+
res.json(envelope);
35+
});
36+
```
37+
38+
## Error response pattern
39+
40+
```typescript
41+
import { createEnvelope } from "@cleocode/lafs-protocol";
42+
43+
const errorEnvelope = createEnvelope({
44+
success: false,
45+
error: {
46+
code: "E_NOT_FOUND_RESOURCE",
47+
message: "user not found",
48+
category: "NOT_FOUND",
49+
retryable: false,
50+
},
51+
meta: {
52+
operation: "users.get",
53+
requestId: "req_123",
54+
transport: "http",
55+
},
56+
});
57+
```

0 commit comments

Comments
 (0)