#Agent Tool
1 messages ยท Page 1 of 1 (latest)
Need help to create a tool that help calling api to my backend and it will add header X-OpenClaw-Agent-Id
Help to create a tool that help calling api to my backend and it will add header X-OpenClaw-Agent-Id
You can do this as a tiny OpenClaw plugin tool and grab the agent id from the tool context (ctx.agentId). Then every request you send to your backend can automatically include X-OpenClaw-Agent-Id.
Docs worth skimming:
- Plugins overview: https://docs.openclaw.ai/tools/plugin
- Plugin agent tools: https://docs.openclaw.ai/plugins/agent-tools
- Plugin manifest: https://docs.openclaw.ai/plugins/manifest
1) Create a local plugin in ~/.openclaw/extensions/
Create a folder:
~/.openclaw/extensions/backend-api/
~/.openclaw/extensions/backend-api/openclaw.plugin.json
{
"id": "backend-api",
"name": "Backend API Tool",
"configSchema": {
"type": "object",
"additionalProperties": false,
"properties": {
"baseUrl": { "type": "string" },
"bearerToken": { "type": "string" }
},
"required": ["baseUrl"]
},
"uiHints": {
"bearerToken": { "sensitive": true, "label": "Bearer token" },
"baseUrl": { "label": "Base URL" }
}
}
~/.openclaw/extensions/backend-api/index.ts
import { Type } from "@sinclair/typebox";
const Methods = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
export default function (api: any) {
api.registerTool(
(ctx: any) => ({
name: "backend_api_call",
description:
"Call my backend API. Always adds X-OpenClaw-Agent-Id (and optionally Authorization).",
parameters: Type.Object({
path: Type.String({ description: "e.g. /v1/todos" }),
method: Type.Optional(Type.Union(Methods.map((m) => Type.Literal(m)) as any)),
headers: Type.Optional(Type.Record(Type.String(), Type.String())),
body: Type.Optional(Type.Unknown()),
}),
async execute(_toolCallId: string, params: any) {
const cfg = (api.pluginConfig ?? {}) as { baseUrl?: string; bearerToken?: string };
if (!cfg.baseUrl) throw new Error("backend-api plugin misconfigured: missing baseUrl");
const method = (params.method ?? "POST") as string;
const url = new URL(params.path, cfg.baseUrl);
const headers: Record<string, string> = {
...(params.headers ?? {}),
"X-OpenClaw-Agent-Id": String(ctx.agentId ?? ""),
};
if (cfg.bearerToken) headers["Authorization"] = `Bearer ${cfg.bearerToken}`;
if (params.body !== undefined) headers["Content-Type"] = "application/json";
const res = await fetch(url.toString(), {
method,
headers,
body: params.body !== undefined ? JSON.stringify(params.body) : undefined,
});
const text = await res.text();
// Return something LLM-friendly:
return {
content: [
{
type: "text",
text: JSON.stringify(
{
ok: res.ok,
status: res.status,
statusText: res.statusText,
url: url.toString(),
body: text,
},
null,
2,
),
},
],
};
},
}),
{ optional: true }, // safer: must be explicitly allowed on the agent
);
}
2) Enable plugin + configure baseUrl
In your main OpenClaw config (JSON5), add:
{
plugins: {
entries: {
"backend-api": {
enabled: true,
config: {
baseUrl: "https://your-backend.example.com/",
// bearerToken: "optional"
}
}
}
}
}
3) Allow the tool for your agent
Because we marked it optional: true, you must allow it:
{
agents: {
list: [
{
id: "main",
tools: { allow: ["backend_api_call"] } // or allow: ["backend-api"] to allow all tools from the plugin
}
]
}
}
4) Restart the Gateway
After that, the agent can call backend_api_call, and your backend will see X-OpenClaw-Agent-Id: <agent-id>.
If you tell me your backend endpoints + what shape you want the tool params to be (single endpoint string vs path/method/body), I can tighten the schema and add safety checks (like restricting paths or methods).
how can I add multiple tools function in 1 plugin