tools: add getUrl and getCookies tools

This commit is contained in:
Adrià Arrufat
2026-04-08 15:10:23 +02:00
parent 701836be88
commit 73f0e0456b
2 changed files with 41 additions and 0 deletions

View File

@@ -57,6 +57,8 @@ const Action = enum {
findElement,
getEnv,
consoleLogs,
getUrl,
getCookies,
};
const action_map = std.StaticStringMap(Action).initComptime(.{
@@ -82,6 +84,8 @@ const action_map = std.StaticStringMap(Action).initComptime(.{
.{ "findElement", .findElement },
.{ "getEnv", .getEnv },
.{ "consoleLogs", .consoleLogs },
.{ "getUrl", .getUrl },
.{ "getCookies", .getCookies },
});
/// Execute a tool by name. Returns the result text.
@@ -119,6 +123,8 @@ pub fn call(
.findElement => execFindElement(session, registry, arena, arguments),
.getEnv => execGetEnv(arena, arguments),
.consoleLogs => execConsoleLogs(session, arena),
.getUrl => execGetUrl(session),
.getCookies => execGetCookies(session, arena),
};
}
@@ -529,6 +535,27 @@ fn execConsoleLogs(
return aw.written();
}
fn execGetUrl(session: *lp.Session) ToolError![]const u8 {
const page = session.currentPage() orelse return ToolError.PageNotLoaded;
return page.url;
}
fn execGetCookies(session: *lp.Session, arena: std.mem.Allocator) ToolError![]const u8 {
const cookies = session.cookie_jar.cookies.items;
if (cookies.len == 0) return "No cookies.";
var aw: std.Io.Writer.Allocating = .init(arena);
const writer = &aw.writer;
for (cookies) |*cookie| {
writer.print("{s}={s}", .{ cookie.name, cookie.value }) catch return ToolError.InternalError;
writer.print("; domain={s}; path={s}", .{ cookie.domain, cookie.path }) catch return ToolError.InternalError;
if (cookie.secure) writer.writeAll("; Secure") catch return ToolError.InternalError;
if (cookie.http_only) writer.writeAll("; HttpOnly") catch return ToolError.InternalError;
writer.writeAll("\n") catch return ToolError.InternalError;
}
return aw.written();
}
// --- 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

@@ -248,6 +248,20 @@ pub const tool_list = [_]protocol.Tool{
\\{ "type": "object", "properties": {} }
),
},
.{
.name = "getUrl",
.description = "Get the current page URL. Useful to check if a navigation or redirect occurred.",
.inputSchema = protocol.minify(
\\{ "type": "object", "properties": {} }
),
},
.{
.name = "getCookies",
.description = "Get all cookies in the browser. Useful for debugging authentication and session state.",
.inputSchema = protocol.minify(
\\{ "type": "object", "properties": {} }
),
},
.{
.name = "getEnv",
.description = "Read the value of an environment variable. Useful for retrieving credentials or configuration without hardcoding them.",