mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-28 15:40:04 +00:00
mcp: improve error handling in resources and tools
- Handle failures during HTML, Markdown, and link serialization. - Return MCP internal errors when result serialization fails. - Refactor resource reading logic for better clarity and consistency.
This commit is contained in:
@@ -28,6 +28,7 @@ pub fn handleList(server: *Server, req: protocol.Request) !void {
|
|||||||
const ReadParams = struct {
|
const ReadParams = struct {
|
||||||
uri: []const u8,
|
uri: []const u8,
|
||||||
};
|
};
|
||||||
|
const Format = enum { html, markdown };
|
||||||
|
|
||||||
const ResourceStreamingResult = struct {
|
const ResourceStreamingResult = struct {
|
||||||
contents: []const struct {
|
contents: []const struct {
|
||||||
@@ -38,7 +39,7 @@ const ResourceStreamingResult = struct {
|
|||||||
|
|
||||||
const StreamingText = struct {
|
const StreamingText = struct {
|
||||||
page: *lp.Page,
|
page: *lp.Page,
|
||||||
format: enum { html, markdown },
|
format: Format,
|
||||||
|
|
||||||
pub fn jsonStringify(self: @This(), jw: *std.json.Stringify) !void {
|
pub fn jsonStringify(self: @This(), jw: *std.json.Stringify) !void {
|
||||||
try jw.beginWriteRaw();
|
try jw.beginWriteRaw();
|
||||||
@@ -47,9 +48,11 @@ const ResourceStreamingResult = struct {
|
|||||||
switch (self.format) {
|
switch (self.format) {
|
||||||
.html => lp.dump.root(self.page.document, .{}, &escaped.writer, self.page) catch |err| {
|
.html => lp.dump.root(self.page.document, .{}, &escaped.writer, self.page) catch |err| {
|
||||||
log.err(.mcp, "html dump failed", .{ .err = err });
|
log.err(.mcp, "html dump failed", .{ .err = err });
|
||||||
|
return error.WriteFailed;
|
||||||
},
|
},
|
||||||
.markdown => lp.markdown.dump(self.page.document.asNode(), .{}, &escaped.writer, self.page) catch |err| {
|
.markdown => lp.markdown.dump(self.page.document.asNode(), .{}, &escaped.writer, self.page) catch |err| {
|
||||||
log.err(.mcp, "markdown dump failed", .{ .err = err });
|
log.err(.mcp, "markdown dump failed", .{ .err = err });
|
||||||
|
return error.WriteFailed;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
try jw.writer.writeByte('"');
|
try jw.writer.writeByte('"');
|
||||||
@@ -86,28 +89,25 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
|
|||||||
return server.sendError(req_id, .PageNotLoaded, "Page not loaded");
|
return server.sendError(req_id, .PageNotLoaded, "Page not loaded");
|
||||||
};
|
};
|
||||||
|
|
||||||
switch (uri) {
|
const format: Format = switch (uri) {
|
||||||
.@"mcp://page/html" => {
|
.@"mcp://page/html" => .html,
|
||||||
|
.@"mcp://page/markdown" => .markdown,
|
||||||
|
};
|
||||||
|
const mime_type: []const u8 = switch (uri) {
|
||||||
|
.@"mcp://page/html" => "text/html",
|
||||||
|
.@"mcp://page/markdown" => "text/markdown",
|
||||||
|
};
|
||||||
|
|
||||||
const result: ResourceStreamingResult = .{
|
const result: ResourceStreamingResult = .{
|
||||||
.contents = &.{.{
|
.contents = &.{.{
|
||||||
.uri = params.uri,
|
.uri = params.uri,
|
||||||
.mimeType = "text/html",
|
.mimeType = mime_type,
|
||||||
.text = .{ .page = page, .format = .html },
|
.text = .{ .page = page, .format = format },
|
||||||
}},
|
}},
|
||||||
};
|
};
|
||||||
try server.sendResult(req_id, result);
|
server.sendResult(req_id, result) catch {
|
||||||
},
|
return server.sendError(req_id, .InternalError, "Failed to serialize resource content");
|
||||||
.@"mcp://page/markdown" => {
|
|
||||||
const result: ResourceStreamingResult = .{
|
|
||||||
.contents = &.{.{
|
|
||||||
.uri = params.uri,
|
|
||||||
.mimeType = "text/markdown",
|
|
||||||
.text = .{ .page = page, .format = .markdown },
|
|
||||||
}},
|
|
||||||
};
|
};
|
||||||
try server.sendResult(req_id, result);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const testing = @import("../testing.zig");
|
const testing = @import("../testing.zig");
|
||||||
|
|||||||
@@ -205,18 +205,19 @@ const ToolStreamingText = struct {
|
|||||||
switch (self.action) {
|
switch (self.action) {
|
||||||
.markdown => lp.markdown.dump(self.page.document.asNode(), .{}, w, self.page) catch |err| {
|
.markdown => lp.markdown.dump(self.page.document.asNode(), .{}, w, self.page) catch |err| {
|
||||||
log.err(.mcp, "markdown dump failed", .{ .err = err });
|
log.err(.mcp, "markdown dump failed", .{ .err = err });
|
||||||
|
return error.WriteFailed;
|
||||||
},
|
},
|
||||||
.links => {
|
.links => {
|
||||||
if (lp.links.collectLinks(self.page.call_arena, self.page.document.asNode(), self.page)) |links| {
|
const links = lp.links.collectLinks(self.page.call_arena, self.page.document.asNode(), self.page) catch |err| {
|
||||||
|
log.err(.mcp, "query links failed", .{ .err = err });
|
||||||
|
return error.WriteFailed;
|
||||||
|
};
|
||||||
var first = true;
|
var first = true;
|
||||||
for (links) |href| {
|
for (links) |href| {
|
||||||
if (!first) try w.writeByte('\n');
|
if (!first) try w.writeByte('\n');
|
||||||
try w.writeAll(href);
|
try w.writeAll(href);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
} else |err| {
|
|
||||||
log.err(.mcp, "query links failed", .{ .err = err });
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
.semantic_tree => {
|
.semantic_tree => {
|
||||||
var root_node = self.page.document.asNode();
|
var root_node = self.page.document.asNode();
|
||||||
@@ -241,6 +242,7 @@ const ToolStreamingText = struct {
|
|||||||
|
|
||||||
st.textStringify(w) catch |err| {
|
st.textStringify(w) catch |err| {
|
||||||
log.err(.mcp, "semantic tree dump failed", .{ .err = err });
|
log.err(.mcp, "semantic tree dump failed", .{ .err = err });
|
||||||
|
return error.WriteFailed;
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -331,7 +333,9 @@ fn handleMarkdown(server: *Server, arena: std.mem.Allocator, id: std.json.Value,
|
|||||||
const content = [_]protocol.TextContent(ToolStreamingText){.{
|
const content = [_]protocol.TextContent(ToolStreamingText){.{
|
||||||
.text = .{ .page = page, .action = .markdown },
|
.text = .{ .page = page, .action = .markdown },
|
||||||
}};
|
}};
|
||||||
try server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content });
|
server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content }) catch {
|
||||||
|
return server.sendError(id, .InternalError, "Failed to serialize markdown content");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handleLinks(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
fn handleLinks(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
@@ -341,7 +345,9 @@ fn handleLinks(server: *Server, arena: std.mem.Allocator, id: std.json.Value, ar
|
|||||||
const content = [_]protocol.TextContent(ToolStreamingText){.{
|
const content = [_]protocol.TextContent(ToolStreamingText){.{
|
||||||
.text = .{ .page = page, .action = .links },
|
.text = .{ .page = page, .action = .links },
|
||||||
}};
|
}};
|
||||||
try server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content });
|
server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content }) catch {
|
||||||
|
return server.sendError(id, .InternalError, "Failed to serialize links content");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handleSemanticTree(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
fn handleSemanticTree(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
@@ -363,7 +369,9 @@ fn handleSemanticTree(server: *Server, arena: std.mem.Allocator, id: std.json.Va
|
|||||||
.maxDepth = args.maxDepth,
|
.maxDepth = args.maxDepth,
|
||||||
},
|
},
|
||||||
}};
|
}};
|
||||||
try server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content });
|
server.sendResult(id, protocol.CallToolResult(ToolStreamingText){ .content = &content }) catch {
|
||||||
|
return server.sendError(id, .InternalError, "Failed to serialize semantic tree content");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handleInteractiveElements(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
fn handleInteractiveElements(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
|||||||
Reference in New Issue
Block a user