Skip to content

@agenticforge/protocols

npm

MCPA2AANP 三大核心智能体通信协议的 TypeScript 实现。

安装

bash
npm install @agenticforge/protocols

协议概览

协议用途核心类
MCP工具调用、资源访问、提示词模板MCPServerMCPClientMCPServerBuilder
A2A智能体间技能共享与通信A2AServerA2AClientAgentNetwork
ANP网络管理与服务发现ANPNetworkANPDiscoveryServiceInfo

MCP

ts
import {MCPServer, MCPClient} from "@agenticforge/protocols";

const server = new MCPServer("my-server");
server.addTool(
  "greet",
  "问候工具",
  {type: "object", properties: {name: {type: "string"}}, required: ["name"]},
  ({name}) => `你好,${name}!`,
);

// 内存模式客户端
const client = new MCPClient(server);
await client.connect();
console.log(await client.callTool("greet", {name: "世界"}));

// HTTP 模式
await server.serve(8000);
const remote = new MCPClient("http://127.0.0.1:8000");
await remote.connect();

MCPServer HTTP 端点:

方法路径说明
GET/health健康检查
GET/info服务器信息
GET/tools工具列表
POST/tools/:name调用工具({ arguments: {...} }
GET/resources资源列表
GET/resources/*读取资源
GET/prompts提示词列表
POST/prompts/:name获取提示词({ arguments: {...} }

A2A

ts
import {A2AServer, A2AClient, AgentNetwork} from "@agenticforge/protocols";

const agent = new A2AServer({name: "my-agent", description: "示例 Agent"});
agent.addSkill("echo", "回显输入", (text) => text);
await agent.serve(5000);

const client = new A2AClient("http://localhost:5000");
const res = await client.executeSkill("echo", "hello");
console.log(res.result); // "hello"

// 自动发现 Agent
const network = new AgentNetwork();
await network.discoverAgents(["http://localhost:5000"]);

A2AServer HTTP 端点:

方法路径说明
GET/health健康检查
GET/infoAgent 信息
GET/skills技能列表
POST/execute/:skill执行技能({ text, data? }
POST/ask自动选择技能({ question }

ANP

ts
import {ANPNetwork, ANPDiscovery, ServiceInfo} from "@agenticforge/protocols";

const discovery = new ANPDiscovery();
discovery.registerService(new ServiceInfo({
  serviceId: "svc-1",
  serviceType: "calculator",
  endpoint: "http://localhost:8001",
  capabilities: ["add", "multiply"],
}));
const svcs = discovery.findServicesByType("calculator");

const network = new ANPNetwork("prod");
network.addNode("n1", "http://localhost:8001");
network.addNode("n2", "http://localhost:8002");
network.connectNodes("n1", "n2");
const route = network.routeMessage("n1", "n2");
const status = network.getNetworkStatus(); // { healthStatus: "healthy", ... }

API 参考

基础

导出说明
ProtocolType枚举:MCP | A2A | ANP
Protocol所有协议的抽象基类

MCP 导出

导出说明
MCPServer服务器,含工具/资源/提示词注册与内置 HTTP
MCPServerBuilderMCPServer 的链式构建器
MCPClient内存模式或 HTTP 模式客户端
createExampleMCPServer()示例服务器工厂函数
createContext()创建 MCP 上下文对象
parseContext()解析 JSON 上下文
createSuccessResponse()构造成功响应
createErrorResponse()构造错误响应

A2A 导出

导出说明
A2AServer含技能注册与 HTTP API 的 Agent 服务器
A2AClient内存模式或 HTTP 模式客户端
AgentNetwork多 Agent 管理,支持自动发现
AgentRegistryA2A Agent 中央注册中心
createExampleA2AServer()示例 Agent 工厂函数

ANP 导出

导出说明
ServiceInfo服务描述类
ANPDiscovery支持过滤的服务注册与发现
ANPNetwork网络拓扑管理
createExampleANPNetwork()三节点示例网络工厂函数

详见 协议指南

Released under the CC BY-NC-SA 4.0 License.