view release on metacpan or search on metacpan
$req->reply (fs_result { result => \@result });
};
}
;
mcp_tool exec =>
"Run argv " . ($bwrap ? "in bubblewrap. " : "UNSANDBOXED with full host-user privileges. ")
. ($bwrap ? "Private /tmp by default (gone after exec); use for temp files/IPC. shared-tmp exposes HOST /tmp (sockets/services). Request ONLY for HOST /tmp, NOT ordinary temp/IPC. " : "")
. "stdin_encoding: utf-8 (default), latin1 or base64; max 4 MiB decoded. "
. "stdout/stderr UTF-8, captured to 1 MiB each; max_lines/max_bytes bound response. Optional grep: PCRE stdout filter after capture; stderr unfiltered. Reports capture truncation. "
. "Explicit sandbox_grants replace default workdir-read. Exit/signal/timeout are normal results, not tool errors. No command-prefix safety classification. Timeout >300 s requires "
. "long-running grant; max six hours.",
{
type => "object",
properties => {
%output_schema,
workdir => $workdir_schema,
argv => {
type => "array",
items => { type => "string" },
minItems => 1,
type => "string",
default => "utf-8",
enum => [qw(utf-8 latin1 base64)],
description => "stdin bytes: UTF-8 (default), Latin-1 code points U+0000..U+00FF, or canonical Base64; max 4 MiB decoded.",
},
timeout => {
type => "integer",
minimum => 1,
maximum => 21600,
default => 30,
description => "Timeout seconds; >300 also requires long-running grant.",
},
},
required => [qw(argv reason)],
additionalProperties => JSON::XS::false,
},
sub {
my ($req, $args) = @_;
my $output = fs_output_options $args;
bin/mcp-webbrowser view on Meta::CPAN
the browser. Code has already executed when the size error is returned;
do not blindly retry commands with side effects.
With C<navigate> true, the broker sends the command, awaits the browser's
C<ready> message, and sends C<proceed> before the browser evaluates it.
The broker then waits for either a new document registering on the same tab
or an observed same-document navigation. The result has C<navigation> equal
to C<document> or C<same-document>, the resulting C<revision>, and reported
URL/title. If neither completion is observed before the timeout, it returns
C<navigation: "timeout"> with a reason, current revision and available metadata.
Timeout does not imply that the command was not executed; no automatic retry
is attempted. One navigation operation at a time is allowed per tab.
The browser client injects C<code> as body of C<async () =E<gt> CODE>
in the page's JavaScript world. Supply only CODE, not the arrow-function
wrapper. For example, C<({title: document.title})> returns an object;
C<async () =E<gt> ({title: document.title})> returns a function and is
rejected as a non-JSON result. Single expression yields its value without
C<return>; braced function body needs explicit C<return> for multiple
statements.
C<await> works inside either form. Declarations in the body do not persist
t/c06_multibroker.t view on Meta::CPAN
my ($step, $value) = @_;
++$http_failures;
print STDERR "multibroker HTTP cli=$cli $step: ", JSON::XS::encode_json ($value), "\n";
};
ok $url =~ m{\Ahttp://127\.0\.0\.1:}, "--bind overrides configured address"
if $cli;
my ($printed_port, $path) = $url =~ m{\Ahttp://127\.0\.0\.1:([0-9]+)(/.*)\z};
$printed_port == $port or die "--print-url returned wrong port\n";
my $socket;
for (1 .. 1500) {
$socket = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1);
last if $socket;
Time::HiRes::sleep (0.02);
}
$socket or die "CORS test socket: $!\n";
print {$socket}
"OPTIONS $path HTTP/1.1\r\nHost: localhost\r\n"
. "Origin: http://localhost:5001\r\n"
. "Access-Control-Request-Method: POST\r\n"
. "Access-Control-Request-Headers: content-type,authorization\r\n\r\n";
my $preflight;
t/c13_vim_stdio.t view on Meta::CPAN
ok defined $substitute && index ($substitute->{description}, q{\%^\_.*\%$}) >= 0,
"$kind: substitute description preserves Vim regex backslashes and end anchor";
ok !$tools->{error},
"$kind: tools/list returned without an error";
# The stdio transport must not prevent the normal Vim/browser TCP listener.
my $sock;
for (1 .. 1500) {
$sock = IO::Socket::INET->new (
PeerAddr => "127.0.0.1", PeerPort => $port,
Proto => "tcp", Timeout => 1,
);
last if $sock;
Time::HiRes::sleep (0.02);
}
ok $sock, "$kind: TCP listener remains available";
if ($sock) {
$sock->autoflush (1);
print {$sock}
"POST / HTTP/1.1\r\nHost: localhost\r\n"
. "Content-Type: application/json\r\nContent-Length: 2\r\n\r\n{}";
t/c16_vim_auth.t view on Meta::CPAN
"-e", 'unshift @INC, sub { die "Coro must not be loaded\\n" if $_[1] =~ m{^Coro(?:/|\\.pm)}; return }; do shift @ARGV; die $@ if $@;',
"./bin/mcp-vim", "--name", "default", "--stdio", "--bind", "127.0.0.1:$port",
"--token", "primary", "--token", "secondary", "--ask", "mcp");
my $old = select $in;
$| = 1;
select $old;
sub tcp {
IO::Socket::INET->new (
PeerAddr => "127.0.0.1", PeerPort => $port,
Proto => "tcp", Timeout => 1,
)
}
my $ready;
for (1 .. 1000) {
$ready = tcp ();
last if $ready;
Time::HiRes::sleep (0.03);
}
ok $ready, "mcp-vim listens on TCP with two static tokens";
{
local %ENV = (%ENV, %env, PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL => "");
$server_cv = AnyEvent::Util::run_cmd (
[$^X, "-Ilib", "bin/mcp-fs", "--name", "default", "--bind", $bind,
"--bwrap", "$dir/bwrap", "$dir/work"],
"<" => "/dev/null", ">" => \$stdout, "2>" => \$stderr, '$$' => \$http_pid, close_all => 1,
);
}
my $ready;
for (1 .. 1500) {
$ready = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1);
last if $ready;
Time::HiRes::sleep (0.02);
}
$ready or die "filesystem listener did not start: $!\n";
close $ready;
my $web = AnyEvent::MCP::Client::HTTP->new (url => $url);
($success, $result) = wait_for (sub { $web->call (workdirs => {}, $_[0]) });
ok $success && $result->{structuredContent}{workdirs}{1} eq "$dir/work", "explicitly discovered HTTP URL is usable";
$web->close;
kill TERM => $http_pid;
t/e22_webbrowser.t view on Meta::CPAN
send_rpc $id, $method, $params;
read_rpc $id
}
sub tool {
rpc "tools/call", { name => $_[0], arguments => $_[1] || {} }
}
sub tabs {
tool ("tabs")->{result}{structuredContent}{tabs}
}
sub tcp {
IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1)
}
my $ready;
for (1 .. 300) {
$ready = tcp;
last if $ready;
Time::HiRes::sleep (.02);
}
ok $ready, "browser broker listens alongside stdio";
close $ready if $ready;
t/e23_sctl.t view on Meta::CPAN
my $id = ++$serial;
send_rpc $id, $method, $params;
read_rpc $id
}
sub tool {
rpc ("tools/call", { name => $_[0], arguments => $_[1] || {} });
}
sub tcp {
IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp", Timeout => 1)
}
my $ready;
for (1 .. 200) {
$ready = tcp;
last if $ready;
Time::HiRes::sleep (.02);
}
ok $ready, "sctl listener runs beside MCP stdio";
close $ready if $ready;
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
title_observer = undefined;
}
const base = plain_title (document.title);
const title = enabled ? `[${title_symbols[status]}MCP]${base ? " " + base : ""}` : base;
if (document.title !== title)
document.title = title;
}
function hide_toast () {
clearTimeout (toast_timer);
toast?.remove ();
toast = undefined;
}
function show_toast () {
if (!tab?.mcp_webbrowser?.enabled || !indicators.toast ||
document.visibilityState !== "visible" || !document.documentElement)
return;
if (!toast) {
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
+ "z-index:2147483647!important;background:#202020!important;color:white!important;"
+ "border:1px solid #888!important;border-radius:8px!important;padding:9px 13px!important;"
+ "font:13px sans-serif!important;pointer-events:none!important";
} catch (err) {
toast = undefined;
console.warn ("mcp-webbrowser: cannot show indicator", err);
return;
}
}
toast.textContent = `${symbols[status]} MCP ${status}${connection_error ? ": " + connection_error : ""}`;
clearTimeout (toast_timer);
if (status === "connected")
toast_timer = setTimeout (hide_toast, 3000);
}
function set_status (next) {
if (status === next)
return;
status = next;
update_title ();
register_menu ();
if (toast)
show_toast ();
}
function clear_status_timers () {
clearTimeout (offline_timer);
clearTimeout (grace_timer);
offline_timer = grace_timer = undefined;
}
function awaiting_connection () {
if (!tab?.mcp_webbrowser?.enabled)
return;
if (!offline_timer && status !== "disconnected")
offline_timer = setTimeout (() => {
offline_timer = undefined;
set_status ("disconnected");
}, 30000);
if (status === "connected" && !grace_timer)
grace_timer = setTimeout (() => {
grace_timer = undefined;
set_status ("connecting");
}, 1500);
}
function connected () {
connection_error = "";
clear_status_timers ();
set_status ("connected");
}
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
if (u.search || u.hash)
throw new Error ("broker URL must not contain a query or fragment");
u.pathname = u.pathname.replace (/\/+$/, "") + "/" + encodeURIComponent (target);
return u.href;
}
function disconnect (revoke = false) {
clearTimeout (reconnect_timer);
reconnect_timer = undefined;
const socket = ws;
ws = undefined;
for (const id in pending) {
pending[id].cancel?.();
delete pending[id];
}
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
function release () {
if (!released) {
released = true;
// A cancelled waiter must not let its successor overtake its predecessor.
ready.then (() => done.resolve ());
}
}
function cleanup () {
window.removeEventListener (channel, receive);
clearTimeout (startup_timer);
cancel_injection?.();
window.dispatchEvent (new CustomEvent (stop_channel));
}
function finish (success, value) {
if (finished)
return;
finished = true;
cleanup ();
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
try {
reply = JSON.parse (ev.detail);
} catch (err) {
finish (false, "Invalid page evaluation result: " + err);
return;
}
if (reply[0] === "compiled") {
compiled = true;
clearTimeout (startup_timer);
release ();
} else
finish (reply[0] === "syntax-error" ? false : reply[0], reply[1]);
}
function start () {
if (finished)
return;
window.addEventListener (channel, receive);
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
else
report (true, value === undefined ? null : value);
}
},
err => report (false, String (err))
);
}
//# sourceURL=mcp-webbrowser-eval-${eval_id}
`, () => {
if (!compiled && !finished)
startup_timer = setTimeout (() => finish (false,
"Page code did not start (syntax error or blocked injection)"), 200);
}, err => finish (false, "Cannot inject page code: " + err));
} catch (err) {
finish (false, "Cannot inject page code: " + err);
}
});
}, err => finish (false, "Cannot install page error listener: " + err));
} catch (err) {
finish (false, "Cannot install page error listener: " + err);
}
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
socket.send (JSON.stringify (["reply", id, false, String (err)]));
}
}
function connect () {
if (ws || !tab?.mcp_webbrowser?.enabled || !broker_url)
return;
awaiting_connection ();
clearTimeout (reconnect_timer);
reconnect_timer = undefined;
let url;
try {
url = webbrowser_url (broker_url, tab.mcp_webbrowser.id);
} catch (err) {
console.warn ("mcp-webbrowser: invalid broker URL", err);
return;
}
let socket;
try {
socket = new WebSocket (url);
} catch (err) {
console.warn ("mcp-webbrowser: cannot open WebSocket", err);
reconnect_timer = setTimeout (connect, 5000);
return;
}
ws = socket;
socket.onopen = () => {
if (socket !== ws)
return;
socket.send (JSON.stringify ([
"mcp-webbrowser", 1, 2, instance_id,
webbrowser/mcp-webbrowser.user.js view on Meta::CPAN
if (ev.code !== 1000 && ev.code !== 1001) {
connection_error = `WebSocket ${ev.code}${ev.reason ? ": " + ev.reason : ""}`;
console.warn ("mcp-webbrowser:", connection_error);
set_status ("disconnected");
show_toast ();
}
ws = undefined;
if (tab?.mcp_webbrowser?.enabled) {
awaiting_connection ();
reconnect_timer = setTimeout (connect, 5000);
}
};
}
function configure_broker () {
const value = prompt ("mcp-webbrowser --print-url (empty to disable):", broker_url);
if (value === null)
return false;