网络:异步:MCP
异步 MCP(模型上下文协议) IO::Async的客户端。
使用async/await从Perl连接到MCP服务器。适用于进程内Perl 服务器(通过 主控程序 模块)和外部服务器 通过stdio(Node.js、Python、Go等)。
剧情简介
use IO::Async::Loop;
use Net::Async::MCP;
use Future::AsyncAwait;
my $loop = IO::Async::Loop->new;
# In-process: direct MCP::Server calls (fastest)
use MCP::Server;
my $server = MCP::Server->new(name => 'MyServer');
$server->tool(
name => 'echo',
description => 'Echo text',
input_schema => {
type => 'object',
properties => { message => { type => 'string' } },
required => ['message'],
},
code => sub { return "Echo: $_[1]->{message}" },
);
my $mcp = Net::Async::MCP->new(server => $server);
$loop->add($mcp);
# Stdio: spawn external MCP server
my $mcp_stdio = Net::Async::MCP->new(
command => ['npx', '@anthropic/mcp-server-web-search'],
);
$loop->add($mcp_stdio);
# HTTP: remote MCP server
my $mcp_http = Net::Async::MCP->new(
url => 'https://example.com/mcp',
);
$loop->add($mcp_http);
# Same API for all transports
async sub main {
await $mcp->initialize;
my $tools = await $mcp->list_tools;
say "Available: ", join(', ', map { $_->{name} } @$tools);
my $result = await $mcp->call_tool('echo', { message => 'Hello MCP!' });
say $result->{content}[0]{text}; # "Echo: Hello MCP!"
await $mcp->shutdown;
}
main()->get;运输
| 运输 | 建造商 | 用例 |
|---|---|---|
| 进行中 | server => $mcp_server | Perl MCP::服务器处于同一进程中 |
| 工作室 | command => [...] | 外部服务器(任何语言) |
| 超文本传输协议 | url => '...' | 远程服务器(流式HTTP) |
安装
cpanm Net::Async::MCP依赖项
- IO:异步 >= 0.78
- 未来:AsyncAwait >= 0.66
- JSON::MaybeXS
- 主控程序 >=0.07(推荐,需要用于进程内运输)
- Net::Async::HTTP (推荐,HTTP传输需要)
