# Toolkit

> Toolkits: your connections' operations, the operations that need no account, and capabilities as LLM tools.

```python
gmail = noclick.Connection.from_name("gmail", integration="gmail", create_if_missing=True)
toolkit = noclick.Toolkit(["search", gmail])
response = anthropic.messages.create(..., tools=toolkit.definitions())
messages.append({"role": "user", "content": toolkit.handle(response)})
```

A call never raises for something the model can act on: a missing connection,
a policy refusal or a hold for approval comes back as an `is_error` result.
A hold's `approval_required` (`approval_id`, `request_id`, `approve_url`,
`operation`, `approver`) is never shown to the model; pass `on_approval=`
to the `handle` helpers to receive it, and `on_connect=` to receive each
result that needs a connection. `request_id` (the request a person answers),
`approve_url` and `operation` (the approval's operation object) are `None`
unless the request is addressed to you; the approver is emailed either way.
Once the person approves, the identical call goes through.

## Toolkit

```typescript
class Toolkit
```

A set of tools for your LLM. Entries are connections (a `Connection` or its
name: every operation, as `{connection}__{operation}` tools), single
operations (`"gmail.send_email_message"`), capabilities (`"search"`,
`"fetch"`, `"code"`) or a `Database` (`{database}__query`, `__execute` and `__schema`;
invite-only beta), a `Bucket` and operations with arguments bound
(`gmail.only("send_email_message").bind({ to })`: the bound parameters leave the tool's schema and
are filled on every call, so a toolkit sends its entries with each call). A `Connection` object
from another scope, another user's or the
account's, stays that connection (a key acting for the whole account only). A name resolves for the user the calls
act for (`user` if given, else the user current at each call, `asUser`), so one module-level
toolkit serves every user: a connection there, else an integration's operations that need no
account (on NoClick's key), so `"exa.search"` needs no connection.

| Attribute | Type | Description |
| --- | --- | --- |
| `refs` | `ToolRef[]` | What was passed: names, `"name.operation"` strings and `Connection` objects. |
| `simulated` | `boolean` | Whether a simulated world answers every call instead of the real accounts: the definitions are the real ones, nothing is read or sent, and each answer is billed as AI at NoClick's model price. |
| `scenario` | `string | null` | What that world is like; its answers keep to it. |
| `tools` | `string[]` | The refs as names (a `Connection` known only by id shows its id until used). |

Types:

```typescript
/**
 * A tool ref: a name, `"name.operation"`, a capability, a `Connection`, `connection.only(...)`, a
 * `Database` or `db.only(...)`. A name is a connection in the scope the tools run in, else an
 * integration's operations that need no account (on NoClick's key), so `"exa.search"` needs no connection.
 * An entry read back from `agent.tools` (`{ tool, bind }`, `{ human, tools }`) is taken as it is.
 */
type ToolRef = string | Connection | ConnectionOperations | Database | DatabaseOperations | ToolSource | JsonObject;

type JsonObject = { [key: string]: unknown };
```

### Toolkit.mcpServer

```typescript
mcpServer(name: string): McpServer
```

These tools as a hosted MCP server called `name`, in the toolkit's user (`McpServer.fromName`
with `createIfMissing`). Sends nothing yet.

Parameters:

- `name` (`string`, required)

Returns: The server, created on first use (or, when it exists, given this toolkit's tools); its `url`
is what you give an MCP client.

### Toolkit.definitions

```typescript
definitions(
  options?: { format?: ToolFormat; user?: UserOption },
): Promise<JsonObject[]>
```

Tool definitions to pass straight to your LLM API. Cached per format.

Parameters:

- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `options.format` (`ToolFormat`, default `"anthropic"`)

Returns: The tool definitions in that API's shape, ready to pass as `tools`. The first call for a
format fetches them; later calls reuse them.

Types:

```typescript
type ToolFormat = "anthropic" | "openai" | "mcp";

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

type JsonObject = { [key: string]: unknown };
```

### Toolkit.call

```typescript
call(
  name: string,
  args?: JsonObject,
  options?: { approvalId?: string; user?: UserOption },
): Promise<ToolResult>
```

Run one tool by its wire name (`gmail__send_email_message`, `search`). A policy refusal or a hold is
an `isError` result, never a throw; a held call goes through once approved when it is repeated
with the same arguments (`approvalId` names the approval explicitly).

Parameters:

- `name` (`string`, required)
- `args` (`JsonObject`)
- `options.approvalId` (`string`)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).

Returns: The tool's `content` (`text` as one string) and whether it `is_error`. A call that couldn't
run says why in `content`, with `needs_connection` or `approval_required` set when that's the reason.

Types:

```typescript
type JsonObject = { [key: string]: unknown };

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Toolkit.handle

```typescript
handle(
  content: unknown,
  options?: HandleOptions,
): Promise<AnthropicToolResultBlock[]>
```

Run every Anthropic `tool_use` block (from a Message, its `content` list,
or plain objects) concurrently and return the `tool_result` blocks for the
next user turn. `onApproval` receives each call held for approval, and `onConnect` each one whose
`needsConnection` is set, so your app can show the user the connect link itself. Each result
carries the model's `callId` and the `arguments`.

Its older name `handleAnthropic` still works, deprecated.

Parameters:

- `content` (`unknown`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `options.onApproval` (`OnApproval`)
- `options.onConnect` (`OnConnect`)

Returns: One `tool_result` block per `tool_use`, in order: the `content` of the next user turn.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** Receives each call held for approval, in call order (the model sees only its error content). */
type OnApproval = (result: ToolResult) => void | Promise<void>;

/** Receives each call whose `needsConnection` is set, in call order, so your app can show the connect link. */
type OnConnect = (result: ToolResult) => void | Promise<void>;

interface AnthropicToolResultBlock {
  type: "tool_result";
  tool_use_id: string;
  content: unknown;
  is_error: boolean;
}
```

### Toolkit.handleOpenAI

```typescript
handleOpenAI(
  toolCalls: unknown,
  options?: HandleOptions,
): Promise<OpenAIToolOutput[]>
```

Run OpenAI tool calls (a ChatCompletion, its message, a `tool_calls` list,
or Responses `function_call` items) and return `{ role: "tool" }` messages
(or `function_call_output` items). `onApproval` and `onConnect` work as in `handle`.

Parameters:

- `toolCalls` (`unknown`, required)
- `options.user` (`UserOption`, default: the current user): Who the call acts for. Defaults to the user set by `noclick.as_user` (none: the account itself).
- `options.onApproval` (`OnApproval`)
- `options.onConnect` (`OnConnect`)

Returns: One `{"role": "tool"}` message per call, in order, or a `function_call_output` item for each
Responses call. A call whose arguments aren't valid JSON isn't run; its message tells the model so.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** Receives each call held for approval, in call order (the model sees only its error content). */
type OnApproval = (result: ToolResult) => void | Promise<void>;

/** Receives each call whose `needsConnection` is set, in call order, so your app can show the connect link. */
type OnConnect = (result: ToolResult) => void | Promise<void>;

type OpenAIToolOutput =
  | { role: "tool"; tool_call_id: string; content: string }
  | { type: "function_call_output"; call_id: string; output: string };
```

## ToolResult

```typescript
interface ToolResult
```

The outcome of one tool call. An unconnected account is a normal result with `needsConnection`.

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `content` | `unknown` |  |
| `isError` | `boolean` |  |
| `needsConnection` | `NeedsConnectionInfo | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |
| `approvalRequired` | `ApprovalRequiredInfo | null` |  |
| `arguments` | `JsonObject` | The arguments the call was made with, as the model gave them. |
| `callId` | `string | null` | The model's id for the call when it came through `handle` or `handleOpenAI`; null from `call`. |
| `connectUrl` | `string | null` |  |
| `approval` | `HumanRequest<boolean> | null` | The approval a held call waits on, as a request: `wait()` resolves to `true` once approved. Null unless the call was held and the request is addressed to you. |
| `approveUrl` | `string | null` | The approval link, when the call waits for your approval; once approved, repeat the identical call. |
| `text` | `string` | The content as one string (what an OpenAI tool message carries). |

Types:

```typescript
/** An unconnected account's `needs_connection` note: which connection, and where to (re)connect it. */
interface NeedsConnectionInfo {
  connection?: string;
  integration?: string;
  connect_url?: string;
  [key: string]: unknown;
}

type JsonObject = { [key: string]: unknown };

/**
 * A call held for a person's approval. `request_id` (the request a person answers), `approve_url`
 * and `operation` (the approval's operation object) are null unless the request is addressed to
 * you; the approver is emailed either way. Once approved, retry the identical call.
 */
interface ApprovalRequiredInfo {
  approval_id?: string;
  request_id?: string | null;
  approve_url?: string | null;
  operation?: JsonObject | null;
  approver?: "user" | "owner";
  [key: string]: unknown;
}
```
