Add dynamic agent discovery tools
All checks were successful
build / test (push) Successful in 20s
publish / npm (push) Successful in 22s

This commit is contained in:
2026-06-05 20:07:37 -03:00
parent 80261e5224
commit 3ebe88708e
9 changed files with 628 additions and 75 deletions

View File

@@ -2,6 +2,9 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";
import { promises as fs } from "node:fs";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import {
@@ -10,7 +13,7 @@ import {
} from "@modelcontextprotocol/sdk/types.js";
import { buildGateway, SEP, type GatewayOptions } from "../src/gateway.js";
import type { EnabledAgent } from "../src/config.js";
import { loadConfig, type EnabledAgent } from "../src/config.js";
/**
* Fake upstream A2A agent: speaks the JSON-RPC subset the gateway uses
@@ -149,9 +152,10 @@ test("tools/list aggregates upstream tools with agent prefix", async () => {
"tools/list",
{},
);
assert.equal(out.tools.length, 1);
assert.equal(out.tools[0].name, `greeter${SEP}greet`);
assert.match(out.tools[0].description, /\[greeter\]/);
const upstreamTools = out.tools.filter((tool: any) => tool.name.includes(SEP));
assert.equal(upstreamTools.length, 1);
assert.equal(upstreamTools[0].name, `greeter${SEP}greet`);
assert.match(upstreamTools[0].description, /\[greeter\]/);
} finally {
await upstream.close();
}
@@ -178,7 +182,9 @@ test("tools/list survives a single upstream failure", async () => {
"tools/list",
{},
);
const names = out.tools.map((t: any) => t.name);
const names = out.tools
.map((t: any) => t.name)
.filter((name: string) => name.includes(SEP));
assert.deepEqual(names, [`good${SEP}ping`]);
} finally {
await good.close();
@@ -414,3 +420,138 @@ test("tools/call relays upstream elicitation requests", async () => {
await upstream.close();
}
});
function startFakeControlPlane(): Promise<{ url: string; close: () => Promise<void>; requests: any[] }> {
const requests: any[] = [];
return new Promise((resolve) => {
const server = http.createServer((req, res) => {
requests.push({ method: req.method, url: req.url, auth: req.headers.authorization });
if (req.method === "GET" && req.url?.startsWith("/v1/agents/search")) {
res.setHeader("content-type", "application/json");
res.end(
JSON.stringify([
{
name: "soc2-evidence",
description: "Collect SOC2 evidence",
status: "running",
public: true,
url: "https://soc2.example.test",
score: 0.91,
match_source: "lexical",
setup_required: false,
skills: [{ name: "collect", description: "", tags: ["security"], input_fields: [] }],
},
]),
);
return;
}
if (req.method === "GET" && req.url === "/v1/agents/soc2-evidence") {
res.setHeader("content-type", "application/json");
res.end(
JSON.stringify({
name: "soc2-evidence",
version: "1.0.0",
status: "running",
url: "https://soc2.example.test",
description: "Collect SOC2 evidence",
card: {
capabilities: {
a2a_cloud_import: {
mcp: { mode: "generated", path: "/mcp" },
},
},
},
}),
);
return;
}
res.statusCode = 404;
res.end("not found");
});
server.listen(0, "127.0.0.1", () => {
const { port } = server.address() as AddressInfo;
resolve({
url: `http://127.0.0.1:${port}`,
requests,
close: () => new Promise((r) => server.close(() => r())),
});
});
});
}
async function withTempHome<T>(fn: () => Promise<T>): Promise<T> {
const originalHome = process.env.HOME;
const tmpHome = await fs.mkdtemp(path.join(os.tmpdir(), "a2a-mcp-gateway-"));
process.env.HOME = tmpHome;
try {
return await fn();
} finally {
if (originalHome !== undefined) process.env.HOME = originalHome;
else delete process.env.HOME;
await fs.rm(tmpHome, { recursive: true, force: true });
}
}
test("tools/list includes a2a management tools", async () => {
const out = await callGateway([], "token", "tools/list", {});
const names = out.tools.map((tool: any) => tool.name);
assert.ok(names.includes("a2a_search_agents"));
assert.ok(names.includes("a2a_add_agent"));
assert.ok(names.includes("a2a_list_enabled_agents"));
});
test("a2a_search_agents returns visible control-plane results", async () => {
const cp = await startFakeControlPlane();
try {
const result = await callGateway(
[],
"access-token",
"tools/call",
{ name: "a2a_search_agents", arguments: { query: "soc2", tags: ["security"] } },
{ gateway: { apiUrl: cp.url } },
);
assert.equal(result.isError, undefined);
assert.equal(result.structuredContent.agents[0].name, "soc2-evidence");
assert.equal(result.structuredContent.agents[0].enabled, false);
assert.equal(cp.requests[0].auth, "Bearer access-token");
assert.match(cp.requests[0].url, /q=soc2/);
assert.match(cp.requests[0].url, /tag=security/);
} finally {
await cp.close();
}
});
test("a2a_add_agent persists control-plane agent and reports list change", async () => {
await withTempHome(async () => {
const cp = await startFakeControlPlane();
const notifications: any[] = [];
try {
const result = await callGateway(
[],
"access-token",
"tools/call",
{ name: "a2a_add_agent", arguments: { name: "soc2-evidence" } },
{
gateway: { apiUrl: cp.url },
extra: {
requestId: "req-add",
sendNotification: async (notification: any) => {
notifications.push(notification);
},
sendRequest: async () => ({}),
},
},
);
assert.equal(result.isError, undefined);
assert.match(result.content[0].text, /Added soc2-evidence/);
assert.equal(notifications[0].method, "notifications/tools/list_changed");
const cfg = await loadConfig();
assert.equal(cfg.agents[0].name, "soc2-evidence");
assert.equal(cfg.agents[0].url, `${cp.url}/v1/agents/soc2-evidence`);
assert.equal(cfg.agents[0].mcpPath, "/mcp");
} finally {
await cp.close();
}
});
});