tools: add getEnv tool to read environment variables

This commit is contained in:
Adrià Arrufat
2026-04-08 13:01:21 +02:00
parent 9f932e69d6
commit babe3c68ca
2 changed files with 25 additions and 0 deletions

View File

@@ -55,6 +55,7 @@ const Action = enum {
selectOption,
setChecked,
findElement,
getEnv,
};
const action_map = std.StaticStringMap(Action).initComptime(.{
@@ -78,6 +79,7 @@ const action_map = std.StaticStringMap(Action).initComptime(.{
.{ "selectOption", .selectOption },
.{ "setChecked", .setChecked },
.{ "findElement", .findElement },
.{ "getEnv", .getEnv },
});
/// Execute a tool by name. Returns the result text.
@@ -113,6 +115,7 @@ pub fn call(
.selectOption => execSelectOption(session, registry, arena, arguments),
.setChecked => execSetChecked(session, registry, arena, arguments),
.findElement => execFindElement(session, registry, arena, arguments),
.getEnv => execGetEnv(arena, arguments),
};
}
@@ -497,6 +500,15 @@ fn execFindElement(session: *lp.Session, registry: *CDPNode.Registry, arena: std
return aw.written();
}
fn execGetEnv(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct { name: []const u8 };
const args = parseArgsOrErr(Params, arena, arguments) orelse return ToolError.InvalidParams;
const name_z = arena.dupeZ(u8, args.name) catch return ToolError.InternalError;
const value = std.posix.getenv(name_z) orelse
return std.fmt.allocPrint(arena, "Environment variable '{s}' is not set", .{args.name}) catch ToolError.InternalError;
return value;
}
// --- Shared helpers ---
fn ensurePage(session: *lp.Session, registry: *CDPNode.Registry, url: ?[:0]const u8, timeout: ?u32, waitUntil: ?lp.Config.WaitUntil) ToolError!*lp.Page {

View File

@@ -241,6 +241,19 @@ pub const tool_list = [_]protocol.Tool{
\\}
),
},
.{
.name = "getEnv",
.description = "Read the value of an environment variable. Useful for retrieving credentials or configuration without hardcoding them.",
.inputSchema = protocol.minify(
\\{
\\ "type": "object",
\\ "properties": {
\\ "name": { "type": "string", "description": "The environment variable name to read." }
\\ },
\\ "required": ["name"]
\\}
),
},
};
pub fn handleList(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {