| 1 |
root |
1.1 |
use common::sense; |
| 2 |
|
|
use Test::More; |
| 3 |
|
|
use AnyEvent (); |
| 4 |
|
|
use AnyEvent::Handle (); |
| 5 |
|
|
use AnyEvent::MCP::Server (); |
| 6 |
|
|
use AnyEvent::MCP::Server::Toolset (); |
| 7 |
|
|
use AnyEvent::MCP::Server::JSONL (); |
| 8 |
|
|
use AnyEvent::MCP::Server::HTTP (); |
| 9 |
|
|
use JSON::XS (); |
| 10 |
|
|
use Socket; |
| 11 |
|
|
|
| 12 |
|
|
my $toolset = AnyEvent::MCP::Server::Toolset->new; |
| 13 |
|
|
my $pending; |
| 14 |
|
|
|
| 15 |
|
|
$toolset->tool (echo => "echo", {}, sub { $_[0]->result ($_[1]) }); |
| 16 |
|
|
$toolset->tool (slow => "slow", {}, sub { $pending = $_[0] }); |
| 17 |
|
|
$toolset->tool (fail => "fail", {}, sub { die "tool failed\n" }); |
| 18 |
|
|
|
| 19 |
|
|
my $server = AnyEvent::MCP::Server->new (toolsets => [$toolset]); |
| 20 |
|
|
my $meta = { |
| 21 |
|
|
"io.modelcontextprotocol/protocolVersion" => "2026-07-28", |
| 22 |
|
|
"io.modelcontextprotocol/clientCapabilities" => {}, |
| 23 |
|
|
}; |
| 24 |
|
|
|
| 25 |
|
|
sub message { |
| 26 |
|
|
my ($id, $method, $params) = @_; |
| 27 |
|
|
|
| 28 |
|
|
{ jsonrpc => "2.0", id => $id, method => $method, params => { %{$params || {}}, _meta => {%$meta} } } |
| 29 |
|
|
} |
| 30 |
|
|
|
| 31 |
|
|
my $response; |
| 32 |
root |
1.3 |
for my $id (1, 0, -1, 1.5, "1", "opaque") { |
| 33 |
|
|
for my $method ("ping", "unknown") { |
| 34 |
|
|
my $json = JSON::XS::encode_json ({ |
| 35 |
|
|
jsonrpc => "2.0", id => $id, method => $method, params => {}, |
| 36 |
|
|
}); |
| 37 |
|
|
$server->json_rpc ($json, sub { $response = shift }, protocol_version => "2025-11-25"); |
| 38 |
|
|
is JSON::XS::encode_json ([JSON::XS::decode_json ($response)->{id}]), |
| 39 |
|
|
JSON::XS::encode_json ([$id]), "wire ID type preserved for $method"; |
| 40 |
|
|
} |
| 41 |
|
|
} |
| 42 |
|
|
$server->json_rpc ( |
| 43 |
|
|
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"probe","version":"0"}}}', |
| 44 |
|
|
sub { $response = shift }, |
| 45 |
|
|
); |
| 46 |
|
|
like $response, qr/"id":1(?:,|})/, "initialization preserves numeric probe ID"; |
| 47 |
|
|
|
| 48 |
root |
1.1 |
$server->rpc (message (1, "server/discover"), sub { $response = shift }); |
| 49 |
|
|
is $response->{result}{resultType}, "complete", "modern result type"; |
| 50 |
|
|
is $response->{result}{supportedVersions}[0], "2026-07-28", "discovery"; |
| 51 |
|
|
is $response->{result}{_meta}{"io.modelcontextprotocol/serverInfo"}{name}, "AnyEvent::MCP", "server identity"; |
| 52 |
|
|
|
| 53 |
|
|
my $request = $server->rpc (message (2, "tools/call", { name => "echo", arguments => { a => 42 } }), |
| 54 |
|
|
sub { $response = shift }); |
| 55 |
|
|
is $response->{result}{structuredContent}{a}, 42, "structured result"; |
| 56 |
|
|
is JSON::XS::decode_json ($response->{result}{content}[0]{text})->{a}, 42, "JSON text fallback"; |
| 57 |
|
|
ok !exists $request->{on_reply} && !exists $request->{_reply}, "release reply callbacks"; |
| 58 |
|
|
eval { $request->reply ({}) }; |
| 59 |
|
|
like $@, qr/already finished/, "duplicate reply dies"; |
| 60 |
|
|
|
| 61 |
|
|
$server->rpc (message (3, "tools/call", { name => "fail" }), sub { $response = shift }); |
| 62 |
|
|
ok $response->{result}{isError}, "tool exception is a tool error"; |
| 63 |
|
|
is $response->{result}{content}[0]{text}, "tool failed\n", "tool exception text"; |
| 64 |
|
|
|
| 65 |
|
|
$server->rpc (message (4, "tools/call", { name => "absent" }), sub { $response = shift }); |
| 66 |
|
|
is $response->{error}{code}, -32602, "unknown tool is a protocol error"; |
| 67 |
|
|
$server->rpc (message (5, "unknown"), sub { $response = shift }); |
| 68 |
|
|
is $response->{error}{code}, -32601, "unknown method"; |
| 69 |
|
|
$server->json_rpc ("{", sub { $response = JSON::XS::decode_json shift }); |
| 70 |
|
|
is $response->{error}{code}, -32700, "parse error"; |
| 71 |
|
|
$server->rpc ([], sub { $response = shift }); |
| 72 |
|
|
is $response->{error}{code}, -32600, "reject batches"; |
| 73 |
|
|
$server->rpc (message (6, "tools/call", { name => "echo", arguments => [] }), sub { $response = shift }); |
| 74 |
|
|
is $response->{error}{code}, -32602, "invalid arguments"; |
| 75 |
|
|
|
| 76 |
|
|
my $unsupported = message (7, "ping"); |
| 77 |
|
|
$unsupported->{params}{_meta}{"io.modelcontextprotocol/protocolVersion"} = "2099-01-01"; |
| 78 |
|
|
$server->rpc ($unsupported, sub { $response = shift }); |
| 79 |
|
|
is $response->{error}{code}, -32022, "unsupported version"; |
| 80 |
|
|
is $response->{error}{data}{requested}, "2099-01-01", "requested version in error"; |
| 81 |
|
|
|
| 82 |
|
|
my $peer = {}; |
| 83 |
|
|
$server->rpc ({ |
| 84 |
|
|
jsonrpc => "2.0", id => 8, method => "initialize", |
| 85 |
|
|
params => { protocolVersion => "2025-06-18", capabilities => {}, clientInfo => { name => "test", version => "0" } }, |
| 86 |
|
|
}, sub { $response = shift }, peer => $peer); |
| 87 |
|
|
is $response->{result}{protocolVersion}, "2025-06-18", "legacy initialization"; |
| 88 |
|
|
ok !exists $response->{result}{resultType}, "legacy result shape"; |
| 89 |
|
|
$server->rpc ({ jsonrpc => "2.0", id => 9, method => "tools/call", params => { name => "echo" } }, |
| 90 |
|
|
sub { $response = shift }, peer => $peer); |
| 91 |
|
|
is_deeply $response->{result}{structuredContent}, {}, "omitted arguments"; |
| 92 |
|
|
$server->rpc (message (10, "ping"), sub { $response = shift }, peer => $peer); |
| 93 |
|
|
is $response->{result}{resultType}, "complete", "modern metadata overrides legacy peer"; |
| 94 |
root |
1.2 |
is $peer->{protocol_version}, "2025-06-18", "modern request does not alter legacy state"; |
| 95 |
root |
1.1 |
|
| 96 |
|
|
$request = $server->rpc (message (11, "tools/call", { name => "slow" }), |
| 97 |
|
|
sub { die "cancelled reply" }, peer => $peer); |
| 98 |
|
|
my $cancels = 0; |
| 99 |
|
|
$request->{on_cancel} = sub { ++$cancels }; |
| 100 |
|
|
$server->rpc ({ jsonrpc => "2.0", method => "notifications/cancelled", params => { requestId => 11 } }, |
| 101 |
|
|
sub { ok !defined $_[0], "notification has no JSON-RPC response" }, peer => $peer); |
| 102 |
|
|
ok $request->{cancelled}, "cancellation"; |
| 103 |
|
|
$request->cancel; |
| 104 |
|
|
is $cancels, 1, "cancel callback once"; |
| 105 |
|
|
eval { $request->reply ({}) }; |
| 106 |
|
|
like $@, qr/already finished/, "reply after cancellation dies"; |
| 107 |
|
|
$pending = undef; |
| 108 |
|
|
|
| 109 |
|
|
my @notifications; |
| 110 |
|
|
$request = AnyEvent::MCP::Server::Request->new ( |
| 111 |
|
|
params => { _meta => { progressToken => 0 } }, |
| 112 |
|
|
on_notify => sub { push @notifications, $_[2] }, |
| 113 |
|
|
); |
| 114 |
|
|
$request->progress (1); |
| 115 |
|
|
is $notifications[0]{progressToken}, 0, "zero progress token"; |
| 116 |
|
|
$request->reply ({}); |
| 117 |
|
|
$request->progress (2); |
| 118 |
|
|
is scalar @notifications, 1, "late progress ignored"; |
| 119 |
|
|
|
| 120 |
|
|
my $custom = AnyEvent::MCP::Server->new ( |
| 121 |
|
|
on_tools_call => sub { |
| 122 |
|
|
my ($server, $req, $args) = @_; |
| 123 |
|
|
is $args, $req->{args}, "custom dispatch receives same arguments"; |
| 124 |
|
|
$req->error ("custom error"); |
| 125 |
|
|
}, |
| 126 |
|
|
); |
| 127 |
|
|
$custom->rpc (message (12, "tools/call", { name => "custom" }), sub { $response = shift }); |
| 128 |
|
|
is $response->{result}{content}[0]{text}, "custom error", "dispatch override"; |
| 129 |
|
|
|
| 130 |
|
|
socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 131 |
|
|
my $jsonl = AnyEvent::MCP::Server::JSONL->new (toolsets => [$toolset]); |
| 132 |
|
|
$jsonl->accept ($a); |
| 133 |
|
|
my $client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 134 |
|
|
my $cv = AnyEvent->condvar; |
| 135 |
|
|
my $timer = AnyEvent->timer (after => 3, cb => sub { die "JSONL timeout" }); |
| 136 |
|
|
|
| 137 |
|
|
$client->push_write ( |
| 138 |
|
|
JSON::XS::encode_json (message (20, "tools/call", { name => "slow" })) . "\n" |
| 139 |
|
|
. JSON::XS::encode_json (message (21, "ping")) . "\n" |
| 140 |
|
|
); |
| 141 |
|
|
$client->push_read (line => sub { |
| 142 |
root |
1.3 |
like $_[1], qr/"id":21(?:,|})/, "JSONL preserves numeric ID"; |
| 143 |
root |
1.1 |
is JSON::XS::decode_json ($_[1])->{id}, 21, "JSONL out-of-order reply"; |
| 144 |
|
|
$pending->reply ({}); |
| 145 |
|
|
$pending = undef; |
| 146 |
|
|
}); |
| 147 |
|
|
$client->push_read (line => sub { |
| 148 |
|
|
is JSON::XS::decode_json ($_[1])->{id}, 20, "delayed JSONL reply"; |
| 149 |
|
|
$cv->send; |
| 150 |
|
|
}); |
| 151 |
|
|
$cv->recv; |
| 152 |
|
|
undef $timer; |
| 153 |
|
|
$jsonl->close; |
| 154 |
|
|
$client->destroy; |
| 155 |
|
|
|
| 156 |
|
|
sub post { |
| 157 |
|
|
my ($msg, $extra) = @_; |
| 158 |
|
|
|
| 159 |
|
|
my $body = JSON::XS::encode_json $msg; |
| 160 |
|
|
"POST /mcp/ HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: " . length ($body) |
| 161 |
|
|
. "\r\nMCP-Protocol-Version: 2026-07-28\r\nMcp-Method: $msg->{method}\r\n" |
| 162 |
|
|
. ($msg->{method} eq "tools/call" ? "Mcp-Name: $msg->{params}{name}\r\n" : "") |
| 163 |
|
|
. ($extra || "") . "\r\n$body" |
| 164 |
|
|
} |
| 165 |
|
|
|
| 166 |
|
|
sub http_read { |
| 167 |
|
|
my ($handle, $callback) = @_; |
| 168 |
|
|
|
| 169 |
|
|
$handle->push_read (line => "\r\n\r\n", sub { |
| 170 |
|
|
my ($handle, $header) = @_; |
| 171 |
|
|
my ($length) = $header =~ /Content-Length: ([0-9]+)/; |
| 172 |
|
|
|
| 173 |
|
|
$handle->push_read (chunk => $length, sub { $callback->($header, $_[1]) }); |
| 174 |
|
|
}); |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
root |
1.7 |
my $headers = AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 178 |
|
|
"POST /mcp/ HTTP/1.1\r\nmCp-MeThOd: ping\r\n" |
| 179 |
|
|
. "Mcp-Param-Region: a\r\nmcp-param-region: b\r\n" |
| 180 |
|
|
. "X-Value: one:two\r\nEmpty:" |
| 181 |
|
|
); |
| 182 |
|
|
is $headers->{REQUEST_METHOD}, "POST", "HTTP parser reads request method"; |
| 183 |
|
|
is $headers->{REQUEST_URI}, "/mcp/", "HTTP parser reads request target"; |
| 184 |
|
|
is $headers->{SERVER_PROTOCOL}, "HTTP/1.1", "HTTP parser reads request protocol"; |
| 185 |
|
|
is $headers->{HTTP_MCP_METHOD}, "ping", "HTTP parser normalizes header case"; |
| 186 |
|
|
is $headers->{HTTP_MCP_PARAM_REGION}, "a,b", "HTTP parser comma-combines repeated fields"; |
| 187 |
|
|
is $headers->{HTTP_X_VALUE}, "one:two", "HTTP parser preserves colons in values"; |
| 188 |
|
|
is $headers->{HTTP_EMPTY}, "", "HTTP parser accepts empty field values"; |
| 189 |
|
|
my $ows = AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 190 |
|
|
"POST /mcp/ HTTP/1.0\r\nContent-Length: \t42 \t" |
| 191 |
|
|
); |
| 192 |
|
|
is $ows->{SERVER_PROTOCOL}, "HTTP/1.0", "HTTP parser accepts legacy HTTP/1.0"; |
| 193 |
|
|
is $ows->{CONTENT_LENGTH}, "42", "HTTP parser strips optional whitespace"; |
| 194 |
|
|
my $future = AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 195 |
|
|
"POST /mcp/ HTTP/1.9\r\nContent-Length: 0" |
| 196 |
|
|
); |
| 197 |
|
|
is $future->{SERVER_PROTOCOL}, "HTTP/1.9", |
| 198 |
|
|
"HTTP parser accepts future HTTP/1.x minor versions"; |
| 199 |
|
|
my $leading_zeroes = AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 200 |
|
|
"POST /mcp/ http/01.09\r\nContent-Length: 0" |
| 201 |
|
|
); |
| 202 |
|
|
is $leading_zeroes->{SERVER_PROTOCOL}, "HTTP/1.09", |
| 203 |
|
|
"HTTP parser follows AnyEvent::HTTP version recognition and normalizes major version"; |
| 204 |
|
|
ok !AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 205 |
|
|
"POST /mcp/ HTTP/1.1\r\nX-Value: a\x00b" |
| 206 |
|
|
), "HTTP parser rejects control bytes in field values"; |
| 207 |
|
|
ok !AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 208 |
|
|
"POST /mcp/ HTTP/2.0\r\nHost: example" |
| 209 |
|
|
), "HTTP parser rejects unsupported request version"; |
| 210 |
|
|
ok !AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 211 |
|
|
"POST /mcp/ HTTP/1.1\r\nBad Header: value" |
| 212 |
|
|
), "HTTP parser rejects malformed field names"; |
| 213 |
|
|
ok !AnyEvent::MCP::Server::HTTP::_parse_request_headers ( |
| 214 |
|
|
"POST /mcp/ HTTP/1.1\r\nMcp-Method: ping\r\n continued" |
| 215 |
|
|
), "HTTP parser rejects obsolete folded headers"; |
| 216 |
|
|
|
| 217 |
root |
1.1 |
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 218 |
root |
1.6 |
my $http = AnyEvent::MCP::Server::HTTP->new (auth => "none", toolsets => [$toolset]); |
| 219 |
root |
1.1 |
$http->accept ($a); |
| 220 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 221 |
|
|
$cv = AnyEvent->condvar; |
| 222 |
|
|
$timer = AnyEvent->timer (after => 3, cb => sub { die "HTTP timeout" }); |
| 223 |
|
|
|
| 224 |
|
|
$client->push_write ( |
| 225 |
|
|
post (message (30, "tools/call", { name => "slow" })) |
| 226 |
|
|
. post (message (31, "ping")) |
| 227 |
|
|
); |
| 228 |
|
|
my $release = AnyEvent->timer (after => 0.01, cb => sub { |
| 229 |
|
|
$pending->reply ({}); |
| 230 |
|
|
$pending = undef; |
| 231 |
|
|
}); |
| 232 |
|
|
http_read ($client, sub { |
| 233 |
|
|
like $_[0], qr/HTTP\/1.1 200 OK/, "HTTP status"; |
| 234 |
root |
1.3 |
like $_[1], qr/"id":30(?:,|})/, "HTTP preserves numeric ID"; |
| 235 |
root |
1.1 |
is JSON::XS::decode_json ($_[1])->{id}, 30, "HTTP first reply"; |
| 236 |
|
|
http_read ($client, sub { |
| 237 |
|
|
is JSON::XS::decode_json ($_[1])->{id}, 31, "HTTP pipelined reply order"; |
| 238 |
|
|
$cv->send; |
| 239 |
|
|
}); |
| 240 |
|
|
}); |
| 241 |
|
|
$cv->recv; |
| 242 |
|
|
undef $timer; |
| 243 |
|
|
undef $release; |
| 244 |
|
|
$http->close; |
| 245 |
|
|
$client->destroy; |
| 246 |
|
|
|
| 247 |
root |
1.7 |
# An unknown future 1.x minor version must still use persistent HTTP framing. |
| 248 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 249 |
|
|
$http = AnyEvent::MCP::Server::HTTP->new (auth => "none", toolsets => [$toolset]); |
| 250 |
|
|
$http->accept ($a); |
| 251 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 252 |
|
|
$cv = AnyEvent->condvar; |
| 253 |
|
|
$timer = AnyEvent->timer (after => 3, cb => sub { die "HTTP/1.9 timeout" }); |
| 254 |
|
|
my $future_requests = post (message (32, "ping")) . post (message (33, "ping")); |
| 255 |
|
|
$future_requests =~ s{HTTP/1\.1}{HTTP/1.9}g; |
| 256 |
|
|
$client->push_write ($future_requests); |
| 257 |
|
|
http_read ($client, sub { |
| 258 |
|
|
like $_[0], qr{HTTP/1.1 200 OK}, "HTTP/1.9 request accepted"; |
| 259 |
|
|
is JSON::XS::decode_json ($_[1])->{id}, 32, "first future-version response"; |
| 260 |
|
|
http_read ($client, sub { |
| 261 |
|
|
is JSON::XS::decode_json ($_[1])->{id}, 33, |
| 262 |
|
|
"future HTTP/1.x minor versions preserve persistent connections"; |
| 263 |
|
|
$cv->send; |
| 264 |
|
|
}); |
| 265 |
|
|
}); |
| 266 |
|
|
$cv->recv; |
| 267 |
|
|
undef $timer; |
| 268 |
|
|
$http->close; |
| 269 |
|
|
$client->destroy; |
| 270 |
|
|
|
| 271 |
root |
1.6 |
$http = AnyEvent::MCP::Server::HTTP->new (auth => "none", toolsets => [$toolset], origins => []); |
| 272 |
root |
1.1 |
for my $case ( |
| 273 |
|
|
["Origin: https://invalid.example\r\n", 403, undef], |
| 274 |
|
|
["Mcp-Method: different\r\n", 400, -32020], |
| 275 |
root |
1.7 |
["Content-Length: 0\r\n", 400, undef], |
| 276 |
|
|
["Content-Length: 999\r\n", 400, undef], |
| 277 |
|
|
["Transfer-Encoding: chunked\r\n", 400, undef], |
| 278 |
|
|
["Not A Header\r\n", 400, undef], |
| 279 |
root |
1.1 |
) { |
| 280 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 281 |
|
|
$http->accept ($a); |
| 282 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 283 |
|
|
$cv = AnyEvent->condvar; |
| 284 |
|
|
$timer = AnyEvent->timer (after => 3, cb => sub { die "HTTP rejection timeout" }); |
| 285 |
|
|
|
| 286 |
|
|
$client->push_write (post (message (40, "ping"), $case->[0])); |
| 287 |
|
|
http_read ($client, sub { |
| 288 |
|
|
like $_[0], qr/HTTP\/1.1 $case->[1]/, "HTTP rejection"; |
| 289 |
|
|
is JSON::XS::decode_json ($_[1])->{error}{code}, $case->[2], "HTTP protocol error" |
| 290 |
|
|
if defined $case->[2]; |
| 291 |
|
|
$cv->send; |
| 292 |
|
|
}); |
| 293 |
|
|
$cv->recv; |
| 294 |
|
|
undef $timer; |
| 295 |
|
|
$http->close; |
| 296 |
|
|
$client->destroy; |
| 297 |
|
|
} |
| 298 |
|
|
|
| 299 |
root |
1.3 |
for my $case ( |
| 300 |
root |
1.4 |
[undef, "https://browser.example", "POST", 200, "*", 1], |
| 301 |
|
|
[undef, "https://browser.example", "OPTIONS", 204, "*", 0], |
| 302 |
root |
1.3 |
[["*"], "https://browser.example", "POST", 200, "*", 1], |
| 303 |
|
|
[["https://browser.example"], "https://browser.example", "POST", 200, "https://browser.example", 1], |
| 304 |
|
|
[["https://browser.example"], "https://other.example", "POST", 403, undef, 1], |
| 305 |
|
|
[["*"], "https://browser.example", "OPTIONS", 204, "*", 0], |
| 306 |
|
|
[[], "https://browser.example", "OPTIONS", 403, undef, 0], |
| 307 |
|
|
[[], undef, "OPTIONS", 405, undef, 0], |
| 308 |
|
|
[["*"], "https://browser.example", "POST", 401, "*", 0], |
| 309 |
|
|
) { |
| 310 |
|
|
my ($origins, $origin, $method, $status, $allowed, $cookie) = @$case; |
| 311 |
|
|
|
| 312 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 313 |
|
|
$http = AnyEvent::MCP::Server::HTTP->new ( |
| 314 |
root |
1.5 |
toolsets => [$toolset], (defined $origins ? (origins => $origins) : ()), tokens => ["token"], |
| 315 |
root |
1.3 |
); |
| 316 |
|
|
$http->accept ($a); |
| 317 |
|
|
$client = AnyEvent::Handle->new (fh => $b, on_error => sub { die $_[2] }); |
| 318 |
|
|
$cv = AnyEvent->condvar; |
| 319 |
|
|
$timer = AnyEvent->timer (after => 3, cb => sub { die "CORS timeout" }); |
| 320 |
|
|
|
| 321 |
|
|
my $request = $method eq "POST" |
| 322 |
|
|
? post (message (50, "ping"), defined $origin ? "Origin: $origin\r\n" : "") |
| 323 |
|
|
: "OPTIONS /mcp/ HTTP/1.1\r\n" |
| 324 |
|
|
. (defined $origin ? "Origin: $origin\r\n" : "") |
| 325 |
|
|
. "Access-Control-Request-Method: POST\r\n" |
| 326 |
|
|
. "Access-Control-Request-Headers: content-type,mcp-method\r\n\r\n"; |
| 327 |
|
|
$request =~ s{POST /mcp/}{POST /token/mcp/} if $cookie; |
| 328 |
|
|
|
| 329 |
|
|
$client->push_write ($request); |
| 330 |
|
|
http_read ($client, sub { |
| 331 |
|
|
my ($header) = @_; |
| 332 |
|
|
|
| 333 |
|
|
like $header, qr/HTTP\/1.1 $status /, "CORS $method status"; |
| 334 |
|
|
if (defined $allowed) { |
| 335 |
|
|
like $header, qr/^Access-Control-Allow-Origin: \Q$allowed\E\r?$/m, "allowed origin"; |
| 336 |
|
|
like $header, qr/^Vary: Origin\r?$/m, "vary by allowed origin" if $allowed ne "*"; |
| 337 |
|
|
} else { |
| 338 |
|
|
unlike $header, qr/Access-Control-Allow-Origin:/, "no allowed origin"; |
| 339 |
|
|
} |
| 340 |
|
|
if ($status == 204) { |
| 341 |
|
|
like $header, qr/Access-Control-Allow-Methods: POST, OPTIONS/, "preflight methods"; |
| 342 |
|
|
like $header, qr/Access-Control-Allow-Headers: .*mcp-method,mcp-name/, "modern preflight headers"; |
| 343 |
|
|
like $header, qr/Access-Control-Max-Age: 86400/, "preflight cache"; |
| 344 |
|
|
} |
| 345 |
|
|
$cv->send; |
| 346 |
|
|
}); |
| 347 |
|
|
$cv->recv; |
| 348 |
|
|
undef $timer; |
| 349 |
|
|
$http->close; |
| 350 |
|
|
$client->destroy; |
| 351 |
|
|
} |
| 352 |
|
|
|
| 353 |
|
|
socketpair $a, $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die $!; |
| 354 |
|
|
my $dispatched = 0; |
| 355 |
root |
1.6 |
$http = AnyEvent::MCP::Server::HTTP->new (auth => "none", |
| 356 |
root |
1.3 |
toolsets => [$toolset], connection_close => 1, on_request => sub { ++$dispatched }, |
| 357 |
|
|
); |
| 358 |
|
|
$http->accept ($a); |
| 359 |
|
|
undef $a; |
| 360 |
|
|
$cv = AnyEvent->condvar; |
| 361 |
|
|
my $received = 0; |
| 362 |
|
|
$client = AnyEvent::Handle->new ( |
| 363 |
|
|
fh => $b, |
| 364 |
|
|
on_error => sub { die $_[2] }, |
| 365 |
|
|
on_read => sub { fail "unexpected second HTTP response"; $cv->send }, |
| 366 |
|
|
on_eof => sub { |
| 367 |
|
|
ok $received, "response drained before EOF"; |
| 368 |
|
|
is $_[0]{rbuf}, "", "no pipelined second response"; |
| 369 |
|
|
is $dispatched, 1, "connection_close prevents dispatch of the next request"; |
| 370 |
|
|
$cv->send; |
| 371 |
|
|
}, |
| 372 |
|
|
); |
| 373 |
|
|
$timer = AnyEvent->timer (after => 3, cb => sub { $cv->croak ("connection_close timeout") }); |
| 374 |
|
|
$client->push_write ( |
| 375 |
|
|
post (message (60, "tools/call", { name => "slow" })) |
| 376 |
|
|
. post (message (61, "ping")) |
| 377 |
|
|
); |
| 378 |
|
|
$release = AnyEvent->timer (after => 0.01, cb => sub { |
| 379 |
|
|
$pending->reply ({ content => [{ type => "text", text => "x" x 131072 }] }); |
| 380 |
|
|
$pending = undef; |
| 381 |
|
|
}); |
| 382 |
|
|
http_read ($client, sub { |
| 383 |
|
|
my ($header, $body) = @_; |
| 384 |
|
|
|
| 385 |
|
|
like $header, qr/^Connection: close\r?$/m, "connection_close header for HTTP/1.1"; |
| 386 |
|
|
my $response = JSON::XS::decode_json $body; |
| 387 |
|
|
is $response->{id}, 60, "delayed response delivered"; |
| 388 |
|
|
is length $response->{result}{content}[0]{text}, 131072, "complete response body before close"; |
| 389 |
|
|
$received = 1; |
| 390 |
|
|
}); |
| 391 |
|
|
$cv->recv; |
| 392 |
|
|
undef $timer; |
| 393 |
|
|
undef $release; |
| 394 |
|
|
$http->close; |
| 395 |
|
|
$client->destroy; |
| 396 |
|
|
|
| 397 |
root |
1.1 |
done_testing; |