AnyEvent-MCP
view release on metacpan or search on metacpan
bin/mcp-http view on Meta::CPAN
#!/opt/bin/perl
=encoding utf8
=head1 NAME
mcp-http - HTTP requests with URL and method permissions
=head1 SYNOPSIS
mcp-http --stdio
mcp-http --connect http://host:port/TOKEN/http
=head1 DESCRIPTION
One MCP C<request> tool backed by L<AnyEvent::HTTP>. HTTP methods are
permission actions. URL scopes: exact URL, path with any query, origin,
hostname, hostname and subdomains, or any HTTP(S) URL.
Supply a C<cookie_jar> from an earlier response to continue a session;
the server keeps no cookies. This uses AnyEvent::HTTP's simplified jar,
not a full browser cookie implementation. Treat the jar as confidential.
Responses include status, headers, UTF-8 body or Base64 bytes. Optional
C<grep> keeps lines containing a literal string; C<regex> keeps lines
matching a Perl regex; C<substitute> replaces regex matches with literal
replacement text. C<max_bytes> bounds downloads and C<truncated> reports
partial results.
Redirects and retries are disabled. HTTPS verifies certificates by default.
Set C<verify_tls> to false to skip certificate and hostname verification
for an individual request.
Network access is NOT sandboxed; permitted names can reach local or
private hosts. HTTP failures do not prove that a write did not happen.
When spawned as a multibroker stdio child, the server exits after five
minutes without MCP requests and is restarted on demand. Directly connected
clients and HTTP listeners are unaffected.
=head1 FIRST USE
Configure your MCP client to launch:
mcp-http --stdio --name web
Or combine it with other servers:
mcp-multibroker --stdio web --- mcp-http --name web --stdio ---
Ask the assistant:
Fetch https://example.org/ and report the status and page title.
Do not submit forms or send any modifying requests.
The C<request> tool receives arguments such as:
{"url": "https://example.org/", "method": "GET", "max_bytes": 8192}
The first request asks for permission to GET that URL. An approval for
GET does not authorize POST, and an approval for an exact URL need not
authorize its whole hostname. Review broader scope choices carefully,
especially on hosts serving private or local resources.
bin/mcp-http view on Meta::CPAN
if (my $subst = $args->{substitute}) {
my $regex = text_regex ($subst->{pattern});
my $replacement = $subst->{replacement};
$text =~ s/$regex/$replacement/g;
}
$text
}
mcp_tool request => "HTTP request. Method and URL permissions. No redirects; verify_tls=false skips HTTPS certificate checks. Caller-managed cookie_jar returned. "
. "UTF-8 body or Base64, bounded response, optional grep/regex/substitute.",
{
type => "object",
required => ["url"],
additionalProperties => JSON::XS::false,
properties => {
url => { type => "string", minLength => 1 },
method => { type => "string", default => "GET" },
headers => { type => "object", additionalProperties => { type => "string" } },
body => { type => "string", description => "UTF-8 request body" },
body_base64 => { type => "string", description => "Raw request body, Base64" },
cookie_jar => { type => "object", description => "AnyEvent::HTTP cookie jar returned by an earlier call" },
timeout => { type => "number", minimum => 1, maximum => 300, default => 30 },
verify_tls => { type => "boolean", default => JSON::XS::true },
max_bytes => { type => "integer", minimum => 1, maximum => 2097152, default => 65536 },
grep => { type => "string", description => "Keep lines containing this literal substring" },
regex => { type => "string", description => "Keep lines matching this Perl regex" },
substitute => {
type => "object",
required => ["pattern", "replacement"],
additionalProperties => JSON::XS::false,
properties => {
pattern => { type => "string", maxLength => 512 },
replacement => { type => "string", maxLength => 512 },
},
},
},
},
sub {
my ($req, $args) = @_;
my $method = $args->{method} // "GET";
$method =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/
or die "Invalid HTTP method\n";
exists $args->{body} && exists $args->{body_base64}
and die "Specify body or body_base64, not both\n";
!exists $args->{verify_tls} || JSON::XS::is_bool ($args->{verify_tls})
or die "Invalid verify_tls\n";
my ($url, $scopes) = url_scopes ($args->{url});
my $headers = $args->{headers} || {};
for my $key (keys %$headers) {
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/
&& defined $headers->{$key} && !ref $headers->{$key}
&& $headers->{$key} !~ /[\r\n\x00]/
or die "Invalid HTTP header\n";
}
$headers = { %$headers };
$headers->{"User-Agent"} = "mcp-http/$AnyEvent::MCP::VERSION"
unless grep lc $_ eq "user-agent", keys %$headers;
my $data = exists $args->{body} ? (AnyEvent::MCP::utf8enc $args->{body}) : undef;
if (defined $args->{body_base64}) {
$args->{body_base64} =~ /\A(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?\z/
or die "Invalid Base64 body\n";
$data = MIME::Base64::decode_base64 ($args->{body_base64});
}
!defined $data || length $data <= 1048576 or die "Request body exceeds 1 MiB\n";
my $limit = $args->{max_bytes} // 65536;
$limit >= 1 && $limit <= 2097152 or die "Invalid max_bytes\n";
!defined $args->{grep} || !ref $args->{grep} or die "Invalid grep\n";
text_regex ($args->{regex}) if defined $args->{regex};
if (defined $args->{substitute}) {
ref $args->{substitute} eq "HASH" && defined $args->{substitute}{replacement}
&& !ref $args->{substitute}{replacement} && length $args->{substitute}{replacement} <= 512
or die "Invalid substitute\n";
text_regex ($args->{substitute}{pattern});
}
my $jar = $args->{cookie_jar};
!defined $jar || ref $jar eq "HASH" or die "Invalid cookie jar\n";
$req->require_permission ({
tool => $req->{tool},
action => $method,
dimensions => { url => { label => "URL", value => $url, choices => $scopes } },
sandbox_grants => [],
reason => "$method $url",
});
my $body = "";
my $truncated = 0;
my $response_headers;
my $guard;
$guard = AnyEvent::HTTP::http_request
$method => $url,
recurse => 0,
persistent => 0,
proxy => undef,
tls_ctx => ($args->{verify_tls} // 1) ? "high" : "low",
timeout => $args->{timeout} // 30,
headers => $headers,
(defined $data ? (body => $data) : ()),
(defined $jar ? (cookie_jar => $jar) : ()),
on_header => sub {
$response_headers = { %{$_[0]} };
1
},
on_body => sub {
my ($part) = @_;
if (length ($body) + length ($part) > $limit) {
$body .= substr $part, 0, $limit - length $body;
$truncated = 1;
return 0;
}
$body .= $part;
1
},
sub {
my ($unused, $hdr) = @_;
undef $guard;
( run in 1.688 second using v1.01-cache-2.11-cpan-234fa220139 )