Skip to content

Commit ef0eb12

Browse files
authored
feat(gateway): add SDK-facing tools.invoke RPC
Adds the SDK-facing tools.invoke Gateway RPC for #74705. Reuses the /tools/invoke policy path for tool policy, deny-list, owner filtering, before-tool-call hooks, session/agent scoping, and plugin approval handling. Returns typed SDK approval/refusal/success results while preserving HTTP compatibility and uses idempotencyKey as the stable tool-call id. Includes protocol schema exports, method scope/list registration, SDK helper/types, docs, generated Swift models, tests, and changelog credit.
1 parent 37f8c38 commit ef0eb12

24 files changed

Lines changed: 932 additions & 251 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Docs: https://docs.openclaw.ai
88

99
- Voice Call/Google Meet: add Twilio Meet join phase logs around pre-connect DTMF, realtime stream setup, and initial greeting handoff for easier live-call debugging. Thanks @donkeykong91 and @PfanP.
1010
- macOS app: move recent session context rows into a Context submenu while keeping usage and cost details root-level, so the menu bar companion stays compact with many active sessions. Thanks @guti.
11+
- Gateway/SDK: add SDK-facing tools.invoke RPC with shared HTTP policy, typed approval/refusal results, and SDK helper support. Refs #74705. Thanks @BunsDev and @ai-hpc.
1112
- Messages/docs: clarify that `BodyForAgent` is the primary inbound model text while `Body` is the legacy envelope fallback, and add Signal coverage so channel hardening patches target the real prompt path. Refs #66198. Thanks @defonota3box.
1213
- Control UI/Usage: add UTC quarter-hour token buckets for the Usage Mosaic and reuse them for hour filtering, keeping the legacy session-span fallback for older summaries. (#74337) Thanks @konanok.
1314
- BlueBubbles: add opt-in `channels.bluebubbles.replyContextApiFallback` that fetches the original message from the BlueBubbles HTTP API when the in-memory reply-context cache misses (multi-instance deployments sharing one BB account, post-restart, after long-lived TTL/LRU eviction). Off by default; channel-level setting propagates to accounts that omit the flag through `mergeAccountConfig`; routed through the typed `BlueBubblesClient` so every fetch is SSRF-guarded by the same three-mode policy as every other BB client request; reply-id shape is validated and part-index prefixes (`p:0/<guid>`) are stripped before the request; concurrent webhooks for the same `replyToId` coalesce into one fetch and successful responses populate the reply cache for subsequent hits. Also promotes BlueBubbles attachment download failures from verbose to runtime error so silently-dropped inbound images are visible at default log level, and extends `sanitizeForLog` to redact `?password=…`/`?token=…` query params and `Authorization:` headers before they reach the log sink (CWE-532). (#71820) Thanks @coletebou and @zqchris.

‎apps/macos/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,100 @@ public struct ToolsEffectiveResult: Codable, Sendable {
38303830
}
38313831
}
38323832

3833+
public struct ToolsInvokeParams: Codable, Sendable {
3834+
public let name: String
3835+
public let args: [String: AnyCodable]?
3836+
public let sessionkey: String?
3837+
public let agentid: String?
3838+
public let confirm: Bool?
3839+
public let idempotencykey: String?
3840+
3841+
public init(
3842+
name: String,
3843+
args: [String: AnyCodable]?,
3844+
sessionkey: String?,
3845+
agentid: String?,
3846+
confirm: Bool?,
3847+
idempotencykey: String?)
3848+
{
3849+
self.name = name
3850+
self.args = args
3851+
self.sessionkey = sessionkey
3852+
self.agentid = agentid
3853+
self.confirm = confirm
3854+
self.idempotencykey = idempotencykey
3855+
}
3856+
3857+
private enum CodingKeys: String, CodingKey {
3858+
case name
3859+
case args
3860+
case sessionkey = "sessionKey"
3861+
case agentid = "agentId"
3862+
case confirm
3863+
case idempotencykey = "idempotencyKey"
3864+
}
3865+
}
3866+
3867+
public struct ToolsInvokeError: Codable, Sendable {
3868+
public let code: String
3869+
public let message: String
3870+
public let details: AnyCodable?
3871+
3872+
public init(
3873+
code: String,
3874+
message: String,
3875+
details: AnyCodable?)
3876+
{
3877+
self.code = code
3878+
self.message = message
3879+
self.details = details
3880+
}
3881+
3882+
private enum CodingKeys: String, CodingKey {
3883+
case code
3884+
case message
3885+
case details
3886+
}
3887+
}
3888+
3889+
public struct ToolsInvokeResult: Codable, Sendable {
3890+
public let ok: Bool
3891+
public let toolname: String
3892+
public let output: AnyCodable?
3893+
public let requiresapproval: Bool?
3894+
public let approvalid: String?
3895+
public let source: AnyCodable?
3896+
public let error: [String: AnyCodable]?
3897+
3898+
public init(
3899+
ok: Bool,
3900+
toolname: String,
3901+
output: AnyCodable?,
3902+
requiresapproval: Bool?,
3903+
approvalid: String?,
3904+
source: AnyCodable?,
3905+
error: [String: AnyCodable]?)
3906+
{
3907+
self.ok = ok
3908+
self.toolname = toolname
3909+
self.output = output
3910+
self.requiresapproval = requiresapproval
3911+
self.approvalid = approvalid
3912+
self.source = source
3913+
self.error = error
3914+
}
3915+
3916+
private enum CodingKeys: String, CodingKey {
3917+
case ok
3918+
case toolname = "toolName"
3919+
case output
3920+
case requiresapproval = "requiresApproval"
3921+
case approvalid = "approvalId"
3922+
case source
3923+
case error
3924+
}
3925+
}
3926+
38333927
public struct SkillsBinsParams: Codable, Sendable {}
38343928

38353929
public struct SkillsBinsResult: Codable, Sendable {

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,100 @@ public struct ToolsEffectiveResult: Codable, Sendable {
38303830
}
38313831
}
38323832

3833+
public struct ToolsInvokeParams: Codable, Sendable {
3834+
public let name: String
3835+
public let args: [String: AnyCodable]?
3836+
public let sessionkey: String?
3837+
public let agentid: String?
3838+
public let confirm: Bool?
3839+
public let idempotencykey: String?
3840+
3841+
public init(
3842+
name: String,
3843+
args: [String: AnyCodable]?,
3844+
sessionkey: String?,
3845+
agentid: String?,
3846+
confirm: Bool?,
3847+
idempotencykey: String?)
3848+
{
3849+
self.name = name
3850+
self.args = args
3851+
self.sessionkey = sessionkey
3852+
self.agentid = agentid
3853+
self.confirm = confirm
3854+
self.idempotencykey = idempotencykey
3855+
}
3856+
3857+
private enum CodingKeys: String, CodingKey {
3858+
case name
3859+
case args
3860+
case sessionkey = "sessionKey"
3861+
case agentid = "agentId"
3862+
case confirm
3863+
case idempotencykey = "idempotencyKey"
3864+
}
3865+
}
3866+
3867+
public struct ToolsInvokeError: Codable, Sendable {
3868+
public let code: String
3869+
public let message: String
3870+
public let details: AnyCodable?
3871+
3872+
public init(
3873+
code: String,
3874+
message: String,
3875+
details: AnyCodable?)
3876+
{
3877+
self.code = code
3878+
self.message = message
3879+
self.details = details
3880+
}
3881+
3882+
private enum CodingKeys: String, CodingKey {
3883+
case code
3884+
case message
3885+
case details
3886+
}
3887+
}
3888+
3889+
public struct ToolsInvokeResult: Codable, Sendable {
3890+
public let ok: Bool
3891+
public let toolname: String
3892+
public let output: AnyCodable?
3893+
public let requiresapproval: Bool?
3894+
public let approvalid: String?
3895+
public let source: AnyCodable?
3896+
public let error: [String: AnyCodable]?
3897+
3898+
public init(
3899+
ok: Bool,
3900+
toolname: String,
3901+
output: AnyCodable?,
3902+
requiresapproval: Bool?,
3903+
approvalid: String?,
3904+
source: AnyCodable?,
3905+
error: [String: AnyCodable]?)
3906+
{
3907+
self.ok = ok
3908+
self.toolname = toolname
3909+
self.output = output
3910+
self.requiresapproval = requiresapproval
3911+
self.approvalid = approvalid
3912+
self.source = source
3913+
self.error = error
3914+
}
3915+
3916+
private enum CodingKeys: String, CodingKey {
3917+
case ok
3918+
case toolname = "toolName"
3919+
case output
3920+
case requiresapproval = "requiresApproval"
3921+
case approvalid = "approvalId"
3922+
case source
3923+
case error
3924+
}
3925+
}
3926+
38333927
public struct SkillsBinsParams: Codable, Sendable {}
38343928

38353929
public struct SkillsBinsResult: Codable, Sendable {

‎docs/concepts/openclaw-sdk.md‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ resources.
2525

2626
`@openclaw/sdk` ships with:
2727

28-
| Surface | Status | What it does |
29-
| ------------------------- | ------- | ---------------------------------------------------------------------------- |
30-
| `OpenClaw` | Ready | Main client entry point. Owns transport, connection, requests, and events. |
31-
| `GatewayClientTransport` | Ready | WebSocket transport backed by the Gateway client. |
32-
| `oc.agents` | Ready | Lists, creates, updates, deletes, and gets agent handles. |
33-
| `Agent.run()` | Ready | Starts a Gateway `agent` run and returns a `Run`. |
34-
| `oc.runs` | Ready | Creates, gets, waits for, cancels, and streams runs. |
35-
| `Run.events()` | Ready | Streams normalized per-run events with replay for fast runs. |
36-
| `Run.wait()` | Ready | Calls `agent.wait` and returns a stable `RunResult`. |
37-
| `Run.cancel()` | Ready | Calls `sessions.abort` by run id, with session key when available. |
38-
| `oc.sessions` | Ready | Creates, resolves, sends to, patches, compacts, and gets session handles. |
39-
| `Session.send()` | Ready | Calls `sessions.send` and returns a `Run`. |
40-
| `oc.models` | Ready | Calls `models.list` and the current `models.authStatus` status RPC. |
41-
| `oc.tools` | Partial | Lists tool catalog and effective tools; direct tool invocation is not wired. |
42-
| `oc.artifacts` | Ready | Lists, gets, and downloads Gateway transcript artifacts. |
43-
| `oc.approvals` | Ready | Lists and resolves exec approvals through Gateway approval RPCs. |
44-
| `oc.rawEvents()` | Ready | Exposes raw Gateway events for advanced consumers. |
45-
| `normalizeGatewayEvent()` | Ready | Converts raw Gateway events into the stable SDK event shape. |
28+
| Surface | Status | What it does |
29+
| ------------------------- | ------ | -------------------------------------------------------------------------- |
30+
| `OpenClaw` | Ready | Main client entry point. Owns transport, connection, requests, and events. |
31+
| `GatewayClientTransport` | Ready | WebSocket transport backed by the Gateway client. |
32+
| `oc.agents` | Ready | Lists, creates, updates, deletes, and gets agent handles. |
33+
| `Agent.run()` | Ready | Starts a Gateway `agent` run and returns a `Run`. |
34+
| `oc.runs` | Ready | Creates, gets, waits for, cancels, and streams runs. |
35+
| `Run.events()` | Ready | Streams normalized per-run events with replay for fast runs. |
36+
| `Run.wait()` | Ready | Calls `agent.wait` and returns a stable `RunResult`. |
37+
| `Run.cancel()` | Ready | Calls `sessions.abort` by run id, with session key when available. |
38+
| `oc.sessions` | Ready | Creates, resolves, sends to, patches, compacts, and gets session handles. |
39+
| `Session.send()` | Ready | Calls `sessions.send` and returns a `Run`. |
40+
| `oc.models` | Ready | Calls `models.list` and the current `models.authStatus` status RPC. |
41+
| `oc.tools` | Ready | Lists, scopes, and invokes Gateway tools through the policy pipeline. |
42+
| `oc.artifacts` | Ready | Lists, gets, and downloads Gateway transcript artifacts. |
43+
| `oc.approvals` | Ready | Lists and resolves exec approvals through Gateway approval RPCs. |
44+
| `oc.rawEvents()` | Ready | Exposes raw Gateway events for advanced consumers. |
45+
| `normalizeGatewayEvent()` | Ready | Converts raw Gateway events into the stable SDK event shape. |
4646

4747
The SDK also exports the core types used by those surfaces:
4848
`AgentRunParams`, `RunResult`, `RunStatus`, `OpenClawEvent`,
@@ -216,11 +216,19 @@ await oc.models.list();
216216
await oc.models.status({ probe: false }); // calls models.authStatus
217217
```
218218

219-
Tool helpers expose the Gateway catalog and effective tool view:
219+
Tool helpers expose the Gateway catalog, effective tool view, and direct
220+
Gateway tool invocation. `oc.tools.invoke()` returns a typed envelope instead
221+
of throwing for policy or approval refusals.
220222

221223
```typescript
222224
await oc.tools.list();
223225
await oc.tools.effective({ sessionKey: "main" });
226+
await oc.tools.invoke("tool-name", {
227+
args: { input: "value" },
228+
sessionKey: "main",
229+
confirm: false,
230+
idempotencyKey: "tool-call-1",
231+
});
224232
```
225233

226234
Artifact helpers expose the Gateway artifact projection for session, run, or
@@ -256,8 +264,6 @@ await oc.tasks.list();
256264
await oc.tasks.get("task-id");
257265
await oc.tasks.cancel("task-id");
258266

259-
await oc.tools.invoke("tool-name", {});
260-
261267
await oc.environments.list();
262268
await oc.environments.create({});
263269
await oc.environments.status("environment-id");

‎docs/gateway/protocol.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ enumeration of `src/gateway/server-methods/*.ts`.
443443

444444
<Accordion title="Automation, skills, and tools">
445445
- Automation: `wake` schedules an immediate or next-heartbeat wake text injection; `cron.list`, `cron.status`, `cron.add`, `cron.update`, `cron.remove`, `cron.run`, `cron.runs` manage scheduled work.
446-
- Skills and tools: `commands.list`, `skills.*`, `tools.catalog`, `tools.effective`.
446+
- Skills and tools: `commands.list`, `skills.*`, `tools.catalog`, `tools.effective`, `tools.invoke`.
447447

448448
</Accordion>
449449
</AccordionGroup>
@@ -501,6 +501,15 @@ enumeration of `src/gateway/server-methods/*.ts`.
501501
caller-supplied auth or delivery context.
502502
- The response is session-scoped and reflects what the active conversation can use right now,
503503
including core, plugin, and channel tools.
504+
- Operators may call `tools.invoke` (`operator.write`) to invoke one available tool through the
505+
same gateway policy path as `/tools/invoke`.
506+
- `name` is required. `args`, `sessionKey`, `agentId`, `confirm`, and
507+
`idempotencyKey` are optional.
508+
- If both `sessionKey` and `agentId` are present, the resolved session agent must match
509+
`agentId`.
510+
- The response is an SDK-facing envelope with `ok`, `toolName`, optional `output`, and typed
511+
`error` fields. Approval or policy refusals return `ok:false` in the payload rather than
512+
bypassing the gateway tool policy pipeline.
504513
- Operators may call `skills.status` (`operator.read`) to fetch the visible
505514
skill inventory for an agent.
506515
- `agentId` is optional; omit it to read the default agent workspace.

‎packages/sdk/src/client.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type {
1818
SessionCreateParams,
1919
SessionSendParams,
2020
SessionTarget,
21+
ToolInvokeParams,
22+
ToolInvokeResult,
2123
} from "./types.js";
2224

2325
const MAX_REPLAY_RUNS = 100;
@@ -764,10 +766,15 @@ export class ToolsNamespace extends RpcNamespace {
764766
return await this.call("effective", params);
765767
}
766768

767-
async invoke(name: string, params?: unknown): Promise<unknown> {
768-
void name;
769-
void params;
770-
return unsupportedGatewayApi("oc.tools.invoke");
769+
async invoke(name: string, params?: ToolInvokeParams): Promise<ToolInvokeResult> {
770+
return await this.call("invoke", {
771+
name,
772+
...(params?.args ? { args: params.args } : {}),
773+
...(params?.sessionKey ? { sessionKey: params.sessionKey } : {}),
774+
...(params?.agentId ? { agentId: params.agentId } : {}),
775+
...(typeof params?.confirm === "boolean" ? { confirm: params.confirm } : {}),
776+
...(params?.idempotencyKey ? { idempotencyKey: params.idempotencyKey } : {}),
777+
});
771778
}
772779
}
773780

‎packages/sdk/src/index.e2e.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ async function createFakeGateway(port = 0): Promise<FakeGateway> {
100100
"sessions.send",
101101
"tools.catalog",
102102
"tools.effective",
103+
"tools.invoke",
103104
],
104105
events: ["agent", "sessions.changed"],
105106
},
@@ -253,6 +254,11 @@ async function createFakeGateway(port = 0): Promise<FakeGateway> {
253254
return;
254255
}
255256

257+
if (frame.method === "tools.invoke") {
258+
reply({ ok: true, toolName: "shell", output: { ok: true } });
259+
return;
260+
}
261+
256262
if (frame.method === "exec.approval.list") {
257263
reply({ approvals: [] });
258264
return;
@@ -414,6 +420,9 @@ describe("OpenClaw SDK websocket e2e", () => {
414420
await expect(oc.tools.effective({ sessionKey: "sdk-session" })).resolves.toMatchObject({
415421
tools: [{ name: "shell", enabled: true }],
416422
});
423+
await expect(
424+
oc.tools.invoke("shell", { args: { command: "pwd" }, sessionKey: "sdk-session" }),
425+
).resolves.toMatchObject({ ok: true, toolName: "shell", output: { ok: true } });
417426
await expect(oc.approvals.list()).resolves.toMatchObject({ approvals: [] });
418427
await expect(
419428
oc.approvals.respond("approval-1", { decision: "approve" }),
@@ -437,6 +446,7 @@ describe("OpenClaw SDK websocket e2e", () => {
437446
"models.authStatus",
438447
"tools.catalog",
439448
"tools.effective",
449+
"tools.invoke",
440450
"exec.approval.list",
441451
"exec.approval.resolve",
442452
]);

0 commit comments

Comments
 (0)