mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-08-02 18:59:36 -04:00
Introduces an HTTP transport option to serve multipleagents from a single process. Each connection is routed to its own isolated browsing session using the `Mcp-Session-Id` header. Also adds new session management tools (`session_new`, `session_list`, `session_close`) and refactors the MCP server to support multiple V8 isolates with parking.
75 lines
2.5 KiB
Zig
75 lines
2.5 KiB
Zig
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
|
|
//
|
|
// Francis Bouvier <francis@lightpanda.io>
|
|
// Pierre Tachoire <pierre@lightpanda.io>
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
//! Stdio JSON-RPC writer for the browser-tools MCP server
|
|
//! (`mcp/Server.zig`).
|
|
|
|
const std = @import("std");
|
|
const protocol = @import("protocol.zig");
|
|
|
|
const Self = @This();
|
|
|
|
writer: *std.io.Writer,
|
|
mutex: std.Thread.Mutex = .{},
|
|
aw: std.io.Writer.Allocating,
|
|
|
|
pub fn init(allocator: std.mem.Allocator, writer: *std.io.Writer) Self {
|
|
return .{ .writer = writer, .aw = .init(allocator) };
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.aw.deinit();
|
|
}
|
|
|
|
/// Point subsequent responses at a different sink. The HTTP transport uses
|
|
/// this to capture each request's response into its own buffer; safe because
|
|
/// the browser worker retargets and writes on a single thread.
|
|
pub fn retarget(self: *Self, writer: *std.io.Writer) void {
|
|
self.writer = writer;
|
|
}
|
|
|
|
pub fn sendResponse(self: *Self, response: anytype) !void {
|
|
self.mutex.lock();
|
|
defer self.mutex.unlock();
|
|
|
|
self.aw.clearRetainingCapacity();
|
|
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &self.aw.writer);
|
|
try self.aw.writer.writeByte('\n');
|
|
try self.writer.writeAll(self.aw.writer.buffered());
|
|
try self.writer.flush();
|
|
}
|
|
|
|
pub fn sendResult(self: *Self, id: std.json.Value, result: anytype) !void {
|
|
const GenericResponse = struct {
|
|
jsonrpc: []const u8 = "2.0",
|
|
id: std.json.Value,
|
|
result: @TypeOf(result),
|
|
};
|
|
try self.sendResponse(GenericResponse{ .id = id, .result = result });
|
|
}
|
|
|
|
pub fn sendError(self: *Self, id: std.json.Value, code: protocol.ErrorCode, message: []const u8) !void {
|
|
try self.sendResponse(protocol.Response{
|
|
.id = id,
|
|
.@"error" = protocol.Error{
|
|
.code = @intFromEnum(code),
|
|
.message = message,
|
|
},
|
|
});
|
|
}
|