mcp: promot Server.zig to file struct

This commit is contained in:
Adrià Arrufat
2026-02-28 21:02:49 +09:00
parent 5ec4305a9f
commit aae9a505e0
6 changed files with 126 additions and 128 deletions

View File

@@ -29,11 +29,7 @@ pub const log = @import("log.zig");
pub const js = @import("browser/js/js.zig"); pub const js = @import("browser/js/js.zig");
pub const dump = @import("browser/dump.zig"); pub const dump = @import("browser/dump.zig");
pub const markdown = @import("browser/markdown.zig"); pub const markdown = @import("browser/markdown.zig");
pub const mcp = struct { pub const mcp = @import("mcp.zig");
pub const Server = @import("mcp/Server.zig").McpServer;
pub const protocol = @import("mcp/protocol.zig");
pub const router = @import("mcp/router.zig");
};
pub const build_config = @import("build_config"); pub const build_config = @import("build_config");
pub const crash_handler = @import("crash_handler.zig"); pub const crash_handler = @import("crash_handler.zig");

3
src/mcp.zig Normal file
View File

@@ -0,0 +1,3 @@
pub const Server = @import("mcp/Server.zig");
pub const protocol = @import("mcp/protocol.zig");
pub const router = @import("mcp/router.zig");

View File

@@ -4,29 +4,27 @@ const lp = @import("lightpanda");
const App = @import("../App.zig"); const App = @import("../App.zig");
const HttpClient = @import("../http/Client.zig"); const HttpClient = @import("../http/Client.zig");
const Self = @This();
pub const McpServer = struct { allocator: std.mem.Allocator,
allocator: std.mem.Allocator, app: *App,
app: *App,
http_client: *HttpClient, http_client: *HttpClient,
notification: *lp.Notification, notification: *lp.Notification,
browser: *lp.Browser, browser: *lp.Browser,
session: *lp.Session, session: *lp.Session,
page: *lp.Page, page: *lp.Page,
io_thread: ?std.Thread = null, io_thread: ?std.Thread = null,
queue_mutex: std.Thread.Mutex = .{}, queue_mutex: std.Thread.Mutex = .{},
queue_condition: std.Thread.Condition = .{}, queue_condition: std.Thread.Condition = .{},
message_queue: std.ArrayListUnmanaged([]const u8) = .empty, message_queue: std.ArrayListUnmanaged([]const u8) = .empty,
is_running: std.atomic.Value(bool) = std.atomic.Value(bool).init(false), is_running: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
stdout_mutex: std.Thread.Mutex = .{}, stdout_mutex: std.Thread.Mutex = .{},
const Self = @This(); pub fn init(allocator: std.mem.Allocator, app: *App) !*Self {
pub fn init(allocator: std.mem.Allocator, app: *App) !*Self {
const self = try allocator.create(Self); const self = try allocator.create(Self);
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
@@ -49,9 +47,9 @@ pub const McpServer = struct {
self.page = try self.session.createPage(); self.page = try self.session.createPage();
return self; return self;
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.stop(); self.stop();
if (self.io_thread) |*thread| { if (self.io_thread) |*thread| {
thread.join(); thread.join();
@@ -67,21 +65,21 @@ pub const McpServer = struct {
self.http_client.deinit(); self.http_client.deinit();
self.allocator.destroy(self); self.allocator.destroy(self);
} }
pub fn start(self: *Self) !void { pub fn start(self: *Self) !void {
self.is_running.store(true, .seq_cst); self.is_running.store(true, .seq_cst);
self.io_thread = try std.Thread.spawn(.{}, ioWorker, .{self}); self.io_thread = try std.Thread.spawn(.{}, ioWorker, .{self});
} }
pub fn stop(self: *Self) void { pub fn stop(self: *Self) void {
self.is_running.store(false, .seq_cst); self.is_running.store(false, .seq_cst);
self.queue_mutex.lock(); self.queue_mutex.lock();
self.queue_condition.signal(); self.queue_condition.signal();
self.queue_mutex.unlock(); self.queue_mutex.unlock();
} }
fn ioWorker(self: *Self) void { fn ioWorker(self: *Self) void {
var stdin_file = std.fs.File.stdin(); var stdin_file = std.fs.File.stdin();
var stdin_buf: [8192]u8 = undefined; var stdin_buf: [8192]u8 = undefined;
var stdin = stdin_file.reader(&stdin_buf); var stdin = stdin_file.reader(&stdin_buf);
@@ -110,9 +108,9 @@ pub const McpServer = struct {
std.Thread.sleep(100 * std.time.ns_per_ms); std.Thread.sleep(100 * std.time.ns_per_ms);
} }
} }
} }
pub fn getNextMessage(self: *Self) ?[]const u8 { pub fn getNextMessage(self: *Self) ?[]const u8 {
self.queue_mutex.lock(); self.queue_mutex.lock();
defer self.queue_mutex.unlock(); defer self.queue_mutex.unlock();
@@ -124,9 +122,9 @@ pub const McpServer = struct {
return self.message_queue.orderedRemove(0); return self.message_queue.orderedRemove(0);
} }
return null; return null;
} }
pub fn sendResponse(self: *Self, response: anytype) !void { pub fn sendResponse(self: *Self, response: anytype) !void {
self.stdout_mutex.lock(); self.stdout_mutex.lock();
defer self.stdout_mutex.unlock(); defer self.stdout_mutex.unlock();
@@ -136,5 +134,4 @@ pub const McpServer = struct {
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &stdout.interface); try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &stdout.interface);
try stdout.interface.writeByte('\n'); try stdout.interface.writeByte('\n');
try stdout.interface.flush(); try stdout.interface.flush();
} }
};

