mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-23 05:04:42 +00:00
mcp: promot Server.zig to file struct
This commit is contained in:
@@ -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
3
src/mcp.zig
Normal 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");
|
||||||
@@ -4,8 +4,8 @@ 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,
|
||||||
|
|
||||||
@@ -24,8 +24,6 @@ pub const McpServer = struct {
|
|||||||
|
|
||||||
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);
|
||||||
@@ -137,4 +135,3 @@ pub const McpServer = struct {
|
|||||||
try stdout.interface.writeByte('\n');
|
try stdout.interface.writeByte('\n');
|
||||||
try stdout.interface.flush();
|
try stdout.interface.flush();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|||||||
@@ -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{
|
||||||
|
|||||||
@@ -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 = .{
|
||||||
|
|||||||
@@ -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{
|
||||||
|
|||||||
Reference in New Issue
Block a user