mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +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 dump = @import("browser/dump.zig");
|
||||
pub const markdown = @import("browser/markdown.zig");
|
||||
pub const mcp = struct {
|
||||
pub const Server = @import("mcp/Server.zig").McpServer;
|
||||
pub const protocol = @import("mcp/protocol.zig");
|
||||
pub const router = @import("mcp/router.zig");
|
||||
};
|
||||
pub const mcp = @import("mcp.zig");
|
||||
pub const build_config = @import("build_config");
|
||||
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 HttpClient = @import("../http/Client.zig");
|
||||
const Self = @This();
|
||||
|
||||
pub const McpServer = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
app: *App,
|
||||
|
||||
@@ -24,8 +24,6 @@ pub const McpServer = struct {
|
||||
|
||||
stdout_mutex: std.Thread.Mutex = .{},
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, app: *App) !*Self {
|
||||
const self = try allocator.create(Self);
|
||||
errdefer allocator.destroy(self);
|
||||
@@ -137,4 +135,3 @@ pub const McpServer = struct {
|
||||
try stdout.interface.writeByte('\n');
|
||||
try stdout.interface.flush();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
const std = @import("std");
|
||||
const McpServer = @import("Server.zig").McpServer;
|
||||
const protocol = @import("protocol.zig");
|
||||
|
||||
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{
|
||||
.{
|
||||
.uri = "mcp://page/html",
|
||||
@@ -32,7 +34,7 @@ const ReadParams = struct {
|
||||
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) {
|
||||
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 {
|
||||
jsonrpc: []const u8 = "2.0",
|
||||
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{
|
||||
.id = id,
|
||||
.@"error" = protocol.Error{
|
||||
|
||||
@@ -3,12 +3,12 @@ const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const log = lp.log;
|
||||
|
||||
const McpServer = @import("Server.zig").McpServer;
|
||||
const protocol = @import("protocol.zig");
|
||||
const resources = @import("resources.zig");
|
||||
const Server = @import("Server.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)) {
|
||||
if (server.getNextMessage()) |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, .{
|
||||
.ignore_unknown_fields = true,
|
||||
}) 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 {
|
||||
jsonrpc: []const u8 = "2.0",
|
||||
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{
|
||||
.protocolVersion = "2024-11-05",
|
||||
.capabilities = .{
|
||||
|
||||
@@ -7,10 +7,10 @@ const js = lp.js;
|
||||
const Element = @import("../browser/webapi/Element.zig");
|
||||
const Selector = @import("../browser/webapi/selector/Selector.zig");
|
||||
const String = @import("../string.zig").String;
|
||||
const McpServer = @import("Server.zig").McpServer;
|
||||
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{
|
||||
.{
|
||||
.name = "goto",
|
||||
@@ -116,7 +116,7 @@ const OverParams = struct {
|
||||
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) {
|
||||
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);
|
||||
_ = server.page.navigate(url_z, .{
|
||||
.reason = .address_bar,
|
||||
@@ -278,7 +278,7 @@ fn performGoto(server: *McpServer, arena: std.mem.Allocator, url: []const u8) !v
|
||||
_ = 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 {
|
||||
jsonrpc: []const u8 = "2.0",
|
||||
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{
|
||||
.id = id,
|
||||
.@"error" = protocol.Error{
|
||||
|
||||
Reference in New Issue
Block a user