capable-kit 0.0.1-alpha.3 → 0.0.1-alpha.4
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/bin.mjs +14 -13
- package/dist/bin.mjs.map +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/http.mjs +1 -1
- package/dist/{index-AqKbVZ8Z.d.mts → index-DCrxWCTv.d.mts} +2 -2
- package/dist/{index-B2ru1zGQ.d.mts → index-DJ--zcWG.d.mts} +4 -4
- package/dist/mcp.d.mts +1 -1
- package/dist/mcp.mjs +1 -1
- package/dist/{mount--YHrgRHJ.mjs → mount-BEBJ2Tss.mjs} +13 -19
- package/dist/mount-BEBJ2Tss.mjs.map +1 -0
- package/dist/runtime-support.d.mts +1 -1
- package/dist/runtime-support.mjs +5 -5
- package/dist/runtime-support.mjs.map +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/server.mjs +2 -2
- package/dist/{src-DtSY5fzY.mjs → src-ChDFqZaE.mjs} +6 -6
- package/dist/{src-DtSY5fzY.mjs.map → src-ChDFqZaE.mjs.map} +1 -1
- package/dist/{src-DX2xrplP.mjs → src-D9pLpfeO.mjs} +2 -2
- package/dist/src-D9pLpfeO.mjs.map +1 -0
- package/docs/CONTEXT.md +108 -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 +126 -0
- package/docs/internal/capability-first-model.md +154 -0
- package/docs/internal/implementation-friction.md +674 -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 +75 -0
- package/docs/site/concepts/capabilities.mdx +249 -0
- package/docs/site/concepts/errors.mdx +93 -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 +142 -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 +293 -0
- package/docs/site/reference/packages-and-entry-points.mdx +70 -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 +97 -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/package.test.ts +101 -0
- package/dist/mount--YHrgRHJ.mjs.map +0 -1
- package/dist/src-DX2xrplP.mjs.map +0 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "CLI"
|
|
3
|
+
description: "Turn an app into a command tree that runs in-process or against a remote server."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/cli", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/cli" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
`createCli()` derives a command tree from the app: paths become commands, input fields become arguments and flags, and titles and descriptions become help text. It works with any app, so the same file can drive a local tool or a remote one.
|
|
13
|
+
|
|
14
|
+
## Build a CLI [#build]
|
|
15
|
+
|
|
16
|
+
```ts title="cli.ts"
|
|
17
|
+
#!/usr/bin/env bun
|
|
18
|
+
import { createApp } from "capable-kit";
|
|
19
|
+
import { createCli } from "capable-kit/cli";
|
|
20
|
+
import definition from "./capable.app";
|
|
21
|
+
import { createContext } from "./context";
|
|
22
|
+
|
|
23
|
+
const app = createApp(definition, { context: createContext });
|
|
24
|
+
const cli = createCli(app, { invocationName: "todo" });
|
|
25
|
+
|
|
26
|
+
process.exit(await cli.run(process.argv.slice(2)));
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The app tree is available as a discoverable root command immediately:
|
|
30
|
+
|
|
31
|
+
```sh title="terminal"
|
|
32
|
+
todo --help
|
|
33
|
+
|
|
34
|
+
Todo App
|
|
35
|
+
Manage your todo list
|
|
36
|
+
|
|
37
|
+
todo <command> [options]
|
|
38
|
+
|
|
39
|
+
Commands
|
|
40
|
+
|
|
41
|
+
todos list List todos
|
|
42
|
+
todos create <title> Create a todo
|
|
43
|
+
todos delete <todoId> Delete a todo
|
|
44
|
+
|
|
45
|
+
Options
|
|
46
|
+
|
|
47
|
+
--json Print canonical JSON
|
|
48
|
+
-h, --help Show help
|
|
49
|
+
|
|
50
|
+
Run `todo <command> --help` for details on a command.
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`run()` resolves to an exit code: `0` on success, `1` for a failed capability, and `2` for a usage error.
|
|
54
|
+
|
|
55
|
+
## How commands are derived [#mapping]
|
|
56
|
+
|
|
57
|
+
| Authored | Becomes |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| App path `todos.create` | `todos create` |
|
|
60
|
+
| Input field `dueDate` | `--due-date` |
|
|
61
|
+
| Field listed in `cli.positional` | `<title>` |
|
|
62
|
+
| Boolean field `notify` | `--notify` |
|
|
63
|
+
| `title` and `description` | Help text for the command |
|
|
64
|
+
| `toMarkdown` | Default terminal output |
|
|
65
|
+
|
|
66
|
+
```sh title="terminal"
|
|
67
|
+
todo --help
|
|
68
|
+
todo todos --help
|
|
69
|
+
todo todos create --help
|
|
70
|
+
|
|
71
|
+
# Positional argument plus flags
|
|
72
|
+
todo todos create "Read the docs" --due 2025-01-31
|
|
73
|
+
|
|
74
|
+
# Canonical JSON instead of the Markdown rendering
|
|
75
|
+
todo todos list --json
|
|
76
|
+
|
|
77
|
+
# Follow a subscribable read capability
|
|
78
|
+
todo todos list --watch
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Values are coerced with the input schema, required fields are checked before the call, and unknown commands or options suggest the nearest match and point at the right `--help`.
|
|
82
|
+
|
|
83
|
+
## CLI metadata [#metadata]
|
|
84
|
+
|
|
85
|
+
Metadata only changes presentation. Everything else — validation, middleware, errors — is the capability's.
|
|
86
|
+
|
|
87
|
+
```ts title="capabilities.ts"
|
|
88
|
+
const createTodo = todo({
|
|
89
|
+
id: "todos.create",
|
|
90
|
+
title: "Create a todo",
|
|
91
|
+
input: z.object({ title: z.string(), dueDate: z.string().optional() }),
|
|
92
|
+
meta: {
|
|
93
|
+
cli: { positional: ["title"] }, // <title> instead of --title
|
|
94
|
+
// cli: { name: "add" }, // rename this command
|
|
95
|
+
// cli: { command: ["todo", "add"] } // place it anywhere
|
|
96
|
+
// cli: false, // keep it out of the CLI
|
|
97
|
+
},
|
|
98
|
+
handler: ({ input, context }) => context.todos.create(input),
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Point it at a server [#remote]
|
|
103
|
+
|
|
104
|
+
A remote app produces an identical command tree, because help and arguments are derived from the manifest rather than from local code.
|
|
105
|
+
|
|
106
|
+
```ts title="cli.ts"
|
|
107
|
+
import { createRemoteApp } from "capable-kit";
|
|
108
|
+
import { createCli } from "capable-kit/cli";
|
|
109
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
110
|
+
import type definition from "./capable.app";
|
|
111
|
+
|
|
112
|
+
const remote = createRemoteApp<typeof definition>(
|
|
113
|
+
createHttpTransport({
|
|
114
|
+
url: process.env.CAPABLE_URL ?? "http://localhost:3000/_capable/",
|
|
115
|
+
headers: () => ({ authorization: `Bearer ${readToken()}` }),
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
process.exit(await createCli(remote).run(process.argv.slice(2)));
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
> **Local and remote in one tree**
|
|
123
|
+
>
|
|
124
|
+
> Merge a local app with a remote one to add commands such as `login` or `status` that must run on the user's machine. See [Composition](/patterns/composition).
|
|
125
|
+
|
|
126
|
+
## Without writing a CLI file [#toolchain]
|
|
127
|
+
|
|
128
|
+
The toolchain ships a generic runner. It imports `capable.server.ts` for a local project, or connects to a URL.
|
|
129
|
+
|
|
130
|
+
```sh title="terminal"
|
|
131
|
+
# Run the local project in-process
|
|
132
|
+
cap cli --app ./ todos list
|
|
133
|
+
|
|
134
|
+
# Run the same commands against a deployed server
|
|
135
|
+
cap cli --app https://todos.example.com todos list
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
</DocsPage>
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Eve"
|
|
3
|
+
description: "Give an Eve agent native in-process tools without a second server."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/eve", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/eve" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
`createEveTools()` projects an executable app into native Eve tools. Start with the app inside the agent process. If it later needs to become its own service, host the same definition and replace the local app with a remote one. The Eve tools do not change.
|
|
13
|
+
|
|
14
|
+
## Start in-process [#local]
|
|
15
|
+
|
|
16
|
+
Put an executable app directly in Eve's `agent/tools/` directory. There is no second server and no HTTP hop.
|
|
17
|
+
|
|
18
|
+
```ts title="agent/tools/todos.ts"
|
|
19
|
+
import { createApp } from "capable-kit";
|
|
20
|
+
import { createEveTools } from "capable-kit/eve";
|
|
21
|
+
import { always } from "eve/tools/approval";
|
|
22
|
+
import definition from "../../capable.app";
|
|
23
|
+
import { todos } from "../../todos";
|
|
24
|
+
|
|
25
|
+
const app = createApp(definition, {
|
|
26
|
+
context: () => ({ todos }),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export default createEveTools(app, {
|
|
30
|
+
approval: (capability) =>
|
|
31
|
+
capability.effect === "destructive" ? always() : undefined,
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The agent now sees `todos__list`, `todos__add`, and `todos__delete` with names, descriptions, input schemas, and output schemas derived from the app. Calls run through the local app runtime, so context creation, middleware, declared errors, and validation all happen in the agent process.
|
|
36
|
+
|
|
37
|
+
## Move it into a standalone service [#host]
|
|
38
|
+
|
|
39
|
+
When the capability needs independent deployment, shared access, or its own scaling boundary, keep `capable.app.ts` unchanged and add a server file. Context moves with execution to the service.
|
|
40
|
+
|
|
41
|
+
```ts title="capable.server.ts"
|
|
42
|
+
import { createServer } from "capable-kit/server";
|
|
43
|
+
import definition from "./capable.app";
|
|
44
|
+
import { todos } from "./todos";
|
|
45
|
+
|
|
46
|
+
export default createServer(definition, {
|
|
47
|
+
context: () => ({ todos }),
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```sh title="terminal"
|
|
52
|
+
cap dev # run the service with file watching
|
|
53
|
+
cap build # build a standalone Node service
|
|
54
|
+
cap preview # run that production build locally
|
|
55
|
+
|
|
56
|
+
# Or build for another deployment target
|
|
57
|
+
cap build --preset vercel
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The standalone server exposes the Capable transport at `/_capable/`. Its manifest describes the same capability tree, effects, schemas, and metadata the local app exposed.
|
|
61
|
+
|
|
62
|
+
## Point Eve at the remote app [#remote]
|
|
63
|
+
|
|
64
|
+
Replace `createApp()` with `createRemoteApp()`. The tool projection and approval policy stay the same; only the placement of execution changes.
|
|
65
|
+
|
|
66
|
+
```ts title="agent/tools/todos.ts"
|
|
67
|
+
import { createRemoteApp } from "capable-kit";
|
|
68
|
+
import { createEveTools } from "capable-kit/eve";
|
|
69
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
70
|
+
import { always } from "eve/tools/approval";
|
|
71
|
+
import type definition from "../../capable.app";
|
|
72
|
+
|
|
73
|
+
const app = createRemoteApp<typeof definition>(
|
|
74
|
+
createHttpTransport({
|
|
75
|
+
url: "https://todos.example.com/_capable/",
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
export default createEveTools(app, {
|
|
80
|
+
approval: (capability) =>
|
|
81
|
+
capability.effect === "destructive" ? always() : undefined,
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
| | Local app | Remote app |
|
|
86
|
+
| --- | --- | --- |
|
|
87
|
+
| Handler runs | Inside the Eve process | Inside the service |
|
|
88
|
+
| Context lives | With the agent | With the service |
|
|
89
|
+
| Tool discovery | In-memory manifest | Remote manifest |
|
|
90
|
+
| Eve tool names and schemas | Derived by createEveTools() | The same projection |
|
|
91
|
+
|
|
92
|
+
> **The definition import is type-only**
|
|
93
|
+
>
|
|
94
|
+
> Eve imports `typeof definition` for static typing, but it does not execute the local handlers. Discovery and calls go through the remote transport.
|
|
95
|
+
|
|
96
|
+
## Approvals from effect [#approvals]
|
|
97
|
+
|
|
98
|
+
The `approval` option receives each capability from the manifest and returns an Eve approval policy, which is a direct use of `effect` as a safety signal.
|
|
99
|
+
|
|
100
|
+
| Effect | Typical policy |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| `read` | No approval |
|
|
103
|
+
| `write` | No approval, or ask once |
|
|
104
|
+
| `destructive` | `always()` — a human confirms every call |
|
|
105
|
+
|
|
106
|
+
```ts title="capabilities.ts"
|
|
107
|
+
meta: {
|
|
108
|
+
eve: { name: "todos_add" }, // default is the app path: todos__add
|
|
109
|
+
// eve: false, // keep this capability away from the agent
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Durability [#durability]
|
|
114
|
+
|
|
115
|
+
The tool definition resolves at Eve's `step.started` boundary. With a local app, Eve keeps the app and its context provider in memory. With a remote app, it keeps the transport-backed runtime live instead. In both cases Eve checkpoints the tool call and its result.
|
|
116
|
+
|
|
117
|
+
> **Interrupted steps can re-run**
|
|
118
|
+
>
|
|
119
|
+
> As with any Eve tool, a side effect must be idempotent: an interrupted step may execute again. This is a property of durable agent execution, not of Capable.
|
|
120
|
+
|
|
121
|
+
</DocsPage>
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "HTTP and OpenAPI"
|
|
3
|
+
description: "Derive routes from the app tree, or author them when the contract is public."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/http", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/http" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
The HTTP mount compiles the app tree into routes and owns the generated OpenAPI document. It is one projection of the app — the Capable transport used by remote Capable clients is a separate mount.
|
|
13
|
+
|
|
14
|
+
## Mount it [#mount]
|
|
15
|
+
|
|
16
|
+
```ts title="capable.server.ts"
|
|
17
|
+
import { createServer, http } from "capable-kit/server";
|
|
18
|
+
import definition from "./capable.app";
|
|
19
|
+
|
|
20
|
+
export default createServer(definition, {
|
|
21
|
+
context: ({ request }) => contextFromRequest(request),
|
|
22
|
+
mounts: [
|
|
23
|
+
http({
|
|
24
|
+
path: "/api",
|
|
25
|
+
openapiPath: "/openapi.json",
|
|
26
|
+
requireExplicitRoutes: false,
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
| Option | Default | Effect |
|
|
33
|
+
| --- | --- | --- |
|
|
34
|
+
| `path` | `/api` | Base path for every route |
|
|
35
|
+
| `openapiPath` | `/openapi.json` | Where the document is served. `false` disables it. |
|
|
36
|
+
| `requireExplicitRoutes` | `false` | Fail at startup if any exposed capability has no authored method and route |
|
|
37
|
+
| `selection` | all | Include or exclude a subtree from this surface |
|
|
38
|
+
|
|
39
|
+
## Derived routes [#derived]
|
|
40
|
+
|
|
41
|
+
Without metadata, the route is the app path under the base path and the method comes from `effect`.
|
|
42
|
+
|
|
43
|
+
| Capability | Effect | Route |
|
|
44
|
+
| --- | --- | --- |
|
|
45
|
+
| `todos.list` | `read` | `GET /api/todos/list` |
|
|
46
|
+
| `todos.create` | `write` | `POST /api/todos/create` |
|
|
47
|
+
| `todos.delete` | `destructive` | `POST /api/todos/delete` |
|
|
48
|
+
|
|
49
|
+
Input is read from path parameters first, then from the query string for `GET` or the JSON body for everything else. Query and path values are coerced using the input schema, so `?limit=10` reaches the handler as a number.
|
|
50
|
+
|
|
51
|
+
## Authored routes [#authored]
|
|
52
|
+
|
|
53
|
+
Derived routes are convenient internally, but a public API usually deserves deliberate methods and paths. Authoring one is metadata, not a second implementation.
|
|
54
|
+
|
|
55
|
+
```ts title="capabilities.ts"
|
|
56
|
+
const getTodo = todo({
|
|
57
|
+
id: "todos.get",
|
|
58
|
+
effect: "read",
|
|
59
|
+
input: z.object({ todoId: z.string() }),
|
|
60
|
+
output: Todo,
|
|
61
|
+
errors: TodoErrors,
|
|
62
|
+
meta: {
|
|
63
|
+
http: { method: "GET", route: "/todos/:todoId" },
|
|
64
|
+
},
|
|
65
|
+
handler: ({ input, context }) => context.todos.get(input.todoId),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Group prefixes apply to derived routes and to authored ones.
|
|
69
|
+
const todos = group({
|
|
70
|
+
meta: { http: { prefix: "/todos" } },
|
|
71
|
+
capabilities: { get: getTodo, archive: archiveTodo },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Keep a capability off HTTP entirely.
|
|
75
|
+
const rotateKeys = todo({ id: "admin.rotate-keys", meta: { http: false } });
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
> **requireExplicitRoutes**
|
|
79
|
+
>
|
|
80
|
+
> Set it to `true` on a public mount. The server then refuses to start if any exposed capability is relying on a derived route, which turns "we forgot to design that URL" into a startup error instead of a shipped one.
|
|
81
|
+
|
|
82
|
+
## OpenAPI [#openapi]
|
|
83
|
+
|
|
84
|
+
The document is generated from the same schemas: path parameters, query parameters for `GET`, request bodies, the success response with its status, and one response per declared error status. Descriptions come from `title` and `description`.
|
|
85
|
+
|
|
86
|
+
Because it is derived, it cannot drift from the implementation. There is no separate spec to update.
|
|
87
|
+
|
|
88
|
+
## Errors and representations [#responses]
|
|
89
|
+
|
|
90
|
+
Declared errors are serialized with their code, message, status, and validated data. Undeclared failures become a 500 with `INTERNAL_SERVER_ERROR` and no details.
|
|
91
|
+
|
|
92
|
+
```ts title="404 response"
|
|
93
|
+
{
|
|
94
|
+
"ok": false,
|
|
95
|
+
"error": {
|
|
96
|
+
"code": "NOT_FOUND",
|
|
97
|
+
"message": "Todo not found",
|
|
98
|
+
"status": 404,
|
|
99
|
+
"data": { "todoId": "t_123" }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
If a capability defines `toMarkdown`, requesting `text/markdown` returns that rendering of the same canonical output.
|
|
105
|
+
|
|
106
|
+
```sh title="terminal"
|
|
107
|
+
curl http://localhost:3000/api/todos/list \
|
|
108
|
+
-H 'accept: text/markdown'
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
</DocsPage>
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "MCP"
|
|
3
|
+
description: "Expose capabilities to any MCP client as tools with schemas and safety hints."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/mcp", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/mcp" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
The MCP mount serves your capabilities as stateless MCP tools over Streamable HTTP. Modern clients use the sessionless MCP 2026-07-28 lifecycle, while older clients use the legacy handshake without requiring server-side session storage. Tool names, input and output schemas, descriptions, and safety hints are all derived from the app, so an agent sees the same contract your code does.
|
|
13
|
+
|
|
14
|
+
## Mount it [#mount]
|
|
15
|
+
|
|
16
|
+
`cap dev` and the toolchain's default production build already mount MCP at `/mcp`, so a contextless app needs no server file to expose tools. Mount it explicitly when you add a `capable.server.ts`, or when you want a different path or name.
|
|
17
|
+
|
|
18
|
+
```ts title="capable.server.ts"
|
|
19
|
+
import { mcp } from "capable-kit/mcp";
|
|
20
|
+
import { protocol, createServer } from "capable-kit/server";
|
|
21
|
+
import definition from "./capable.app";
|
|
22
|
+
|
|
23
|
+
export default createServer(definition, {
|
|
24
|
+
context: ({ request }) => contextFromRequest(request),
|
|
25
|
+
mounts: [protocol(), mcp({ path: "/mcp", name: "todos" })],
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
| Option | Default | Effect |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `path` | `/mcp` | Endpoint path |
|
|
32
|
+
| `name` | App name | Server name reported during connection setup |
|
|
33
|
+
| `version` | App version | Server version reported during connection setup |
|
|
34
|
+
|
|
35
|
+
## What an agent sees [#derivation]
|
|
36
|
+
|
|
37
|
+
| Authored | Tool field |
|
|
38
|
+
| --- | --- |
|
|
39
|
+
| App path `todos.get` | `todos__get` |
|
|
40
|
+
| `title` | Tool title |
|
|
41
|
+
| `description` | Tool description |
|
|
42
|
+
| `input` | JSON Schema for arguments |
|
|
43
|
+
| `output` | Structured output schema |
|
|
44
|
+
| `effect: "read"` | `readOnlyHint` |
|
|
45
|
+
| `effect: "destructive"` | `destructiveHint` |
|
|
46
|
+
|
|
47
|
+
```ts title="capabilities.ts"
|
|
48
|
+
const getTodo = todo({
|
|
49
|
+
id: "todos.get",
|
|
50
|
+
title: "Get a todo",
|
|
51
|
+
description: "Returns one todo with its owner and due date.",
|
|
52
|
+
effect: "read",
|
|
53
|
+
input: z.object({ todoId: z.string() }),
|
|
54
|
+
output: Todo,
|
|
55
|
+
meta: {
|
|
56
|
+
mcp: { name: "todos_get" }, // default is the app path: todos__get
|
|
57
|
+
// mcp: false, // never expose this capability as a tool
|
|
58
|
+
},
|
|
59
|
+
handler: ({ input, context }) => context.todos.get(input.todoId),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> **Descriptions are part of the API now**
|
|
64
|
+
>
|
|
65
|
+
> An agent chooses tools by reading them. A precise `description` is as load-bearing as the input schema, and it is written once for every surface instead of separately for each one.
|
|
66
|
+
|
|
67
|
+
## Expose less than everything [#scope]
|
|
68
|
+
|
|
69
|
+
Most apps should not offer their whole tree to an agent. Set `meta.mcp: false` on the capabilities that are never tools, and narrow a projected app with `selectApp()`.
|
|
70
|
+
|
|
71
|
+
```ts title="tools.ts"
|
|
72
|
+
import { selectApp } from "capable-kit";
|
|
73
|
+
import { createMcp } from "capable-kit/mcp";
|
|
74
|
+
|
|
75
|
+
const agentApp = selectApp(app, { exclude: { admin: true } });
|
|
76
|
+
const tools = await createMcp(agentApp);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Tools without a server [#tools]
|
|
80
|
+
|
|
81
|
+
`createMcp()` returns plain tool descriptors you can host yourself — over stdio, inside a test, or in another MCP server.
|
|
82
|
+
|
|
83
|
+
```ts title="tools.ts"
|
|
84
|
+
import { createMcp } from "capable-kit/mcp";
|
|
85
|
+
|
|
86
|
+
const tools = await createMcp(app);
|
|
87
|
+
|
|
88
|
+
for (const tool of tools) {
|
|
89
|
+
console.log(tool.name, tool.annotations);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await tools[0].call({ todoId: "t_123" });
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
For an agent running in the same process, skip MCP entirely and use [native Eve tools](/surfaces/eve).
|
|
96
|
+
|
|
97
|
+
</DocsPage>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "React Query"
|
|
3
|
+
description: "Derive query keys, query options, and mutations for TanStack Query."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/react-query", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/react-query" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
`@capable/react-query` mirrors the app tree into TanStack Query primitives. Keys, query functions, and mutation functions are derived, so components stop repeating endpoint names and cache keys.
|
|
13
|
+
|
|
14
|
+
## Create the client [#create]
|
|
15
|
+
|
|
16
|
+
Point it at any app. In the browser this is usually a remote app over the Capable transport; on the server it can be a local app. Both App creation and the React Query projection are synchronous. Manifest discovery is deferred until a query actually executes.
|
|
17
|
+
|
|
18
|
+
```ts title="query.ts"
|
|
19
|
+
import { createRemoteApp } from "capable-kit";
|
|
20
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
21
|
+
import { createReactQuery } from "@capable/react-query";
|
|
22
|
+
import type definition from "../capable.app";
|
|
23
|
+
|
|
24
|
+
const app = createRemoteApp<typeof definition>(
|
|
25
|
+
createHttpTransport({ url: "/_capable/" }),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export const query = createReactQuery(app);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Use it in components [#use]
|
|
32
|
+
|
|
33
|
+
```tsx title="todo-list.tsx"
|
|
34
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
35
|
+
import { query } from "./query";
|
|
36
|
+
|
|
37
|
+
export function TodoList() {
|
|
38
|
+
const client = useQueryClient();
|
|
39
|
+
const todos = useQuery(query.todos.list.queryOptions({}));
|
|
40
|
+
|
|
41
|
+
const createTodo = useMutation(
|
|
42
|
+
query.todos.create.mutationOptions({
|
|
43
|
+
onSuccess: () =>
|
|
44
|
+
client.invalidateQueries({ queryKey: query.todos.list.queryKey() }),
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (todos.isPending) return <Spinner />;
|
|
49
|
+
if (todos.error) return <Error error={todos.error} />;
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<>
|
|
53
|
+
<ul>{todos.data.map((todo) => <li key={todo.id}>{todo.title}</li>)}</ul>
|
|
54
|
+
<button onClick={() => createTodo.mutate({ title: "New" })}>Add</button>
|
|
55
|
+
</>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
| Method | Returns |
|
|
61
|
+
| --- | --- |
|
|
62
|
+
| `queryKey(input?)` | `["capable", ...path, input?]`, for invalidation |
|
|
63
|
+
| `queryOptions(input, options?)` | Options for `useQuery`, with the signal wired to cancellation |
|
|
64
|
+
| `mutationOptions(options?)` | Options for `useMutation`, taking the input as variables |
|
|
65
|
+
| `liveQueryOptions(input, options?)` | Streamed query, only on subscribable read capabilities |
|
|
66
|
+
|
|
67
|
+
Pass `keyPrefix` to `createReactQuery()` if the `capable` prefix collides with existing keys.
|
|
68
|
+
|
|
69
|
+
## Live queries [#live]
|
|
70
|
+
|
|
71
|
+
When a capability defines [subscribe](/patterns/subscriptions), `liveQueryOptions()` appears on it and each replacement lands in the same cache entry.
|
|
72
|
+
|
|
73
|
+
```tsx title="todo-list.tsx"
|
|
74
|
+
// Backed by the capability's subscribe(), streamed into the same cache entry.
|
|
75
|
+
const todos = useQuery(query.todos.list.liveQueryOptions({}));
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
> The projection stays at the data layer. Loading states, error rendering, optimistic updates, and layout remain ordinary React Query and React code.
|
|
79
|
+
|
|
80
|
+
</DocsPage>
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "TypeScript"
|
|
3
|
+
description: "Call capabilities as typed functions, in this process or across a transport."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { DocsPage, docMetadata } from "../../_components/docs-page";
|
|
7
|
+
|
|
8
|
+
export const metadata = docMetadata("/surfaces/typescript", frontmatter);
|
|
9
|
+
|
|
10
|
+
<DocsPage href="/surfaces/typescript" frontmatter={frontmatter}>
|
|
11
|
+
|
|
12
|
+
The TypeScript surface is the app itself. An app is a callable tree, so calling a capability is an ordinary function call with an awaited result. There is no client to generate, no request object to build, and no route to remember.
|
|
13
|
+
|
|
14
|
+
## Calling a capability [#call]
|
|
15
|
+
|
|
16
|
+
Each leaf of the tree is a function that takes the capability's input and resolves to its output. Both are inferred from the definition, so renaming a field or adding an error is a compile error at every call site.
|
|
17
|
+
|
|
18
|
+
```ts title="script.ts"
|
|
19
|
+
import { createApp } from "capable-kit";
|
|
20
|
+
import definition from "./capable.app";
|
|
21
|
+
|
|
22
|
+
const app = createApp(definition);
|
|
23
|
+
|
|
24
|
+
// Input is checked against the capability's schema.
|
|
25
|
+
const todos = await app.todos.list({ status: "open" });
|
|
26
|
+
// ^? Todo[]
|
|
27
|
+
|
|
28
|
+
await app.todos.create({ title: "Write the docs" });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
| Argument | Meaning |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `input` | Validated against the capability schema. Optional when the capability declares no input. |
|
|
34
|
+
| `options.signal` | An `AbortSignal` passed to the handler and, for a remote app, to the underlying request. |
|
|
35
|
+
|
|
36
|
+
## Calling a remote app [#remote]
|
|
37
|
+
|
|
38
|
+
[A remote app](/patterns/remote-apps) has the identical shape. The definition is imported as a _type_, so nothing from the server bundles into the caller and no client is generated.
|
|
39
|
+
|
|
40
|
+
```ts title="client.ts"
|
|
41
|
+
import { createRemoteApp } from "capable-kit";
|
|
42
|
+
import { createHttpTransport } from "capable-kit/http";
|
|
43
|
+
import type definition from "../server/capable.app";
|
|
44
|
+
|
|
45
|
+
// Same tree, same types, different placement.
|
|
46
|
+
const app = createRemoteApp<typeof definition>(
|
|
47
|
+
createHttpTransport({ url: "https://todos.example.com/_capable/" }),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const todos = await app.todos.list({ status: "open" });
|
|
51
|
+
// ^? Todo[]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Everything below applies to both. Code that takes an app as a parameter does not need to know which one it received.
|
|
55
|
+
|
|
56
|
+
## Errors [#errors]
|
|
57
|
+
|
|
58
|
+
Declared errors survive the boundary. The same guard narrows a local throw and a decoded remote failure, with typed `data` either way. Anything undeclared arrives as an internal error.
|
|
59
|
+
|
|
60
|
+
```ts title="caller.ts"
|
|
61
|
+
import { TodoErrors } from "./errors";
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
await app.todos.delete({ todoId });
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (TodoErrors.NOT_FOUND.is(error)) {
|
|
67
|
+
console.error(error.data.todoId); // typed
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The full model, including which failures belong in a contract, is on the [errors page](/concepts/errors).
|
|
75
|
+
|
|
76
|
+
## Cancellation [#cancellation]
|
|
77
|
+
|
|
78
|
+
Pass a signal as the second argument. A local call forwards it to the handler; a remote call also aborts the in-flight request.
|
|
79
|
+
|
|
80
|
+
```ts title="script.ts"
|
|
81
|
+
const controller = new AbortController();
|
|
82
|
+
setTimeout(() => controller.abort(), 2_000);
|
|
83
|
+
|
|
84
|
+
const todos = await app.todos.list({}, { signal: controller.signal });
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Streaming reads [#subscribe]
|
|
88
|
+
|
|
89
|
+
When a capability defines [subscribe](/patterns/subscriptions), pass its call to `subscribe()` to get an async iterator instead of a single result. Calling it normally still returns one value.
|
|
90
|
+
|
|
91
|
+
```ts title="watch.ts"
|
|
92
|
+
import { subscribe } from "capable-kit";
|
|
93
|
+
|
|
94
|
+
for await (const todos of subscribe(app.todos.list, { status: "open" })) {
|
|
95
|
+
render(todos); // each value is a complete replacement
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> `subscribe()` takes the capability function itself, not its result, which is why it stays fully typed: `subscribe(app.todos.list, input)`.
|
|
100
|
+
|
|
101
|
+
## One capability without an app [#without-app]
|
|
102
|
+
|
|
103
|
+
Tests and scripts sometimes want a single capability with no tree around it. `executeCapability()` runs it directly. Group middleware, group errors, and group metadata do not apply, because there is no group.
|
|
104
|
+
|
|
105
|
+
```ts title="list-todos.test.ts"
|
|
106
|
+
import { executeCapability } from "capable-kit";
|
|
107
|
+
import { listTodos } from "./capabilities/list-todos";
|
|
108
|
+
|
|
109
|
+
const todos = await executeCapability(listTodos, {
|
|
110
|
+
input: { status: "open" },
|
|
111
|
+
context: { todos: store },
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Limits of this surface [#notes]
|
|
116
|
+
|
|
117
|
+
| Not this | Instead |
|
|
118
|
+
| --- | --- |
|
|
119
|
+
| A generated SDK | Types come from a type-only import of the definition; the tree is a proxy built from the manifest. |
|
|
120
|
+
| A place for presentation | Titles, routes, and tool names belong to [surface metadata](/reference/metadata), not to the call. |
|
|
121
|
+
| A cache or a UI binding | Use [React Query](/surfaces/react-query) for keys, caching, and live queries in a React app. |
|
|
122
|
+
|
|
123
|
+
</DocsPage>
|