View File

@@ -1,9 +1,11 @@
const std = @import("std"); const std = @import("std");
const McpServer = @import("Server.zig").McpServer;
const protocol = @import("protocol.zig");
const lp = @import("lightpanda"); const lp = @import("lightpanda");
pub fn handleList(server: *McpServer, req: protocol.Request) !void { const protocol = @import("protocol.zig");
const Server = @import("Server.zig");
pub fn handleList(server: *Server, req: protocol.Request) !void {
const resources = [_]protocol.Resource{ const resources = [_]protocol.Resource{
.{ .{
.uri = "mcp://page/html", .uri = "mcp://page/html",
@@ -32,7 +34,7 @@ const ReadParams = struct {
uri: []const u8, uri: []const u8,
}; };
pub fn handleRead(server: *McpServer, arena: std.mem.Allocator, req: protocol.Request) !void { pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
if (req.params == null) { if (req.params == null) {
return sendError(server, req.id.?, -32602, "Missing params"); return sendError(server, req.id.?, -32602, "Missing params");
} }
@@ -78,7 +80,7 @@ pub fn handleRead(server: *McpServer, arena: std.mem.Allocator, req: protocol.Re
} }
} }
pub fn sendResult(server: *McpServer, id: std.json.Value, result: anytype) !void { pub fn sendResult(server: *Server, id: std.json.Value, result: anytype) !void {
const GenericResponse = struct { const GenericResponse = struct {
jsonrpc: []const u8 = "2.0", jsonrpc: []const u8 = "2.0",
id: std.json.Value, id: std.json.Value,
@@ -90,7 +92,7 @@ pub fn sendResult(server: *McpServer, id: std.json.Value, result: anytype) !void
}); });
} }
pub fn sendError(server: *McpServer, id: std.json.Value, code: i64, message: []const u8) !void { pub fn sendError(server: *Server, id: std.json.Value, code: i64, message: []const u8) !void {
try server.sendResponse(protocol.Response{ try server.sendResponse(protocol.Response{
.id = id, .id = id,
.@"error" = protocol.Error{ .@"error" = protocol.Error{

View File

@@ -3,12 +3,12 @@ const std = @import("std");
const lp = @import("lightpanda"); const lp = @import("lightpanda");
const log = lp.log; const log = lp.log;
const McpServer = @import("Server.zig").McpServer;
const protocol = @import("protocol.zig"); const protocol = @import("protocol.zig");
const resources = @import("resources.zig"); const resources = @import("resources.zig");
const Server = @import("Server.zig");
const tools = @import("tools.zig"); const tools = @import("tools.zig");
pub fn processRequests(server: *McpServer) void { pub fn processRequests(server: *Server) void {
while (server.is_running.load(.seq_cst)) { while (server.is_running.load(.seq_cst)) {
if (server.getNextMessage()) |msg| { if (server.getNextMessage()) |msg| {
defer server.allocator.free(msg); defer server.allocator.free(msg);
@@ -25,7 +25,7 @@ pub fn processRequests(server: *McpServer) void {
} }
} }
fn handleMessage(server: *McpServer, arena: std.mem.Allocator, msg: []const u8) !void { fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8) !void {
const parsed = std.json.parseFromSliceLeaky(protocol.Request, arena, msg, .{ const parsed = std.json.parseFromSliceLeaky(protocol.Request, arena, msg, .{
.ignore_unknown_fields = true, .ignore_unknown_fields = true,
}) catch |err| { }) catch |err| {
@@ -62,7 +62,7 @@ fn handleMessage(server: *McpServer, arena: std.mem.Allocator, msg: []const u8)
} }
} }
fn sendResponseGeneric(server: *McpServer, id: std.json.Value, result: anytype) !void { fn sendResponseGeneric(server: *Server, id: std.json.Value, result: anytype) !void {
const GenericResponse = struct { const GenericResponse = struct {
jsonrpc: []const u8 = "2.0", jsonrpc: []const u8 = "2.0",
id: std.json.Value, id: std.json.Value,
@@ -74,7 +74,7 @@ fn sendResponseGeneric(server: *McpServer, id: std.json.Value, result: anytype)
}); });
} }
fn handleInitialize(server: *McpServer, req: protocol.Request) !void { fn handleInitialize(server: *Server, req: protocol.Request) !void {
const result = protocol.InitializeResult{ const result = protocol.InitializeResult{
.protocolVersion = "2024-11-05", .protocolVersion = "2024-11-05",
.capabilities = .{ .capabilities = .{

View File

@@ -7,10 +7,10 @@ const js = lp.js;
const Element = @import("../browser/webapi/Element.zig"); const Element = @import("../browser/webapi/Element.zig");
const Selector = @import("../browser/webapi/selector/Selector.zig"); const Selector = @import("../browser/webapi/selector/Selector.zig");
const String = @import("../string.zig").String; const String = @import("../string.zig").String;
const McpServer = @import("Server.zig").McpServer;
const protocol = @import("protocol.zig"); const protocol = @import("protocol.zig");
const Server = @import("Server.zig");
pub fn handleList(server: *McpServer, arena: std.mem.Allocator, req: protocol.Request) !void { pub fn handleList(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
const tools = [_]protocol.Tool{ const tools = [_]protocol.Tool{
.{ .{
.name = "goto", .name = "goto",
@@ -116,7 +116,7 @@ const OverParams = struct {
result: []const u8, result: []const u8,
}; };
pub fn handleCall(server: *McpServer, arena: std.mem.Allocator, req: protocol.Request) !void { pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
if (req.params == null) { if (req.params == null) {
return sendError(server, req.id.?, -32602, "Missing params"); return sendError(server, req.id.?, -32602, "Missing params");
} }
@@ -266,7 +266,7 @@ pub fn handleCall(server: *McpServer, arena: std.mem.Allocator, req: protocol.Re
} }
} }
fn performGoto(server: *McpServer, arena: std.mem.Allocator, url: []const u8) !void { fn performGoto(server: *Server, arena: std.mem.Allocator, url: []const u8) !void {
const url_z = try arena.dupeZ(u8, url); const url_z = try arena.dupeZ(u8, url);
_ = server.page.navigate(url_z, .{ _ = server.page.navigate(url_z, .{
.reason = .address_bar, .reason = .address_bar,
@@ -278,7 +278,7 @@ fn performGoto(server: *McpServer, arena: std.mem.Allocator, url: []const u8) !v
_ = server.session.wait(5000); _ = server.session.wait(5000);
} }
pub fn sendResult(server: *McpServer, id: std.json.Value, result: anytype) !void { pub fn sendResult(server: *Server, id: std.json.Value, result: anytype) !void {
const GenericResponse = struct { const GenericResponse = struct {
jsonrpc: []const u8 = "2.0", jsonrpc: []const u8 = "2.0",
id: std.json.Value, id: std.json.Value,
@@ -290,7 +290,7 @@ pub fn sendResult(server: *McpServer, id: std.json.Value, result: anytype) !void
}); });
} }
pub fn sendError(server: *McpServer, id: std.json.Value, code: i64, message: []const u8) !void { pub fn sendError(server: *Server, id: std.json.Value, code: i64, message: []const u8) !void {
try server.sendResponse(protocol.Response{ try server.sendResponse(protocol.Response{
.id = id, .id = id,
.@"error" = protocol.Error{ .@"error" = protocol.Error{