capable-kit 0.0.1-alpha.3 → 0.0.1-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -3
- package/dist/ai-sdk.d.mts +2 -2
- package/dist/ai-sdk.mjs +1 -1
- package/dist/app.d.mts +1 -1
- package/dist/app.mjs +1 -1
- package/dist/bin.mjs +17 -16
- package/dist/bin.mjs.map +1 -1
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/core.d.mts +3 -3
- package/dist/core.mjs +2 -2
- package/dist/eve.d.mts +1 -1
- package/dist/eve.mjs +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/http.mjs +1 -1
- package/dist/{index-BB5ogMv-.d.mts → index-Be8-UVBC.d.mts} +49 -8
- package/dist/{index-B2ru1zGQ.d.mts → index-D2CRRUzH.d.mts} +6 -6
- package/dist/{index-B_I9n6gN.d.mts → index-Dbc4g9WM.d.mts} +4 -3
- package/dist/{index-AqKbVZ8Z.d.mts → index-DwXlaTXI.d.mts} +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -3
- package/dist/mcp.d.mts +2 -2
- package/dist/mcp.mjs +1 -1
- package/dist/{mount--YHrgRHJ.mjs → mount-xvMsjz1B.mjs} +65 -27
- package/dist/mount-xvMsjz1B.mjs.map +1 -0
- package/dist/runtime-support.d.mts +1 -1
- package/dist/runtime-support.mjs +6 -6
- package/dist/runtime-support.mjs.map +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/server.mjs +2 -2
- package/dist/{src-DX2xrplP.mjs → src-BF70JLxI.mjs} +24 -13
- package/dist/src-BF70JLxI.mjs.map +1 -0
- package/dist/{src-3UaBqI8H.mjs → src-C2MGIlT0.mjs} +17 -18
- package/dist/src-C2MGIlT0.mjs.map +1 -0
- package/dist/{src-DtSY5fzY.mjs → src-CDxL6w3d.mjs} +7 -7
- package/dist/{src-DtSY5fzY.mjs.map → src-CDxL6w3d.mjs.map} +1 -1
- package/dist/{src-BjBxOv7x.mjs → src-Db6-XSnU.mjs} +20 -12
- package/dist/src-Db6-XSnU.mjs.map +1 -0
- package/dist/{src-B53ZbNtC.mjs → src-DdV82An-.mjs} +137 -3
- package/dist/src-DdV82An-.mjs.map +1 -0
- package/dist/{subscription-McaLyHM2.d.mts → subscription-Bq792Ad2.d.mts} +2 -2
- package/docs/CONTEXT.md +113 -0
- package/docs/README.md +28 -0
- package/docs/internal/README.md +16 -0
- package/docs/internal/adr/0001-capability-is-the-semantic-boundary.md +24 -0
- package/docs/internal/architecture.md +134 -0
- package/docs/internal/capability-first-model.md +160 -0
- package/docs/internal/implementation-friction.md +705 -0
- package/docs/internal/why-capable.md +94 -0
- package/docs/site/capable-orpc-and-trpc.mdx +133 -0
- package/docs/site/concepts/apps.mdx +217 -0
- package/docs/site/concepts/architecture.mdx +83 -0
- package/docs/site/concepts/capabilities.mdx +249 -0
- package/docs/site/concepts/errors.mdx +93 -0
- package/docs/site/concepts/presentations.mdx +161 -0
- package/docs/site/hosting/any-fetch-host.mdx +74 -0
- package/docs/site/hosting/next-js.mdx +77 -0
- package/docs/site/hosting/standalone-server.mdx +94 -0
- package/docs/site/introduction.mdx +259 -0
- package/docs/site/patterns/composition.mdx +142 -0
- package/docs/site/patterns/remote-apps.mdx +117 -0
- package/docs/site/patterns/subscriptions.mdx +87 -0
- package/docs/site/quickstart.mdx +386 -0
- package/docs/site/reference/packages-and-entry-points.mdx +72 -0
- package/docs/site/reference/surface-metadata.mdx +84 -0
- package/docs/site/reference/the-cap-cli.mdx +81 -0
- package/docs/site/surfaces/ai-sdk.mdx +94 -0
- package/docs/site/surfaces/cli.mdx +138 -0
- package/docs/site/surfaces/eve.mdx +121 -0
- package/docs/site/surfaces/http-and-openapi.mdx +111 -0
- package/docs/site/surfaces/mcp.mdx +110 -0
- package/docs/site/surfaces/react-query.mdx +80 -0
- package/docs/site/surfaces/typescript.mdx +123 -0
- package/docs/site/surfaces.mdx +78 -0
- package/docs/site/why-capable.mdx +99 -0
- package/package.json +13 -3
- package/src/index.test.ts +1 -0
- package/src/index.ts +5 -1
- package/src/package.test.ts +102 -0
- package/dist/mount--YHrgRHJ.mjs.map +0 -1
- package/dist/src-3UaBqI8H.mjs.map +0 -1
- package/dist/src-B53ZbNtC.mjs.map +0 -1
- package/dist/src-BjBxOv7x.mjs.map +0 -1
- package/dist/src-DX2xrplP.mjs.map +0 -1
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Standalone server"
|
|
3
|
+
description: "Run, build, and preview an app as its own service with the cap CLI."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/hosting/capable-cli", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/hosting/capable-cli" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
Use the toolchain when the app should be its own service. It discovers the project, runs a dev server with watching, and produces a production build.
|
|
13
|
+
|
|
14
|
+
## The server file is optional [#server]
|
|
15
|
+
|
|
16
|
+
A contextless `capable.app.ts` runs on its own. Add `capable.server.ts` when server policy becomes part of the application — context from the request, a specific set of mounts, or middleware around every surface.
|
|
17
|
+
|
|
18
|
+
```ts title="capable.server.ts"
|
|
19
|
+
import { createServer } from "capable-kit/server";
|
|
20
|
+
import definition from "./capable.app";
|
|
21
|
+
|
|
22
|
+
export default createServer(definition);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Without this file, the toolchain generates the server for you. That default mounts the Capable transport, HTTP and OpenAPI, and MCP at`/mcp` — in both `cap dev` and production builds. Adding `capable.server.ts` replaces that default, so list every mount you want, including `mcp()`.
|
|
26
|
+
|
|
27
|
+
Calling `createServer()` yourself is the lower-level choice: with no `mounts` list it exposes only the Capable transport and the HTTP/OpenAPI projection. MCP is opt-in there.
|
|
28
|
+
|
|
29
|
+
## Lifecycle [#commands]
|
|
30
|
+
|
|
31
|
+
```sh title="terminal"
|
|
32
|
+
$ cap dev
|
|
33
|
+
|
|
34
|
+
◆ Todos 1.0.0
|
|
35
|
+
|
|
36
|
+
Local http://localhost:3000
|
|
37
|
+
Capable /_capable
|
|
38
|
+
HTTP /api
|
|
39
|
+
OpenAPI /openapi.json
|
|
40
|
+
MCP /mcp
|
|
41
|
+
|
|
42
|
+
✓ 2 capabilities ready, watching for changes
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```sh title="terminal"
|
|
46
|
+
cap dev # develop with file watching
|
|
47
|
+
cap build # build a standalone Node server
|
|
48
|
+
cap preview # run the built server
|
|
49
|
+
cap build --preset vercel # build for another target
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
| Command | What it does |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `cap dev` | Watches the app's dependency graph, validates the manifest, and serves every derived surface plus a development-only index at / |
|
|
55
|
+
| `cap build` | Writes a deployable build to `.output` |
|
|
56
|
+
| `cap preview` | Runs a `node-server` build locally |
|
|
57
|
+
| `cap cli` | Invokes capabilities in the local project or against a URL |
|
|
58
|
+
|
|
59
|
+
Full options and project discovery rules are in the [CLI reference](/reference/cli).
|
|
60
|
+
|
|
61
|
+
## Growing the boundary [#boundary]
|
|
62
|
+
|
|
63
|
+
Everything specific to running as a service lives in the server file. None of it reaches the app definition, so the same capabilities stay usable from tests, scripts, and other hosts.
|
|
64
|
+
|
|
65
|
+
```ts title="capable.server.ts"
|
|
66
|
+
import { mcp } from "capable-kit/mcp";
|
|
67
|
+
import { protocol, createServer, http } from "capable-kit/server";
|
|
68
|
+
import definition from "./capable.app";
|
|
69
|
+
|
|
70
|
+
export default createServer(definition, {
|
|
71
|
+
context: ({ request }) => ({
|
|
72
|
+
user: authenticate(request),
|
|
73
|
+
db,
|
|
74
|
+
}),
|
|
75
|
+
mounts: [
|
|
76
|
+
protocol({ path: "/_capable" }),
|
|
77
|
+
http({ path: "/api", requireExplicitRoutes: true }),
|
|
78
|
+
mcp({ path: "/mcp" }),
|
|
79
|
+
],
|
|
80
|
+
middleware: async ({ request, next }) => {
|
|
81
|
+
const started = performance.now();
|
|
82
|
+
const response = await next();
|
|
83
|
+
log(request, response, performance.now() - started);
|
|
84
|
+
return response;
|
|
85
|
+
},
|
|
86
|
+
onError: (error) => reportError(error),
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
> **Server middleware is not capability middleware**
|
|
91
|
+
>
|
|
92
|
+
> Server middleware wraps HTTP requests and can inspect headers. Capability middleware wraps execution on every surface and cannot. Keep authentication of a request here, and authorization of a capability in the capability.
|
|
93
|
+
|
|
94
|
+
</DocsPage>
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Introduction"
|
|
3
|
+
description: "Define typed application capabilities and use them across runtime surfaces."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "./_components/docs-page";
|
|
7
|
+
import { Card, Cards } from "./_components/cards";
|
|
8
|
+
import { Tab, Tabs } from "./_components/tabs";
|
|
9
|
+
|
|
10
|
+
export const metadata = docMetadata("/", frontmatter);
|
|
11
|
+
|
|
12
|
+
<DocsPage href="/" frontmatter={frontmatter}>
|
|
13
|
+
|
|
14
|
+
Capable is a framework for defining the operations an application provides—its capabilities—using types and schemas. Add them to an app, then run or call that app in-process, over HTTP, through a CLI, or with AI tools.
|
|
15
|
+
|
|
16
|
+
## 1. Define a capability [#capability]
|
|
17
|
+
|
|
18
|
+
Start with an id, input schema, and handler. A capability has no HTTP method, command name, or framework attached.
|
|
19
|
+
|
|
20
|
+
```ts title="capabilities/create-todo.ts"
|
|
21
|
+
import { capability } from "capable-kit";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
export const createTodo = capability({
|
|
25
|
+
id: "todos.create",
|
|
26
|
+
input: z.object({ title: z.string().min(1) }),
|
|
27
|
+
handler: ({ input }) => ({ id: "1", title: input.title }),
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 2. Put it in an app [#app]
|
|
32
|
+
|
|
33
|
+
An app gives capabilities a path: `todos.create`.
|
|
34
|
+
|
|
35
|
+
```ts title="capable.app.ts"
|
|
36
|
+
import { defineApp } from "capable-kit";
|
|
37
|
+
import { createTodo } from "./capabilities/create-todo";
|
|
38
|
+
|
|
39
|
+
export default defineApp({
|
|
40
|
+
name: "Todos",
|
|
41
|
+
version: "1.0.0",
|
|
42
|
+
capabilities: {
|
|
43
|
+
todos: { create: createTodo },
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Add output schemas, errors, middleware, or surface metadata only when the app needs them.
|
|
49
|
+
|
|
50
|
+
## 3. Initialize the app [#choose]
|
|
51
|
+
|
|
52
|
+
Choose where the app runs. The in-process option has no network boundary; the other options accept requests from outside the process.
|
|
53
|
+
|
|
54
|
+
<Tabs id="introduction-hosting" label="Choose a host">
|
|
55
|
+
<Tab label="In-Process">
|
|
56
|
+
|
|
57
|
+
Use the executable app directly in a script, test, server action, or agent. No HTTP server is started.
|
|
58
|
+
|
|
59
|
+
```ts title="app.ts"
|
|
60
|
+
import { createApp } from "capable-kit";
|
|
61
|
+
import definition from "./capable.app";
|
|
62
|
+
|
|
63
|
+
export const app = createApp(definition);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
</Tab>
|
|
67
|
+
<Tab label="Capable CLI">
|
|
68
|
+
|
|
69
|
+
The zero-config standalone option. With only `capable.app.ts`, the toolchain mounts Capable, HTTP/OpenAPI, and MCP for you.
|
|
70
|
+
|
|
71
|
+
```sh title="terminal"
|
|
72
|
+
$ cap dev
|
|
73
|
+
|
|
74
|
+
◆ Todos 1.0.0
|
|
75
|
+
|
|
76
|
+
Local http://localhost:3000
|
|
77
|
+
Capable /_capable
|
|
78
|
+
HTTP /api
|
|
79
|
+
OpenAPI /openapi.json
|
|
80
|
+
MCP /mcp
|
|
81
|
+
|
|
82
|
+
✓ 1 capability ready, watching for changes
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
</Tab>
|
|
86
|
+
<Tab label="Next.js">
|
|
87
|
+
|
|
88
|
+
Keep your existing Next.js application, authentication, and deployment. Mount a Capable server in App Router handlers.
|
|
89
|
+
|
|
90
|
+
```ts title="capable/server.ts"
|
|
91
|
+
import { createNextRouteHandlers } from "@capable/next";
|
|
92
|
+
import { mcp } from "capable-kit/mcp";
|
|
93
|
+
import { createServer, http, protocol } from "capable-kit/server";
|
|
94
|
+
import definition from "../capable.app";
|
|
95
|
+
|
|
96
|
+
const server = createServer(definition, {
|
|
97
|
+
mounts: [protocol(), http(), mcp()],
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
export const { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT } =
|
|
101
|
+
createNextRouteHandlers(server.fetch);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Re-export those handlers at the `/api`, `/_capable`, `/openapi.json`, and `/mcp` URLs. The [Next.js guide](/hosting/next) shows the complete route layout.
|
|
105
|
+
|
|
106
|
+
</Tab>
|
|
107
|
+
<Tab label="Any Fetch host">
|
|
108
|
+
|
|
109
|
+
Use the server directly in Bun, Hono, H3, or another Fetch-compatible runtime.
|
|
110
|
+
|
|
111
|
+
```ts title="server.ts"
|
|
112
|
+
import { mcp } from "capable-kit/mcp";
|
|
113
|
+
import { createServer, http, protocol } from "capable-kit/server";
|
|
114
|
+
import definition from "./capable.app";
|
|
115
|
+
|
|
116
|
+
const server = createServer(definition, {
|
|
117
|
+
mounts: [protocol(), http(), mcp()],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
Bun.serve({ port: 3000, fetch: server.fetch });
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
</Tab>
|
|
124
|
+
</Tabs>
|
|
125
|
+
|
|
126
|
+
## 4. Use the app [#consuming]
|
|
127
|
+
|
|
128
|
+
Call the same capability in-process or through an endpoint exposed by the app above.
|
|
129
|
+
|
|
130
|
+
<Tabs id="introduction-consuming" label="Choose a consumer">
|
|
131
|
+
<Tab label="TypeScript">
|
|
132
|
+
|
|
133
|
+
```ts title="app.ts"
|
|
134
|
+
import { app } from "./app";
|
|
135
|
+
|
|
136
|
+
const todo = await app.todos.create({ title: "Read the docs" });
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
</Tab>
|
|
140
|
+
<Tab label="Capable CLI">
|
|
141
|
+
|
|
142
|
+
Run the app in this directory. The CLI imports it and calls it in-process.
|
|
143
|
+
|
|
144
|
+
```sh title="terminal"
|
|
145
|
+
$ cap cli todos create --title "Read the docs"
|
|
146
|
+
|
|
147
|
+
{
|
|
148
|
+
"id": "1",
|
|
149
|
+
"title": "Read the docs"
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Point the CLI at an app that is already running or deployed.
|
|
154
|
+
|
|
155
|
+
```sh title="terminal"
|
|
156
|
+
$ cap cli --app http://localhost:3000 todos create --title "Read the docs"
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
</Tab>
|
|
160
|
+
<Tab label="HTTP">
|
|
161
|
+
|
|
162
|
+
```sh title="terminal"
|
|
163
|
+
# Against a host that mounts http():
|
|
164
|
+
$ curl -X POST http://localhost:3000/api/todos/create \
|
|
165
|
+
-H 'content-type: application/json' \
|
|
166
|
+
-d '{"title":"Read the docs"}'
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
</Tab>
|
|
170
|
+
<Tab label="MCP">
|
|
171
|
+
|
|
172
|
+
Connect any Streamable HTTP MCP client to `http://localhost:3000/mcp`. It discovers `todos__create` with the same input schema.
|
|
173
|
+
|
|
174
|
+
```json title="tools/call params"
|
|
175
|
+
{
|
|
176
|
+
"name": "todos__create",
|
|
177
|
+
"arguments": { "title": "Read the docs" }
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
</Tab>
|
|
182
|
+
<Tab label="AI SDK">
|
|
183
|
+
|
|
184
|
+
```ts title="agent.ts"
|
|
185
|
+
import { generateText } from "ai";
|
|
186
|
+
import { createAiSdkTools } from "capable-kit/ai-sdk";
|
|
187
|
+
import { app } from "./app";
|
|
188
|
+
|
|
189
|
+
await generateText({
|
|
190
|
+
model: "anthropic/claude-sonnet-4.5",
|
|
191
|
+
tools: await createAiSdkTools(app),
|
|
192
|
+
prompt: "Create a todo to read the docs.",
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
</Tab>
|
|
197
|
+
<Tab label="Eve">
|
|
198
|
+
|
|
199
|
+
```ts title="agent/tools/todos.ts"
|
|
200
|
+
import { createEveTools } from "capable-kit/eve";
|
|
201
|
+
import { app } from "../../app";
|
|
202
|
+
|
|
203
|
+
export default createEveTools(app);
|
|
204
|
+
// Eve resolves todos__create as a native tool at each step.
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
</Tab>
|
|
208
|
+
<Tab label="React Query">
|
|
209
|
+
|
|
210
|
+
```tsx title="add-todo.tsx"
|
|
211
|
+
"use client";
|
|
212
|
+
|
|
213
|
+
import { createReactQuery } from "@capable/react-query";
|
|
214
|
+
import { useMutation } from "@tanstack/react-query";
|
|
215
|
+
import { createRemoteApp } from "capable-kit";
|
|
216
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
217
|
+
import type definition from "./capable.app";
|
|
218
|
+
|
|
219
|
+
const query = createReactQuery(
|
|
220
|
+
createRemoteApp<typeof definition>(
|
|
221
|
+
createHttpTransport({
|
|
222
|
+
url: () => new URL("/_capable/", window.location.origin),
|
|
223
|
+
}),
|
|
224
|
+
),
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
// Render this component beneath a QueryClientProvider.
|
|
228
|
+
export function AddTodo() {
|
|
229
|
+
const createTodo = useMutation(query.todos.create.mutationOptions());
|
|
230
|
+
|
|
231
|
+
return (
|
|
232
|
+
<button onClick={() => createTodo.mutate({ title: "Read the docs" })}>
|
|
233
|
+
Add todo
|
|
234
|
+
</button>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
</Tab>
|
|
240
|
+
</Tabs>
|
|
241
|
+
|
|
242
|
+
## Where to go next [#next]
|
|
243
|
+
|
|
244
|
+
<Cards>
|
|
245
|
+
<Card href="/quickstart" title="Quickstart">
|
|
246
|
+
Build, run, and deploy an app in four steps.
|
|
247
|
+
</Card>
|
|
248
|
+
<Card href="/concepts/capabilities" title="Capabilities">
|
|
249
|
+
The execution lifecycle every caller shares.
|
|
250
|
+
</Card>
|
|
251
|
+
<Card href="/surfaces" title="Surfaces">
|
|
252
|
+
Pick the interfaces your product actually needs.
|
|
253
|
+
</Card>
|
|
254
|
+
<Card href="/why-capable" title="Why Capable">
|
|
255
|
+
The problem this model is trying to remove.
|
|
256
|
+
</Card>
|
|
257
|
+
</Cards>
|
|
258
|
+
|
|
259
|
+
</DocsPage>
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Composition"
|
|
3
|
+
description: "Merge local and remote apps into one typed tree, and narrow it per boundary."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/patterns/composition", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/patterns/composition" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
A remote app and a local app are the same thing — a typed callable tree — backed by different sources. You merge them when one process needs both: a CLI that drives a server API but also touches this machine's files, credentials, or processes.
|
|
13
|
+
|
|
14
|
+
## The remote app [#remote]
|
|
15
|
+
|
|
16
|
+
The product API, on a server. `createRemoteApp()` takes the server definition as a type parameter, so the client is typed without code generation. It returns immediately; the Capable transport manifest is fetched and validated on the first call. See [remote apps](/patterns/remote-apps).
|
|
17
|
+
|
|
18
|
+
```ts title="remote.ts"
|
|
19
|
+
import { createRemoteApp } from "capable-kit";
|
|
20
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
21
|
+
import type definition from "../server/capable.app";
|
|
22
|
+
|
|
23
|
+
// The product API, running on a server. Typed from the server
|
|
24
|
+
// definition, no client generated. Talks to the Capable transport.
|
|
25
|
+
export const remote = createRemoteApp<typeof definition>(
|
|
26
|
+
createHttpTransport({ url: "https://todos.example.com/_capable/" }),
|
|
27
|
+
);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## The local app [#local]
|
|
31
|
+
|
|
32
|
+
A capability that _must_ run here. Reading a file from disk, writing a token to `~/.config`, or starting a local process can't live on the server, so they become local capabilities. A local capability can still orchestrate the remote app through its typed tree.
|
|
33
|
+
|
|
34
|
+
```ts title="import-todos.ts"
|
|
35
|
+
import { capability } from "capable-kit";
|
|
36
|
+
import { readFile } from "node:fs/promises";
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
import { remote } from "./remote";
|
|
39
|
+
|
|
40
|
+
// This Capability reads a file on this machine, then calls the server.
|
|
41
|
+
// It cannot live on the server — the file is not there.
|
|
42
|
+
const localTodoCapability = capability.withContext<{
|
|
43
|
+
remote: typeof remote;
|
|
44
|
+
}>();
|
|
45
|
+
|
|
46
|
+
export const importTodos = localTodoCapability({
|
|
47
|
+
id: "todos.import",
|
|
48
|
+
title: "Import todos from a file",
|
|
49
|
+
effect: "write",
|
|
50
|
+
input: z.object({ file: z.string() }),
|
|
51
|
+
meta: { cli: { positional: ["file"] } },
|
|
52
|
+
handler: async ({ input, context }) => {
|
|
53
|
+
const lines = (await readFile(input.file, "utf8"))
|
|
54
|
+
.split("\n")
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
for (const title of lines) {
|
|
57
|
+
await context.remote.todos.create({ title });
|
|
58
|
+
}
|
|
59
|
+
return { imported: lines.length };
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```ts title="local.ts"
|
|
65
|
+
import { createApp, defineApp } from "capable-kit";
|
|
66
|
+
import { importTodos } from "./import-todos";
|
|
67
|
+
import { remote } from "./remote";
|
|
68
|
+
|
|
69
|
+
const localDefinition = defineApp({
|
|
70
|
+
name: "Local",
|
|
71
|
+
capabilities: {
|
|
72
|
+
todos: {
|
|
73
|
+
import: importTodos,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export const local = createApp(localDefinition, {
|
|
79
|
+
context: () => ({ remote }),
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Both definitions compose named capabilities under the same `todos` path. When the apps are merged, those branches combine — `todos list` and `todos create` from the server, `todos import` from here.
|
|
84
|
+
|
|
85
|
+
## Merge them into one CLI [#merge]
|
|
86
|
+
|
|
87
|
+
`mergeApps()` combines the two apps into one. Calls dispatch by capability id to whichever app owns them. No proxy server, no logic moved — the import runs in this process, the creates run on the server.
|
|
88
|
+
|
|
89
|
+
```ts title="cli.ts"
|
|
90
|
+
import { mergeApps } from "capable-kit";
|
|
91
|
+
import { createCli } from "capable-kit/cli";
|
|
92
|
+
import { remote } from "./remote";
|
|
93
|
+
import { local } from "./local";
|
|
94
|
+
|
|
95
|
+
// One command tree. Calls dispatch by capability id to whichever
|
|
96
|
+
// app owns them — the server for list and create, this process for import.
|
|
97
|
+
const app = mergeApps(remote, local);
|
|
98
|
+
|
|
99
|
+
process.exit(
|
|
100
|
+
await createCli(app, { invocationName: "mycli" }).run(process.argv.slice(2)),
|
|
101
|
+
);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```sh title="terminal"
|
|
105
|
+
mycli todos --help
|
|
106
|
+
|
|
107
|
+
todos
|
|
108
|
+
|
|
109
|
+
Usage
|
|
110
|
+
mycli todos <command> [options]
|
|
111
|
+
|
|
112
|
+
Commands
|
|
113
|
+
list List todos # runs on the server
|
|
114
|
+
create Create a todo # runs on the server
|
|
115
|
+
import <file> Import todos from a file # runs here
|
|
116
|
+
|
|
117
|
+
mycli todos import ./todos.txt
|
|
118
|
+
# { "imported": 2 }
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Path collisions, duplicate ids, and leaf/subtree conflicts fail when the merged manifest first resolves, not at call time. Namespaces belong in the app definitions, so the final command tree stays visible in source.
|
|
122
|
+
|
|
123
|
+
> **Placement stays explicit**
|
|
124
|
+
>
|
|
125
|
+
> Merging makes calling uniform, not location-transparent. Each capability still executes where it was created — behind the server's context and policy, or in this process. The CLI does not hide which is which; the source code does.
|
|
126
|
+
|
|
127
|
+
## Narrow what a surface sees [#select]
|
|
128
|
+
|
|
129
|
+
The same merged tree can feed more than one surface. Before handing it to an agent, narrow it with `selectApp()`. An omitted `include` means everything; `exclude` wins.
|
|
130
|
+
|
|
131
|
+
```ts title="agent-surface.ts"
|
|
132
|
+
import { selectApp } from "capable-kit";
|
|
133
|
+
|
|
134
|
+
// Keep destructive capabilities away from this surface.
|
|
135
|
+
const agentApp = selectApp(app, {
|
|
136
|
+
include: { todos: { list: true, get: true } },
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
await createMcp(agentApp);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
</DocsPage>
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Remote Apps"
|
|
3
|
+
description: "Use the same typed tree when capabilities execute in another process or deployment."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/patterns/remote-apps", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/patterns/remote-apps" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
A capability describes what something does. It says nothing about where it runs. That is why the same definition can be instantiated as a local app in one process and as a remote app in another, and why the caller writes the same line of code in both.
|
|
13
|
+
|
|
14
|
+
## Local and remote placement [#two-instantiations]
|
|
15
|
+
|
|
16
|
+
`createApp()` places the capabilities in this process. `createRemoteApp()` places them somewhere else, behind a transport. Both return the same typed tree.
|
|
17
|
+
|
|
18
|
+
```ts title="apps.ts"
|
|
19
|
+
import { createApp, createRemoteApp } from "capable-kit";
|
|
20
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
21
|
+
import type definition from "./capable.app";
|
|
22
|
+
|
|
23
|
+
// Runs here, in this process.
|
|
24
|
+
const local = createApp(definition);
|
|
25
|
+
|
|
26
|
+
// Runs there, behind someone else's context and policy.
|
|
27
|
+
const remote = createRemoteApp<typeof definition>(
|
|
28
|
+
createHttpTransport({ url: "https://todos.example.com/_capable/" }),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// The calling code cannot tell them apart.
|
|
32
|
+
export async function openTodos(app: typeof local | typeof remote) {
|
|
33
|
+
return app.todos.list({ status: "open" });
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| | createApp() | createRemoteApp() |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| Needs | The implementation, and context only if one is declared | A transport, and the definition as a type |
|
|
40
|
+
| Executes | In this process | Wherever the server placed it |
|
|
41
|
+
| Manifest | Derived from the definition | Fetched once, then cached, on first call |
|
|
42
|
+
| Failure mode | Throws from the handler | Also transport errors |
|
|
43
|
+
|
|
44
|
+
> **No code generation**
|
|
45
|
+
>
|
|
46
|
+
> The consumer imports `type definition`only. Types are erased at build time, so no server code, no dependency, and no generated client ends up in the caller. Ship the definition's types in a package and any consumer can be typed against it.
|
|
47
|
+
|
|
48
|
+
## What crosses the boundary [#boundary]
|
|
49
|
+
|
|
50
|
+
Two things: the [manifest](/concepts/apps#manifest), which describes the tree — ids, titles, effects, JSON Schemas, declared errors — and individual calls addressed by capability id. Paths are navigation, so renaming a group does not break a client; the id is the contract.
|
|
51
|
+
|
|
52
|
+
A server exposes both with the `protocol()` mount. Nothing else is required to make an app consumable from anywhere.
|
|
53
|
+
|
|
54
|
+
```ts title="capable.server.ts"
|
|
55
|
+
import { protocol, createServer } from "capable-kit/server";
|
|
56
|
+
import definition from "./capable.app";
|
|
57
|
+
|
|
58
|
+
export default createServer(definition, {
|
|
59
|
+
context: ({ request }) => contextFromRequest(request),
|
|
60
|
+
mounts: [protocol({ path: "/_capable" })],
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Because surfaces are built from the manifest, a remote app can produce the same [CLI](/surfaces/cli), [MCP tools](/surfaces/mcp), or [React Query](/surfaces/react-query) bindings as a local one. The projection never learns where execution happens.
|
|
65
|
+
|
|
66
|
+
## Transports [#transports]
|
|
67
|
+
|
|
68
|
+
HTTP is the transport that ships, but it is not privileged. `ApplicationTransport` is an interface with `manifest()`, `call()`, and `subscribe()`, so "remote" can mean a worker, a child process, a message queue, a browser extension port, or a stub in a test.
|
|
69
|
+
|
|
70
|
+
```ts title="worker-transport.ts"
|
|
71
|
+
import type { ApplicationTransport } from "capable-kit/app";
|
|
72
|
+
|
|
73
|
+
// Three methods. Anything that can move JSON can back an App.
|
|
74
|
+
export function createWorkerTransport(worker: Worker): ApplicationTransport {
|
|
75
|
+
return {
|
|
76
|
+
manifest: (options) => send(worker, { kind: "manifest" }, options),
|
|
77
|
+
call: (call) => send(worker, { kind: "call", call }),
|
|
78
|
+
subscribe: (call) => stream(worker, call),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```ts title="main.ts"
|
|
84
|
+
import { createRemoteApp } from "capable-kit";
|
|
85
|
+
import type definition from "./capable.app";
|
|
86
|
+
|
|
87
|
+
const app = createRemoteApp<typeof definition>(
|
|
88
|
+
createWorkerTransport(new Worker("./worker.js")),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
await app.image.resize({ url, width: 512 }); // runs off the main thread
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Credentials and base URLs [#credentials]
|
|
95
|
+
|
|
96
|
+
The transport owns everything about reaching the server, so capabilities never see tokens or hostnames. Both `url` and `headers` accept a function, which is resolved per request — enough for token refresh, environment switching, or per-tenant routing.
|
|
97
|
+
|
|
98
|
+
```ts title="transport.ts"
|
|
99
|
+
createHttpTransport({
|
|
100
|
+
url: () => `${process.env.API_URL}/_capable/`,
|
|
101
|
+
headers: async () => ({ authorization: `Bearer ${await accessToken()}` }),
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Choosing where a capability runs [#honesty]
|
|
106
|
+
|
|
107
|
+
Calling is uniform; placement is still a decision you make in source. A remote call is a network call, with the latency and failure modes that implies, and it runs under the server's context and policy rather than yours. Capable does not pretend otherwise — it only removes the boilerplate between the two.
|
|
108
|
+
|
|
109
|
+
| Put it here | Put it there |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| It touches this machine: files, credentials, local processes | It touches shared state: a database, a queue, another service |
|
|
112
|
+
| It needs to work offline or without a server | It enforces authorization the client must not be trusted with |
|
|
113
|
+
| Latency per call matters more than trust | The implementation must not ship to the caller |
|
|
114
|
+
|
|
115
|
+
When one process needs both, merge them: [composition](/patterns/composition) combines a remote app and a local app into a single tree that dispatches each call to whichever app owns it.
|
|
116
|
+
|
|
117
|
+
</DocsPage>
|